These changes are the raw update to qemu-2.6.
[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_process_op(struct disk_op_s *op)
67 {
68     if (!CONFIG_USB_MSC)
69         return 0;
70
71     dprintf(16, "usb_cmd_data id=%p write=%d count=%d buf=%p\n"
72             , op->drive_gf, 0, op->count, 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     struct cbw_s cbw;
78     memset(&cbw, 0, sizeof(cbw));
79     int blocksize = scsi_fill_cmd(op, cbw.CBWCB, USB_CDB_SIZE);
80     if (blocksize < 0)
81         return default_process_op(op);
82     u32 bytes = blocksize * op->count;
83     cbw.dCBWSignature = CBW_SIGNATURE;
84     cbw.dCBWTag = 999; // XXX
85     cbw.dCBWDataTransferLength = bytes;
86     cbw.bmCBWFlags = scsi_is_read(op) ? USB_DIR_IN : USB_DIR_OUT;
87     cbw.bCBWLUN = GET_GLOBALFLAT(udrive_gf->lun);
88     cbw.bCBWCBLength = USB_CDB_SIZE;
89
90     // Transfer cbw to device.
91     int ret = usb_msc_send(udrive_gf, USB_DIR_OUT
92                            , MAKE_FLATPTR(GET_SEG(SS), &cbw), sizeof(cbw));
93     if (ret)
94         goto fail;
95
96     // Transfer data to/from device.
97     if (bytes) {
98         ret = usb_msc_send(udrive_gf, cbw.bmCBWFlags, op->buf_fl, bytes);
99         if (ret)
100             goto fail;
101     }
102
103     // Transfer csw info.
104     struct csw_s csw;
105     ret = usb_msc_send(udrive_gf, USB_DIR_IN
106                        , MAKE_FLATPTR(GET_SEG(SS), &csw), sizeof(csw));
107     if (ret)
108         goto fail;
109
110     if (!csw.bCSWStatus)
111         return DISK_RET_SUCCESS;
112     if (csw.bCSWStatus == 2)
113         goto fail;
114
115     if (blocksize)
116         op->count -= csw.dCSWDataResidue / blocksize;
117     return DISK_RET_EBADTRACK;
118
119 fail:
120     // XXX - reset connection
121     dprintf(1, "USB transmission failed\n");
122     return DISK_RET_EBADTRACK;
123 }
124
125 static int
126 usb_msc_maxlun(struct usb_pipe *pipe)
127 {
128     struct usb_ctrlrequest req;
129     req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
130     req.bRequest = 0xfe;
131     req.wValue = 0;
132     req.wIndex = 0;
133     req.wLength = 1;
134     unsigned char maxlun;
135     int ret = usb_send_default_control(pipe, &req, &maxlun);
136     if (ret)
137         return 0;
138     return maxlun;
139 }
140
141 static int
142 usb_msc_lun_setup(struct usb_pipe *inpipe, struct usb_pipe *outpipe,
143                   struct usbdevice_s *usbdev, int lun)
144 {
145     // Allocate drive structure.
146     struct usbdrive_s *drive = malloc_fseg(sizeof(*drive));
147     if (!drive) {
148         warn_noalloc();
149         return -1;
150     }
151     memset(drive, 0, sizeof(*drive));
152     if (usb_32bit_pipe(inpipe))
153         drive->drive.type = DTYPE_USB_32;
154     else
155         drive->drive.type = DTYPE_USB;
156     drive->bulkin = inpipe;
157     drive->bulkout = outpipe;
158     drive->lun = lun;
159
160     int prio = bootprio_find_usb(usbdev, lun);
161     int ret = scsi_drive_setup(&drive->drive, "USB MSC", prio);
162     if (ret) {
163         dprintf(1, "Unable to configure USB MSC drive.\n");
164         free(drive);
165         return -1;
166     }
167     return 0;
168 }
169
170 /****************************************************************
171  * Setup
172  ****************************************************************/
173
174 // Configure a usb msc device.
175 int
176 usb_msc_setup(struct usbdevice_s *usbdev)
177 {
178     if (!CONFIG_USB_MSC)
179         return -1;
180
181     // Verify right kind of device
182     struct usb_interface_descriptor *iface = usbdev->iface;
183     if ((iface->bInterfaceSubClass != US_SC_SCSI &&
184          iface->bInterfaceSubClass != US_SC_ATAPI_8070 &&
185          iface->bInterfaceSubClass != US_SC_ATAPI_8020)
186         || iface->bInterfaceProtocol != US_PR_BULK) {
187         dprintf(1, "Unsupported MSC USB device (subclass=%02x proto=%02x)\n"
188                 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
189         return -1;
190     }
191
192     // Find bulk in and bulk out endpoints.
193     struct usb_pipe *inpipe = NULL, *outpipe = NULL;
194     struct usb_endpoint_descriptor *indesc = usb_find_desc(
195         usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
196     struct usb_endpoint_descriptor *outdesc = usb_find_desc(
197         usbdev, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
198     if (!indesc || !outdesc)
199         goto fail;
200     inpipe = usb_alloc_pipe(usbdev, indesc);
201     outpipe = usb_alloc_pipe(usbdev, outdesc);
202     if (!inpipe || !outpipe)
203         goto fail;
204
205     int maxlun = usb_msc_maxlun(usbdev->defpipe);
206     int lun, pipesused = 0;
207     for (lun = 0; lun < maxlun + 1; lun++) {
208         int ret = usb_msc_lun_setup(inpipe, outpipe, usbdev, lun);
209         if (!ret)
210             pipesused = 1;
211     }
212
213     if (!pipesused)
214         goto fail;
215
216     return 0;
217 fail:
218     dprintf(1, "Unable to configure USB MSC device.\n");
219     usb_free_pipe(usbdev, inpipe);
220     usb_free_pipe(usbdev, outpipe);
221     return -1;
222 }