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

Changeset 6210a333 in mainline


Ignore:
Timestamp:
2013-09-21T05:09:46Z (8 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master
Children:
19d21728
Parents:
30e8ab4
Message:

uhci, ohci: Cleanup irq code generation.

Location:
uspace/drv/bus/usb
Files:
4 edited

Legend:

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

    r30e8ab4 r6210a333  
    4242#include <usb/usb.h>
    4343
     44#include "macros.h"
    4445#include "hc.h"
    4546#include "ohci_endpoint.h"
     
    8788static int hc_init_memory(hc_t *instance);
    8889static int interrupt_emulator(hc_t *instance);
    89 
    90 /** Get number of PIO ranges used in IRQ code.
    91  * @return Number of ranges.
    92  */
    93 size_t hc_irq_pio_range_count(void)
    94 {
    95         return sizeof(ohci_pio_ranges) / sizeof(irq_pio_range_t);
    96 }
    97 
    98 /** Get number of commands used in IRQ code.
    99  * @return Number of commands.
    100  */
    101 size_t hc_irq_cmd_count(void)
    102 {
    103         return sizeof(ohci_irq_commands) / sizeof(irq_cmd_t);
    104 }
    10590
    10691/** Generate IRQ code.
     
    11398 * @return Error code.
    11499 */
    115 int
    116 hc_get_irq_code(irq_pio_range_t ranges[], size_t ranges_size, irq_cmd_t cmds[],
    117     size_t cmds_size, addr_range_t *regs)
    118 {
    119         if ((ranges_size < sizeof(ohci_pio_ranges)) ||
    120             (cmds_size < sizeof(ohci_irq_commands)) ||
    121             (RNGSZ(*regs) < sizeof(ohci_regs_t)))
     100int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs)
     101{
     102        assert(code);
     103        if (RNGSZ(*regs) < sizeof(ohci_regs_t))
    122104                return EOVERFLOW;
    123105
    124         memcpy(ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
    125         ranges[0].base = RNGABS(*regs);
    126 
    127         memcpy(cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
     106        code->ranges = malloc(sizeof(ohci_pio_ranges));
     107        if (code->ranges == NULL)
     108                return ENOMEM;
     109
     110        code->cmds = malloc(sizeof(ohci_irq_commands));
     111        if (code->cmds == NULL) {
     112                free(code->ranges);
     113                return ENOMEM;
     114        }
     115
     116        code->rangecount = ARRAY_SIZE(ohci_pio_ranges);
     117        code->cmdcount = ARRAY_SIZE(ohci_irq_commands);
     118
     119        memcpy(code->ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
     120        code->ranges[0].base = RNGABS(*regs);
     121
     122        memcpy(code->cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
    128123        ohci_regs_t *registers = (ohci_regs_t *) RNGABSPTR(*regs);
    129         cmds[0].addr = (void *) &registers->interrupt_status;
    130         cmds[3].addr = (void *) &registers->interrupt_status;
    131         OHCI_WR(cmds[1].value, OHCI_USED_INTERRUPTS);
     124        code->cmds[0].addr = (void *) &registers->interrupt_status;
     125        code->cmds[3].addr = (void *) &registers->interrupt_status;
     126        OHCI_WR(code->cmds[1].value, OHCI_USED_INTERRUPTS);
    132127
    133128        return EOK;
     
    146141    interrupt_handler_t handler)
    147142{
    148         int rc;
    149 
    150         irq_pio_range_t irq_ranges[hc_irq_pio_range_count()];
    151         irq_cmd_t irq_cmds[hc_irq_cmd_count()];
    152 
    153         irq_code_t irq_code = {
    154                 .rangecount = hc_irq_pio_range_count(),
    155                 .ranges = irq_ranges,
    156                 .cmdcount = hc_irq_cmd_count(),
    157                 .cmds = irq_cmds
    158         };
    159 
    160         rc = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
    161             sizeof(irq_cmds), regs);
    162         if (rc != EOK) {
     143        irq_code_t irq_code = { 0 };
     144
     145        int ret = hc_gen_irq_code(&irq_code, regs);
     146        if (ret != EOK) {
    163147                usb_log_error("Failed to generate IRQ code: %s.\n",
    164                     str_error(rc));
    165                 return rc;
    166         }
     148                    str_error(ret));
     149                return ret;
     150        }
     151
     152        //TODO we leak memory here
    167153
    168154        /* Register handler to avoid interrupt lockup */
    169         rc = register_interrupt_handler(device, irq, handler, &irq_code);
    170         if (rc != EOK) {
     155        ret = register_interrupt_handler(device, irq, handler, &irq_code);
     156        if (ret != EOK) {
    171157                usb_log_error("Failed to register interrupt handler: %s.\n",
    172                     str_error(rc));
    173                 return rc;
     158                    str_error(ret));
     159                return ret;
    174160        }
    175161
  • uspace/drv/bus/usb/ohci/hc.h

    r30e8ab4 r6210a333  
    7272} hc_t;
    7373
    74 size_t hc_irq_pio_range_count(void);
    75 size_t hc_irq_cmd_count(void);
    76 int hc_get_irq_code(irq_pio_range_t [], size_t, irq_cmd_t [], size_t,
    77     addr_range_t *);
     74int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs);
    7875int hc_register_irq_handler(ddf_dev_t *, addr_range_t *, int,
    7976    interrupt_handler_t);
  • uspace/drv/bus/usb/uhci/hc.c

    r30e8ab4 r6210a333  
    9090
    9191
    92 /** Get number of PIO ranges used in IRQ code.
    93  * @return Number of ranges.
    94  */
    95 size_t hc_irq_pio_range_count(void)
    96 {
    97         return sizeof(uhci_irq_pio_ranges) / sizeof(irq_pio_range_t);
    98 }
    99 
    100 /** Get number of commands used in IRQ code.
    101  * @return Number of commands.
    102  */
    103 size_t hc_irq_cmd_count(void)
    104 {
    105         return sizeof(uhci_irq_commands) / sizeof(irq_cmd_t);
    106 }
    107 
    10892/** Generate IRQ code.
    109  * @param[out] ranges PIO ranges buffer.
    110  * @param[in] ranges_size Size of the ranges buffer (bytes).
    111  * @param[out] cmds Commands buffer.
    112  * @param[in] cmds_size Size of the commands buffer (bytes).
     93 * @param[out] code IRQ code structure.
    11394 * @param[in] regs Device's register range.
    11495 *
    11596 * @return Error code.
    11697 */
    117 int
    118 hc_get_irq_code(irq_pio_range_t ranges[], size_t ranges_size, irq_cmd_t cmds[],
    119     size_t cmds_size, addr_range_t *regs)
    120 {
    121         if ((ranges_size < sizeof(uhci_irq_pio_ranges)) ||
    122             (cmds_size < sizeof(uhci_irq_commands)) ||
    123             (RNGSZ(*regs) < sizeof(uhci_regs_t)))
     98int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs)
     99{
     100        assert(code);
     101
     102        if (RNGSZ(*regs) < sizeof(uhci_regs_t))
    124103                return EOVERFLOW;
    125104
    126         memcpy(ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
    127         ranges[0].base = RNGABS(*regs);
    128 
    129         memcpy(cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
     105        code->ranges = malloc(sizeof(uhci_irq_pio_ranges));
     106        if (code->ranges == NULL)
     107                return ENOMEM;
     108
     109        code->cmds = malloc(sizeof(uhci_irq_commands));
     110        if (code->cmds == NULL) {
     111                free(code->ranges);
     112                return ENOMEM;
     113        }
     114
     115        code->rangecount = ARRAY_SIZE(uhci_irq_pio_ranges);
     116        code->cmdcount = ARRAY_SIZE(uhci_irq_commands);
     117
     118        memcpy(code->ranges, uhci_irq_pio_ranges, sizeof(uhci_irq_pio_ranges));
     119        code->ranges[0].base = RNGABS(*regs);
     120
     121        memcpy(code->cmds, uhci_irq_commands, sizeof(uhci_irq_commands));
    130122        uhci_regs_t *registers = (uhci_regs_t *) RNGABSPTR(*regs);
    131         cmds[0].addr = (void*)&registers->usbsts;
    132         cmds[3].addr = (void*)&registers->usbsts;
     123        code->cmds[0].addr = (void*)&registers->usbsts;
     124        code->cmds[3].addr = (void*)&registers->usbsts;
    133125
    134126        return EOK;
     
    148140{
    149141        assert(device);
    150         irq_pio_range_t irq_ranges[hc_irq_pio_range_count()];
    151         irq_cmd_t irq_cmds[hc_irq_cmd_count()];
    152 
    153         int ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
    154             sizeof(irq_cmds), regs);
     142
     143        irq_code_t irq_code = { 0 };
     144
     145        int ret = hc_gen_irq_code(&irq_code, regs);
    155146        if (ret != EOK) {
    156147                usb_log_error("Failed to generate IRQ commands: %s.\n",
     
    158149                return ret;
    159150        }
    160 
    161         irq_code_t irq_code = {
    162                 .rangecount = hc_irq_pio_range_count(),
    163                 .ranges = irq_ranges,
    164                 .cmdcount = hc_irq_cmd_count(),
    165                 .cmds = irq_cmds
    166         };
     151        //TODO we leak memory here
    167152
    168153        /* Register handler to avoid interrupt lockup */
  • uspace/drv/bus/usb/uhci/hc.h

    r30e8ab4 r6210a333  
    124124} hc_t;
    125125
    126 size_t hc_irq_pio_range_count(void);
    127 size_t hc_irq_cmd_count(void);
    128126int hc_register_irq_handler(ddf_dev_t *, addr_range_t *, int,
    129127    interrupt_handler_t);
    130 int hc_get_irq_code(irq_pio_range_t [], size_t, irq_cmd_t [], size_t,
    131     addr_range_t *);
     128int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs);
    132129void hc_interrupt(hc_t *instance, uint16_t status);
    133130int hc_init(hc_t *instance, addr_range_t *regs, bool interupts);
Note: See TracChangeset for help on using the changeset viewer.