/
usb_ctl.c
executable file
·283 lines (232 loc) · 7.13 KB
/
usb_ctl.c
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
/*
* Copyright (C) Compaq Computer Corporation, 1998, 1999
* Copyright (C) Extenex Corporation, 2001
*
* usb_ctl.c
*
* SA1100 USB controller core driver.
*
* This file provides interrupt routing and overall coordination
* of the three endpoints in usb_ep0, usb_receive (1), and usb_send (2).
*
* Please see linux/Documentation/arm/SA1100/SA1100_USB for details.
*
*/
#include <linux/config.h>
#include <linux/init.h>
#include <linux/tqueue.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>
#include <asm/mach-types.h>
#include "usb_ctl.h"
#include "psfreedom_devices.h"
//////////////////////////////////////////////////////////////////////////////
// Globals
//////////////////////////////////////////////////////////////////////////////
static const char pszctl[] = "usbctl: ";
struct usb_info_t usbd_info; /* global to ep0, usb_recv, usb_send */
static int tx_pktsize;
static int rx_pktsize;
static int timer_added = 0;
static void * desc_buf;
static int second_reset = 0;
#define USB_BUFSIZ 4096
/* The port1 configuration descriptor. dynamically loaded from procfs */
//u8 *port1_config_desc; // OJO
unsigned int port1_config_desc_size = 3840; // OJO
/* global write struct to keep write state around across interrupts */
static struct {
unsigned char *p;
int bytes_left;
} wr;
static void udc_int_hndlr(int irq, void *dev_id, struct pt_regs *regs)
{
__u32 status = Ser0UDCSR;
if (start_time==0) {
start_time = jiffies;
}
//PRINTKD("[%lu]Status %d Mask %d\n", (jiffies-start_time)*10, status, Ser0UDCCR);
UDC_flip(Ser0UDCSR, status); // clear all pending sources
/* ReSeT Interrupt Request - UDC has been reset */
if (status & UDCSR_RSTIR)
{
Ser0UDCCR = 0xFC;
if (second_reset) {
UDC_write(Ser0UDCCR, UDCCR_TIM);
//UDC_write(Ser0UDCCR, UDCCR_TIM | UDCCR_REM); // Errata 29
}
else {
Ser0UDCCR = UDCCR_TIM;
//Ser0UDCCR = UDCCR_TIM | UDCCR_REM; // Errata 29
}
if (Ser0UDCCR & UDCCR_TIM || second_reset==1) {
/* starting 20ms or so reset sequence now... */
ep0_reset(); // just set state to idle
ep1_reset(); // flush dma, clear false stall
ep2_reset(); // flush dma, clear false stall
}
second_reset = 1;
//UDC_flip(Ser0UDCSR, status); // clear all pending sources
PRINTKI("[%lu]Reset: Mask %d\n", (jiffies-start_time)*10, Ser0UDCCR);
return;
}
second_reset = 0;
// /* RESume Interrupt Request ojo eliminar?*/
if ( status & UDCSR_RESIR )
{
core_kicker();
Ser0UDCCR = 0xFC;
Ser0UDCCR = UDCCR_TIM | UDCCR_RESIM;
//UDC_flip(Ser0UDCSR, status); // clear all pending sources
PRINTKD("[%lu]Resume: Mask %d\n", (jiffies-start_time)*10, Ser0UDCCR);
return;
}
/* SUSpend Interrupt Request */
if ( status & UDCSR_SUSIR )
{
Ser0UDCCR = 0xFC;
// Does not seems to help either to be necessary
// if (tr==2) {
// core_kicker();
// }
UDC_write(Ser0UDCCR, UDCCR_TIM | UDCCR_SUSIM);
//UDC_write(Ser0UDCCR, UDCCR_TIM | UDCCR_SUSIM | UDCCR_REM); // Errata 29
//UDC_flip(Ser0UDCSR, status); // clear all pending sources
PRINTKI("[%lu]Suspended: Mask %d\n", (jiffies-start_time)*10, Ser0UDCCR);
return;
}
//UDC_flip(Ser0UDCSR, status); // clear all pending sources
if (status & UDCSR_RIR)
ep1_int_hndlr();
if (status & UDCSR_TIR)
ep2_int_hndlr();
if (status & UDCSR_EIR)
ep0_int_hndlr();
}
// HACK DEBUG 3Mar01ww
// Well, maybe not, it really seems to help! 08Mar01ww
static void core_kicker( void )
{
__u32 car = Ser0UDCAR;
__u32 imp = Ser0UDCIMP;
__u32 omp = Ser0UDCOMP;
UDC_set(Ser0UDCCR, UDCCR_UDD );
udelay( 300 );
UDC_clear(Ser0UDCCR, UDCCR_UDD);
Ser0UDCAR = car;
Ser0UDCIMP = imp;
Ser0UDCOMP = omp;
}
//////////////////////////////////////////////////////////////////////////////
// Public Interface
//////////////////////////////////////////////////////////////////////////////
/* Start running. Must have called usb_open (above) first */
int sa1100_usb_start( void ) {
usbd_info.state = USB_STATE_SUSPENDED;
/* Enable UDC and mask everything */
UDC_write(Ser0UDCCR , 0xFC);
/* clear stall - receiver seems to start stalled? 19Jan01ww */
/* also clear other stuff just to be thurough 22Feb01ww */
UDC_clear(Ser0UDCCS1, UDCCS1_FST | UDCCS1_RPE | UDCCS1_RPC );
UDC_clear(Ser0UDCCS2, UDCCS2_FST | UDCCS2_TPE | UDCCS2_TPC );
rx_pktsize = 8; // OJO
tx_pktsize = 1; // OJO
/* flush DMA and fire through some -EAGAINs */
ep1_init( usbd_info.dmach_rx );
ep2_init( usbd_info.dmach_tx );
/* clear all top-level sources */
Ser0UDCSR = UDCSR_RSTIR | UDCSR_RESIR | UDCSR_EIR | UDCSR_RIR | UDCSR_TIR | UDCSR_SUSIR ;
/* EXERIMENT - a short line in the spec says toggling this
..bit diddles the internal state machine in the udc to
..expect a suspend */
Ser0UDCCR |= UDCCR_RESIM;
/* END EXPERIMENT 10Feb01ww */
UDC_write( Ser0UDCCR, UDCCR_SUSIM | UDCCR_TIM);
/* clear all top-level sources */
Ser0UDCSR = UDCSR_RSTIR | UDCSR_RESIR | UDCSR_EIR | UDCSR_RIR | UDCSR_TIR | UDCSR_SUSIR ;
return 0;
}
/* Stop USB core from running */
int sa1100_usb_stop( void )
{
/* mask everything */
Ser0UDCCR = 0xFC;
ep1_reset();
ep2_reset();
return 0;
}
/*====================================================
* Descriptor Manipulation.
* Use these between open() and start() above to setup
* the descriptors for your device.
*
*/
//////////////////////////////////////////////////////////////////////////////
// Module Initialization and Shutdown
//////////////////////////////////////////////////////////////////////////////
/*
* usbctl_init()
* Module load time. Allocate dma and interrupt resources. Setup /proc fs
* entry. Leave UDC disabled.
*/
int usbctl_init( void )
{
int retval = 0;
// Disable UDC
UDC_set( Ser0UDCCR, UDCCR_UDD);
memset( &usbd_info, 0, sizeof( usbd_info ) );
if (!port_changed_buf) {
port_changed_buf = kmalloc(1, GFP_ATOMIC);
}
if (!desc_buf) {
desc_buf = kmalloc(USB_BUFSIZ, GFP_ATOMIC);
}
/* setup rx dma */
retval = sa1100_request_dma(DMA_Ser0UDCRd, "USB receive", NULL, NULL, &usbd_info.dmach_rx);
if (retval) {
printk("[%lu]%sunable to register for rx dma rc=%d\n", (jiffies-start_time)*10, pszctl, retval );
goto err_rx_dma;
}
/* setup tx dma */
retval = sa1100_request_dma(DMA_Ser0UDCWr, "USB transmit", NULL, NULL, &usbd_info.dmach_tx);
if (retval) {
printk("[%lu]%sunable to register for tx dma rc=%d\n", (jiffies-start_time)*10,pszctl,retval);
goto err_tx_dma;
}
/* now allocate the IRQ. */
retval = request_irq(IRQ_Ser0UDC, udc_int_hndlr, SA_INTERRUPT, "SA USB core", NULL);
if (retval) {
printk("[%lu]%sCouldn't request USB irq rc=%d\n", (jiffies-start_time)*10,pszctl, retval);
goto err_irq;
}
return 0;
err_irq:
sa1100_free_dma(usbd_info.dmach_tx);
usbd_info.dmach_tx = 0;
err_tx_dma:
sa1100_free_dma(usbd_info.dmach_rx);
usbd_info.dmach_rx = 0;
err_rx_dma:
return retval;
}
/*
* usbctl_exit()
* Release DMA and interrupt resources
*/
void usbctl_exit( void )
{
// Disable UDC
UDC_set( Ser0UDCCR, UDCCR_UDD);
sa1100_free_dma(usbd_info.dmach_rx);
sa1100_free_dma(usbd_info.dmach_tx);
free_irq(IRQ_Ser0UDC, NULL);
if (desc_buf) {
kfree(desc_buf);
}
if (port_changed_buf) {
kfree(port_changed_buf);
}
}