00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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