52b41b0db0caa18448716e78c4569b68f9f170f4
[seabios.git] / src / usb-msc.c
1 // 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" // boot_add_hd
15
16 struct usbdrive_s {
17     struct drive_s drive;
18     struct usb_pipe *bulkin, *bulkout;
19 };
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
30 struct 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
42 struct csw_s {
43     u32 dCSWSignature;
44     u32 dCSWTag;
45     u32 dCSWDataResidue;
46     u8 bCSWStatus;
47 } PACKED;
48
49 // Low-level usb command transmit function.
50 int
51 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
52 {
53     if (!CONFIG_USB_MSC)
54         return 0;
55
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);
62
63     // Setup command block wrapper.
64     u32 bytes = blocksize * op->count;
65     struct cbw_s cbw;
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);
74
75     // Transfer cbw to device.
76     int ret = usb_send_bulk(bulkout, USB_DIR_OUT
77                             , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
78     if (ret)
79         goto fail;
80
81     // Transfer data from device.
82     if (bytes) {
83         ret = usb_send_bulk(bulkin, USB_DIR_IN, op->buf_fl, bytes);
84         if (ret)
85             goto fail;
86     }
87
88     // Transfer csw info.
89     struct csw_s csw;
90     ret = usb_send_bulk(bulkin, USB_DIR_IN
91                         , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
92     if (ret)
93         goto fail;
94
95     if (!csw.bCSWStatus)
96         return DISK_RET_SUCCESS;
97     if (csw.bCSWStatus == 2)
98         goto fail;
99
100     if (blocksize)
101         op->count -= csw.dCSWDataResidue / blocksize;
102     return DISK_RET_EBADTRACK;
103
104 fail:
105     // XXX - reset connection
106     dprintf(1, "USB transmission failed\n");
107     op->count = 0;
108     return DISK_RET_EBADTRACK;
109 }
110
111
112 /****************************************************************
113  * Drive ops
114  ****************************************************************/
115
116 // 16bit command demuxer for ATAPI cdroms.
117 int
118 process_usb_op(struct disk_op_s *op)
119 {
120     if (!CONFIG_USB_MSC)
121         return 0;
122     switch (op->command) {
123     case CMD_READ:
124         return cdb_read(op);
125     case CMD_FORMAT:
126     case CMD_WRITE:
127         return DISK_RET_EWRITEPROTECT;
128     case CMD_RESET:
129     case CMD_ISREADY:
130     case CMD_VERIFY:
131     case CMD_SEEK:
132         return DISK_RET_SUCCESS;
133     default:
134         op->count = 0;
135         return DISK_RET_EPARAM;
136     }
137 }
138
139
140 /****************************************************************
141  * Setup
142  ****************************************************************/
143
144 static int
145 setup_drive_cdrom(struct disk_op_s *op, char *desc)
146 {
147     op->drive_g->blksize = CDROM_SECTOR_SIZE;
148     op->drive_g->sectors = (u64)-1;
149     struct usb_pipe *pipe = container_of(
150         op->drive_g, struct usbdrive_s, drive)->bulkout;
151     int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
152     boot_add_cd(op->drive_g, desc, prio);
153     return 0;
154 }
155
156 static int
157 setup_drive_hd(struct disk_op_s *op, char *desc)
158 {
159     struct cdbres_read_capacity info;
160     int ret = cdb_read_capacity(op, &info);
161     if (ret)
162         return ret;
163     // XXX - retry for some timeout?
164
165     u32 blksize = ntohl(info.blksize), sectors = ntohl(info.sectors);
166     if (blksize != DISK_SECTOR_SIZE) {
167         if (blksize == CDROM_SECTOR_SIZE)
168             return setup_drive_cdrom(op, desc);
169         dprintf(1, "Unsupported USB MSC block size %d\n", blksize);
170         return -1;
171     }
172     op->drive_g->blksize = blksize;
173     op->drive_g->sectors = sectors;
174     dprintf(1, "USB MSC blksize=%d sectors=%d\n", blksize, sectors);
175
176     // Register with bcv system.
177     struct usb_pipe *pipe = container_of(
178         op->drive_g, struct usbdrive_s, drive)->bulkout;
179     int prio = bootprio_find_usb(pipe->cntl->pci, pipe->path);
180     boot_add_hd(op->drive_g, desc, prio);
181
182     return 0;
183 }
184
185 // Configure a usb msc device.
186 int
187 usb_msc_init(struct usb_pipe *pipe
188              , struct usb_interface_descriptor *iface, int imax)
189 {
190     if (!CONFIG_USB_MSC)
191         return -1;
192
193     // Verify right kind of device
194     if ((iface->bInterfaceSubClass != US_SC_SCSI &&
195          iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
196          iface->bInterfaceSubClass != US_SC_ATAPI_8020)
197         || iface->bInterfaceProtocol != US_PR_BULK) {
198         dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
199                 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
200         return -1;
201     }
202
203     // Allocate drive structure.
204     struct usbdrive_s *udrive_g = malloc_fseg(sizeof(*udrive_g));
205     if (!udrive_g) {
206         warn_noalloc();
207         goto fail;
208     }
209     memset(udrive_g, 0, sizeof(*udrive_g));
210     udrive_g->drive.type = DTYPE_USB;
211
212     // Find bulk in and bulk out endpoints.
213     struct usb_endpoint_descriptor *indesc = findEndPointDesc(
214         iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
215     struct usb_endpoint_descriptor *outdesc = findEndPointDesc(
216         iface, imax, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
217     if (!indesc || !outdesc)
218         goto fail;
219     udrive_g->bulkin = alloc_bulk_pipe(pipe, indesc);
220     udrive_g->bulkout = alloc_bulk_pipe(pipe, outdesc);
221     if (!udrive_g->bulkin || !udrive_g->bulkout)
222         goto fail;
223
224     // Validate drive and find block size and sector count.
225     struct disk_op_s dop;
226     memset(&dop, 0, sizeof(dop));
227     dop.drive_g = &udrive_g->drive;
228     struct cdbres_inquiry data;
229     int ret = cdb_get_inquiry(&dop, &data);
230     if (ret)
231         goto fail;
232     char vendor[sizeof(data.vendor)+1], product[sizeof(data.product)+1];
233     char rev[sizeof(data.rev)+1];
234     strtcpy(vendor, data.vendor, sizeof(vendor));
235     nullTrailingSpace(vendor);
236     strtcpy(product, data.product, sizeof(product));
237     nullTrailingSpace(product);
238     strtcpy(rev, data.rev, sizeof(rev));
239     nullTrailingSpace(rev);
240     int pdt = data.pdt & 0x1f;
241     int removable = !!(data.removable & 0x80);
242     dprintf(1, "USB MSC vendor='%s' product='%s' rev='%s' type=%d removable=%d\n"
243             , vendor, product, rev, pdt, removable);
244     udrive_g->drive.removable = removable;
245
246     if (pdt == USB_MSC_TYPE_CDROM) {
247         char *desc = znprintf(MAXDESCSIZE, "DVD/CD [USB Drive %s %s %s]"
248                               , vendor, product, rev);
249         ret = setup_drive_cdrom(&dop, desc);
250     } else {
251         char *desc = znprintf(MAXDESCSIZE, "USB Drive %s %s %s"
252                               , vendor, product, rev);
253         ret = setup_drive_hd(&dop, desc);
254     }
255     if (ret)
256         goto fail;
257
258     return 0;
259 fail:
260     dprintf(1, "Unable to configure USB MSC device.\n");
261     free(udrive_g);
262     return -1;
263 }