v0.1 board believed to be reading Vbat, Pressure, and X/Y/Z correctly now,
[fw/openalt] / usbmass / usbmass.c
1 /*
2         LPCUSB, an USB device driver for LPC microcontrollers   
3         Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)
4
5         Redistribution and use in source and binary forms, with or without
6         modification, are permitted provided that the following conditions are met:
7
8         1. Redistributions of source code must retain the above copyright
9            notice, this list of conditions and the following disclaimer.
10         2. Redistributions in binary form must reproduce the above copyright
11            notice, this list of conditions and the following disclaimer in the
12            documentation and/or other materials provided with the distribution.
13         3. The name of the author may not be used to endorse or promote products
14            derived from this software without specific prior written permission.
15
16         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 
20         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "FreeRTOS.h"
29
30 #include "../usb/usbapi.h"
31
32 #include "mscdebug.h"
33 #include "mscbot.h"
34 #include "mscblock.h"
35 #include "usbmass.h"
36
37 //
38 //
39 //
40 #define MAX_PACKET_SIZE 64
41 #define LE_WORD(x) ((x)&0xff),((x)>>8)
42
43 //
44 //
45 //
46 static U8 abClassReqData [4];
47
48 static const U8 abDescriptors [] = 
49 {
50   //
51   //  Device descriptor 
52   //
53   0x12,
54   DESC_DEVICE,                  
55   LE_WORD(0x0200),              // bcdUSB
56   0x00,                                       // bDeviceClass
57   0x00,                                       // bDeviceSubClass
58   0x00,                                       // bDeviceProtocol
59   MAX_PACKET_SIZE0,             // bMaxPacketSize
60   LE_WORD(0xFFFF),              // idVendor
61   LE_WORD(0x0003),              // idProduct
62   LE_WORD(0x0100),              // bcdDevice
63   0x01,                                       // iManufacturer
64   0x02,                                       // iProduct
65   0x03,                                       // iSerialNumber
66   0x01,                                       // bNumConfigurations
67
68   //
69   //  Configuration descriptor
70   //
71   0x09,
72   DESC_CONFIGURATION,
73   LE_WORD(32),                  // wTotalLength
74   0x01,                                 // bNumInterfaces
75   0x01,                                 // bConfigurationValue
76   0x00,                                 // iConfiguration
77   0xC0,                                 // bmAttributes
78   0x32,                                 // bMaxPower
79
80   //
81   //  Interface
82   //
83   0x09,
84   DESC_INTERFACE,
85   0x00,                                 // bInterfaceNumber
86   0x00,                                 // bAlternateSetting
87   0x02,                                 // bNumEndPoints
88   0x08,                                 // bInterfaceClass = mass storage
89   0x06,                                 // bInterfaceSubClass = transparent SCSI
90   0x50,                                 // bInterfaceProtocol = BOT
91   0x00,                                 // iInterface
92
93   //
94   //  EP
95   //
96   0x07,
97   DESC_ENDPOINT,
98   MSC_BULK_IN_EP,                             // bEndpointAddress
99   0x02,                                             // bmAttributes = bulk
100   LE_WORD(MAX_PACKET_SIZE), // wMaxPacketSize
101   0x00,                                             // bInterval
102
103   //
104   //  EP
105   //
106   0x07,
107   DESC_ENDPOINT,
108   MSC_BULK_OUT_EP,                    // bEndpointAddress
109   0x02,                                             // bmAttributes = bulk
110   LE_WORD(MAX_PACKET_SIZE), // wMaxPacketSize
111   0x00,                                             // bInterval
112
113   //
114   //  String descriptors
115   //
116   0x04,
117   DESC_STRING,
118   LE_WORD(0x0409),
119
120   0x0E,
121   DESC_STRING,
122   'L', 0, 'P', 0, 'C', 0, 'U', 0, 'S', 0, 'B', 0,
123
124   0x12,
125   DESC_STRING,
126   'P', 0, 'r', 0, 'o', 0, 'd', 0, 'u', 0, 'c', 0, 't', 0, 'X', 0,
127
128   0x1A,
129   DESC_STRING,
130   'D', 0, 'E', 0, 'A', 0, 'D', 0, 'C', 0, '0', 0, 'D', 0, 'E', 0, 'C', 0, 'A', 0, 'F', 0, 'E', 0,
131
132   // terminating zero
133   0
134 };
135
136
137 //
138 //
139 //
140 static BOOL HandleClassRequest (TSetupPacket *pSetup, int *piLen, U8 **ppbData)
141 {
142   if (pSetup->wIndex != 0)
143   {
144     DBG ("Invalid idx %X\n", pSetup->wIndex);
145     return FALSE;
146   }
147
148   if (pSetup->wValue != 0)
149   {
150     DBG ("Invalid val %X\n", pSetup->wValue);
151     return FALSE;
152   }
153
154   switch (pSetup->bRequest) 
155   {
156     //
157     //  Get max LUN (always return no LUNs)
158     //
159     case 0xfe:
160       {
161         *ppbData [0] = 0;
162         *piLen = 1;
163       }
164       break;
165
166     //
167     //  MSC reset
168     //
169     case 0xff:
170       {
171         if (pSetup->wLength > 0)
172           return FALSE;
173
174         mscbotReset ();
175       }
176       break;
177
178     default:
179       {
180         DBG ("Unhandled class\n");
181       }
182       return FALSE;
183   }
184
185   return TRUE;
186 }
187
188 //
189 //
190 //
191 void usbmassInit (void)
192 {
193   portENTER_CRITICAL ();
194         mscblockInit ();
195   usbRegisterHandlers ();
196         usbRegisterDescriptors (abDescriptors);
197         usbRegisterRequestHandler (REQTYPE_TYPE_CLASS, HandleClassRequest, abClassReqData);
198         usbHardwareRegisterEPIntHandler (MSC_BULK_IN_EP, mscbotBulkIn);
199         usbHardwareRegisterEPIntHandler (MSC_BULK_OUT_EP, mscbotBulkOut);
200         usbHardwareNakIntEnable (INACK_BI);
201   usbSetupInterruptHandler ();
202   portEXIT_CRITICAL ();
203
204         usbHardwareConnect (TRUE);
205 }