aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/fx2/common/usb_common.c
blob: 3b0547b2f7c9c7bc0e3963fdc001b32d189ccac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* -*- c++ -*- */
/*
 * Copyright 2003 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include "usb_common.h"
#include "fx2regs.h"
#include "syncdelay.h"
#include "fx2utils.h"
#include "isr.h"
#include "usb_descriptors.h"
#include "usb_requests.h"

extern xdata char str0[];
extern xdata char str1[];
extern xdata char str2[];
extern xdata char str3[];
extern xdata char str4[];
extern xdata char str5[];


#define	bRequestType	SETUPDAT[0]
#define	bRequest	SETUPDAT[1]
#define	wValueL		SETUPDAT[2]
#define	wValueH		SETUPDAT[3]
#define	wIndexL		SETUPDAT[4]
#define	wIndexH		SETUPDAT[5]
#define	wLengthL	SETUPDAT[6]
#define	wLengthH	SETUPDAT[7]

#define MSB(x)	(((unsigned short) x) >> 8)
#define LSB(x)	(((unsigned short) x) & 0xff)

volatile bit _usb_got_SUDAV;

unsigned char	_usb_config = 0;
unsigned char	_usb_alt_setting = 0;	// FIXME really 1/interface

xdata unsigned char *current_device_descr;
xdata unsigned char *current_devqual_descr;
xdata unsigned char *current_config_descr;
xdata unsigned char *other_config_descr;

static void
setup_descriptors (void)
{
  if (USBCS & bmHSM){		// high speed mode
    current_device_descr  = high_speed_device_descr;
    current_devqual_descr = high_speed_devqual_descr;
    current_config_descr  = high_speed_config_descr;
    other_config_descr    = full_speed_config_descr;
  }
  else {
    current_device_descr  = full_speed_device_descr;
    current_devqual_descr = full_speed_devqual_descr;
    current_config_descr  = full_speed_config_descr;
    other_config_descr    = high_speed_config_descr;
  }

  // whack the type fields
  // FIXME, may not be required.
  // current_config_descr[1] = DT_CONFIG;
  // other_config_descr[1]   = DT_OTHER_SPEED;
}

static void
isr_SUDAV (void) interrupt
{
  clear_usb_irq ();
  _usb_got_SUDAV = 1;
}

static void
isr_USBRESET (void) interrupt
{
  clear_usb_irq ();
  setup_descriptors ();
}

static void
isr_HIGHSPEED (void) interrupt
{
  clear_usb_irq ();
  setup_descriptors ();
}

void
usb_install_handlers (void)
{
  setup_descriptors ();	    // ensure that they're set before use

  hook_uv (UV_SUDAV,     (unsigned short) isr_SUDAV);
  hook_uv (UV_USBRESET,  (unsigned short) isr_USBRESET);
  hook_uv (UV_HIGHSPEED, (unsigned short) isr_HIGHSPEED);

  USBIE = bmSUDAV | bmURES | bmHSGRANT;
}

// On the FX2 the only plausible endpoints are 0, 1, 2, 4, 6, 8
// This doesn't check to see that they're enabled

unsigned char
plausible_endpoint (unsigned char ep)
{
  ep &= ~0x80;	// ignore direction bit

  if (ep > 8)
    return 0;

  if (ep == 1)
    return 1;

  return (ep & 0x1) == 0;	// must be even
}

// return pointer to control and status register for endpoint.
// only called with plausible_endpoints

xdata volatile unsigned char *
epcs (unsigned char ep)
{
  if (ep == 0x01)		// ep1 has different in and out CS regs
    return EP1OUTCS;

  if (ep == 0x81)
    return EP1INCS;

  ep &= ~0x80;			// ignore direction bit

  if (ep == 0x00)		// ep0
    return EP0CS;

  return EP2CS + (ep >> 1);	// 2, 4, 6, 8 are consecutive
}

void
usb_handle_setup_packet (void)
{
  _usb_got_SUDAV = 0;

  // handle the standard requests...

  switch (bRequestType & bmRT_TYPE_MASK){

  case bmRT_TYPE_CLASS:
  case bmRT_TYPE_RESERVED:
    fx2_stall_ep0 ();		// we don't handle these.  indicate error
    break;
    
  case bmRT_TYPE_VENDOR:
    // call the application code.
    // If it handles the command it returns non-zero

    if (!app_vendor_cmd ())	
      fx2_stall_ep0 ();
    break;

  case bmRT_TYPE_STD:
    // these are the standard requests...

    if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_IN){

      ////////////////////////////////////
      //    handle the IN requests
      ////////////////////////////////////

      switch (bRequest){

      case RQ_GET_CONFIG:
	EP0BUF[0] = _usb_config;	// FIXME app should handle
	EP0BCH = 0;
	EP0BCL = 1;
	break;
	
      // --------------------------------

      case RQ_GET_INTERFACE:
	EP0BUF[0] = _usb_alt_setting;	// FIXME app should handle
	EP0BCH = 0;
	EP0BCL = 1;
	break;

      // --------------------------------

      case RQ_GET_DESCR:
	switch (wValueH){

	case DT_DEVICE:
	  SUDPTRH = MSB (current_device_descr);
	  SUDPTRL = LSB (current_device_descr);
	  break;
	  
	case DT_DEVQUAL:
	  SUDPTRH = MSB (current_devqual_descr);
	  SUDPTRL = LSB (current_devqual_descr);
	  break;

	case DT_CONFIG:
	  if (0 && wValueL != 1)	// FIXME only a single configuration
	    fx2_stall_ep0 ();
	  else {
	    SUDPTRH = MSB (current_config_descr);
	    SUDPTRL = LSB (current_config_descr);
	  }
	  break;

	case DT_OTHER_SPEED:
	  if (0 && wValueL != 1)	// FIXME only a single configuration
	    fx2_stall_ep0 ();
	  else {
	    SUDPTRH = MSB (other_config_descr);
	    SUDPTRL = LSB (other_config_descr);
	  }
	  break;

	case DT_STRING:
	  if (wValueL >= nstring_descriptors)
	    fx2_stall_ep0 ();
	  else {
	    xdata char *p = string_descriptors[wValueL];
	    SUDPTRH = MSB (p);
	    SUDPTRL = LSB (p);
	  }
	  break;

	default:
	  fx2_stall_ep0 ();	// invalid request
	  break;
	}
	break;
	
      // --------------------------------

      case RQ_GET_STATUS:
	switch (bRequestType & bmRT_RECIP_MASK){
	case bmRT_RECIP_DEVICE:
	  EP0BUF[0] = bmGSDA_SELF_POWERED;	// FIXME app should handle
	  EP0BUF[1] = 0;
	  EP0BCH = 0;
	  EP0BCL = 2;
	  break;

	case bmRT_RECIP_INTERFACE:
	  EP0BUF[0] = 0;
	  EP0BUF[1] = 0;
	  EP0BCH = 0;
	  EP0BCL = 2;
	  break;

	case bmRT_RECIP_ENDPOINT:
	  if (plausible_endpoint (wIndexL)){
	    EP0BUF[0] = *epcs (wIndexL) & bmEPSTALL;
	    EP0BUF[1] = 0;
	    EP0BCH = 0;
	    EP0BCL = 2;
	  }
	  else
	    fx2_stall_ep0 ();
	  break;

	default:
	  fx2_stall_ep0 ();
	  break;
	}
	break;

      // --------------------------------

      case RQ_SYNCH_FRAME:	// not implemented
      default:
	fx2_stall_ep0 ();
	break;
      }
    }

    else {

      ////////////////////////////////////
      //    handle the OUT requests
      ////////////////////////////////////

      switch (bRequest){

      case RQ_SET_CONFIG:
	_usb_config = wValueL;		// FIXME app should handle
	break;

      case RQ_SET_INTERFACE:
	_usb_alt_setting = wValueL;	// FIXME app should handle
	break;

      // --------------------------------

      case RQ_CLEAR_FEATURE:
	switch (bRequestType & bmRT_RECIP_MASK){

	case bmRT_RECIP_DEVICE:
	  switch (wValueL){
	  case FS_DEV_REMOTE_WAKEUP:
	  default:
	    fx2_stall_ep0 ();
	  }
	  break;

	case bmRT_RECIP_ENDPOINT:
	  if (wValueL == FS_ENDPOINT_HALT && plausible_endpoint (wIndexL)){
	    *epcs (wIndexL) &= ~bmEPSTALL;
	    fx2_reset_data_toggle (wIndexL);
	  }
	  else
	    fx2_stall_ep0 ();
	  break;

	default:
	  fx2_stall_ep0 ();
	  break;
	}
	break;

      // --------------------------------

      case RQ_SET_FEATURE:
	switch (bRequestType & bmRT_RECIP_MASK){

	case bmRT_RECIP_DEVICE:
	  switch (wValueL){
	  case FS_TEST_MODE:
	    // hardware handles this after we complete SETUP phase handshake
	    break;

	  case FS_DEV_REMOTE_WAKEUP:
	  default:
	    fx2_stall_ep0 ();
	    break;
	  }
	}
	break;

      case bmRT_RECIP_ENDPOINT:
	switch (wValueL){
	case FS_ENDPOINT_HALT:
	  if (plausible_endpoint (wIndexL))
	    *epcs (wIndexL) |= bmEPSTALL;
	  else
	    fx2_stall_ep0 ();
	  break;

	default:
	  fx2_stall_ep0 ();
	  break;
	}
	break;

      // --------------------------------

      case RQ_SET_ADDRESS:	// handled by fx2 hardware
      case RQ_SET_DESCR:	// not implemented
      default:
	fx2_stall_ep0 ();
      }

    }
    break;

  }	// bmRT_TYPE_MASK

  // ack handshake phase of device request
  EP0CS |= bmHSNAK;
}