Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / 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 "biosvar.h" // GET_GLOBALFLAT
8 #include "block.h" // DTYPE_USB
9 #include "blockcmd.h" // cdb_read
10 #include "config.h" // CONFIG_USB_MSC
11 #include "malloc.h" // free
12 #include "output.h" // dprintf
13 #include "std/disk.h" // DISK_RET_SUCCESS
14 #include "string.h" // memset
15 #include "usb.h" // struct usb_s
16 #include "usb-msc.h" // usb_msc_setup
17 #include "util.h" // bootprio_find_usb
18
19 struct usbdrive_s {
20     struct drive_s drive;
21     struct usb_pipe *bulkin, *bulkout;
22     int lun;
23 };
24
25
26 /****************************************************************
27  * Bulk-only drive command processing
28  ****************************************************************/
29
30 #define USB_CDB_SIZE 12
31
32 #define CBW_SIGNATURE 0x43425355 // USBC
33
34 struct cbw_s {
35     u32 dCBWSignature;
36     u32 dCBWTag;
37     u32 dCBWDataTransferLength;
38     u8 bmCBWFlags;
39     u8 bCBWLUN;
40     u8 bCBWCBLength;
41     u8 CBWCB[16];
42 } PACKED;
43
44 #define CSW_SIGNATURE 0x53425355 // USBS
45
46 struct csw_s {
47     u32 dCSWSignature;
48     u32 dCSWTag;
49     u32 dCSWDataResidue;
50     u8 bCSWStatus;
51 } PACKED;
52
53 static int
54 usb_msc_send(struct usbdrive_s *udrive_gf, int dir, void *buf, u32 bytes)
55 {
56     struct usb_pipe *pipe;
57     if (dir == USB_DIR_OUT)
58         pipe = GET_GLOBALFLAT(udrive_gf->bulkout);
59     else
60         pipe = GET_GLOBALFLAT(udrive_gf->bulkin);
61     return usb_send_bulk(pipe, dir, buf, bytes);
62 }
63
64 // Low-level usb command transmit function.
65 int
66 usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
67 {
68     if (!CONFIG_USB_MSC)
69         return 0;
70
71     dprintf(16, "usb_cmd_data id=%p write=%d count=%d bs=%d buf=%p\n"
72             , op->drive_gf, 0, op->count, blocksize, op->buf_fl);
73     struct usbdrive_s *udrive_gf = container_of(
74         op->drive_gf, struct usbdrive_s, drive);
75
76     // Setup command block wrapper.
77     u32 bytes = blocksize * op->count;
78     struct cbw_s cbw;
79     memset(&cbw, 0, sizeof(cbw));
80     memcpy(cbw.CBWCB, cdbcmd, USB_CDB_SIZE);
81     cbw.dCBWSignature = CBW_SIGNATURE;
82     cbw.dCBWTag = 999; // XXX
83     cbw.dCBWDataTransferLength = bytes;
84     cbw.bmCBWFlags = cdb_is_read(cdbcmd, blocksize) ? USB_DIR_IN : USB_DIR_OUT;
85     cbw.bCBWLUN = GET_GLOBALFLAT(udrive_gf->lun);
86     cbw.bCBWCBLength = USB_CDB_SIZE;
87
88     // Transfer cbw to device.
89     int ret = usb_msc_send(udrive_gf, USB_DIR_OUT
90                            , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
91     if (ret)
92         goto fail;
93
94     // Transfer data to/from device.
95     if (bytes) {
96         ret = usb_msc_send(udrive_gf, cbw.bmCBWFlags, op->buf_fl, bytes);
97         if (ret)
98             goto fail;
99     }
100
101     // Transfer csw info.
102     struct csw_s csw;
103     ret = usb_msc_send(udrive_gf, USB_DIR_IN
104                        , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
105     if (ret)
106         goto fail;
107
108     if (!csw.bCSWStatus)
109         return DISK_RET_SUCCESS;
110     if (csw.bCSWStatus == 2)
111         goto fail;
112
113     if (blocksize)
114         op->count -= csw.dCSWDataResidue / blocksize;
115     return DISK_RET_EBADTRACK;
116
117 fail:
118     // XXX - reset connection
119     dprintf(1, "USB transmission failed\n");
120     return DISK_RET_EBADTRACK;
121 }
122
123 static int
124 usb_msc_maxlun(struct usb_pipe *pipe)
125 {
126     struct usb_ctrlrequest req;
127     req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
128     req.bRequest = 0xfe;
129     req.wValue = 0;
130     req.wIndex = 0;
131     req.wLength = 1;
132     unsigned char maxlun;
133     int ret = usb_send_default_control(pipe, &req, &maxlun);
134     if (ret)
135         return 0;
136     return maxlun;
137 }
138
139 static int
140 usb_msc_lun_setup(struct usb_pipe *inpipe, struct usb_pipe *outpipe,
141                   struct usbdevice_s *usbdev, int lun)
142 {
143     // Allocate drive structure.
144     struct usbdrive_s *drive = malloc_fseg(sizeof(*drive));
145     if (!drive) {
146         warn_noalloc();
147         return -1;
148     }
149     memset(drive, 0, sizeof(*drive));
150     if (usb_32bit_pipe(inpipe))
151         drive->drive.type = DTYPE_USB_32;
152     else
153         drive->drive.type = DTYPE_USB;
154     drive->bulkin = inpipe;
155     drive->bulkout = outpipe;
156     drive->lun = lun;
157
158     int prio = bootprio_find_usb(usbdev, lun);
159     int ret = scsi_drive_setup(&drive->drive, "USB MSC", prio);
160     if (ret) {
161         dprintf(1, "Unable to configure USB MSC drive.\n");
162         free(drive);
163         return -1;
164     }
165     return 0;
166 }
167
168 /****************************************************************
169  * Setup
170  ****************************************************************/
171
172 // Configure a usb msc device.
173 int
174 usb_msc_setup(struct usbdevice_s *usbdev)
175 {
176     if (!CONFIG_USB_MSC)
177         return -1;
178
179     // Verify right kind of device
180     struct usb_interface_descriptor *iface = usbdev->iface;
181     if ((iface->bInterfaceSubClass != US_SC_SCSI &&
182          iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
183          iface->bInterfaceSubClass != US_SC_ATAPI_8020)
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
190     // Find bulk in and bulk out endpoints.
191     struct usb_pipe *inpipe = NULL, *outpipe = NULL;
192     struct usb_endpoint_descriptor *indesc = usb_find_desc(
193         usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
194     struct usb_endpoint_descriptor *outdesc = usb_find_desc(
195         usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
196     if (!indesc || !outdesc)
197         goto fail;
198     inpipe = usb_alloc_pipe(usbdev, indesc);
199     outpipe = usb_alloc_pipe(usbdev, outdesc);
200     if (!inpipe || !outpipe)
201         goto fail;
202
203     int maxlun = usb_msc_maxlun(usbdev->defpipe);
204     int lun, pipesused = 0;
205     for (lun = 0; lun < maxlun + 1; lun++) {
206         int ret = usb_msc_lun_setup(inpipe, outpipe, usbdev, lun);
207         if (!ret)
208             pipesused = 1;
209     }
210
211     if (!pipesused)
212         goto fail;
213
214     return 0;
215 fail:
216     dprintf(1, "Unable to configure USB MSC device.\n");
217     usb_free_pipe(usbdev, inpipe);
218     usb_free_pipe(usbdev, outpipe);
219     return -1;
220 }