blob: e91d9161c0ebe1db31e723d43f55d6ec26466577 [file] [log] [blame]
Kevin O'Connor7149fc82010-02-17 23:24:42 -05001// Code for handling USB Mass Storage Controller devices.
2//
3// Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
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
15
Kevin O'Connor7149fc82010-02-17 23:24:42 -050016struct usbdrive_s {
17 struct drive_s drive;
18 struct usb_pipe *bulkin, *bulkout;
Kevin O'Connor7149fc82010-02-17 23:24:42 -050019};
20
21
22/****************************************************************
23 * Bulk-only drive command processing
24 ****************************************************************/
25
26#define USB_CDB_SIZE 12
27
28#define CBW_SIGNATURE 0x43425355 // USBC
29
30struct cbw_s {
31 u32 dCBWSignature;
32 u32 dCBWTag;
33 u32 dCBWDataTransferLength;
34 u8 bmCBWFlags;
35 u8 bCBWLUN;
36 u8 bCBWCBLength;
37 u8 CBWCB[16];
38} PACKED;
39
40#define CSW_SIGNATURE 0x53425355 // USBS
41
42struct csw_s {
43 u32 dCSWSignature;
44 u32 dCSWTag;
45 u32 dCSWDataResidue;
46 u8 bCSWStatus;
47} PACKED;
48
49// Low-level usb command transmit function.
50int
51usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
52{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050053 dprintf(16, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
Kevin O'Connor7149fc82010-02-17 23:24:42 -050054 , 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);
59
60 // Setup command block wrapper.
61 u32 bytes = blocksize * op->count;
62 struct cbw_s cbw;
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);
71
72 // Transfer cbw to device.
73 int ret = usb_send_bulk(bulkout, USB_DIR_OUT
74 , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
75 if (ret)
76 goto fail;
77
78 // Transfer data from device.
79 ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
80 if (ret)
81 goto fail;
82
83 // Transfer csw info.
84 struct csw_s csw;
85 ret = usb_send_bulk(bulkin, USB_DIR_IN
86 , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
87 if (ret)
88 goto fail;
89
90 if (!csw.bCSWStatus)
91 return DISK_RET_SUCCESS;
92 if (csw.bCSWStatus == 2)
93 goto fail;
94
95 op->count -= csw.dCSWDataResidue / blocksize;
96 return DISK_RET_EBADTRACK;
97
98fail:
99 // XXX - reset connection
100 dprintf(1, "USB transmission failed\n");
101 op->count = 0;
102 return DISK_RET_EBADTRACK;
103}
104
105
106/****************************************************************
107 * Drive ops
108 ****************************************************************/
109
110// 16bit command demuxer for ATAPI cdroms.
111int
112process_usb_op(struct disk_op_s *op)
113{
114 switch (op->command) {
115 case CMD_READ:
116 return cdb_read(op);
117 case CMD_FORMAT:
118 case CMD_WRITE:
119 return DISK_RET_EWRITEPROTECT;
120 case CMD_RESET:
121 case CMD_ISREADY:
122 case CMD_VERIFY:
123 case CMD_SEEK:
124 return DISK_RET_SUCCESS;
125 default:
126 op->count = 0;
127 return DISK_RET_EPARAM;
128 }
129}
130
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500131
132/****************************************************************
133 * Setup
134 ****************************************************************/
135
136static int
137setup_drive_cdrom(struct disk_op_s *op)
138{
139 op->drive_g->blksize = CDROM_SECTOR_SIZE;
140 op->drive_g->sectors = (u64)-1;
141 map_cd_drive(op->drive_g);
142 return 0;
143}
144
145static int
146setup_drive_hd(struct disk_op_s *op)
147{
148 struct cdbres_read_capacity info;
149 int ret = cdb_read_capacity(op, &info);
150 if (ret)
151 return ret;
152 // XXX - retry for some timeout?
153
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);
159 return -1;
160 }
161 op->drive_g->blksize = blksize;
162 op->drive_g->sectors = sectors;
163 dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
164
165 // Setup disk geometry translation.
166 setup_translation(op->drive_g);
167
168 // Register with bcv system.
169 add_bcv_internal(op->drive_g);
170
171 return 0;
172}
173
174// Configure a usb msc device.
175int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500176usb_msc_init(struct usb_pipe *pipe
177 , struct usb_interface_descriptor *iface, int imax)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500178{
179 if (!CONFIG_USB_MSC)
180 return -1;
181
182 // Verify right kind of device
183 if (iface->bInterfaceSubClass != US_SC_SCSI
184 || iface->bInterfaceProtocol != US_PR_BULK) {
185 dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
186 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
187 return -1;
188 }
189
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500190 // Allocate drive structure.
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500191 char *desc = malloc_tmphigh(MAXDESCSIZE);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500192 struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
193 if (!udrive_g || !desc) {
194 warn_noalloc();
195 goto fail;
196 }
197 memset(udrive_g, 0, sizeof(*udrive_g));
198 udrive_g->drive.type = DTYPE_USB;
Kevin O'Connor67f6d372010-04-08 20:40:47 -0400199
200 // Find bulk in and bulk out endpoints.
201 struct usb_endpoint_descriptor *indesc = findEndPointDesc(
202 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
203 struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
204 iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
205 if (!indesc || !outdesc)
206 goto fail;
207 udrive_g->bulkin = alloc_bulk_pipe(pipe, indesc);
208 udrive_g->bulkout = alloc_bulk_pipe(pipe, outdesc);
209 if (!udrive_g->bulkin || !udrive_g->bulkout)
210 goto fail;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500211
212 // Validate drive and find block size and sector count.
213 struct disk_op_s dop;
214 memset(&dop, 0, sizeof(dop));
215 dop.drive_g = &udrive_g->drive;
216 struct cdbres_inquiry data;
217 int ret = cdb_get_inquiry(&dop, &data);
218 if (ret)
219 goto fail;
220 char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
221 char rev[sizeof(data.rev)+1];
222 int pdt = data.pdt & 0x1f;
223 int removable = !!(data.removable & 0x80);
224 dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s'"
225 " type=%d removable=%d\n"
226 , strtcpy(vendor, data.vendor, sizeof(vendor))
227 , strtcpy(product, data.product, sizeof(product))
228 , strtcpy(rev, data.rev, sizeof(rev))
229 , pdt, removable);
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500230 udrive_g->drive.removable = removable;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500231
232 if (pdt == USB_MSC_TYPE_CDROM)
233 ret = setup_drive_cdrom(&dop);
234 else
235 ret = setup_drive_hd(&dop);
236 if (ret)
237 goto fail;
238
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500239 snprintf(desc, MAXDESCSIZE, "USB Drive %s %s %s", vendor, product, rev);
240 udrive_g->drive.desc = desc;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500241
242 return 0;
243fail:
244 dprintf(1, "Unable to configure USB MSC device.\n");
245 free(desc);
246 free(udrive_g);
247 return -1;
248}