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
50 usb_msc_send(struct usbdrive_s *udrive_g, int dir, void *buf, u32 bytes)
52 struct usb_pipe *pipe;
53 if (dir == USB_DIR_OUT)
54 pipe = GET_GLOBAL(udrive_g->bulkout);
56 pipe = GET_GLOBAL(udrive_g->bulkin);
57 return usb_send_bulk(pipe, dir, buf, bytes);
60 // Low-level usb command transmit function.
62 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
67 dprintf(16, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
68 , op->drive_g, 0, op->count, blocksize, op->buf_fl);
69 struct usbdrive_s *udrive_g = container_of(
70 op->drive_g, struct usbdrive_s, drive);
72 // Setup command block wrapper.
73 u32 bytes = blocksize * op->count;
75 memset(&cbw, 0, sizeof(cbw));
76 memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
77 cbw.dCBWSignature = CBW_SIGNATURE;
78 cbw.dCBWTag = 999; // XXX
79 cbw.dCBWDataTransferLength = bytes;
80 cbw.bmCBWFlags = (cbw.CBWCB[0] == CDB_CMD_WRITE_10) ? USB_DIR_OUT : USB_DIR_IN;
81 cbw.bCBWLUN = 0; // XXX
82 cbw.bCBWCBLength = USB_CDB_SIZE;
84 // Transfer cbw to device.
85 int ret = usb_msc_send(udrive_g, USB_DIR_OUT
86 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
90 // Transfer data to/from device.
92 ret = usb_msc_send(udrive_g, cbw.bmCBWFlags, op->buf_fl, bytes);
99 ret = usb_msc_send(udrive_g, USB_DIR_IN
100 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
105 return DISK_RET_SUCCESS;
106 if (csw.bCSWStatus == 2)
110 op->count -= csw.dCSWDataResidue / blocksize;
111 return DISK_RET_EBADTRACK;
114 // XXX - reset connection
115 dprintf(1, "USB transmission failed\n");
117 return DISK_RET_EBADTRACK;
121 /****************************************************************
123 ****************************************************************/
125 // 16bit command demuxer for ATAPI cdroms.
127 process_usb_op(struct disk_op_s *op)
131 switch (op->command) {
135 return cdb_write(op);
141 return DISK_RET_SUCCESS;
144 return DISK_RET_EPARAM;
149 /****************************************************************
151 ****************************************************************/
154 setup_drive_cdrom(struct disk_op_s *op, char *desc)
156 op->drive_g->sectors = (u64)-1;
157 struct usb_pipe *pipe = container_of(
158 op->drive_g, struct usbdrive_s, drive)->bulkout;
159 int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
160 boot_add_cd(op->drive_g, desc, prio);
165 setup_drive_hd(struct disk_op_s *op, char *desc)
167 if (op->drive_g->blksize != DISK_SECTOR_SIZE) {
168 dprintf(1, "Unsupported USB MSC block size %d\n", op->drive_g->blksize);
172 // Register with bcv system.
173 struct usb_pipe *pipe = container_of(
174 op->drive_g, struct usbdrive_s, drive)->bulkout;
175 int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
176 boot_add_hd(op->drive_g, desc, prio);
180 // Configure a usb msc device.
182 usb_msc_init(struct usb_pipe *pipe
183 , struct usb_interface_descriptor *iface, int imax)
188 // Verify right kind of device
189 if ((iface->bInterfaceSubClass != US_SC_SCSI &&
190 iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
191 iface->bInterfaceSubClass != US_SC_ATAPI_8020)
192 || iface->bInterfaceProtocol != US_PR_BULK) {
193 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
194 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
198 // Allocate drive structure.
199 struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
204 memset(udrive_g, 0, sizeof(*udrive_g));
205 udrive_g->drive.type = DTYPE_USB;
207 // Find bulk in and bulk out endpoints.
208 struct usb_endpoint_descriptor *indesc = findEndPointDesc(
209 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
210 struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
211 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
212 if (!indesc || !outdesc)
214 udrive_g->bulkin = alloc_bulk_pipe(pipe, indesc);
215 udrive_g->bulkout = alloc_bulk_pipe(pipe, outdesc);
216 if (!udrive_g->bulkin || !udrive_g->bulkout)
219 // Validate drive and find block size and sector count.
220 struct disk_op_s dop;
221 memset(&dop, 0, sizeof(dop));
222 dop.drive_g = &udrive_g->drive;
223 struct cdbres_inquiry data;
224 int ret = cdb_get_inquiry(&dop, &data);
227 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
228 char rev[sizeof(data.rev)+1];
229 strtcpy(vendor, data.vendor, sizeof(vendor));
230 nullTrailingSpace(vendor);
231 strtcpy(product, data.product, sizeof(product));
232 nullTrailingSpace(product);
233 strtcpy(rev, data.rev, sizeof(rev));
234 nullTrailingSpace(rev);
235 int pdt = data.pdt & 0x1f;
236 int removable = !!(data.removable & 0x80);
237 dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
238 , vendor, product, rev, pdt, removable);
239 udrive_g->drive.removable = removable;
241 if (pdt == USB_MSC_TYPE_CDROM) {
242 char *desc = znprintf(MAXDESCSIZE, "DVD/CD [USB Drive %s %s %s]"
243 , vendor, product, rev);
244 ret = setup_drive_cdrom(&dop, desc);
246 struct cdbres_read_capacity capdata;
247 ret = cdb_read_capacity(&dop, &capdata);
250 // XXX - retry for some timeout?
252 // READ CAPACITY returns the address of the last block
253 udrive_g->drive.blksize = ntohl(capdata.blksize);
254 udrive_g->drive.sectors = ntohl(capdata.sectors) + 1;
255 dprintf(1, "USB MSC blksize=%d sectors=%d\n",
256 udrive_g->drive.blksize, (int)udrive_g->drive.sectors);
258 char *desc = znprintf(MAXDESCSIZE, "USB Drive %s %s %s"
259 , vendor, product, rev);
260 ret = setup_drive_hd(&dop, desc);
267 dprintf(1, "Unable to configure USB MSC device.\n");