2 * Copyright (c) 2007, Benedikt Sauter <sauter@ixbat.de>
5 * Short descripton of file:
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of the FH Augsburg nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "../../core/core.h"
39 #include "../../core/usb.h"
40 #include "../../usbspec/usb11spec.h"
41 #include "../../../malloc.h"
48 void usb_storage_probe();
49 void usb_storage_check();
51 usb_device * massstorage[MAX_DEVICES];
52 u16 sectorsize[MAX_DEVICES];
53 u8 massstorage_in_use;
55 usb_driver storage = {
57 .probe = usb_storage_probe,
58 .check = usb_storage_check,
62 void usb_storage_init()
64 massstorage_in_use = 0;
65 usb_register_driver(&storage);
69 void usb_storage_probe()
71 // schaue ob aktuell enumeriertes geraet ein storage ist
73 core.stdout("Probe: Storage\r\n");
76 /* read interface descriptor for class code */
80 element * iterator = core.devices->head;
82 while(iterator != NULL) {
83 dev = (usb_device*)iterator->data;
85 /* get interface descriptor */
86 usb_control_msg(dev, 0x80, GET_DESCRIPTOR,2, 0, 32,buf, 8, 0);
88 if(buf[14]==MASS_STORAGE_CLASSCODE){
89 massstorage[massstorage_in_use] = dev;
92 core.stdout("Storage: Found Storage Device\r\n");
95 /* here is only my lib driver test */
97 usb_storage_inquiry(0);
98 usb_storage_read_capacity(0);
100 //char * buf = (char*)malloc(512);
103 //usb_storage_read_sector(0,1,buf);
105 /* end of driver test */
108 iterator=iterator->next;
113 void usb_storage_check()
115 // wird periodisch augerufen
116 // da ein mass storage aber keinen interrupt oder isochronen endpunkt
117 // hat passiert hier nichts
123 * open connection to an storage device
125 u8 usb_storage_open(u8 device)
127 /* set configuration */
129 usb_control_msg(massstorage[device], 0x00,SET_CONFIGURATION,0x0100, 0, 0,tmp, 8, 0);
133 usb_control_msg(massstorage[device], 0xA1,0xFE,0, 0, 1,tmp, 8, 0);
135 /* wait until the stick is complete ready */
138 //FIXME and all other return values!!!
142 u8 usb_storage_inquiry(u8 device)
144 /* send cwb "usbc" */
146 usb_storage_cbw *cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
147 cbw->dCBWSignature= 0x43425355;
148 cbw->dCBWTag=0x826A6008;
149 cbw->dCBWDataTransferLength=0x00000024;
152 cbw->bCBWCBLength=0x01;
158 cbw->CBWCB[0]=0x12; // 0x12 = INQUIRY
160 usb_bulk_write(massstorage[device], 2, (char*)cbw, 31, 0);
161 usb_bulk_read(massstorage[device], 1, (char*)cbw, 36, 0);
162 usb_bulk_read(massstorage[device], 1, (char*)cbw, 13, 0);
170 u8 usb_storage_read_capacity(u8 device)
172 /* send cwb "usbc" */
176 usb_storage_cbw * cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
178 usb_control_msg(massstorage[device], 0x02,1,0, 0x8100, 0,tmp, 8, 0); //
180 cbw->dCBWSignature= 0x43425355;
181 cbw->dCBWTag=0x826A6008;
182 cbw->dCBWDataTransferLength=0x00000008;
185 cbw->bCBWCBLength=0x0A;
190 cbw->CBWCB[0]=0x25; // 0x12 = INQUIRY
192 usb_bulk_write(massstorage[device], 2, (char*)cbw, 31, 0);
193 usb_bulk_read(massstorage[device], 1, (char*)cbw, 8, 0);
194 usb_bulk_read(massstorage[device], 1, (char*)cbw, 13, 0);
202 u8 usb_storage_read_sector(u8 device, u32 sector, char * buf)
204 /* send cwb "usbc" */
205 char tmpbuf[] = {0x55,0x53,0x42,0x43,0x08,
206 0xE0,0x63,0x82,0x00,0x02,
207 0x00,0x00,0x80,0x00,0x0A,
208 0x28,0x00,0x00,0x00,0x00,
209 0x00,0x00,0x00,0x01,0x00,
210 0x00,0x00,0x00,0x00,0x00,
212 0x00,0x00,0x00,0x00,0x00};
214 usb_bulk_write(massstorage[device], 2, tmpbuf, 31, 0);
215 //usb_bulk_read(massstorage[device], 1, buf,64,0);
216 //usb_bulk_read(massstorage[device], 1, buf, 13, 0);
223 u8 usb_storage_write_sector(u8 device, u32 sector, char * buf)