e0e84fd2d83ee6ece484565217f952e7c000e901
[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 u8 usb_storage_inquiry(u8 device)
123 {
124         /* send cwb "usbc" */
125         
126         usb_storage_cbw *cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
127         cbw->dCBWSignature= 0x43425355;
128         cbw->dCBWTag=0x826A6008;
129         cbw->dCBWDataTransferLength=0x00000024;
130         cbw->bCWDFlags=0x80;
131         cbw->bCBWLun=0x00;
132         cbw->bCBWCBLength=0x01;
133
134         u8 i;
135         for(i=0;i<16;i++)
136                 cbw->CBWCB[i]=0x00;
137
138         cbw->CBWCB[0]=0x12; // 0x12 = INQUIRY
139
140         usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
141         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 36, 0); 
142         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 
143
144         free(cbw);
145
146         return 0;
147 }
148
149
150 u8 usb_storage_read_capacity(u8 device)
151 {
152         /* send cwb "usbc" */
153
154         u8 tmp[8];
155         u8 i;
156         usb_storage_cbw  * cbw = (usb_storage_cbw*)malloc(sizeof(usb_storage_cbw));
157
158         usb_control_msg(massstorage[device], 0x02,1,0, 0x8100, 0,tmp, 0);
159
160         cbw->dCBWSignature= 0x43425355;
161         cbw->dCBWTag=0x826A6008;
162         cbw->dCBWDataTransferLength=0x00000008;
163         cbw->bCWDFlags=0x80;
164         cbw->bCBWLun=0x00;
165         cbw->bCBWCBLength=0x0A;
166
167         for(i=0;i<16;i++)
168                 cbw->CBWCB[i]=0x00;
169
170         cbw->CBWCB[0]=0x25; // 0x12 = INQUIRY
171
172         usb_bulk_write(massstorage[device], 2, (u8*)cbw, 31, 0); 
173         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 8, 0); 
174         usb_bulk_read(massstorage[device], 1, (u8*)cbw, 13, 0); 
175
176         free(cbw);
177
178         return 0;
179 }
180
181
182 u8 usb_storage_read_sector(u8 device, u32 sector, char * buf)
183 {
184         /* send cwb "usbc" */
185         u8 tmpbuf[] = {0x55,0x53,0x42,0x43,0x08,
186                 0xE0,0x63,0x82,0x00,0x02,
187                 0x00,0x00,0x80,0x00,0x0A,
188                 0x28,0x00,0x00,0x00,0x00,
189                 0x00,0x00,0x00,0x01,0x00,
190                 0x00,0x00,0x00,0x00,0x00,
191                 0x00,
192                 0x00,0x00,0x00,0x00,0x00};
193
194         usb_bulk_write(massstorage[device], 2, tmpbuf, 31, 0);
195         //usb_bulk_read(massstorage[device], 1, buf,64,0);
196         //usb_bulk_read(massstorage[device], 1, buf, 13, 0); 
197
198         
199         return 0;
200 }
201
202
203 u8 usb_storage_write_sector(u8 device, u32 sector, char * buf)
204 {
205
206         return 0;
207 }