blob: f9c53b089a313ec4ecda4fcf87ba7ee7538164a6 [file] [log] [blame]
Christina Quast968b9742015-02-25 14:10:12 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*----------------------------------------------------------------------------
31 * Headers
32 *----------------------------------------------------------------------------*/
33
34#include "board.h"
35
Christina Quastf5549502015-02-24 14:27:08 +010036/*------------------------------------------------------------------------------
37 * USB String descriptors
38 *------------------------------------------------------------------------------*/
39
40const unsigned char productStringDescriptor[] = {
41
42 USBStringDescriptor_LENGTH(8),
43 USBGenericDescriptor_STRING,
44 USBStringDescriptor_UNICODE('S'),
45 USBStringDescriptor_UNICODE('I'),
46 USBStringDescriptor_UNICODE('M'),
47 USBStringDescriptor_UNICODE('t'),
48 USBStringDescriptor_UNICODE('r'),
49 USBStringDescriptor_UNICODE('a'),
50 USBStringDescriptor_UNICODE('c'),
51 USBStringDescriptor_UNICODE('e'),
52};
53
54const unsigned char snifferConfigStringDescriptor[] = {
55
56 USBStringDescriptor_LENGTH(15),
57 USBGenericDescriptor_STRING,
58 USBStringDescriptor_UNICODE('S'),
59 USBStringDescriptor_UNICODE('I'),
60 USBStringDescriptor_UNICODE('M'),
61 USBStringDescriptor_UNICODE('t'),
62 USBStringDescriptor_UNICODE('r'),
63 USBStringDescriptor_UNICODE('a'),
64 USBStringDescriptor_UNICODE('c'),
65 USBStringDescriptor_UNICODE('e'),
66 USBStringDescriptor_UNICODE('S'),
67 USBStringDescriptor_UNICODE('n'),
68 USBStringDescriptor_UNICODE('i'),
69 USBStringDescriptor_UNICODE('f'),
70 USBStringDescriptor_UNICODE('f'),
71 USBStringDescriptor_UNICODE('e'),
72 USBStringDescriptor_UNICODE('r'),
73};
74
75const unsigned char CCIDConfigStringDescriptor[] = {
76
77 USBStringDescriptor_LENGTH(12),
78 USBGenericDescriptor_STRING,
79 USBStringDescriptor_UNICODE('S'),
80 USBStringDescriptor_UNICODE('I'),
81 USBStringDescriptor_UNICODE('M'),
82 USBStringDescriptor_UNICODE('t'),
83 USBStringDescriptor_UNICODE('r'),
84 USBStringDescriptor_UNICODE('a'),
85 USBStringDescriptor_UNICODE('c'),
86 USBStringDescriptor_UNICODE('e'),
87 USBStringDescriptor_UNICODE('C'),
88 USBStringDescriptor_UNICODE('C'),
89 USBStringDescriptor_UNICODE('I'),
90 USBStringDescriptor_UNICODE('D'),
91};
92
93const unsigned char phoneConfigStringDescriptor[] = {
94
95 USBStringDescriptor_LENGTH(13),
96 USBGenericDescriptor_STRING,
97 USBStringDescriptor_UNICODE('S'),
98 USBStringDescriptor_UNICODE('I'),
99 USBStringDescriptor_UNICODE('M'),
100 USBStringDescriptor_UNICODE('t'),
101 USBStringDescriptor_UNICODE('r'),
102 USBStringDescriptor_UNICODE('a'),
103 USBStringDescriptor_UNICODE('c'),
104 USBStringDescriptor_UNICODE('e'),
105 USBStringDescriptor_UNICODE('P'),
106 USBStringDescriptor_UNICODE('h'),
107 USBStringDescriptor_UNICODE('o'),
108 USBStringDescriptor_UNICODE('n'),
109 USBStringDescriptor_UNICODE('e'),
110};
111
112
113const unsigned char MITMConfigStringDescriptor[] = {
114
115 USBStringDescriptor_LENGTH(12),
116 USBGenericDescriptor_STRING,
117 USBStringDescriptor_UNICODE('S'),
118 USBStringDescriptor_UNICODE('I'),
119 USBStringDescriptor_UNICODE('M'),
120 USBStringDescriptor_UNICODE('t'),
121 USBStringDescriptor_UNICODE('r'),
122 USBStringDescriptor_UNICODE('a'),
123 USBStringDescriptor_UNICODE('c'),
124 USBStringDescriptor_UNICODE('e'),
125 USBStringDescriptor_UNICODE('M'),
126 USBStringDescriptor_UNICODE('I'),
127 USBStringDescriptor_UNICODE('T'),
128 USBStringDescriptor_UNICODE('M'),
129};
130
131enum strDescNum {
Christina Quast01bbdc32015-02-24 17:38:45 +0100132 NONE = 1, PRODUCT_STRING, SNIFFER_CONF_STR, CCID_CONF_STR, PHONE_CONF_STR, MITM_CONF_STR, STRING_DESC_CNT
Christina Quastf5549502015-02-24 14:27:08 +0100133};
134
135/** List of string descriptors used by the device */
136const unsigned char *stringDescriptors[] = {
137/* FIXME: Is it true that I can't use the string desc #0,
138 * because 0 also stands for "no string desc"?
139 * on the other hand, dmesg output:
140 * "string descriptor 0 malformed (err = -61), defaulting to 0x0409" */
141 0,
142 productStringDescriptor,
143 snifferConfigStringDescriptor,
Christina Quast968b9742015-02-25 14:10:12 +0100144 CCIDConfigStringDescriptor,
Christina Quastf5549502015-02-24 14:27:08 +0100145 phoneConfigStringDescriptor,
146 MITMConfigStringDescriptor
147};
148
Christina Quast01bbdc32015-02-24 17:38:45 +0100149/* Endpoint numbers */
150#define DATAOUT 1
151#define DATAIN 2
Christina Quastf5549502015-02-24 14:27:08 +0100152
153/*------------------------------------------------------------------------------
154 * USB Device descriptors
155 *------------------------------------------------------------------------------*/
156
157typedef struct _SIMTraceDriverConfigurationDescriptorSniffer {
158
159 /** Standard configuration descriptor. */
160 USBConfigurationDescriptor configuration;
161 USBInterfaceDescriptor sniffer;
162 USBEndpointDescriptor sniffer_dataOut;
163 USBEndpointDescriptor sniffer_dataIn;
164
Christina Quast01bbdc32015-02-24 17:38:45 +0100165} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorSniffer;
Christina Quastf5549502015-02-24 14:27:08 +0100166
167const SIMTraceDriverConfigurationDescriptorSniffer configurationDescriptorSniffer = {
168 /* Standard configuration descriptor */
169 {
170 sizeof(USBConfigurationDescriptor),
171 USBGenericDescriptor_CONFIGURATION,
172 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
173 1, /* There is one interface in this configuration */
Christina Quast968b9742015-02-25 14:10:12 +0100174 0, /* This is configuration #0 */
Christina Quastf5549502015-02-24 14:27:08 +0100175 SNIFFER_CONF_STR, /* string descriptor for this configuration */
176 USBD_BMATTRIBUTES,
177 USBConfigurationDescriptor_POWER(100)
178 },
179 /* Communication class interface standard descriptor */
180 {
181 sizeof(USBInterfaceDescriptor),
182 USBGenericDescriptor_INTERFACE,
183 0, /* This is interface #0 */
184 0, /* This is alternate setting #0 for this interface */
185 2, /* This interface uses 2 endpoints */
186 0xff, /* Descriptor Class: Vendor specific */
187 0, /* No subclass */
188 0, /* No l */
189 SNIFFER_CONF_STR /* Third in string descriptor for this interface */
190 },
191 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100192 {
193 sizeof(USBEndpointDescriptor),
194 USBGenericDescriptor_ENDPOINT,
195 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
196 DATAOUT),
197 USBEndpointDescriptor_BULK,
198 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
199 USBEndpointDescriptor_MAXBULKSIZE_FS),
200 0 /* Must be 0 for full-speed bulk endpoints */
201 },
202 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100203 {
204 sizeof(USBEndpointDescriptor),
205 USBGenericDescriptor_ENDPOINT,
206 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
207 DATAIN),
208 USBEndpointDescriptor_BULK,
209 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
210 USBEndpointDescriptor_MAXBULKSIZE_FS),
211 0 /* Must be 0 for full-speed bulk endpoints */
212 }
213};
214
215/* FIXME: CCID descriptor: External C file */
Christina Quast01bbdc32015-02-24 17:38:45 +0100216typedef struct {
217
218 USBConfigurationDescriptor configuration;
219 USBInterfaceDescriptor interface;
220 CCIDDescriptor ccid;
221 USBEndpointDescriptor bulkOut;
222 USBEndpointDescriptor bulkIn;
223 USBEndpointDescriptor interruptIn;
224} __attribute__ ((packed)) CCIDDriverConfigurationDescriptorsCCID;
225
226const CCIDDriverConfigurationDescriptorsCCID configurationDescriptorCCID = { 0 };
227
228/* SIM card emulator */
229typedef struct _SIMTraceDriverConfigurationDescriptorPhone {
230
231 /** Standard configuration descriptor. */
232 USBConfigurationDescriptor configuration;
233 USBInterfaceDescriptor sniffer;
234 USBEndpointDescriptor sniffer_dataOut;
235 USBEndpointDescriptor sniffer_dataIn;
236
237} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorPhone;
238
239const SIMTraceDriverConfigurationDescriptorPhone configurationDescriptorPhone = {
240 /* Standard configuration descriptor */
241 {
242 sizeof(USBConfigurationDescriptor),
243 USBGenericDescriptor_CONFIGURATION,
244 sizeof(SIMTraceDriverConfigurationDescriptorSniffer),
245 1, /* There is one interface in this configuration */
Christina Quast968b9742015-02-25 14:10:12 +0100246 2, /* This is configuration #2 */
Christina Quast01bbdc32015-02-24 17:38:45 +0100247 PHONE_CONF_STR, /* string descriptor for this configuration */
248 USBD_BMATTRIBUTES,
249 USBConfigurationDescriptor_POWER(100)
250 },
251 /* Communication class interface standard descriptor */
252 {
253 sizeof(USBInterfaceDescriptor),
254 USBGenericDescriptor_INTERFACE,
255 0, /* This is interface #0 */
256 0, /* This is alternate setting #0 for this interface */
257 2, /* This interface uses 2 endpoints */
258 0xff, /* Descriptor Class: Vendor specific */
259 0, /* No subclass */
260 0, /* No l */
261 PHONE_CONF_STR /* Third in string descriptor for this interface */
262 },
263 /* Bulk-OUT endpoint standard descriptor */
264 {
265 sizeof(USBEndpointDescriptor),
266 USBGenericDescriptor_ENDPOINT,
267 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
268 DATAOUT),
269 USBEndpointDescriptor_BULK,
270 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
271 USBEndpointDescriptor_MAXBULKSIZE_FS),
272 0 /* Must be 0 for full-speed bulk endpoints */
273 },
274 /* Bulk-IN endpoint descriptor */
275 {
276 sizeof(USBEndpointDescriptor),
277 USBGenericDescriptor_ENDPOINT,
278 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
279 DATAIN),
280 USBEndpointDescriptor_BULK,
281 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
282 USBEndpointDescriptor_MAXBULKSIZE_FS),
283 0 /* Must be 0 for full-speed bulk endpoints */
284 }
285};
286
Christina Quastf5549502015-02-24 14:27:08 +0100287
288typedef struct _SIMTraceDriverConfigurationDescriptorMITM {
289
290 /** Standard configuration descriptor. */
291 USBConfigurationDescriptor configuration;
292 USBInterfaceDescriptor simcard;
293 USBEndpointDescriptor simcard_dataOut;
294 USBEndpointDescriptor simcard_dataIn;
295 USBInterfaceDescriptor phone;
296 USBEndpointDescriptor phone_dataOut;
297 USBEndpointDescriptor phone_dataIn;
298
Christina Quast01bbdc32015-02-24 17:38:45 +0100299} __attribute__ ((packed)) SIMTraceDriverConfigurationDescriptorMITM;
Christina Quastf5549502015-02-24 14:27:08 +0100300
Christina Quast968b9742015-02-25 14:10:12 +0100301const SIMTraceDriverConfigurationDescriptorMITM configurationDescriptorMITM = {
Christina Quastf5549502015-02-24 14:27:08 +0100302 /* Standard configuration descriptor */
303 {
304 sizeof(USBConfigurationDescriptor),
305 USBGenericDescriptor_CONFIGURATION,
Christina Quast968b9742015-02-25 14:10:12 +0100306 sizeof(SIMTraceDriverConfigurationDescriptorMITM),
Christina Quastf5549502015-02-24 14:27:08 +0100307 2, /* There are two interfaces in this configuration */
308 4, /* This is configuration #4 */
309 MITM_CONF_STR, /* string descriptor for this configuration */
310 USBD_BMATTRIBUTES,
311 USBConfigurationDescriptor_POWER(100)
312 },
313 /* Communication class interface standard descriptor */
314 {
315 sizeof(USBInterfaceDescriptor),
316 USBGenericDescriptor_INTERFACE,
317 0, /* This is interface #0 */
318 0, /* This is alternate setting #0 for this interface */
319 2, /* This interface uses 2 endpoints */
320 //CDCCommunicationInterfaceDescriptor_CLASS,
321 0xff,
322// CDCCommunicationInterfaceDescriptor_ABSTRACTCONTROLMODEL,
323 0,
324// CDCCommunicationInterfaceDescriptor_NOPROTOCOL,
325 0,
326 MITM_CONF_STR /* string descriptor for this interface */
327 },
328 /* Bulk-OUT endpoint standard descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100329 {
330 sizeof(USBEndpointDescriptor),
331 USBGenericDescriptor_ENDPOINT,
332 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
333 DATAOUT),
334 USBEndpointDescriptor_BULK,
335 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
336 USBEndpointDescriptor_MAXBULKSIZE_FS),
337 0 /* Must be 0 for full-speed bulk endpoints */
338 },
339 /* Bulk-IN endpoint descriptor */
Christina Quast01bbdc32015-02-24 17:38:45 +0100340 {
341 sizeof(USBEndpointDescriptor),
342 USBGenericDescriptor_ENDPOINT,
343 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
344 DATAIN),
345 USBEndpointDescriptor_BULK,
346 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
347 USBEndpointDescriptor_MAXBULKSIZE_FS),
348 0 /* Must be 0 for full-speed bulk endpoints */
Christina Quast968b9742015-02-25 14:10:12 +0100349 },
Christina Quast01bbdc32015-02-24 17:38:45 +0100350 /* Communication class interface standard descriptor */
351 {
352 sizeof(USBInterfaceDescriptor),
353 USBGenericDescriptor_INTERFACE,
354 1, /* This is interface #1 */
355 0, /* This is alternate setting #0 for this interface */
356 2, /* This interface uses 2 endpoints */
357 0xff,
358 0,
359 0,
360 0, /* FIXME: string descriptor for this interface */
361 },
362 /* Bulk-OUT endpoint standard descriptor */
363 {
364 sizeof(USBEndpointDescriptor),
365 USBGenericDescriptor_ENDPOINT,
366 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_OUT,
367 DATAOUT),
368 USBEndpointDescriptor_BULK,
369 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAOUT),
370 USBEndpointDescriptor_MAXBULKSIZE_FS),
371 0 /* Must be 0 for full-speed bulk endpoints */
372 },
373 /* Bulk-IN endpoint descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100374 {
375 sizeof(USBEndpointDescriptor),
376 USBGenericDescriptor_ENDPOINT,
377 USBEndpointDescriptor_ADDRESS(USBEndpointDescriptor_IN,
378 DATAIN),
379 USBEndpointDescriptor_BULK,
380 MIN(BOARD_USB_ENDPOINTS_MAXPACKETSIZE(DATAIN),
381 USBEndpointDescriptor_MAXBULKSIZE_FS),
382 0 /* Must be 0 for full-speed bulk endpoints */
383 }
384};
385
386
387/** Standard USB device descriptor for the CDC serial driver */
388const USBDeviceDescriptor deviceDescriptor = {
389
390 sizeof(USBDeviceDescriptor),
391 USBGenericDescriptor_DEVICE,
392 USBDeviceDescriptor_USB2_00,
393 0xff,
394// CDCDeviceDescriptor_CLASS,
395 0xff,
396// CDCDeviceDescriptor_SUBCLASS,
397 0xff,
398// CDCDeviceDescriptor_PROTOCOL,
399 BOARD_USB_ENDPOINTS_MAXPACKETSIZE(0),
400 ATMEL_VENDOR_ID,
401 SIMTRACE_PRODUCT_ID,
402 1, /* Release number */
403 0, /* No string descriptor for manufacturer */
404 PRODUCT_STRING, /* Index of product string descriptor */
405 0, /* No string descriptor for serial number */
406 4 /* Device has 4 possible configurations */
407};
408
Christina Quast968b9742015-02-25 14:10:12 +0100409const USBConfigurationDescriptor *configurationDescriptorsArr[] = {
Christina Quast01bbdc32015-02-24 17:38:45 +0100410 &configurationDescriptorSniffer,
411 &configurationDescriptorCCID,
412 &configurationDescriptorPhone,
413 &configurationDescriptorMITM,
414};
415
416
Christina Quastf5549502015-02-24 14:27:08 +0100417/* AT91SAM3S does only support full speed, but not high speed USB */
418const USBDDriverDescriptors driverDescriptors = {
419 &deviceDescriptor,
Christina Quast01bbdc32015-02-24 17:38:45 +0100420 (USBConfigurationDescriptor **) &(configurationDescriptorsArr), /* first full-speed configuration descriptor */
Christina Quastf5549502015-02-24 14:27:08 +0100421 0, /* No full-speed device qualifier descriptor */
422 0, /* No full-speed other speed configuration */
423 0, /* No high-speed device descriptor */
424 0, /* No high-speed configuration descriptor */
425 0, /* No high-speed device qualifier descriptor */
426 0, /* No high-speed other speed configuration descriptor */
427 stringDescriptors,
428 STRING_DESC_CNT-1 /* cnt string descriptors in list */
429};
430
Christina Quast968b9742015-02-25 14:10:12 +0100431/*----------------------------------------------------------------------------
432 * Functions
433 *----------------------------------------------------------------------------*/
434
435/**
436 * \brief Configure 48MHz Clock for USB
437 */
438static void _ConfigureUsbClock(void)
439{
440 /* Enable PLLB for USB */
441// FIXME: are these the dividers I actually need?
442// FIXME: I could just use PLLA, since it has a frequ of 48Mhz anyways?
443 PMC->CKGR_PLLBR = CKGR_PLLBR_DIVB(5)
444 | CKGR_PLLBR_MULB(0xc) /* MULT+1=0xd*/
445 | CKGR_PLLBR_PLLBCOUNT_Msk;
446 while((PMC->PMC_SR & PMC_SR_LOCKB) == 0);
447 /* USB Clock uses PLLB */
448 PMC->PMC_USB = PMC_USB_USBDIV(0) /* /1 (no divider) */
449 | PMC_USB_USBS; /* PLLB */
450}
451
452
453void SIMtrace_USB_Initialize( void )
454{
455 _ConfigureUsbClock();
456 // Get std USB driver
457 USBDDriver *pUsbd = USBD_GetDriver();
458
459 TRACE_DEBUG(".");
460
461 // Initialize standard USB driver
462 USBDDriver_Initialize(pUsbd,
463 &driverDescriptors,
464// FIXME: 2 interface settings supported in MITM mode
465 0); // Multiple interface settings not supported
466
467 USBD_Init();
468
469 USBD_Connect();
470
471 NVIC_EnableIRQ( UDP_IRQn );
472}