refactoring ugly typedefs...
[ppcskel.git] / usb / drivers / class / storage.c
1 /*
2  * Copyright (c) 2007, Benedikt Sauter <sauter@ixbat.de>
3  * All rights reserved.
4  *
5  * Short descripton of file:
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without 
9  * modification, are permitted provided that the following conditions 
10  * are met:
11  *
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.
21  *
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.
33  */
34
35 //#include <wait.h>
36 //#include <stdlib.h>
37
38 #include "../../core/core.h"
39 #include "../../core/usb.h"
40 #include "../../usbspec/usb11spec.h"
41 #include "../../../malloc.h"
42
43 #include "storage.h"
44
45
46 #define MAX_DEVICES 2
47
48 void usb_storage_probe();
49 void usb_storage_check();
50
51 struct usb_device *massstorage[MAX_DEVICES];
52 u16 sectorsize[MAX_DEVICES];
53 u8 massstorage_in_use;
54
55 struct usb_driver storage = {
56         .name     = "storage",
57         .probe  = usb_storage_probe,
58         .check  = usb_storage_check,
59         .data     = NULL
60 };
61
62 void usb_storage_init()
63 {
64         massstorage_in_use = 0;
65         usb_register_driver(&storage);  
66 }
67
68
69 void usb_storage_probe()
70 {
71         // schaue ob aktuell enumeriertes geraet ein storage ist
72         #if DEBUG
73         core.stdout("Probe: Storage\r\n");
74         #endif
75  
76         /* read interface descriptor for class code */
77         u8 buf[32];
78         
79         struct usb_device* dev;
80         struct element * iterator = core.devices->head;
81         
82         while(iterator != NULL) {
83                 dev = (struct usb_device*)iterator->data;
84
85                 /* get interface descriptor */
86                 usb_control_msg(dev, 0x80, GET_DESCRIPTOR,2, 0, 32, buf, 0);
87
88                 if(buf[14]==MASS_STORAGE_CLASSCODE){
89                         massstorage[massstorage_in_use] = dev;
90                         massstorage_in_use++;
91                         #if DEBUG
92                         core.stdout("Storage: Found Storage Device\r\n");
93                         #endif 
94
95                         /* here is only my lib driver test */
96                         usb_storage_open(0);
97                         usb_storage_inquiry(0);
98                         usb_storage_read_capacity(0);
99
100                         //char * buf = (char*)malloc(512);
101                         //free(buf);
102                         //char buf[512];
103                         //usb_storage_read_sector(0,1,buf);
104
105                         /* end of driver test */
106                 }
107
108                 iterator=iterator->next;
109         }
110 }
111
112
113 void usb_storage_check()
114 {
115         // wird periodisch augerufen
116         // da ein mass storage aber keinen interrupt oder isochronen endpunkt
117         // hat passiert hier nichts
118 }
119
120
121
122 /**
123  * open connection to an storage device
124  */
125 u8 usb_storage_open(u8 device)
126 {
127         /* set configuration */
128         u8 tmp[8];
129         usb_control_msg(massstorage[device], 0x00,SET_CONFIGURATION,0x0100, 0, 0, tmp, 0);
130
131
132         /* class request */
133         usb_control_msg(massstorage[device], 0xA1,0xFE,0, 0, 1,tmp, 0);
134
135         /* wait until the stick is complete ready */
136         wait_ms(10);
137         
138         //FIXME and all other return values!!!
139         return 1;
140 }
141
142 u8 usb_storage_inquiry(u8 device)
143 {
144         /* send cwb "usbc" */
145         
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;
150         cbw->bCWDFlags=0x80;
151         cbw->bCBWLun=0x00;
152         cbw->bCBWCBLength=0x01;
153
154         u8 i;
155         for(i=0;i<16;i++)
156                 cbw->CBWCB[i]=0x00;
157
158         cbw->CBWCB[0]=0x12; // 0x12 = INQUIRY
159
160         usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
161         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 36, 0); 
162         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 
163
164         free(cbw);
165
166         return 0;
167 }
168
169
170 u8 usb_storage_read_capacity(u8 device)
171 {
172         /* send cwb "usbc" */
173
174         u8 tmp[8];
175         u8 i;
176         usb_storage_cbw  * cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
177
178         usb_control_msg(massstorage[device], 0x02,1,0, 0x8100, 0,tmp, 0);
179
180         cbw->dCBWSignature= 0x43425355;
181         cbw->dCBWTag=0x826A6008;
182         cbw->dCBWDataTransferLength=0x00000008;
183         cbw->bCWDFlags=0x80;
184         cbw->bCBWLun=0x00;
185         cbw->bCBWCBLength=0x0A;
186
187         for(i=0;i<16;i++)
188                 cbw->CBWCB[i]=0x00;
189
190         cbw->CBWCB[0]=0x25; // 0x12 = INQUIRY
191
192         usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
193         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 8, 0); 
194         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 
195
196         free(cbw);
197
198         return 0;
199 }
200
201
202 u8 usb_storage_read_sector(u8 device, u32 sector, char * buf)
203 {
204         /* send cwb "usbc" */
205         u8 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,
211                 0x00,
212                 0x00,0x00,0x00,0x00,0x00};
213
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); 
217
218         
219         return 0;
220 }
221
222
223 u8 usb_storage_write_sector(u8 device, u32 sector, char * buf)
224 {
225
226         return 0;
227 }