main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include <usb/dev/driver.h>
00037 #include <usb/debug.h>
00038 #include <usb/classes/classes.h>
00039 #include <usb/classes/massstor.h>
00040 #include <errno.h>
00041 #include <str_error.h>
00042 #include "cmds.h"
00043 #include "scsi.h"
00044 #include "mast.h"
00045 
00046 #define NAME "usbmast"
00047 
00048 #define BULK_IN_EP 0
00049 #define BULK_OUT_EP 1
00050 
00051 #define GET_BULK_IN(dev) ((dev)->pipes[BULK_IN_EP].pipe)
00052 #define GET_BULK_OUT(dev) ((dev)->pipes[BULK_OUT_EP].pipe)
00053 
00054 static usb_endpoint_description_t bulk_in_ep = {
00055         .transfer_type = USB_TRANSFER_BULK,
00056         .direction = USB_DIRECTION_IN,
00057         .interface_class = USB_CLASS_MASS_STORAGE,
00058         .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
00059         .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
00060         .flags = 0
00061 };
00062 static usb_endpoint_description_t bulk_out_ep = {
00063         .transfer_type = USB_TRANSFER_BULK,
00064         .direction = USB_DIRECTION_OUT,
00065         .interface_class = USB_CLASS_MASS_STORAGE,
00066         .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
00067         .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
00068         .flags = 0
00069 };
00070 
00071 usb_endpoint_description_t *mast_endpoints[] = {
00072         &bulk_in_ep,
00073         &bulk_out_ep,
00074         NULL
00075 };
00076 
00082 static int usbmast_add_device(usb_device_t *dev)
00083 {
00084         int rc;
00085         const char *fun_name = "ctl";
00086 
00087         ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
00088             fun_name);
00089         if (ctl_fun == NULL) {
00090                 usb_log_error("Failed to create control function.\n");
00091                 return ENOMEM;
00092         }
00093         rc = ddf_fun_bind(ctl_fun);
00094         if (rc != EOK) {
00095                 usb_log_error("Failed to bind control function: %s.\n",
00096                     str_error(rc));
00097                 return rc;
00098         }
00099 
00100         usb_log_info("Pretending to control mass storage `%s'.\n",
00101             dev->ddf_dev->name);
00102         usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
00103             dev->pipes[BULK_IN_EP].pipe->endpoint_no,
00104             (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
00105         usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
00106             dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
00107             (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
00108 
00109         size_t lun_count = usb_masstor_get_lun_count(dev);
00110 
00111         usb_massstor_inquiry_result_t inquiry;
00112         rc = usb_massstor_inquiry(dev, BULK_IN_EP, BULK_OUT_EP, &inquiry);
00113         if (rc != EOK) {
00114                 usb_log_warning("Failed to inquiry device `%s': %s.\n",
00115                     dev->ddf_dev->name, str_error(rc));
00116                 return EOK;
00117         }
00118 
00119         usb_log_info("Mass storage `%s': " \
00120             "`%s' by `%s' is %s (%s), %zu LUN(s).\n",
00121             dev->ddf_dev->name,
00122             inquiry.product_and_revision, inquiry.vendor_id,
00123             usb_str_masstor_scsi_peripheral_device_type(inquiry.peripheral_device_type),
00124             inquiry.removable ? "removable" : "non-removable",
00125             lun_count);
00126 
00127         return EOK;
00128 }
00129 
00131 static usb_driver_ops_t usbmast_driver_ops = {
00132         .add_device = usbmast_add_device,
00133 };
00134 
00136 static usb_driver_t usbmast_driver = {
00137         .name = NAME,
00138         .ops = &usbmast_driver_ops,
00139         .endpoints = mast_endpoints
00140 };
00141 
00142 int main(int argc, char *argv[])
00143 {
00144         usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
00145 
00146         return usb_driver_main(&usbmast_driver);
00147 }
00148 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7