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 "util.h" // dprintf
8 #include "config.h" // CONFIG_USB_MSC
9 #include "usb-msc.h" // usb_msc_init
10 #include "usb.h" // struct usb_s
11 #include "biosvar.h" // GET_GLOBAL
12 #include "blockcmd.h" // cdb_read
13 #include "disk.h" // DTYPE_USB
14 #include "boot.h" // add_bcv_internal
18 struct usb_pipe *bulkin, *bulkout;
22 /****************************************************************
23 * Bulk-only drive command processing
24 ****************************************************************/
26 #define USB_CDB_SIZE 12
28 #define CBW_SIGNATURE 0x43425355 // USBC
33 u32 dCBWDataTransferLength;
40 #define CSW_SIGNATURE 0x53425355 // USBS
49 // Low-level usb command transmit function.
51 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
53 dprintf(1, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
54 , op->drive_g, 0, op->count, blocksize, op->buf_fl);
55 struct usbdrive_s *udrive_g = container_of(
56 op->drive_g, struct usbdrive_s, drive);
57 struct usb_pipe *bulkin = GET_GLOBAL(udrive_g->bulkin);
58 struct usb_pipe *bulkout = GET_GLOBAL(udrive_g->bulkout);
60 // Setup command block wrapper.
61 u32 bytes = blocksize * op->count;
63 memset(&cbw, 0, sizeof(cbw));
64 cbw.dCBWSignature = CBW_SIGNATURE;
65 cbw.dCBWTag = 999; // XXX
66 cbw.dCBWDataTransferLength = bytes;
67 cbw.bmCBWFlags = USB_DIR_IN; // XXX
68 cbw.bCBWLUN = 0; // XXX
69 cbw.bCBWCBLength = USB_CDB_SIZE;
70 memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
72 // Transfer cbw to device.
73 int ret = usb_send_bulk(bulkout, USB_DIR_OUT
74 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
78 // Transfer data from device.
79 ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
85 ret = usb_send_bulk(bulkin, USB_DIR_IN
86 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
91 return DISK_RET_SUCCESS;
92 if (csw.bCSWStatus == 2)
95 op->count -= csw.dCSWDataResidue / blocksize;
96 return DISK_RET_EBADTRACK;
99 // XXX - reset connection
100 dprintf(1, "USB transmission failed\n");
102 return DISK_RET_EBADTRACK;
106 /****************************************************************
108 ****************************************************************/
110 // 16bit command demuxer for ATAPI cdroms.
112 process_usb_op(struct disk_op_s *op)
114 switch (op->command) {
119 return DISK_RET_EWRITEPROTECT;
124 return DISK_RET_SUCCESS;
127 return DISK_RET_EPARAM;
132 /****************************************************************
134 ****************************************************************/
137 setup_drive_cdrom(struct disk_op_s *op)
139 op->drive_g->blksize = CDROM_SECTOR_SIZE;
140 op->drive_g->sectors = (u64)-1;
141 map_cd_drive(op->drive_g);
146 setup_drive_hd(struct disk_op_s *op)
148 struct cdbres_read_capacity info;
149 int ret = cdb_read_capacity(op, &info);
152 // XXX - retry for some timeout?
154 u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
155 if (blksize != DISK_SECTOR_SIZE) {
156 if (blksize == CDROM_SECTOR_SIZE)
157 return setup_drive_cdrom(op);
158 dprintf(1, "Unsupported USB MSC block size %d\n", blksize);
161 op->drive_g->blksize = blksize;
162 op->drive_g->sectors = sectors;
163 dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
165 // Setup disk geometry translation.
166 setup_translation(op->drive_g);
168 // Register with bcv system.
169 add_bcv_internal(op->drive_g);
174 // Configure a usb msc device.
176 usb_msc_init(u32 endp, struct usb_interface_descriptor *iface, int imax)
181 // Verify right kind of device
182 if (iface->bInterfaceSubClass != US_SC_SCSI
183 || iface->bInterfaceProtocol != US_PR_BULK) {
184 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
185 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
189 // Find bulk in and bulk out endpoints.
190 struct usb_endpoint_descriptor *indesc = findEndPointDesc(
191 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
192 struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
193 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
194 if (!indesc || !outdesc)
196 u32 inendp = mkendpFromDesc(endp, indesc);
197 struct usb_pipe *bulkin = alloc_bulk_pipe(inendp);
198 u32 outendp = mkendpFromDesc(endp, outdesc);
199 struct usb_pipe *bulkout = alloc_bulk_pipe(outendp);
200 if (!bulkin || !bulkout)
203 // Allocate drive structure.
204 char *desc = malloc_tmphigh(MAXDESCSIZE);
205 struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
206 if (!udrive_g || !desc) {
210 memset(udrive_g, 0, sizeof(*udrive_g));
211 udrive_g->drive.type = DTYPE_USB;
212 udrive_g->bulkin = bulkin;
213 udrive_g->bulkout = bulkout;
215 // Validate drive and find block size and sector count.
216 struct disk_op_s dop;
217 memset(&dop, 0, sizeof(dop));
218 dop.drive_g = &udrive_g->drive;
219 struct cdbres_inquiry data;
220 int ret = cdb_get_inquiry(&dop, &data);
223 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
224 char rev[sizeof(data.rev)+1];
225 int pdt = data.pdt & 0x1f;
226 int removable = !!(data.removable & 0x80);
227 dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s'"
228 " type=%d removable=%d\n"
229 , strtcpy(vendor, data.vendor, sizeof(vendor))
230 , strtcpy(product, data.product, sizeof(product))
231 , strtcpy(rev, data.rev, sizeof(rev))
233 udrive_g->drive.removable = removable;
235 if (pdt == USB_MSC_TYPE_CDROM)
236 ret = setup_drive_cdrom(&dop);
238 ret = setup_drive_hd(&dop);
242 snprintf(desc, MAXDESCSIZE, "USB Drive %s %s %s", vendor, product, rev);
243 udrive_g->drive.desc = desc;
247 dprintf(1, "Unable to configure USB MSC device.\n");