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" // boot_add_hd
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)
56 dprintf(16, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
57 , op->drive_g, 0, op->count, blocksize, op->buf_fl);
58 struct usbdrive_s *udrive_g = container_of(
59 op->drive_g, struct usbdrive_s, drive);
60 struct usb_pipe *bulkin = GET_GLOBAL(udrive_g->bulkin);
61 struct usb_pipe *bulkout = GET_GLOBAL(udrive_g->bulkout);
63 // Setup command block wrapper.
64 u32 bytes = blocksize * op->count;
66 memset(&cbw, 0, sizeof(cbw));
67 cbw.dCBWSignature = CBW_SIGNATURE;
68 cbw.dCBWTag = 999; // XXX
69 cbw.dCBWDataTransferLength = bytes;
70 cbw.bmCBWFlags = USB_DIR_IN; // XXX
71 cbw.bCBWLUN = 0; // XXX
72 cbw.bCBWCBLength = USB_CDB_SIZE;
73 memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
75 // Transfer cbw to device.
76 int ret = usb_send_bulk(bulkout, USB_DIR_OUT
77 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
81 // Transfer data from device.
82 ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
88 ret = usb_send_bulk(bulkin, USB_DIR_IN
89 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
94 return DISK_RET_SUCCESS;
95 if (csw.bCSWStatus == 2)
98 op->count -= csw.dCSWDataResidue / blocksize;
99 return DISK_RET_EBADTRACK;
102 // XXX - reset connection
103 dprintf(1, "USB transmission failed\n");
105 return DISK_RET_EBADTRACK;
109 /****************************************************************
111 ****************************************************************/
113 // 16bit command demuxer for ATAPI cdroms.
115 process_usb_op(struct disk_op_s *op)
119 switch (op->command) {
124 return DISK_RET_EWRITEPROTECT;
129 return DISK_RET_SUCCESS;
132 return DISK_RET_EPARAM;
137 /****************************************************************
139 ****************************************************************/
142 setup_drive_cdrom(struct disk_op_s *op, char *desc)
144 op->drive_g->blksize = CDROM_SECTOR_SIZE;
145 op->drive_g->sectors = (u64)-1;
146 struct usb_pipe *pipe = container_of(
147 op->drive_g, struct usbdrive_s, drive)->bulkout;
148 int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
149 boot_add_cd(op->drive_g, desc, prio);
154 setup_drive_hd(struct disk_op_s *op, char *desc)
156 struct cdbres_read_capacity info;
157 int ret = cdb_read_capacity(op, &info);
160 // XXX - retry for some timeout?
162 u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
163 if (blksize != DISK_SECTOR_SIZE) {
164 if (blksize == CDROM_SECTOR_SIZE)
165 return setup_drive_cdrom(op, desc);
166 dprintf(1, "Unsupported USB MSC block size %d\n", blksize);
169 op->drive_g->blksize = blksize;
170 op->drive_g->sectors = sectors;
171 dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
173 // Register with bcv system.
174 struct usb_pipe *pipe = container_of(
175 op->drive_g, struct usbdrive_s, drive)->bulkout;
176 int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
177 boot_add_hd(op->drive_g, desc, prio);
182 // Configure a usb msc device.
184 usb_msc_init(struct usb_pipe *pipe
185 , struct usb_interface_descriptor *iface, int imax)
190 // Verify right kind of device
191 if ((iface->bInterfaceSubClass != US_SC_SCSI &&
192 iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
193 iface->bInterfaceSubClass != US_SC_ATAPI_8020)
194 || iface->bInterfaceProtocol != US_PR_BULK) {
195 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
196 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
200 // Allocate drive structure.
201 struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
206 memset(udrive_g, 0, sizeof(*udrive_g));
207 udrive_g->drive.type = DTYPE_USB;
209 // Find bulk in and bulk out endpoints.
210 struct usb_endpoint_descriptor *indesc = findEndPointDesc(
211 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
212 struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
213 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
214 if (!indesc || !outdesc)
216 udrive_g->bulkin = alloc_bulk_pipe(pipe, indesc);
217 udrive_g->bulkout = alloc_bulk_pipe(pipe, outdesc);
218 if (!udrive_g->bulkin || !udrive_g->bulkout)
221 // Validate drive and find block size and sector count.
222 struct disk_op_s dop;
223 memset(&dop, 0, sizeof(dop));
224 dop.drive_g = &udrive_g->drive;
225 struct cdbres_inquiry data;
226 int ret = cdb_get_inquiry(&dop, &data);
229 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
230 char rev[sizeof(data.rev)+1];
231 strtcpy(vendor, data.vendor, sizeof(vendor));
232 nullTrailingSpace(vendor);
233 strtcpy(product, data.product, sizeof(product));
234 nullTrailingSpace(product);
235 strtcpy(rev, data.rev, sizeof(rev));
236 nullTrailingSpace(rev);
237 int pdt = data.pdt & 0x1f;
238 int removable = !!(data.removable & 0x80);
239 dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
240 , vendor, product, rev, pdt, removable);
241 udrive_g->drive.removable = removable;
243 if (pdt == USB_MSC_TYPE_CDROM) {
244 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [USB Drive %s %s %s]"
245 , vendor, product, rev);
246 ret = setup_drive_cdrom(&dop, desc);
248 char *desc = znprintf(MAXDESCSIZE, "USB Drive %s %s %s"
249 , vendor, product, rev);
250 ret = setup_drive_hd(&dop, desc);
257 dprintf(1, "Unable to configure USB MSC device.\n");