Changeset 00aece0 in mainline for uspace/drv/bus/usb/usbmast/main.c


Ignore:
Timestamp:
2012-02-18T16:47:38Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4449c6c
Parents:
bd5f3b7 (diff), f943dd3 (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:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbmast/main.c

    rbd5f3b7 r00aece0  
    5555#define GET_BULK_OUT(dev) ((dev)->pipes[BULK_OUT_EP].pipe)
    5656
    57 static usb_endpoint_description_t bulk_in_ep = {
     57static const usb_endpoint_description_t bulk_in_ep = {
    5858        .transfer_type = USB_TRANSFER_BULK,
    5959        .direction = USB_DIRECTION_IN,
     
    6363        .flags = 0
    6464};
    65 static usb_endpoint_description_t bulk_out_ep = {
     65static const usb_endpoint_description_t bulk_out_ep = {
    6666        .transfer_type = USB_TRANSFER_BULK,
    6767        .direction = USB_DIRECTION_OUT,
     
    7272};
    7373
    74 usb_endpoint_description_t *mast_endpoints[] = {
     74static const usb_endpoint_description_t *mast_endpoints[] = {
    7575        &bulk_in_ep,
    7676        &bulk_out_ep,
     
    8282    void *arg);
    8383
     84/** Callback when a device is removed from the system.
     85 *
     86 * @param dev Representation of USB device.
     87 * @return Error code.
     88 */
     89static int usbmast_device_gone(usb_device_t *dev)
     90{
     91        usbmast_dev_t *mdev = dev->driver_data;
     92        assert(mdev);
     93
     94        for (size_t i = 0; i < mdev->lun_count; ++i) {
     95                const int rc = ddf_fun_unbind(mdev->luns[i]);
     96                if (rc != EOK) {
     97                        usb_log_error("Failed to unbind LUN function %zu: "
     98                            "%s\n", i, str_error(rc));
     99                        return rc;
     100                }
     101                ddf_fun_destroy(mdev->luns[i]);
     102                mdev->luns[i] = NULL;
     103        }
     104        free(mdev->luns);
     105        return EOK;
     106}
     107
     108/** Callback when a device is about to be removed.
     109 *
     110 * @param dev Representation of USB device.
     111 * @return Error code.
     112 */
     113static int usbmast_device_remove(usb_device_t *dev)
     114{
     115        //TODO: flush buffers, or whatever.
     116        //TODO: remove device
     117        return ENOTSUP;
     118}
     119
    84120/** Callback when new device is attached and recognized as a mass storage.
    85121 *
    86  * @param dev Representation of a the USB device.
     122 * @param dev Representation of USB device.
    87123 * @return Error code.
    88124 */
    89 static int usbmast_add_device(usb_device_t *dev)
     125static int usbmast_device_add(usb_device_t *dev)
    90126{
    91127        int rc;
     
    94130
    95131        /* Allocate softstate */
    96         mdev = calloc(1, sizeof(usbmast_dev_t));
     132        mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
    97133        if (mdev == NULL) {
    98134                usb_log_error("Failed allocating softstate.\n");
    99                 rc = ENOMEM;
    100                 goto error;
     135                return ENOMEM;
    101136        }
    102137
     
    104139        mdev->usb_dev = dev;
    105140
    106         usb_log_info("Initializing mass storage `%s'.\n",
    107             dev->ddf_dev->name);
    108         usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
    109             dev->pipes[BULK_IN_EP].pipe->endpoint_no,
    110             (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
     141        usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
     142        usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
     143            dev->pipes[BULK_IN_EP].pipe.endpoint_no,
     144            dev->pipes[BULK_IN_EP].pipe.max_packet_size);
    111145        usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
    112             dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
    113             (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
     146            dev->pipes[BULK_OUT_EP].pipe.endpoint_no,
     147            dev->pipes[BULK_OUT_EP].pipe.max_packet_size);
    114148
    115149        usb_log_debug("Get LUN count...\n");
    116         mdev->luns = usb_masstor_get_lun_count(mdev);
    117 
    118         for (i = 0; i < mdev->luns; i++) {
     150        mdev->lun_count = usb_masstor_get_lun_count(mdev);
     151        mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
     152        if (mdev->luns == NULL) {
     153                rc = ENOMEM;
     154                usb_log_error("Failed allocating luns table.\n");
     155                goto error;
     156        }
     157
     158        for (i = 0; i < mdev->lun_count; i++) {
    119159                rc = usbmast_fun_create(mdev, i);
    120160                if (rc != EOK)
     
    124164        return EOK;
    125165error:
    126         /* XXX Destroy functions */
    127         if (mdev != NULL)
    128                 free(mdev);
     166        /* Destroy functions */
     167        for (size_t i = 0; i < mdev->lun_count; ++i) {
     168                if (mdev->luns[i] == NULL)
     169                        continue;
     170                const int rc = ddf_fun_unbind(mdev->luns[i]);
     171                if (rc != EOK) {
     172                        usb_log_warning("Failed to unbind LUN function %zu: "
     173                            "%s.\n", i, str_error(rc));
     174                }
     175                ddf_fun_destroy(mdev->luns[i]);
     176        }
     177        free(mdev->luns);
    129178        return rc;
    130179}
     
    145194        usbmast_fun_t *mfun = NULL;
    146195
    147         /* Allocate softstate */
    148         mfun = calloc(1, sizeof(usbmast_fun_t));
    149         if (mfun == NULL) {
    150                 usb_log_error("Failed allocating softstate.\n");
    151                 rc = ENOMEM;
    152                 goto error;
    153         }
    154 
    155         mfun->mdev = mdev;
    156         mfun->lun = lun;
    157 
    158196        if (asprintf(&fun_name, "l%u", lun) < 0) {
    159197                usb_log_error("Out of memory.\n");
     
    169207        }
    170208
    171         free(fun_name);
    172         fun_name = NULL;
     209        /* Allocate soft state */
     210        mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
     211        if (mfun == NULL) {
     212                usb_log_error("Failed allocating softstate.\n");
     213                rc = ENOMEM;
     214                goto error;
     215        }
     216
     217        mfun->ddf_fun = fun;
     218        mfun->mdev = mdev;
     219        mfun->lun = lun;
    173220
    174221        /* Set up a connection handler. */
    175222        fun->conn_handler = usbmast_bd_connection;
    176         fun->driver_data = mfun;
    177223
    178224        usb_log_debug("Inquire...\n");
     
    219265        }
    220266
     267        free(fun_name);
     268        mdev->luns[lun] = fun;
     269
    221270        return EOK;
    222271
     
    227276        if (fun_name != NULL)
    228277                free(fun_name);
    229         if (mfun != NULL)
    230                 free(mfun);
    231278        return rc;
    232279}
     
    253300                return;
    254301        }
    255 
    256         comm_buf = as_get_mappable_page(comm_size);
    257         if (comm_buf == NULL) {
     302       
     303        (void) async_share_out_finalize(callid, &comm_buf);
     304        if (comm_buf == (void *) -1) {
    258305                async_answer_0(callid, EHANGUP);
    259306                return;
    260307        }
    261 
    262         (void) async_share_out_finalize(callid, comm_buf);
    263 
     308       
    264309        mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
    265310
     
    301346
    302347/** USB mass storage driver ops. */
    303 static usb_driver_ops_t usbmast_driver_ops = {
    304         .add_device = usbmast_add_device,
     348static const usb_driver_ops_t usbmast_driver_ops = {
     349        .device_add = usbmast_device_add,
     350        .device_rem = usbmast_device_remove,
     351        .device_gone = usbmast_device_gone,
    305352};
    306353
    307354/** USB mass storage driver. */
    308 static usb_driver_t usbmast_driver = {
     355static const usb_driver_t usbmast_driver = {
    309356        .name = NAME,
    310357        .ops = &usbmast_driver_ops,
Note: See TracChangeset for help on using the changeset viewer.