Changeset 8c9e71a in mainline


Ignore:
Timestamp:
2011-09-27T15:55:27Z (12 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1069714
Parents:
d3a9ae74 (diff), 747a1e71 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merg with mainline

Files:
2 added
4 deleted
13 edited
1 moved

Legend:

Unmodified
Added
Removed
  • kernel/arch/ia32/src/boot/boot.S

    rd3a9ae74 r8c9e71a  
    22 * Copyright (c) 2001 Jakub Jermar
    33 * Copyright (c) 2005 Martin Decky
     4 * Copyright (c) 2011 Martin Sucha
    45 * All rights reserved.
    56 *
     
    124125                /* Map kernel and turn paging on */
    125126                pm_status $status_non_pse
    126                 call map_kernel
     127                call map_kernel_non_pse
    127128       
    128129        stack_init:
     
    195196 *
    196197 */
    197 .global map_kernel
    198 map_kernel:
     198map_kernel_non_pse:
    199199        /* Paging features */
    200200        movl %cr4, %ecx
  • kernel/arch/ia32/src/smp/ap.S

    rd3a9ae74 r8c9e71a  
    2828#
    2929
    30 #
    31 # Init code for application processors.
    32 #
     30/*
     31 * Init code for application processors.
     32 */
    3333
    3434#include <arch/boot/boot.h>
     
    4646KDATA=16
    4747
    48 # This piece of code is real-mode and is meant to be aligned at 4K boundary.
    49 # The requirement for such an alignment comes from MP Specification's STARTUP IPI
    50 # requirements.
     48/*
     49 * This piece of code is real-mode and is meant to be aligned at 4K boundary.
     50 * The requirement for such an alignment comes from MP Specification's
     51 * STARTUP IPI requirements.
     52 */
    5153
    5254.align 4096
     
    5759        movw %ax, %ds
    5860
    59         lgdtl ap_gdtr           # initialize Global Descriptor Table register
     61        /* initialize Global Descriptor Table register */
     62        lgdtl ap_gdtr
    6063       
     64        /* switch to protected mode */
    6165        movl %cr0, %eax
    6266        orl $1, %eax
    63         movl %eax, %cr0                         # switch to protected mode
     67        movl %eax, %cr0
    6468        jmpl $KTEXT, $jump_to_kernel - BOOT_OFFSET + AP_BOOT_OFFSET
    6569       
     
    7074        movw %ax, %es
    7175        movw %ax, %ss
    72         movl $KA2PA(ctx), %eax                  # KA2PA((uintptr_t) &ctx)
     76        movl $KA2PA(ctx), %eax  /* KA2PA((uintptr_t) &ctx) */
    7377        movl (%eax), %esp
    74         subl $0x80000000, %esp                  # KA2PA(ctx.sp)
     78        subl $0x80000000, %esp  /* KA2PA(ctx.sp) */
    7579
    76         call map_kernel                                 # map kernel and turn paging on
     80        /*
     81         * Map kernel and turn paging on.
     82         * We assume that when using SMP, PSE is always available
     83         */
     84        call map_kernel_pse
    7785       
    78         addl $0x80000000, %esp                  # PA2KA(ctx.sp)
     86        addl $0x80000000, %esp  /*  PA2KA(ctx.sp) */
    7987       
    80         pushl $0                                # create the first stack frame
     88        /* create the first stack frame */
     89        pushl $0
    8190        movl %esp, %ebp
    8291
  • uspace/app/bdsh/cmds/modules/mkfile/mkfile.c

    rd3a9ae74 r8c9e71a  
    5454static struct option const long_options[] = {
    5555        {"size", required_argument, 0, 's'},
     56        {"sparse", no_argument, 0, 'p'},
    5657        {"help", no_argument, 0, 'h'},
    5758        {0, 0, 0, 0}
     
    6970                "  -h, --help       A short option summary\n"
    7071                "  -s, --size sz    Size of the file\n"
     72                "  -p, --sparse     Create a sparse file\n"
    7173                "\n"
    7274                "Size is a number followed by 'k', 'm' or 'g' for kB, MB, GB.\n"
     
    115117        ssize_t file_size;
    116118        ssize_t total_written;
    117         ssize_t to_write, rc;
     119        ssize_t to_write, rc, rc2 = 0;
    118120        char *file_name;
    119121        void *buffer;
     122        bool create_sparse = false;
    120123
    121124        file_size = 0;
     
    124127
    125128        for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
    126                 c = getopt_long(argc, argv, "s:h", long_options, &opt_ind);
     129                c = getopt_long(argc, argv, "ps:h", long_options, &opt_ind);
    127130                switch (c) {
    128131                case 'h':
    129132                        help_cmd_mkfile(HELP_LONG);
    130133                        return CMD_SUCCESS;
     134                case 'p':
     135                        create_sparse = true;
     136                        break;
    131137                case 's':
    132138                        file_size = read_size(optarg);
     
    154160                printf("%s: failed to create file %s.\n", cmdname, file_name);
    155161                return CMD_FAILURE;
     162        }
     163
     164        if (create_sparse && file_size > 0) {
     165                const char byte = 0x00;
     166
     167                if ((rc2 = lseek(fd, file_size - 1, SEEK_SET)) < 0)
     168                        goto exit;
     169
     170                rc2 = write(fd, &byte, sizeof(char));
     171                goto exit;
    156172        }
    157173
     
    174190        }
    175191
     192        free(buffer);
     193exit:
    176194        rc = close(fd);
    177         if (rc != 0) {
     195
     196        if (rc != 0 || rc2 < 0) {
    178197                printf("%s: Error writing file (%zd).\n", cmdname, rc);
    179198                return CMD_FAILURE;
    180199        }
    181 
    182         free(buffer);
    183200
    184201        return CMD_SUCCESS;
  • uspace/drv/bus/usb/usbhub/Makefile

    rd3a9ae74 r8c9e71a  
    11#
    22# Copyright (c) 2010 Vojtech Horky
     3# Copyright (c) 2011 Jan Vesely
    34# All rights reserved.
    45#
     
    4344SOURCES = \
    4445        main.c \
    45         utils.c \
    4646        usbhub.c \
    47         ports.c
     47        port.c
    4848
    4949include $(USPACE_PREFIX)/Makefile.common
  • uspace/drv/bus/usb/usbhub/main.c

    rd3a9ae74 r8c9e71a  
    11/*
    22 * Copyright (c) 2010 Vojtech Horky
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    3839#include <usb/dev/driver.h>
    3940#include <usb/classes/classes.h>
     41#include <usb/debug.h>
    4042
    4143#include "usbhub.h"
    42 #include "usbhub_private.h"
    4344
    4445/** Hub status-change endpoint description.
     
    5657
    5758/**
    58  * usb hub driver operations
     59 * USB hub driver operations
    5960 *
    6061 * The most important one is add_device, which is set to usb_hub_add_device.
     
    6465};
    6566
    66 /**
    67  * hub endpoints, excluding control endpoint
    68  */
     67/** Hub endpoints, excluding control endpoint. */
    6968static usb_endpoint_description_t *usb_hub_endpoints[] = {
    7069        &hub_status_change_endpoint_description,
    71         NULL
     70        NULL,
    7271};
    73 
    74 /**
    75  * static usb hub driver information
    76  */
     72/** Static usb hub driver information. */
    7773static usb_driver_t usb_hub_driver = {
    7874        .name = NAME,
     
    8581{
    8682        printf(NAME ": HelenOS USB hub driver.\n");
    87 
    8883        usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
    8984
     
    9489 * @}
    9590 */
    96 
  • uspace/drv/bus/usb/usbhub/port.h

    rd3a9ae74 r8c9e71a  
    11/*
    22 * Copyright (c) 2011 Vojtech Horky
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
    28 
    2929/** @addtogroup drvusbhub
    3030 * @{
     
    3333 * Hub ports related functions.
    3434 */
    35 #ifndef DRV_USBHUB_PORTS_H
    36 #define DRV_USBHUB_PORTS_H
     35#ifndef DRV_USBHUB_PORT_H
     36#define DRV_USBHUB_PORT_H
    3737
    3838#include <usb/dev/driver.h>
    3939#include <usb/dev/hub.h>
     40#include <usb/classes/hub.h>
    4041
    4142typedef struct usb_hub_info_t usb_hub_info_t;
     
    4344/** Information about single port on a hub. */
    4445typedef struct {
    45         /** Mutex needed by CV for checking port reset. */
    46         fibril_mutex_t reset_mutex;
     46        size_t port_number;
     47        usb_pipe_t *control_pipe;
     48        /** Mutex needed not only by CV for checking port reset. */
     49        fibril_mutex_t mutex;
    4750        /** CV for waiting to port reset completion. */
    4851        fibril_condvar_t reset_cv;
     
    6265 * @param port Port to be initialized.
    6366 */
    64 static inline void usb_hub_port_init(usb_hub_port_t *port) {
     67static inline void usb_hub_port_init(usb_hub_port_t *port, size_t port_number,
     68    usb_pipe_t *control_pipe)
     69{
     70        assert(port);
    6571        port->attached_device.address = -1;
    6672        port->attached_device.handle = 0;
    67         fibril_mutex_initialize(&port->reset_mutex);
     73        port->port_number = port_number;
     74        port->control_pipe = control_pipe;
     75        fibril_mutex_initialize(&port->mutex);
    6876        fibril_condvar_initialize(&port->reset_cv);
    6977}
    7078
    71 
    72 void usb_hub_process_port_interrupt(usb_hub_info_t *hub,
    73         uint16_t port);
    74 
    75 
     79void usb_hub_port_reset_fail(usb_hub_port_t *port);
     80void usb_hub_port_process_interrupt(usb_hub_port_t *port, usb_hub_info_t *hub);
     81int usb_hub_port_clear_feature(
     82    usb_hub_port_t *port, usb_hub_class_feature_t feature);
     83int usb_hub_port_set_feature(
     84    usb_hub_port_t *port, usb_hub_class_feature_t feature);
    7685
    7786#endif
  • uspace/drv/bus/usb/usbhub/usbhub.c

    rd3a9ae74 r8c9e71a  
    11/*
    22 * Copyright (c) 2010 Matus Dekanek
     3 * Copyright (c) 2011 Jan Vesely
    34 * All rights reserved.
    45 *
     
    3839#include <str_error.h>
    3940#include <inttypes.h>
    40 
    41 #include <usb_iface.h>
     41#include <stdio.h>
     42
     43#include <usb/usb.h>
     44#include <usb/debug.h>
     45#include <usb/dev/pipes.h>
     46#include <usb/classes/classes.h>
    4247#include <usb/ddfiface.h>
    4348#include <usb/descriptor.h>
     
    4651#include <usb/classes/hub.h>
    4752#include <usb/dev/poll.h>
    48 #include <stdio.h>
     53#include <usb_iface.h>
    4954
    5055#include "usbhub.h"
    51 #include "usbhub_private.h"
    52 #include "port_status.h"
    53 #include <usb/usb.h>
    54 #include <usb/dev/pipes.h>
    55 #include <usb/classes/classes.h>
    56 
    57 
     56#include "status.h"
     57
     58#define HUB_FNC_NAME "hub"
     59
     60/** Standard get hub global status request */
     61static const usb_device_request_setup_packet_t get_hub_status_request = {
     62        .request_type = USB_HUB_REQ_TYPE_GET_HUB_STATUS,
     63        .request = USB_HUB_REQUEST_GET_STATUS,
     64        .index = 0,
     65        .value = 0,
     66        .length = sizeof(usb_hub_status_t),
     67};
     68
     69static int usb_set_first_configuration(usb_device_t *usb_device);
    5870static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev);
    59 
    6071static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info);
    61 
    62 static int usb_hub_set_configuration(usb_hub_info_t *hub_info);
    63 
    64 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info);
    65 
    66 static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
     72static void usb_hub_over_current(const usb_hub_info_t *hub_info,
    6773    usb_hub_status_t status);
    68 
    69 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
    70     usb_hub_status_t status);
    71 
    72 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info);
    73 
     74static void usb_hub_global_interrupt(const usb_hub_info_t *hub_info);
    7475static void usb_hub_polling_terminated_callback(usb_device_t *device,
    7576    bool was_error, void *data);
    7677
    77 
    78 //*********************************************
    79 //
    80 //  hub driver code, initialization
    81 //
    82 //*********************************************
    83 
    8478/**
    8579 * Initialize hub device driver fibril
    8680 *
    87  * Creates hub representation and fibril that periodically checks hub`s status.
     81 * Creates hub representation and fibril that periodically checks hub's status.
    8882 * Hub representation is passed to the fibril.
    8983 * @param usb_dev generic usb device information
    9084 * @return error code
    9185 */
    92 int usb_hub_add_device(usb_device_t *usb_dev) {
    93         if (!usb_dev) return EINVAL;
     86int usb_hub_add_device(usb_device_t *usb_dev)
     87{
     88        assert(usb_dev);
     89        /* Create driver soft-state structure */
    9490        usb_hub_info_t *hub_info = usb_hub_info_create(usb_dev);
    95         //create hc connection
     91        if (hub_info == NULL) {
     92                usb_log_error("Failed to create hun driver structure.\n");
     93                return ENOMEM;
     94        }
     95
     96        /* Create hc connection */
    9697        usb_log_debug("Initializing USB wire abstraction.\n");
    9798        int opResult = usb_hc_connection_initialize_from_device(
    98             &hub_info->connection,
    99             hub_info->usb_device->ddf_dev);
    100         if (opResult != EOK) {
    101                 usb_log_error("Could not initialize connection to device, "
    102                     " %s\n",
    103                     str_error(opResult));
    104                 free(hub_info);
    105                 return opResult;
    106         }
    107 
    108         //set hub configuration
    109         opResult = usb_hub_set_configuration(hub_info);
    110         if (opResult != EOK) {
    111                 usb_log_error("Could not set hub configuration, %s\n",
    112                     str_error(opResult));
    113                 free(hub_info);
    114                 return opResult;
    115         }
     99            &hub_info->connection, hub_info->usb_device->ddf_dev);
     100        if (opResult != EOK) {
     101                usb_log_error("Could not initialize connection to device: %s\n",
     102                    str_error(opResult));
     103                free(hub_info);
     104                return opResult;
     105        }
     106
     107        /* Set hub's first configuration. (There should be only one) */
     108        opResult = usb_set_first_configuration(usb_dev);
     109        if (opResult != EOK) {
     110                usb_log_error("Could not set hub configuration: %s\n",
     111                    str_error(opResult));
     112                free(hub_info);
     113                return opResult;
     114        }
     115
    116116        //get port count and create attached_devs
    117117        opResult = usb_hub_process_hub_specific_info(hub_info);
     
    123123        }
    124124
    125         usb_log_debug("Creating 'hub' function in DDF.\n");
     125        usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
    126126        ddf_fun_t *hub_fun = ddf_fun_create(hub_info->usb_device->ddf_dev,
    127             fun_exposed, "hub");
    128         assert(hub_fun != NULL);
    129         hub_fun->ops = NULL;
     127            fun_exposed, HUB_FNC_NAME);
     128        if (hub_fun == NULL) {
     129                usb_log_error("Failed to create hub function.\n");
     130                free(hub_info);
     131                return ENOMEM;
     132        }
    130133
    131134        opResult = ddf_fun_bind(hub_fun);
    132         assert(opResult == EOK);
    133 
    134         opResult = usb_hub_start_hub_fibril(hub_info);
    135         if (opResult != EOK)
    136                 free(hub_info);
    137         return opResult;
    138 }
    139 
     135        if (opResult != EOK) {
     136                usb_log_error("Failed to bind hub function: %s.\n",
     137                   str_error(opResult));
     138                free(hub_info);
     139                ddf_fun_destroy(hub_fun);
     140                return opResult;
     141        }
     142
     143        opResult = usb_device_auto_poll(hub_info->usb_device, 0,
     144            hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
     145            usb_hub_polling_terminated_callback, hub_info);
     146        if (opResult != EOK) {
     147                ddf_fun_destroy(hub_fun);
     148                free(hub_info);
     149                usb_log_error("Failed to create polling fibril: %s.\n",
     150                    str_error(opResult));
     151                return opResult;
     152        }
     153        usb_log_info("Controlling hub '%s' (%zu ports).\n",
     154            hub_info->usb_device->ddf_dev->name, hub_info->port_count);
     155
     156        return EOK;
     157}
     158/*----------------------------------------------------------------------------*/
    140159/** Callback for polling hub for changes.
    141160 *
     
    147166 */
    148167bool hub_port_changes_callback(usb_device_t *dev,
    149     uint8_t *change_bitmap, size_t change_bitmap_size, void *arg) {
     168    uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
     169{
    150170        usb_log_debug("hub_port_changes_callback\n");
    151         usb_hub_info_t *hub = (usb_hub_info_t *) arg;
    152 
    153         /* FIXME: check that we received enough bytes. */
     171        usb_hub_info_t *hub = arg;
     172        assert(hub);
     173
     174        /* It is an error condition if we didn't receive enough data */
    154175        if (change_bitmap_size == 0) {
    155                 goto leave;
    156         }
    157 
    158         bool change;
    159         change = ((uint8_t*) change_bitmap)[0] & 1;
     176                return false;
     177        }
     178
     179        /* Lowest bit indicates global change */
     180        const bool change = change_bitmap[0] & 1;
    160181        if (change) {
    161                 usb_hub_process_global_interrupt(hub);
    162         }
    163 
    164         size_t port;
    165         for (port = 1; port < hub->port_count + 1; port++) {
    166                 bool change = (change_bitmap[port / 8] >> (port % 8)) % 2;
     182                usb_hub_global_interrupt(hub);
     183        }
     184
     185        /* N + 1 bit indicates change on port N */
     186        size_t port = 1;
     187        for (; port < hub->port_count + 1; port++) {
     188                const bool change = (change_bitmap[port / 8] >> (port % 8)) & 1;
    167189                if (change) {
    168                         usb_hub_process_port_interrupt(hub, port);
     190                        usb_hub_port_process_interrupt(&hub->ports[port], hub);
    169191                }
    170192        }
    171 leave:
    172         /* FIXME: proper interval. */
    173         async_usleep(1000 * 250);
    174 
    175193        return true;
    176194}
    177 
    178 
    179 //*********************************************
    180 //
    181 //  support functions
    182 //
    183 //*********************************************
    184 
     195/*----------------------------------------------------------------------------*/
    185196/**
    186197 * create usb_hub_info_t structure
     
    190201 * @return basic usb_hub_info_t structure
    191202 */
    192 static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev) {
    193         usb_hub_info_t * result = malloc(sizeof (usb_hub_info_t));
    194         if (!result) return NULL;
    195         result->usb_device = usb_dev;
    196         result->status_change_pipe = usb_dev->pipes[0].pipe;
    197         result->control_pipe = &usb_dev->ctrl_pipe;
    198         result->is_default_address_used = false;
    199 
    200         result->ports = NULL;
    201         result->port_count = (size_t) - 1;
    202         fibril_mutex_initialize(&result->port_mutex);
    203 
    204         fibril_mutex_initialize(&result->pending_ops_mutex);
    205         fibril_condvar_initialize(&result->pending_ops_cv);
    206         result->pending_ops_count = 0;
    207         return result;
    208 }
    209 
     203static usb_hub_info_t * usb_hub_info_create(usb_device_t *usb_dev)
     204{
     205        assert(usb_dev);
     206        usb_hub_info_t *info = malloc(sizeof(usb_hub_info_t));
     207        if (!info)
     208            return NULL;
     209
     210        info->usb_device = usb_dev;
     211
     212        info->ports = NULL;
     213        info->port_count = -1;
     214        fibril_mutex_initialize(&info->pending_ops_mutex);
     215        fibril_condvar_initialize(&info->pending_ops_cv);
     216        info->pending_ops_count = 0;
     217
     218        return info;
     219}
     220/*----------------------------------------------------------------------------*/
    210221/**
    211222 * Load hub-specific information into hub_info structure and process if needed
    212223 *
    213  * Particularly read port count and initialize structure holding port
    214  * information. If there are non-removable devices, start initializing them.
     224 * Read port count and initialize structures holding per port information.
     225 * If there are any non-removable devices, start initializing them.
    215226 * This function is hub-specific and should be run only after the hub is
    216  * configured using usb_hub_set_configuration function.
     227 * configured using usb_set_first_configuration function.
    217228 * @param hub_info hub representation
    218229 * @return error code
    219230 */
    220 int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info)
    221 {
    222         // get hub descriptor
     231static int usb_hub_process_hub_specific_info(usb_hub_info_t *hub_info)
     232{
     233        assert(hub_info);
     234
     235        /* Get hub descriptor. */
    223236        usb_log_debug("Retrieving descriptor\n");
    224         uint8_t serialized_descriptor[USB_HUB_MAX_DESCRIPTOR_SIZE];
    225         int opResult;
    226 
     237        usb_pipe_t *control_pipe = &hub_info->usb_device->ctrl_pipe;
     238
     239        usb_hub_descriptor_header_t descriptor;
    227240        size_t received_size;
    228         opResult = usb_request_get_descriptor(hub_info->control_pipe,
     241        int opResult = usb_request_get_descriptor(control_pipe,
    229242            USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_DEVICE,
    230             USB_DESCTYPE_HUB, 0, 0, serialized_descriptor,
    231             USB_HUB_MAX_DESCRIPTOR_SIZE, &received_size);
    232 
     243            USB_DESCTYPE_HUB, 0, 0, &descriptor,
     244            sizeof(usb_hub_descriptor_t), &received_size);
    233245        if (opResult != EOK) {
    234246                usb_log_error("Failed to receive hub descriptor: %s.\n",
     
    236248                return opResult;
    237249        }
    238         usb_log_debug2("Parsing descriptor\n");
    239         usb_hub_descriptor_t descriptor;
    240         opResult = usb_deserialize_hub_desriptor(
    241                 serialized_descriptor, received_size, &descriptor);
    242         if (opResult != EOK) {
    243                 usb_log_error("Could not parse descriptor: %s\n",
    244                     str_error(opResult));
    245                 return opResult;
    246         }
    247         usb_log_debug("Setting port count to %d.\n", descriptor.ports_count);
    248         hub_info->port_count = descriptor.ports_count;
    249 
     250
     251        usb_log_debug("Setting port count to %d.\n", descriptor.port_count);
     252        hub_info->port_count = descriptor.port_count;
     253
     254        // TODO: +1 hack is no longer necessary
    250255        hub_info->ports =
    251256            malloc(sizeof(usb_hub_port_t) * (hub_info->port_count + 1));
     
    256261        size_t port;
    257262        for (port = 0; port < hub_info->port_count + 1; ++port) {
    258                 usb_hub_port_init(&hub_info->ports[port]);
     263                usb_hub_port_init(&hub_info->ports[port], port, control_pipe);
    259264        }
    260265
    261266        const bool is_power_switched =
    262             !(descriptor.hub_characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
     267            !(descriptor.characteristics & HUB_CHAR_NO_POWER_SWITCH_FLAG);
    263268        if (is_power_switched) {
    264269                usb_log_debug("Hub power switched\n");
    265                 const bool per_port_power = descriptor.hub_characteristics
     270                const bool per_port_power = descriptor.characteristics
    266271                    & HUB_CHAR_POWER_PER_PORT_FLAG;
    267272
    268273                for (port = 1; port <= hub_info->port_count; ++port) {
    269274                        usb_log_debug("Powering port %zu.\n", port);
    270                         opResult = usb_hub_set_port_feature(
    271                             hub_info->control_pipe,
    272                             port, USB_HUB_FEATURE_PORT_POWER);
     275                        opResult = usb_hub_port_set_feature(
     276                            &hub_info->ports[port], USB_HUB_FEATURE_PORT_POWER);
    273277                        if (opResult != EOK) {
    274278                                usb_log_error("Cannot power on port %zu: %s.\n",
     
    283287                        }
    284288                }
    285 
    286289        } else {
    287                 usb_log_debug("Power not switched, not going to be powered\n");
     290                usb_log_debug("Power not switched, ports always powered\n");
    288291        }
    289292        return EOK;
    290293}
    291 
    292 /**
    293  * Set configuration of hub
     294/*----------------------------------------------------------------------------*/
     295/**
     296 * Set configuration of and USB device
    294297 *
    295298 * Check whether there is at least one configuration and sets the first one.
    296299 * This function should be run prior to running any hub-specific action.
    297  * @param hub_info hub representation
     300 * @param usb_device usb device representation
    298301 * @return error code
    299302 */
    300 static int usb_hub_set_configuration(usb_hub_info_t *hub_info) {
    301         //device descriptor
    302         usb_standard_device_descriptor_t *std_descriptor
    303             = &hub_info->usb_device->descriptors.device;
    304         usb_log_debug("Hub has %d configurations\n",
    305             std_descriptor->configuration_count);
    306         if (std_descriptor->configuration_count < 1) {
     303static int usb_set_first_configuration(usb_device_t *usb_device)
     304{
     305        assert(usb_device);
     306        /* Get number of possible configurations from device descriptor */
     307        const size_t configuration_count =
     308            usb_device->descriptors.device.configuration_count;
     309        usb_log_debug("Hub has %d configurations.\n", configuration_count);
     310
     311        if (configuration_count < 1) {
    307312                usb_log_error("There are no configurations available\n");
    308313                return EINVAL;
    309314        }
    310315
     316        // TODO: Make sure that there is enough data and the cast is correct
    311317        usb_standard_configuration_descriptor_t *config_descriptor
    312318            = (usb_standard_configuration_descriptor_t *)
    313             hub_info->usb_device->descriptors.configuration;
    314 
    315         /* Set configuration. */
    316         int opResult = usb_request_set_configuration(
    317             &hub_info->usb_device->ctrl_pipe,
    318             config_descriptor->configuration_number);
    319 
     319            usb_device->descriptors.configuration;
     320
     321        /* Set configuration. Use the configuration that was in
     322         * usb_device->descriptors.configuration i.e. The first one. */
     323        const int opResult = usb_request_set_configuration(
     324            &usb_device->ctrl_pipe, config_descriptor->configuration_number);
    320325        if (opResult != EOK) {
    321326                usb_log_error("Failed to set hub configuration: %s.\n",
    322327                    str_error(opResult));
    323                 return opResult;
    324         }
    325         usb_log_debug("\tUsed configuration %d\n",
    326             config_descriptor->configuration_number);
    327 
    328         return EOK;
    329 }
    330 
    331 /**
    332  * create and start fibril with hub control loop
    333  *
    334  * Before the fibril is started, the control pipe and host controller
    335  * connection of the hub is open.
    336  *
    337  * @param hub_info hub representing structure
    338  * @return error code
    339  */
    340 static int usb_hub_start_hub_fibril(usb_hub_info_t *hub_info) {
    341         int rc;
    342 
    343         rc = usb_device_auto_poll(hub_info->usb_device, 0,
    344             hub_port_changes_callback, ((hub_info->port_count + 1) / 8) + 1,
    345             usb_hub_polling_terminated_callback, hub_info);
    346         if (rc != EOK) {
    347                 usb_log_error("Failed to create polling fibril: %s.\n",
    348                     str_error(rc));
    349                 free(hub_info);
    350                 return rc;
    351         }
    352 
    353         usb_log_info("Controlling hub `%s' (%zu ports).\n",
    354             hub_info->usb_device->ddf_dev->name, hub_info->port_count);
    355         return EOK;
    356 }
    357 
    358 //*********************************************
    359 //
    360 //  change handling functions
    361 //
    362 //*********************************************
    363 
    364 /**
    365  * process hub over current change
     328        } else {
     329                usb_log_debug("\tUsed configuration %d\n",
     330                    config_descriptor->configuration_number);
     331        }
     332        return opResult;
     333}
     334/*----------------------------------------------------------------------------*/
     335/**
     336 * Process hub over current change
    366337 *
    367338 * This means either to power off the hub or power it on.
     
    370341 * @return error code
    371342 */
    372 static int usb_process_hub_over_current(usb_hub_info_t *hub_info,
    373     usb_hub_status_t status) {
    374         int opResult;
    375         if (usb_hub_is_status(status, USB_HUB_FEATURE_HUB_OVER_CURRENT)) {
    376                 //poweroff all ports
    377                 unsigned int port;
     343static void usb_hub_over_current(const usb_hub_info_t *hub_info,
     344    usb_hub_status_t status)
     345{
     346        if (status & USB_HUB_STATUS_OVER_CURRENT) {
     347                /* Hub should remove power from all ports if it detects OC */
     348                usb_log_warning("Detected hub over-current condition, "
     349                    "all ports should be powered off.");
     350        } else {
     351                /* Over-current condition is gone, it is safe to turn the
     352                 * ports on. */
     353                size_t port;
    378354                for (port = 1; port <= hub_info->port_count; ++port) {
    379                         opResult = usb_hub_clear_port_feature(
    380                             hub_info->control_pipe, port,
    381                             USB_HUB_FEATURE_PORT_POWER);
     355                        const int opResult = usb_hub_port_set_feature(
     356                            &hub_info->ports[port], USB_HUB_FEATURE_PORT_POWER);
    382357                        if (opResult != EOK) {
    383358                                usb_log_warning(
    384                                     "Cannot power off port %d;  %s\n",
     359                                    "HUB OVER-CURRENT GONE: Cannot power on "
     360                                    "port %d;  %s\n",
    385361                                    port, str_error(opResult));
    386362                        }
    387363                }
    388         } else {
    389                 //power all ports
    390                 unsigned int port;
    391                 for (port = 1; port <= hub_info->port_count; ++port) {
    392                         opResult = usb_hub_set_port_feature(
    393                             hub_info->control_pipe, port,
    394                             USB_HUB_FEATURE_PORT_POWER);
    395                         if (opResult != EOK) {
    396                                 usb_log_warning(
    397                                     "Cannot power off port %d;  %s\n",
    398                                     port, str_error(opResult));
    399                         }
    400                 }
    401         }
    402         return opResult;
    403 }
    404 
    405 /**
    406  * process hub local power change
    407  *
    408  * This change is ignored.
     364        }
     365        const int opResult = usb_request_clear_feature(
     366            &hub_info->usb_device->ctrl_pipe, USB_REQUEST_TYPE_CLASS,
     367            USB_REQUEST_RECIPIENT_DEVICE,
     368            USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
     369        if (opResult != EOK) {
     370                usb_log_error(
     371                    "Failed to clear hub over-current change flag: %s.\n",
     372                    str_error(opResult));
     373        }
     374}
     375/*----------------------------------------------------------------------------*/
     376/**
     377 * Process hub interrupts.
     378 *
     379 * The change can be either in the over-current condition or local-power change.
    409380 * @param hub_info hub instance
    410  * @param status hub status bitmask
    411  * @return error code
    412  */
    413 static int usb_process_hub_local_power_change(usb_hub_info_t *hub_info,
    414     usb_hub_status_t status) {
    415         int opResult = EOK;
    416         opResult = usb_hub_clear_feature(hub_info->control_pipe,
    417             USB_HUB_FEATURE_C_HUB_LOCAL_POWER);
    418         if (opResult != EOK) {
    419                 usb_log_error("Cannnot clear hub power change flag: "
    420                     "%s\n",
    421                     str_error(opResult));
    422         }
    423         return opResult;
    424 }
    425 
    426 /**
    427  * process hub interrupts
    428  *
    429  * The change can be either in the over-current condition or
    430  * local-power change.
    431  * @param hub_info hub instance
    432  */
    433 static void usb_hub_process_global_interrupt(usb_hub_info_t *hub_info) {
     381 */
     382static void usb_hub_global_interrupt(const usb_hub_info_t *hub_info)
     383{
     384        assert(hub_info);
     385        assert(hub_info->usb_device);
    434386        usb_log_debug("Global interrupt on a hub\n");
    435         usb_pipe_t *pipe = hub_info->control_pipe;
    436         int opResult;
    437 
    438         usb_port_status_t status;
     387        usb_pipe_t *control_pipe = &hub_info->usb_device->ctrl_pipe;
     388
     389        usb_hub_status_t status;
    439390        size_t rcvd_size;
    440         usb_device_request_setup_packet_t request;
    441         //int opResult;
    442         usb_hub_set_hub_status_request(&request);
    443         //endpoint 0
    444 
    445         opResult = usb_pipe_control_read(
    446             pipe,
    447             &request, sizeof (usb_device_request_setup_packet_t),
    448             &status, 4, &rcvd_size
    449             );
     391        /* NOTE: We can't use standard USB GET_STATUS request, because
     392         * hubs reply is 4byte instead of 2 */
     393        const int opResult = usb_pipe_control_read(control_pipe,
     394            &get_hub_status_request, sizeof(get_hub_status_request),
     395            &status, sizeof(usb_hub_status_t), &rcvd_size);
    450396        if (opResult != EOK) {
    451397                usb_log_error("Could not get hub status: %s\n",
     
    453399                return;
    454400        }
    455         if (rcvd_size != sizeof (usb_port_status_t)) {
     401        if (rcvd_size != sizeof(usb_hub_status_t)) {
    456402                usb_log_error("Received status has incorrect size\n");
    457403                return;
    458404        }
    459         //port reset
    460         if (
    461             usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_OVER_CURRENT)) {
    462                 usb_process_hub_over_current(hub_info, status);
    463         }
    464         if (
    465             usb_hub_is_status(status, 16 + USB_HUB_FEATURE_C_HUB_LOCAL_POWER)) {
    466                 usb_process_hub_local_power_change(hub_info, status);
    467         }
    468 }
    469 
     405
     406        /* Handle status changes */
     407        if (status & USB_HUB_STATUS_C_OVER_CURRENT) {
     408                usb_hub_over_current(hub_info, status);
     409        }
     410
     411        if (status & USB_HUB_STATUS_C_LOCAL_POWER) {
     412                /* NOTE: Handling this is more complicated.
     413                 * If the transition is from bus power to local power, all
     414                 * is good and we may signal the parent hub that we don't
     415                 * need the power.
     416                 * If the transition is from local power to bus power
     417                 * the hub should turn off all the ports and devices need
     418                 * to be reinitialized taking into account the limited power
     419                 * that is now available.
     420                 * There is no support for power distribution in HelenOS,
     421                 * (or other OSes/hub devices that I've seen) so this is not
     422                 * implemented.
     423                 * Just ACK the change.
     424                 */
     425                const int opResult = usb_request_clear_feature(
     426                    control_pipe, USB_REQUEST_TYPE_CLASS,
     427                    USB_REQUEST_RECIPIENT_DEVICE,
     428                    USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
     429                if (opResult != EOK) {
     430                        usb_log_error(
     431                            "Failed to clear hub power change flag: %s.\n",
     432                            str_error(opResult));
     433                }
     434        }
     435}
     436/*----------------------------------------------------------------------------*/
    470437/**
    471438 * callback called from hub polling fibril when the fibril terminates
     
    477444 */
    478445static void usb_hub_polling_terminated_callback(usb_device_t *device,
    479     bool was_error, void *data) {
    480         usb_hub_info_t * hub = data;
     446    bool was_error, void *data)
     447{
     448        usb_hub_info_t *hub = data;
    481449        assert(hub);
    482450
     
    492460         */
    493461        if (hub->pending_ops_count > 0) {
    494                 fibril_mutex_lock(&hub->port_mutex);
    495462                size_t port;
    496463                for (port = 0; port < hub->port_count; port++) {
    497                         usb_hub_port_t *the_port = hub->ports + port;
    498                         fibril_mutex_lock(&the_port->reset_mutex);
    499                         the_port->reset_completed = true;
    500                         the_port->reset_okay = false;
    501                         fibril_condvar_broadcast(&the_port->reset_cv);
    502                         fibril_mutex_unlock(&the_port->reset_mutex);
     464                        usb_hub_port_reset_fail(&hub->ports[port]);
    503465                }
    504                 fibril_mutex_unlock(&hub->port_mutex);
    505466        }
    506467        /* And now wait for them. */
     
    516477        free(hub);
    517478}
    518 
    519 
    520 
    521 
    522479/**
    523480 * @}
  • uspace/drv/bus/usb/usbhub/usbhub.h

    rd3a9ae74 r8c9e71a  
    11/*
    22 * Copyright (c) 2010 Vojtech Horky
     3 * Copyright (c) 2011 Vojtech Horky
    34 * All rights reserved.
    45 *
     
    2627 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2728 */
    28 
    2929/** @addtogroup drvusbhub
    3030 * @{
     
    4949#define NAME "usbhub"
    5050
    51 #include "ports.h"
     51#include "port.h"
    5252
    5353/** Information about attached hub. */
     
    5656        size_t port_count;
    5757
    58         /** attached device handles, for each port one */
     58        /** Attached device handles, for each port one */
    5959        usb_hub_port_t *ports;
    6060
    61         fibril_mutex_t port_mutex;
    62 
    63         /** connection to hcd */
     61        /** Connection to hcd */
    6462        usb_hc_connection_t connection;
    6563
    66         /** default address is used indicator
    67          *
    68          * If default address is requested by this device, it cannot
    69          * be requested by the same hub again, otherwise a deadlock will occur.
    70          */
    71         bool is_default_address_used;
    72 
    73         /** convenience pointer to status change pipe
    74          *
    75          * Status change pipe is initialized in usb_device structure. This is
    76          * pointer into this structure, so that it does not have to be
    77          * searched again and again for the 'right pipe'.
    78          */
    79         usb_pipe_t * status_change_pipe;
    80 
    81         /** convenience pointer to control pipe
    82          *
    83          * Control pipe is initialized in usb_device structure. This is
    84          * pointer into this structure, so that it does not have to be
    85          * searched again and again for the 'right pipe'.
    86          */
    87         usb_pipe_t * control_pipe;
    88 
    89         /** generic usb device data*/
    90         usb_device_t * usb_device;
     64        /** Generic usb device data*/
     65        usb_device_t *usb_device;
    9166
    9267        /** Number of pending operations on the mutex to prevent shooting
     
    10176        /** Condition variable for pending_ops_count. */
    10277        fibril_condvar_t pending_ops_cv;
    103 
    10478};
    10579
  • uspace/lib/usb/include/usb/classes/hub.h

    rd3a9ae74 r8c9e71a  
    7272        uint8_t port_count;
    7373        /** Characteristics bitmask. */
    74         uint16_t characteristics;
     74        uint8_t characteristics;
     75#define HUB_CHAR_POWER_PER_PORT_FLAG  (1 << 0)
     76#define HUB_CHAR_NO_POWER_SWITCH_FLAG (1 << 1)
     77        /* Unused part of characteristics field */
     78        uint8_t characteristics_reserved;
    7579        /** Time from power-on to stabilization of current on the port. */
    7680        uint8_t power_good_time;
     
    9296
    9397    /** Number of downstream ports that this hub supports */
    94     uint8_t ports_count;
     98    uint8_t port_count;
    9599
    96100    /**
     
    119123     */
    120124    uint16_t hub_characteristics;
    121 #define HUB_CHAR_POWER_PER_PORT_FLAG  (1 << 0)
    122 #define HUB_CHAR_NO_POWER_SWITCH_FLAG (1 << 1)
    123125
    124126    /**
     
    214216 *      Maximum size of usb hub descriptor in bytes
    215217 */
    216 extern size_t USB_HUB_MAX_DESCRIPTOR_SIZE;
    217 
    218 
    219 
    220 
    221 
    222 
     218/* 7 (basic size) + 2*32 (port bitmasks) */
     219#define USB_HUB_MAX_DESCRIPTOR_SIZE 71
    223220
    224221#endif
  • uspace/lib/usbdev/include/usb/dev/pipes.h

    rd3a9ae74 r8c9e71a  
    183183int usb_pipe_write(usb_pipe_t *, void *, size_t);
    184184
    185 int usb_pipe_control_read(usb_pipe_t *, void *, size_t,
     185int usb_pipe_control_read(usb_pipe_t *, const void *, size_t,
    186186    void *, size_t, size_t *);
    187187int usb_pipe_control_write(usb_pipe_t *, void *, size_t,
  • uspace/lib/usbdev/src/pipesio.c

    rd3a9ae74 r8c9e71a  
    328328 */
    329329static int usb_pipe_control_read_no_check(usb_pipe_t *pipe,
    330     void *setup_buffer, size_t setup_buffer_size,
     330    const void *setup_buffer, size_t setup_buffer_size,
    331331    void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
    332332{
     
    411411 */
    412412int usb_pipe_control_read(usb_pipe_t *pipe,
    413     void *setup_buffer, size_t setup_buffer_size,
     413    const void *setup_buffer, size_t setup_buffer_size,
    414414    void *data_buffer, size_t data_buffer_size, size_t *data_transfered_size)
    415415{
  • uspace/srv/fs/mfs/mfs_ops.c

    rd3a9ae74 r8c9e71a  
    143143{
    144144        enum cache_mode cmode;
    145         struct mfs_superblock *sb;
    146         struct mfs3_superblock *sb3;
    147         struct mfs_sb_info *sbi;
    148         struct mfs_instance *instance;
     145        struct mfs_superblock *sb = NULL;
     146        struct mfs3_superblock *sb3 = NULL;
     147        struct mfs_sb_info *sbi = NULL;
     148        struct mfs_instance *instance = NULL;
    149149        bool native, longnames;
    150150        mfs_version_t version;
     
    166166        sbi = malloc(sizeof(*sbi));
    167167        if (!sbi) {
    168                 block_fini(service_id);
    169                 return ENOMEM;
     168                rc = ENOMEM;
     169                goto out_error;
    170170        }
    171171
     
    173173        instance = malloc(sizeof(*instance));
    174174        if (!instance) {
    175                 free(sbi);
    176                 block_fini(service_id);
    177                 return ENOMEM;
     175                rc = ENOMEM;
     176                goto out_error;
    178177        }
    179178
    180179        sb = malloc(MFS_SUPERBLOCK_SIZE);
    181180        if (!sb) {
    182                 free(instance);
    183                 free(sbi);
    184                 block_fini(service_id);
    185                 return ENOMEM;
     181                rc = ENOMEM;
     182                goto out_error;
    186183        }
    187184
    188185        /* Read the superblock */
    189186        rc = block_read_direct(service_id, MFS_SUPERBLOCK << 1, 2, sb);
    190         if (rc != EOK) {
    191                 free(instance);
    192                 free(sbi);
    193                 free(sb);
    194                 block_fini(service_id);
    195                 return rc;
    196         }
     187        if (rc != EOK)
     188                goto out_error;
    197189
    198190        sb3 = (struct mfs3_superblock *) sb;
     
    207199                /*Not recognized*/
    208200                mfsdebug("magic number not recognized\n");
    209                 free(instance);
    210                 free(sbi);
    211                 free(sb);
    212                 block_fini(service_id);
    213                 return ENOTSUP;
     201                rc = ENOTSUP;
     202                goto out_error;
    214203        }
    215204
     
    256245                                    MFS_MAX_NAME_LEN;
    257246        }
     247
     248        if (sbi->log2_zone_size != 0) {
     249                /* In MFS, file space is allocated per zones.
     250                 * Zones are a collection of consecutive blocks on disk.
     251                 *
     252                 * The current MFS implementation supports only filesystems
     253                 * where the size of a zone is equal to the
     254                 * size of a block.
     255                 */
     256                rc = ENOTSUP;
     257                goto out_error;
     258        }
     259
    258260        sbi->itable_off = 2 + sbi->ibmap_blocks + sbi->zbmap_blocks;
    259 
    260         free(sb);
    261261
    262262        rc = block_cache_init(service_id, sbi->block_size, 0, cmode);
    263263        if (rc != EOK) {
    264                 free(instance);
    265                 free(sbi);
    266                 block_cache_fini(service_id);
    267                 block_fini(service_id);
    268264                mfsdebug("block cache initialization failed\n");
    269                 return EINVAL;
     265                rc = EINVAL;
     266                goto out_error;
    270267        }
    271268
     
    295292        *linkcnt = 1;
    296293
     294        free(sb);
     295
    297296        return mfs_node_put(fn);
     297
     298out_error:
     299        block_fini(service_id);
     300        if (sb)
     301                free(sb);
     302        if (sbi)
     303                free(sbi);
     304        if(instance)
     305                free(instance);
     306        return rc;
    298307}
    299308
     
    880889        struct mfs_ino_info *ino_i = mnode->ino_i;
    881890        const size_t bs = sbi->block_size;
    882         size_t bytes = min(len, bs - pos % bs);
    883         size_t boundary = ROUND_UP(ino_i->i_size, bs);
     891        size_t bytes = min(len, bs - (pos % bs));
    884892        uint32_t block;
    885893
     
    887895                flags = BLOCK_FLAGS_NOREAD;
    888896
    889         if (pos < boundary) {
    890                 r = mfs_read_map(&block, mnode, pos);
    891                 if (r != EOK)
    892                         goto out_err;
    893 
    894                 if (block == 0) {
    895                         /*Writing in a sparse block*/
    896                         r = mfs_alloc_zone(mnode->instance, &block);
    897                         if (r != EOK)
    898                                 goto out_err;
    899                         flags = BLOCK_FLAGS_NOREAD;
    900                 }
    901         } else {
     897        r = mfs_read_map(&block, mnode, pos);
     898        if (r != EOK)
     899                goto out_err;
     900
     901        if (block == 0) {
     902                /*Writing in a sparse block*/
    902903                uint32_t dummy;
    903904
     
    905906                if (r != EOK)
    906907                        goto out_err;
    907 
     908               
    908909                r = mfs_write_map(mnode, pos, block, &dummy);
    909910                if (r != EOK)
    910911                        goto out_err;
     912
     913                flags = BLOCK_FLAGS_NOREAD;
    911914        }
    912915
     
    916919                goto out_err;
    917920
    918         async_data_write_finalize(callid, b->data + pos % bs, bytes);
     921        if (flags == BLOCK_FLAGS_NOREAD)
     922                memset(b->data, 0, sbi->block_size);
     923
     924        async_data_write_finalize(callid, b->data + (pos % bs), bytes);
    919925        b->dirty = true;
    920926
     
    925931        }
    926932
    927         ino_i->i_size = pos + bytes;
    928         ino_i->dirty = true;
     933        if (pos + bytes > ino_i->i_size) {
     934                ino_i->i_size = pos + bytes;
     935                ino_i->dirty = true;
     936        }
    929937        r = mfs_node_put(fn);
    930         *nsize = pos + bytes;
     938        *nsize = ino_i->i_size;
    931939        *wbytes = bytes;
    932940        return r;
  • uspace/srv/fs/mfs/mfs_rw.c

    rd3a9ae74 r8c9e71a  
    3131 */
    3232
     33#include <align.h>
    3334#include "mfs.h"
    3435
     
    7071        int rblock = pos / block_size;
    7172
    72         if (mnode->ino_i->i_size < pos) {
     73        if (ROUND_UP(mnode->ino_i->i_size, sbi->block_size) < pos) {
    7374                /*Trying to read beyond the end of file*/
    7475                r = EOK;
  • uspace/srv/hw/irc/apic/apic.c

    rd3a9ae74 r8c9e71a  
    206206        }
    207207
    208         if (pio_enable((void *) IO_APIC_BASE, IO_APIC_SIZE,
    209             (void **) &io_apic) != EOK)
     208        int rc = pio_enable((void *) IO_APIC_BASE, IO_APIC_SIZE,
     209                (void **) &io_apic);
     210        if (rc != EOK) {
     211                printf("%s: Failed to enable PIO for APIC: %d\n", NAME, rc);
    210212                return false;   
     213        }
    211214       
    212215        async_set_client_connection(apic_connection);
Note: See TracChangeset for help on using the changeset viewer.