1 // Code for handling USB Mass Storage Controller devices.
3 // Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "biosvar.h" // GET_GLOBALFLAT
8 #include "block.h" // DTYPE_USB
9 #include "blockcmd.h" // cdb_read
10 #include "config.h" // CONFIG_USB_MSC
11 #include "malloc.h" // free
12 #include "output.h" // dprintf
13 #include "std/disk.h" // DISK_RET_SUCCESS
14 #include "string.h" // memset
15 #include "usb.h" // struct usb_s
16 #include "usb-msc.h" // usb_msc_setup
17 #include "util.h" // bootprio_find_usb
21 struct usb_pipe *bulkin, *bulkout;
26 /****************************************************************
27 * Bulk-only drive command processing
28 ****************************************************************/
30 #define USB_CDB_SIZE 12
32 #define CBW_SIGNATURE 0x43425355 // USBC
37 u32 dCBWDataTransferLength;
44 #define CSW_SIGNATURE 0x53425355 // USBS
54 usb_msc_send(struct usbdrive_s *udrive_gf, int dir, void *buf, u32 bytes)
56 struct usb_pipe *pipe;
57 if (dir == USB_DIR_OUT)
58 pipe = GET_GLOBALFLAT(udrive_gf->bulkout);
60 pipe = GET_GLOBALFLAT(udrive_gf->bulkin);
61 return usb_send_bulk(pipe, dir, buf, bytes);
64 // Low-level usb command transmit function.
66 usb_process_op(struct disk_op_s *op)
71 dprintf(16, "usb_cmd_data id=%p write=%d count=%d buf=%p\n"
72 , op->drive_gf, 0, op->count, op->buf_fl);
73 struct usbdrive_s *udrive_gf = container_of(
74 op->drive_gf, struct usbdrive_s, drive);
76 // Setup command block wrapper.
78 memset(&cbw, 0, sizeof(cbw));
79 int blocksize = scsi_fill_cmd(op, cbw.CBWCB, USB_CDB_SIZE);
81 return default_process_op(op);
82 u32 bytes = blocksize * op->count;
83 cbw.dCBWSignature = CBW_SIGNATURE;
84 cbw.dCBWTag = 999; // XXX
85 cbw.dCBWDataTransferLength = bytes;
86 cbw.bmCBWFlags = scsi_is_read(op) ? USB_DIR_IN : USB_DIR_OUT;
87 cbw.bCBWLUN = GET_GLOBALFLAT(udrive_gf->lun);
88 cbw.bCBWCBLength = USB_CDB_SIZE;
90 // Transfer cbw to device.
91 int ret = usb_msc_send(udrive_gf, USB_DIR_OUT
92 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
96 // Transfer data to/from device.
98 ret = usb_msc_send(udrive_gf, cbw.bmCBWFlags, op->buf_fl, bytes);
103 // Transfer csw info.
105 ret = usb_msc_send(udrive_gf, USB_DIR_IN
106 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
111 return DISK_RET_SUCCESS;
112 if (csw.bCSWStatus == 2)
116 op->count -= csw.dCSWDataResidue / blocksize;
117 return DISK_RET_EBADTRACK;
120 // XXX - reset connection
121 dprintf(1, "USB transmission failed\n");
122 return DISK_RET_EBADTRACK;
126 usb_msc_maxlun(struct usb_pipe *pipe)
128 struct usb_ctrlrequest req;
129 req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
134 unsigned char maxlun;
135 int ret = usb_send_default_control(pipe, &req, &maxlun);
142 usb_msc_lun_setup(struct usb_pipe *inpipe, struct usb_pipe *outpipe,
143 struct usbdevice_s *usbdev, int lun)
145 // Allocate drive structure.
146 struct usbdrive_s *drive = malloc_fseg(sizeof(*drive));
151 memset(drive, 0, sizeof(*drive));
152 if (usb_32bit_pipe(inpipe))
153 drive->drive.type = DTYPE_USB_32;
155 drive->drive.type = DTYPE_USB;
156 drive->bulkin = inpipe;
157 drive->bulkout = outpipe;
160 int prio = bootprio_find_usb(usbdev, lun);
161 int ret = scsi_drive_setup(&drive->drive, "USB MSC", prio);
163 dprintf(1, "Unable to configure USB MSC drive.\n");
170 /****************************************************************
172 ****************************************************************/
174 // Configure a usb msc device.
176 usb_msc_setup(struct usbdevice_s *usbdev)
181 // Verify right kind of device
182 struct usb_interface_descriptor *iface = usbdev->iface;
183 if ((iface->bInterfaceSubClass != US_SC_SCSI &&
184 iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
185 iface->bInterfaceSubClass != US_SC_ATAPI_8020)
186 || iface->bInterfaceProtocol != US_PR_BULK) {
187 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
188 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
192 // Find bulk in and bulk out endpoints.
193 struct usb_pipe *inpipe = NULL, *outpipe = NULL;
194 struct usb_endpoint_descriptor *indesc = usb_find_desc(
195 usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
196 struct usb_endpoint_descriptor *outdesc = usb_find_desc(
197 usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
198 if (!indesc || !outdesc)
200 inpipe = usb_alloc_pipe(usbdev, indesc);
201 outpipe = usb_alloc_pipe(usbdev, outdesc);
202 if (!inpipe || !outpipe)
205 int maxlun = usb_msc_maxlun(usbdev->defpipe);
206 int lun, pipesused = 0;
207 for (lun = 0; lun < maxlun + 1; lun++) {
208 int ret = usb_msc_lun_setup(inpipe, outpipe, usbdev, lun);
218 dprintf(1, "Unable to configure USB MSC device.\n");
219 usb_free_pipe(usbdev, inpipe);
220 usb_free_pipe(usbdev, outpipe);