Changeset 7dddd7b in mainline


Ignore:
Timestamp:
2018-01-13T20:47:58Z (7 years ago)
Author:
Petr Manek <petr.manek@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
71f211f
Parents:
2489353
git-author:
Petr Manek <petr.manek@…> (2018-01-13 19:20:41)
git-committer:
Petr Manek <petr.manek@…> (2018-01-13 20:47:58)
Message:

usbdev: refactor polling

Until now, device polling had to be executed by calling one of four
proxy functions, which served as a syntax sugar and had no clear
distinction between each other (not to mention misleading names and high
number of arguments).

In this commit, the four mentioned functions are discarded in favor of
one main function, which was proxied by them either way. The number of
arguments have decreased in favor of named struct fields in the auto
polling config structure.

Drivers, which make use of polling, such as usbhid and usbhub were
updated to the latest API design.

Location:
uspace
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbhub/usbhub.c

    r2489353 r7dddd7b  
    164164
    165165        /* Start hub operation. */
    166         opResult = usb_device_auto_poll_desc(hub_dev->usb_device,
    167             &hub_status_change_endpoint_description,
    168             hub_port_changes_callback, ((hub_dev->port_count + 1 + 7) / 8),
    169             -1, usb_hub_polling_error_callback,
    170             usb_hub_polling_terminated_callback, hub_dev);
     166        const usb_device_auto_polling_t auto_polling = {
     167                .debug = 1,
     168                .auto_clear_halt = true,
     169                .delay = -1,
     170                .max_failures = 3,
     171                .on_data = hub_port_changes_callback,
     172                .on_polling_end = usb_hub_polling_terminated_callback,
     173                .on_error = usb_hub_polling_error_callback,
     174                .arg = hub_dev,
     175        };
     176
     177        usb_endpoint_mapping_t *epm =
     178            usb_device_get_mapped_ep_desc(hub_dev->usb_device,
     179            &hub_status_change_endpoint_description);
     180        opResult = usb_device_auto_polling(hub_dev->usb_device, epm,
     181            &auto_polling, ((hub_dev->port_count + 1 + 7) / 8));
     182       
    171183        if (opResult != EOK) {
    172184                /* Function is already bound */
  • uspace/drv/hid/usbhid/main.c

    r2489353 r7dddd7b  
    9090         * This will create a separate fibril that will query the device
    9191         * for the data continuously. */
    92         rc = usb_device_auto_poll_desc(dev,
    93            /* Index of the polling pipe. */
    94            hid_dev->poll_pipe_mapping->description,
    95            /* Callback when data arrives. */
    96            usb_hid_polling_callback,
    97            /* How much data to request. */
    98            hid_dev->poll_pipe_mapping->pipe.desc.max_transfer_size,
    99            /* Delay */
    100            -1,
    101            /* Callback when the polling fails. */
    102            usb_hid_polling_error_callback,
    103            /* Callback when the polling ends. */
    104            usb_hid_polling_ended_callback,
    105            /* Custom argument. */
    106            hid_dev);
     92        const usb_device_auto_polling_t auto_polling = {
     93                .debug = 1,
     94                .auto_clear_halt = true,
     95                .delay = -1,
     96                .max_failures = 3,
     97                .on_data = usb_hid_polling_callback,
     98                .on_polling_end = usb_hid_polling_ended_callback,
     99                .on_error = usb_hid_polling_error_callback,
     100                .arg = hid_dev,
     101        };
     102
     103        rc = usb_device_auto_polling(dev, hid_dev->poll_pipe_mapping,
     104            &auto_polling, hid_dev->poll_pipe_mapping->pipe.desc.max_transfer_size);
    107105
    108106        if (rc != EOK) {
  • uspace/lib/usbdev/include/usb/dev/poll.h

    r2489353 r7dddd7b  
    9696typedef void (*usb_polling_terminted_callback_t)(usb_device_t *, bool, void *);
    9797
    98 extern int usb_device_auto_polling(usb_device_t *, usb_endpoint_t,
     98extern int usb_device_auto_polling(usb_device_t *, usb_endpoint_mapping_t *,
    9999    const usb_device_auto_polling_t *, size_t);
    100 
    101 extern int usb_device_auto_poll(usb_device_t *, usb_endpoint_t,
    102     usb_polling_callback_t, size_t, int, usb_polling_terminted_callback_t, void *);
    103 
    104 extern int usb_device_auto_polling_desc(usb_device_t *,
    105     const usb_endpoint_description_t *, const usb_device_auto_polling_t *,
    106     size_t);
    107 
    108 extern int usb_device_auto_poll_desc(usb_device_t *,
    109     const usb_endpoint_description_t *, usb_polling_callback_t, size_t, int,
    110     usb_polling_error_callback_t, usb_polling_terminted_callback_t, void *);
    111100
    112101#endif
  • uspace/lib/usbdev/src/devpoll.c

    r2489353 r7dddd7b  
    203203 * @param epm Endpoint mapping to use.
    204204 * @param polling Polling settings.
    205  * @param request_size How many bytes to ask for in each request.
    206  * @param arg Custom argument (passed as is to the callbacks).
     205 * @param req_size How many bytes to ask for in each request.
    207206 * @return Error code.
    208207 * @retval EOK New fibril polling the device was already started.
    209208 */
    210 static int usb_device_auto_polling_internal(usb_device_t *dev,
    211     usb_endpoint_mapping_t *epm, const usb_device_auto_polling_t *polling,
    212     size_t request_size)
     209int usb_device_auto_polling(usb_device_t *dev, usb_endpoint_mapping_t *epm,
     210    const usb_device_auto_polling_t *polling, size_t req_size)
    213211{
    214         if ((dev == NULL) || (polling == NULL) || (polling->on_data == NULL)) {
     212        int rc;
     213        if (!dev || !polling || !polling->on_data)
    215214                return EBADMEM;
    216         }
    217 
    218         if (request_size == 0)
     215
     216        if (!req_size)
    219217                return EINVAL;
    220218
     
    223221                return EINVAL;
    224222
    225 
    226223        polling_data_t *polling_data = malloc(sizeof(polling_data_t));
    227         if (polling_data == NULL) {
     224        if (!polling_data)
    228225                return ENOMEM;
    229         }
    230226
    231227        /* Fill-in the data. */
    232         polling_data->buffer = malloc(request_size);
     228        polling_data->buffer = malloc(req_size);
    233229        if (polling_data->buffer == NULL) {
    234                 free(polling_data);
    235                 return ENOMEM;
    236         }
    237         polling_data->request_size = request_size;
     230                rc = ENOMEM;
     231                goto err_polling_data;
     232        }
     233        polling_data->request_size = req_size;
    238234        polling_data->dev = dev;
    239235        polling_data->polling_mapping = epm;
     
    249245
    250246        fid_t fibril = fibril_create(polling_fibril, polling_data);
    251         if (fibril == 0) {
    252                 free(polling_data->buffer);
    253                 free(polling_data);
    254                 return ENOMEM;
     247        if (!fibril) {
     248                rc = ENOMEM;
     249                goto err_buffer;
    255250        }
    256251        fibril_add_ready(fibril);
    257252
    258253        /* Fibril launched. That fibril will free the allocated data. */
    259 
    260254        return EOK;
    261 }
    262 /** Start automatic device polling over interrupt in pipe.
    263  *
    264  * The polling settings is copied thus it is okay to destroy the structure
    265  * after this function returns.
    266  *
    267  * @warning There is no guarantee when the request to the device
    268  * will be sent for the first time (it is possible that this
    269  * first request would be executed prior to return from this function).
    270  *
    271  * @param dev Device to be periodically polled.
    272  * @param pipe_index Index of the endpoint pipe used for polling.
    273  * @param polling Polling settings.
    274  * @param req_size How many bytes to ask for in each request.
    275  * @param arg Custom argument (passed as is to the callbacks).
    276  * @return Error code.
    277  * @retval EOK New fibril polling the device was already started.
    278  */
    279 int usb_device_auto_polling(usb_device_t *usb_dev, usb_endpoint_t ep,
    280     const usb_device_auto_polling_t *polling, size_t req_size)
    281 {
    282         usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(usb_dev, ep);
    283         return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
    284 }
    285 
    286 /** Start automatic device polling over interrupt in pipe.
    287  *
    288  * @warning It is up to the callback to produce delays between individual
    289  * requests.
    290  *
    291  * @warning There is no guarantee when the request to the device
    292  * will be sent for the first time (it is possible that this
    293  * first request would be executed prior to return from this function).
    294  *
    295  * @param dev Device to be periodically polled.
    296  * @param ep Endpoint  used for polling.
    297  * @param callback Callback when data are available.
    298  * @param request_size How many bytes to ask for in each request.
    299  * @param delay NUmber of ms to wait between queries, -1 to use descriptor val.
    300  * @param terminated_callback Callback when polling is terminated.
    301  * @param arg Custom argument (passed as is to the callbacks).
    302  * @return Error code.
    303  * @retval EOK New fibril polling the device was already started.
    304  */
    305 int usb_device_auto_poll(usb_device_t *dev, usb_endpoint_t ep,
    306     usb_polling_callback_t callback, size_t request_size, int delay,
    307     usb_polling_terminted_callback_t terminated_callback, void *arg)
    308 {
    309         const usb_device_auto_polling_t auto_polling = {
    310                 .debug = 1,
    311                 .auto_clear_halt = true,
    312                 .delay = delay,
    313                 .max_failures = MAX_FAILED_ATTEMPTS,
    314                 .on_data = callback,
    315                 .on_polling_end = terminated_callback,
    316                 .on_error = NULL,
    317                 .arg = arg,
    318         };
    319 
    320         usb_endpoint_mapping_t *epm = usb_device_get_mapped_ep(dev, ep);
    321         return usb_device_auto_polling_internal(
    322             dev, epm, &auto_polling, request_size);
    323 }
    324 
    325 int usb_device_auto_polling_desc(usb_device_t *usb_dev,
    326     const usb_endpoint_description_t *desc,
    327     const usb_device_auto_polling_t *polling, size_t req_size)
    328 {
    329         usb_endpoint_mapping_t *epm =
    330             usb_device_get_mapped_ep_desc(usb_dev, desc);
    331         return usb_device_auto_polling_internal(usb_dev, epm, polling, req_size);
    332 }
    333 
    334 int usb_device_auto_poll_desc(usb_device_t * usb_dev,
    335     const usb_endpoint_description_t *desc, usb_polling_callback_t callback,
    336     size_t req_size, int delay, usb_polling_error_callback_t error_callback,
    337     usb_polling_terminted_callback_t terminated_callback, void *arg)
    338 {
    339         const usb_device_auto_polling_t auto_polling = {
    340                 .debug = 1,
    341                 .auto_clear_halt = true,
    342                 .delay = delay,
    343                 .max_failures = MAX_FAILED_ATTEMPTS,
    344                 .on_data = callback,
    345                 .on_polling_end = terminated_callback,
    346                 .on_error = error_callback,
    347                 .arg = arg,
    348         };
    349 
    350         usb_endpoint_mapping_t *epm =
    351             usb_device_get_mapped_ep_desc(usb_dev, desc);
    352         return usb_device_auto_polling_internal(
    353             usb_dev, epm, &auto_polling, req_size);
     255
     256err_buffer:
     257        free(polling_data->buffer);
     258err_polling_data:
     259        free(polling_data);
     260        return rc;
    354261}
    355262
Note: See TracChangeset for help on using the changeset viewer.