Changeset 48ae3ef in mainline


Ignore:
Timestamp:
2011-10-28T21:52:15Z (13 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
57e06ef
Parents:
7265558
Message:

libusbhost: Make interfaces more symmetric.

Make usb_endpoint_manager interface easier to use and understand.
Move ep removal hook pointer from endpoint_t to hcd_t.

Location:
uspace
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/ohci/hc.c

    r7265558 r48ae3ef  
    142142if (ret != EOK) { \
    143143        usb_log_error(message); \
    144         usb_endpoint_manager_unregister_ep( \
    145             &instance->generic.ep_manager, hub_address, 0, USB_DIRECTION_BOTH);\
     144        usb_endpoint_manager_remove_ep( \
     145            &instance->generic.ep_manager, hub_address, 0, USB_DIRECTION_BOTH, \
     146            NULL, NULL);\
    146147        usb_device_manager_release( \
    147148            &instance->generic.dev_manager, hub_address); \
     
    150151        int ret = usb_endpoint_manager_add_ep(
    151152            &instance->generic.ep_manager, hub_address, 0, USB_DIRECTION_BOTH,
    152             USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64, 0);
     153            USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64, 0, NULL, NULL);
    153154        CHECK_RET_UNREG_RETURN(ret,
    154155            "Failed to register root hub control endpoint: %s.\n",
     
    197198        instance->generic.schedule = hc_schedule;
    198199        instance->generic.ep_add_hook = ohci_endpoint_init;
     200        instance->generic.ep_remove_hook = ohci_endpoint_fini;
    199201
    200202        ret = hc_init_memory(instance);
  • uspace/drv/bus/usb/ohci/ohci_endpoint.c

    r7265558 r48ae3ef  
    6262}
    6363/*----------------------------------------------------------------------------*/
    64 /** Disposes hcd endpoint structure
    65  *
    66  * @param[in] hcd_ep endpoint structure
    67  */
    68 static void ohci_endpoint_fini(endpoint_t *ep)
    69 {
    70         ohci_endpoint_t *instance = ep->hc_data.data;
    71         hc_dequeue_endpoint(instance->hcd->private_data, ep);
    72         if (instance) {
    73                 free32(instance->ed);
    74                 free32(instance->td);
    75                 free(instance);
    76         }
    77 }
    78 /*----------------------------------------------------------------------------*/
    7964/** Creates new hcd endpoint representation.
    8065 *
     
    10489        ed_init(ohci_ep->ed, ep, ohci_ep->td);
    10590        endpoint_set_hc_data(
    106             ep, ohci_ep, ohci_endpoint_fini, ohci_ep_toggle_get, ohci_ep_toggle_set);
     91            ep, ohci_ep, ohci_ep_toggle_get, ohci_ep_toggle_set);
    10792        ohci_ep->hcd = hcd;
    10893        hc_enqueue_endpoint(hcd->private_data, ep);
    10994        return EOK;
    11095}
     96/*----------------------------------------------------------------------------*/
     97/** Disposes hcd endpoint structure
     98 *
     99 * @param[in] hcd_ep endpoint structure
     100 */
     101void ohci_endpoint_fini(hcd_t *hcd, endpoint_t *ep)
     102{
     103        assert(hcd);
     104        assert(ep);
     105        ohci_endpoint_t *instance = ohci_endpoint_get(ep);
     106        hc_dequeue_endpoint(hcd->private_data, ep);
     107        if (instance) {
     108                free32(instance->ed);
     109                free32(instance->td);
     110                free(instance);
     111        }
     112        endpoint_clear_hc_data(ep);
     113}
    111114/**
    112115 * @}
  • uspace/drv/bus/usb/ohci/ohci_endpoint.h

    r7265558 r48ae3ef  
    5656
    5757int ohci_endpoint_init(hcd_t *hcd, endpoint_t *ep);
     58void ohci_endpoint_fini(hcd_t *hcd, endpoint_t *ep);
    5859
    5960/** Get and convert assigned ohci_endpoint_t structure
  • uspace/drv/bus/usb/vhc/connhost.c

    r7265558 r48ae3ef  
    140140    size_t max_packet_size, unsigned int interval)
    141141{
    142         /* TODO: Use usb_endpoint_manager_add_ep */
    143         VHC_DATA(vhc, fun);
    144 
    145         endpoint_t *ep = endpoint_create(
    146             address, endpoint, direction, transfer_type, USB_SPEED_FULL, 1, 0);
    147         if (ep == NULL) {
    148                 return ENOMEM;
    149         }
    150 
    151         int rc = usb_endpoint_manager_register_ep(&vhc->ep_manager, ep, 1);
    152         if (rc != EOK) {
    153                 endpoint_destroy(ep);
    154                 return rc;
    155         }
    156 
    157         return EOK;
     142        VHC_DATA(vhc, fun);
     143
     144        return usb_endpoint_manager_add_ep(&vhc->ep_manager,
     145            address, endpoint, direction, transfer_type, USB_SPEED_FULL, 1, 0,
     146            NULL, NULL);
     147
    158148}
    159149
     
    171161        VHC_DATA(vhc, fun);
    172162
    173         int rc = usb_endpoint_manager_unregister_ep(&vhc->ep_manager,
    174             address, endpoint, direction);
     163        int rc = usb_endpoint_manager_remove_ep(&vhc->ep_manager,
     164            address, endpoint, direction, NULL, NULL);
    175165
    176166        return rc;
     
    413403        VHC_DATA(vhc, fun);
    414404
    415         endpoint_t *ep = usb_endpoint_manager_get_ep(&vhc->ep_manager,
     405        endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
    416406            target.address, target.endpoint, USB_DIRECTION_IN);
    417407        if (ep == NULL) {
     
    455445        VHC_DATA(vhc, fun);
    456446
    457         endpoint_t *ep = usb_endpoint_manager_get_ep(&vhc->ep_manager,
     447        endpoint_t *ep = usb_endpoint_manager_find_ep(&vhc->ep_manager,
    458448            target.address, target.endpoint, USB_DIRECTION_OUT);
    459449        if (ep == NULL) {
  • uspace/lib/usbhost/include/usb/host/endpoint.h

    r7265558 r48ae3ef  
    5454        fibril_condvar_t avail;
    5555        volatile bool active;
    56         void (*destroy_hook)(struct endpoint *);
    5756        struct {
    5857                void *data;
     
    6867
    6968void endpoint_set_hc_data(endpoint_t *instance,
    70     void *data, void (*destroy_hook)(endpoint_t *),
    71     int (*toggle_get)(void *), void (*toggle_set)(void *, int));
     69    void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int));
    7270void endpoint_clear_hc_data(endpoint_t *instance);
    7371
  • uspace/lib/usbhost/include/usb/host/hcd.h

    r7265558 r48ae3ef  
    5151        int (*schedule)(hcd_t *, usb_transfer_batch_t *);
    5252        int (*ep_add_hook)(hcd_t *, endpoint_t *);
     53        void (*ep_remove_hook)(hcd_t *, endpoint_t *);
    5354};
    5455/*----------------------------------------------------------------------------*/
     
    6566{
    6667        assert(hcd);
    67         usb_endpoint_manager_reset_if_need(
     68        usb_endpoint_manager_reset_eps_if_need(
    6869            &hcd->ep_manager, target, (const uint8_t *)setup_data);
    6970}
  • uspace/lib/usbhost/include/usb/host/usb_endpoint_manager.h

    r7265558 r48ae3ef  
    6464    size_t (*bw_count)(usb_speed_t, usb_transfer_type_t, size_t, size_t));
    6565
    66 void usb_endpoint_manager_destroy(usb_endpoint_manager_t *instance);
     66void usb_endpoint_manager_reset_eps_if_need(
     67    usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data);
    6768
    68 int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
    69     endpoint_t *ep, size_t data_size);
    70 
    71 int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
     69int usb_endpoint_manager_register_ep(
     70    usb_endpoint_manager_t *instance, endpoint_t *ep, size_t data_size);
     71int usb_endpoint_manager_unregister_ep(
     72    usb_endpoint_manager_t *instance, endpoint_t *ep);
     73endpoint_t * usb_endpoint_manager_find_ep(usb_endpoint_manager_t *instance,
    7274    usb_address_t address, usb_endpoint_t ep, usb_direction_t direction);
    7375
    74 endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
    75     usb_address_t address, usb_endpoint_t ep, usb_direction_t direction);
    76 
    77 void usb_endpoint_manager_reset_if_need(
    78     usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data);
    79 
    80 /** Wrapper combining allocation and insertion */
    81 static inline int usb_endpoint_manager_add_ep(usb_endpoint_manager_t *instance,
     76int usb_endpoint_manager_add_ep(usb_endpoint_manager_t *instance,
    8277    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
    8378    usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size,
    84     size_t data_size)
    85 {
    86         assert(instance);
    87         const size_t bw =
    88             instance->bw_count(speed, type, data_size, max_packet_size);
     79    size_t data_size, int (*callback)(endpoint_t *, void *), void *arg);
    8980
    90         endpoint_t *ep = endpoint_create(
    91             address, endpoint, direction, type, speed, max_packet_size, bw);
    92         if (!ep)
    93                 return ENOMEM;
    94 
    95         const int ret =
    96             usb_endpoint_manager_register_ep(instance, ep, data_size);
    97         if (ret != EOK) {
    98                 endpoint_destroy(ep);
    99         }
    100         return ret;
    101 }
     81int usb_endpoint_manager_remove_ep(usb_endpoint_manager_t *instance,
     82    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
     83    void (*callback)(endpoint_t *, void *), void *arg);
    10284#endif
    10385/**
    10486 * @}
    10587 */
    106 
  • uspace/lib/usbhost/src/endpoint.c

    r7265558 r48ae3ef  
    5454                instance->toggle = 0;
    5555                instance->active = false;
    56                 instance->destroy_hook = NULL;
    5756                instance->hc_data.data = NULL;
    5857                instance->hc_data.toggle_get = NULL;
     
    7069        assert(instance);
    7170        assert(!instance->active);
    72         if (instance->hc_data.data) {
    73                 assert(instance->destroy_hook);
    74                 instance->destroy_hook(instance);
    75         }
     71        assert(instance->hc_data.data == NULL);
    7672        free(instance);
    7773}
    7874/*----------------------------------------------------------------------------*/
    7975void endpoint_set_hc_data(endpoint_t *instance,
    80     void *data, void (*destroy_hook)(endpoint_t *),
    81     int (*toggle_get)(void *), void (*toggle_set)(void *, int))
     76    void *data, int (*toggle_get)(void *), void (*toggle_set)(void *, int))
    8277{
    8378        assert(instance);
    84         instance->destroy_hook = destroy_hook;
    8579        instance->hc_data.data = data;
    8680        instance->hc_data.toggle_get = toggle_get;
     
    9185{
    9286        assert(instance);
    93         instance->destroy_hook = NULL;
    9487        instance->hc_data.data = NULL;
    9588        instance->hc_data.toggle_get = NULL;
  • uspace/lib/usbhost/src/iface.c

    r7265558 r48ae3ef  
    4949        assert(hcd);
    5050
    51         endpoint_t *ep = usb_endpoint_manager_get_ep(&hcd->ep_manager,
     51        endpoint_t *ep = usb_endpoint_manager_find_ep(&hcd->ep_manager,
    5252            target.address, target.endpoint, direction);
    5353        if (ep == NULL) {
     
    165165}
    166166/*----------------------------------------------------------------------------*/
     167static int register_helper(endpoint_t *ep, void *arg)
     168{
     169        hcd_t *hcd = arg;
     170        assert(ep);
     171        assert(hcd);
     172        if (hcd->ep_add_hook)
     173                return hcd->ep_add_hook(hcd, ep);
     174        return EOK;
     175}
     176/*----------------------------------------------------------------------------*/
     177static void unregister_helper(endpoint_t *ep, void *arg)
     178{
     179        hcd_t *hcd = arg;
     180        assert(ep);
     181        assert(hcd);
     182        if (hcd->ep_remove_hook)
     183                hcd->ep_remove_hook(hcd, ep);
     184}
     185/*----------------------------------------------------------------------------*/
    167186static int register_endpoint(
    168187    ddf_fun_t *fun, usb_address_t address, usb_speed_t ep_speed,
     
    188207            max_packet_size, interval);
    189208
    190         endpoint_t *ep =
    191             endpoint_create(address, endpoint, direction, transfer_type,
    192                 speed, max_packet_size, 0);
    193         if (!ep)
    194                 return ENOMEM;
    195 
    196         if (hcd->ep_add_hook) {
    197                 const int ret = hcd->ep_add_hook(hcd, ep);
    198                 if (ret != EOK) {
    199                         endpoint_destroy(ep);
    200                         return ret;
    201                 }
    202         }
    203 
    204         const int ret =
    205             usb_endpoint_manager_register_ep(&hcd->ep_manager, ep, size);
    206         if (ret != EOK) {
    207                 endpoint_destroy(ep);
    208         }
    209         return ret;
     209        return usb_endpoint_manager_add_ep(&hcd->ep_manager, address, endpoint,
     210            direction, transfer_type, speed, max_packet_size, size,
     211            register_helper, hcd);
    210212}
    211213/*----------------------------------------------------------------------------*/
     
    219221        usb_log_debug("Unregister endpoint %d:%d %s.\n",
    220222            address, endpoint, usb_str_direction(direction));
    221         return usb_endpoint_manager_unregister_ep(&hcd->ep_manager, address,
    222             endpoint, direction);
     223        return usb_endpoint_manager_remove_ep(&hcd->ep_manager, address,
     224            endpoint, direction, unregister_helper, hcd);
    223225}
    224226/*----------------------------------------------------------------------------*/
  • uspace/lib/usbhost/src/usb_endpoint_manager.c

    r7265558 r48ae3ef  
    120120}
    121121/*----------------------------------------------------------------------------*/
    122 int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
    123     endpoint_t *ep, size_t data_size)
    124 {
    125         assert(instance);
    126         assert(instance->bw_count);
    127         assert(ep);
    128         ep->bandwidth = instance->bw_count(ep->speed, ep->transfer_type,
    129             data_size, ep->max_packet_size);
    130 
    131         fibril_mutex_lock(&instance->guard);
    132 
    133         if (ep->bandwidth > instance->free_bw) {
    134                 fibril_mutex_unlock(&instance->guard);
    135                 return ENOSPC;
    136         }
    137 
    138         /* Check for existence */
    139         const endpoint_t *endpoint =
    140             find_locked(instance, ep->address, ep->endpoint, ep->direction);
    141         if (endpoint != NULL) {
    142                 fibril_mutex_unlock(&instance->guard);
    143                 return EEXISTS;
    144         }
    145         list_t *list = get_list(instance, ep->address);
    146         list_append(&ep->link, list);
    147 
    148         instance->free_bw -= ep->bandwidth;
    149         fibril_mutex_unlock(&instance->guard);
    150         return EOK;
    151 }
    152 /*----------------------------------------------------------------------------*/
    153 int usb_endpoint_manager_unregister_ep(usb_endpoint_manager_t *instance,
    154     usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
    155 {
    156         assert(instance);
    157 
    158         fibril_mutex_lock(&instance->guard);
    159         endpoint_t *ep = find_locked(instance, address, endpoint, direction);
    160         if (ep != NULL) {
    161                 list_remove(&ep->link);
    162                 instance->free_bw += ep->bandwidth;
    163         }
    164 
    165         fibril_mutex_unlock(&instance->guard);
    166         endpoint_destroy(ep);
    167         return (ep != NULL) ? EOK : EINVAL;
    168 }
    169 /*----------------------------------------------------------------------------*/
    170 endpoint_t * usb_endpoint_manager_get_ep(usb_endpoint_manager_t *instance,
    171     usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
    172 {
    173         assert(instance);
    174 
    175         fibril_mutex_lock(&instance->guard);
    176         endpoint_t *ep = find_locked(instance, address, endpoint, direction);
    177         fibril_mutex_unlock(&instance->guard);
    178 
    179         return ep;
    180 }
    181 /*----------------------------------------------------------------------------*/
    182122/** Check setup packet data for signs of toggle reset.
    183123 *
     
    188128 * Really ugly one.
    189129 */
    190 void usb_endpoint_manager_reset_if_need(
     130void usb_endpoint_manager_reset_eps_if_need(
    191131    usb_endpoint_manager_t *instance, usb_target_t target, const uint8_t *data)
    192132{
     
    234174        }
    235175}
     176/*----------------------------------------------------------------------------*/
     177int usb_endpoint_manager_register_ep(usb_endpoint_manager_t *instance,
     178    endpoint_t *ep, size_t data_size)
     179{
     180        assert(instance);
     181        assert(instance->bw_count);
     182        assert(ep);
     183        fibril_mutex_lock(&instance->guard);
     184
     185        if (ep->bandwidth > instance->free_bw) {
     186                fibril_mutex_unlock(&instance->guard);
     187                return ENOSPC;
     188        }
     189
     190        /* Check for existence */
     191        const endpoint_t *endpoint =
     192            find_locked(instance, ep->address, ep->endpoint, ep->direction);
     193        if (endpoint != NULL) {
     194                fibril_mutex_unlock(&instance->guard);
     195                return EEXISTS;
     196        }
     197        list_t *list = get_list(instance, ep->address);
     198        list_append(&ep->link, list);
     199
     200        instance->free_bw -= ep->bandwidth;
     201        fibril_mutex_unlock(&instance->guard);
     202        return EOK;
     203}
     204/*----------------------------------------------------------------------------*/
     205int usb_endpoint_manager_unregister_ep(
     206    usb_endpoint_manager_t *instance, endpoint_t *ep)
     207{
     208        assert(instance);
     209        if (ep == NULL)
     210                return ENOENT;
     211        fibril_mutex_lock(&instance->guard);
     212        list_remove(&ep->link);
     213        fibril_mutex_unlock(&instance->guard);
     214        return EOK;
     215}
     216/*----------------------------------------------------------------------------*/
     217endpoint_t * usb_endpoint_manager_find_ep(usb_endpoint_manager_t *instance,
     218    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction)
     219{
     220        assert(instance);
     221
     222        fibril_mutex_lock(&instance->guard);
     223        endpoint_t *ep = find_locked(instance, address, endpoint, direction);
     224        fibril_mutex_unlock(&instance->guard);
     225
     226        return ep;
     227}
     228/*----------------------------------------------------------------------------*/
     229int usb_endpoint_manager_add_ep(usb_endpoint_manager_t *instance,
     230    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
     231    usb_transfer_type_t type, usb_speed_t speed, size_t max_packet_size,
     232    size_t data_size, int (*callback)(endpoint_t *, void *), void *arg)
     233{
     234        assert(instance);
     235        const size_t bw =
     236            instance->bw_count(speed, type, data_size, max_packet_size);
     237
     238        endpoint_t *ep = endpoint_create(
     239            address, endpoint, direction, type, speed, max_packet_size, bw);
     240        if (!ep)
     241                return ENOMEM;
     242
     243        if (callback) {
     244                const int ret = callback(ep, arg);
     245                if (ret != EOK) {
     246                        endpoint_destroy(ep);
     247                        return ret;
     248                }
     249        }
     250
     251        const int ret =
     252            usb_endpoint_manager_register_ep(instance, ep, data_size);
     253        if (ret != EOK) {
     254                endpoint_destroy(ep);
     255        }
     256        return ret;
     257}
     258/*----------------------------------------------------------------------------*/
     259int usb_endpoint_manager_remove_ep(usb_endpoint_manager_t *instance,
     260    usb_address_t address, usb_endpoint_t endpoint, usb_direction_t direction,
     261    void (*callback)(endpoint_t *, void *), void *arg)
     262{
     263        assert(instance);
     264        fibril_mutex_lock(&instance->guard);
     265        endpoint_t *ep = find_locked(instance, address, endpoint, direction);
     266        if (ep != NULL) {
     267                list_remove(&ep->link);
     268                instance->free_bw += ep->bandwidth;
     269        }
     270        fibril_mutex_unlock(&instance->guard);
     271        if (ep == NULL)
     272                return ENOENT;
     273
     274        if (callback) {
     275                callback(ep, arg);
     276        }
     277        endpoint_destroy(ep);
     278        return EOK;
     279}
Note: See TracChangeset for help on using the changeset viewer.