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