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_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
71 dprintf(16, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
72 , op->drive_gf, 0, op->count, blocksize, op->buf_fl);
73 struct usbdrive_s *udrive_gf = container_of(
74 op->drive_gf, struct usbdrive_s, drive);
76 // Setup command block wrapper.
77 u32 bytes = blocksize * op->count;
79 memset(&cbw, 0, sizeof(cbw));
80 memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
81 cbw.dCBWSignature = CBW_SIGNATURE;
82 cbw.dCBWTag = 999; // XXX
83 cbw.dCBWDataTransferLength = bytes;
84 cbw.bmCBWFlags = cdb_is_read(cdbcmd, blocksize) ? USB_DIR_IN : USB_DIR_OUT;
85 cbw.bCBWLUN = GET_GLOBALFLAT(udrive_gf->lun);
86 cbw.bCBWCBLength = USB_CDB_SIZE;
88 // Transfer cbw to device.
89 int ret = usb_msc_send(udrive_gf, USB_DIR_OUT
90 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
94 // Transfer data to/from device.
96 ret = usb_msc_send(udrive_gf, cbw.bmCBWFlags, op->buf_fl, bytes);
101 // Transfer csw info.
103 ret = usb_msc_send(udrive_gf, USB_DIR_IN
104 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
109 return DISK_RET_SUCCESS;
110 if (csw.bCSWStatus == 2)
114 op->count -= csw.dCSWDataResidue / blocksize;
115 return DISK_RET_EBADTRACK;
118 // XXX - reset connection
119 dprintf(1, "USB transmission failed\n");
120 return DISK_RET_EBADTRACK;
124 usb_msc_maxlun(struct usb_pipe *pipe)
126 struct usb_ctrlrequest req;
127 req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
132 unsigned char maxlun;
133 int ret = usb_send_default_control(pipe, &req, &maxlun);
140 usb_msc_lun_setup(struct usb_pipe *inpipe, struct usb_pipe *outpipe,
141 struct usbdevice_s *usbdev, int lun)
143 // Allocate drive structure.
144 struct usbdrive_s *drive = malloc_fseg(sizeof(*drive));
149 memset(drive, 0, sizeof(*drive));
150 if (usb_32bit_pipe(inpipe))
151 drive->drive.type = DTYPE_USB_32;
153 drive->drive.type = DTYPE_USB;
154 drive->bulkin = inpipe;
155 drive->bulkout = outpipe;
158 int prio = bootprio_find_usb(usbdev, lun);
159 int ret = scsi_drive_setup(&drive->drive, "USB MSC", prio);
161 dprintf(1, "Unable to configure USB MSC drive.\n");
168 /****************************************************************
170 ****************************************************************/
172 // Configure a usb msc device.
174 usb_msc_setup(struct usbdevice_s *usbdev)
179 // Verify right kind of device
180 struct usb_interface_descriptor *iface = usbdev->iface;
181 if ((iface->bInterfaceSubClass != US_SC_SCSI &&
182 iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
183 iface->bInterfaceSubClass != US_SC_ATAPI_8020)
184 || iface->bInterfaceProtocol != US_PR_BULK) {
185 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
186 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
190 // Find bulk in and bulk out endpoints.
191 struct usb_pipe *inpipe = NULL, *outpipe = NULL;
192 struct usb_endpoint_descriptor *indesc = usb_find_desc(
193 usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
194 struct usb_endpoint_descriptor *outdesc = usb_find_desc(
195 usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
196 if (!indesc || !outdesc)
198 inpipe = usb_alloc_pipe(usbdev, indesc);
199 outpipe = usb_alloc_pipe(usbdev, outdesc);
200 if (!inpipe || !outpipe)
203 int maxlun = usb_msc_maxlun(usbdev->defpipe);
204 int lun, pipesused = 0;
205 for (lun = 0; lun < maxlun + 1; lun++) {
206 int ret = usb_msc_lun_setup(inpipe, outpipe, usbdev, lun);
216 dprintf(1, "Unable to configure USB MSC device.\n");
217 usb_free_pipe(usbdev, inpipe);
218 usb_free_pipe(usbdev, outpipe);