[etrax] refresh kernel patches
[openwrt.git] / target / linux / etrax / patches / 301-usb_support.patch
1 Index: linux-2.6.25.4/drivers/usb/Makefile
2 ===================================================================
3 --- linux-2.6.25.4.orig/drivers/usb/Makefile
4 +++ linux-2.6.25.4/drivers/usb/Makefile
5 @@ -16,6 +16,7 @@ obj-$(CONFIG_USB_UHCI_HCD)    += host/
6  obj-$(CONFIG_USB_SL811_HCD)    += host/
7  obj-$(CONFIG_USB_U132_HCD)     += host/
8  obj-$(CONFIG_USB_R8A66597_HCD) += host/
9 +obj-$(CONFIG_ETRAX_USB_HOST)   += host/
10  
11  obj-$(CONFIG_USB_ACM)          += class/
12  obj-$(CONFIG_USB_PRINTER)      += class/
13 Index: linux-2.6.25.4/drivers/usb/host/Makefile
14 ===================================================================
15 --- linux-2.6.25.4.orig/drivers/usb/host/Makefile
16 +++ linux-2.6.25.4/drivers/usb/host/Makefile
17 @@ -17,3 +17,5 @@ obj-$(CONFIG_USB_SL811_CS)    += sl811_cs.o
18  obj-$(CONFIG_USB_U132_HCD)     += u132-hcd.o
19  obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
20  
21 +#obj-$(CONFIG_USB_CARNEOL)     += hc-crisv10.o
22 +obj-$(CONFIG_ETRAX_USB_HOST)   += hc-crisv10.o
23 Index: linux-2.6.25.4/drivers/usb/host/hc-cris-dbg.h
24 ===================================================================
25 --- /dev/null
26 +++ linux-2.6.25.4/drivers/usb/host/hc-cris-dbg.h
27 @@ -0,0 +1,143 @@
28 +
29 +/* macros for debug output */
30 +
31 +#define hcd_dbg(hcd, fmt, args...) \
32 +       dev_info(hcd->self.controller, fmt, ## args)
33 +#define hcd_err(hcd, fmt, args...) \
34 +       dev_err(hcd->self.controller, fmt, ## args)
35 +#define hcd_info(hcd, fmt, args...) \
36 +       dev_info(hcd->self.controller, fmt, ## args)
37 +#define hcd_warn(hcd, fmt, args...) \
38 +       dev_warn(hcd->self.controller, fmt, ## args)
39 +
40 +/*
41 +#define devdrv_dbg(fmt, args...) \
42 +        printk(KERN_INFO "usb_devdrv dbg: ");printk(fmt, ## args)
43 +*/
44 +#define devdrv_dbg(fmt, args...) {}
45 +
46 +#define devdrv_err(fmt, args...) \
47 +        printk(KERN_ERR "usb_devdrv error: ");printk(fmt, ## args)
48 +#define devdrv_info(fmt, args...) \
49 +        printk(KERN_INFO "usb_devdrv: ");printk(fmt, ## args)
50 +
51 +#define irq_dbg(fmt, args...) \
52 +        printk(KERN_INFO "crisv10_irq dbg: ");printk(fmt, ## args)
53 +#define irq_err(fmt, args...) \
54 +        printk(KERN_ERR "crisv10_irq error: ");printk(fmt, ## args)
55 +#define irq_warn(fmt, args...) \
56 +        printk(KERN_INFO "crisv10_irq warn: ");printk(fmt, ## args)
57 +#define irq_info(fmt, args...) \
58 +        printk(KERN_INFO "crisv10_hcd: ");printk(fmt, ## args)
59 +
60 +/*
61 +#define rh_dbg(fmt, args...) \
62 +  printk(KERN_DEBUG "crisv10_rh dbg: ");printk(fmt, ## args)
63 +*/
64 +#define rh_dbg(fmt, args...) {}
65 +
66 +#define rh_err(fmt, args...) \
67 +        printk(KERN_ERR "crisv10_rh error: ");printk(fmt, ## args)
68 +#define rh_warn(fmt, args...) \
69 +        printk(KERN_INFO "crisv10_rh warning: ");printk(fmt, ## args)
70 +#define rh_info(fmt, args...) \
71 +        printk(KERN_INFO "crisv10_rh: ");printk(fmt, ## args)
72 +
73 +/*
74 +#define tc_dbg(fmt, args...) \
75 +        printk(KERN_INFO "crisv10_tc dbg: ");printk(fmt, ## args)
76 +*/
77 +#define tc_dbg(fmt, args...) {while(0){}}
78 +
79 +#define tc_err(fmt, args...) \
80 +        printk(KERN_ERR "crisv10_tc error: ");printk(fmt, ## args)
81 +/*
82 +#define tc_warn(fmt, args...) \
83 +        printk(KERN_INFO "crisv10_tc warning: ");printk(fmt, ## args)
84 +*/
85 +#define tc_warn(fmt, args...) {while(0){}}
86 +
87 +#define tc_info(fmt, args...) \
88 +        printk(KERN_INFO "crisv10_tc: ");printk(fmt, ## args)
89 +
90 +
91 +/* Debug print-outs for various traffic types */
92 +
93 +#define intr_warn(fmt, args...) \
94 +        printk(KERN_INFO "crisv10_intr warning: ");printk(fmt, ## args)
95 +
96 +#define intr_dbg(fmt, args...) \
97 +        printk(KERN_DEBUG "crisv10_intr dbg: ");printk(fmt, ## args)
98 +/*
99 +#define intr_dbg(fmt, args...) {while(0){}}
100 +*/
101 +
102 +
103 +#define isoc_err(fmt, args...) \
104 +        printk(KERN_ERR "crisv10_isoc error: ");printk(fmt, ## args)
105 +/*
106 +#define isoc_warn(fmt, args...) \
107 +        printk(KERN_INFO "crisv10_isoc warning: ");printk(fmt, ## args)
108 +*/
109 +#define isoc_warn(fmt, args...) {while(0){}}
110 +
111 +/*
112 +#define isoc_dbg(fmt, args...) \
113 +        printk(KERN_INFO "crisv10_isoc dbg: ");printk(fmt, ## args)
114 +*/
115 +#define isoc_dbg(fmt, args...) {while(0){}}
116 +
117 +/*
118 +#define timer_warn(fmt, args...) \
119 +        printk(KERN_INFO "crisv10_timer warning: ");printk(fmt, ## args)
120 +*/
121 +#define timer_warn(fmt, args...) {while(0){}}
122 +
123 +/*
124 +#define timer_dbg(fmt, args...) \
125 +        printk(KERN_INFO "crisv10_timer dbg: ");printk(fmt, ## args)
126 +*/
127 +#define timer_dbg(fmt, args...) {while(0){}}
128 +
129 +
130 +/* Debug printouts for events related to late finishing of URBs */
131 +
132 +#define late_dbg(fmt, args...) \
133 +        printk(KERN_INFO "crisv10_late dbg: ");printk(fmt, ## args)
134 +/*
135 +#define late_dbg(fmt, args...) {while(0){}}
136 +*/
137 +
138 +#define late_warn(fmt, args...) \
139 +        printk(KERN_INFO "crisv10_late warning: ");printk(fmt, ## args)
140 +/*
141 +#define errno_dbg(fmt, args...) \
142 +        printk(KERN_INFO "crisv10_errno dbg: ");printk(fmt, ## args)
143 +*/
144 +#define errno_dbg(fmt, args...) {while(0){}}
145 +
146 +
147 +#define dma_dbg(fmt, args...) \
148 +        printk(KERN_INFO "crisv10_dma dbg: ");printk(fmt, ## args)
149 +#define dma_err(fmt, args...) \
150 +        printk(KERN_ERR "crisv10_dma error: ");printk(fmt, ## args)
151 +#define dma_warn(fmt, args...) \
152 +        printk(KERN_INFO "crisv10_dma warning: ");printk(fmt, ## args)
153 +#define dma_info(fmt, args...) \
154 +        printk(KERN_INFO "crisv10_dma: ");printk(fmt, ## args)
155 +
156 +
157 +
158 +#define str_dir(pipe) \
159 +       (usb_pipeout(pipe) ? "out" : "in")
160 +#define str_type(pipe) \
161 +       ({                                                              \
162 +               char *s = "?";                                          \
163 +               switch (usb_pipetype(pipe)) {                           \
164 +               case PIPE_ISOCHRONOUS:  s = "iso";  break;              \
165 +               case PIPE_INTERRUPT:    s = "intr"; break;              \
166 +               case PIPE_CONTROL:      s = "ctrl"; break;              \
167 +               case PIPE_BULK:         s = "bulk"; break;              \
168 +               };                                                      \
169 +               s;                                                      \
170 +       })
171 Index: linux-2.6.25.4/drivers/usb/host/hc-crisv10.c
172 ===================================================================
173 --- /dev/null
174 +++ linux-2.6.25.4/drivers/usb/host/hc-crisv10.c
175 @@ -0,0 +1,4800 @@
176 +/*
177 + *
178 + * ETRAX 100LX USB Host Controller Driver
179 + *
180 + * Copyright (C) 2005, 2006  Axis Communications AB
181 + *
182 + * Author: Konrad Eriksson <konrad.eriksson@axis.se>
183 + *
184 + */
185 +
186 +#include <linux/module.h>
187 +#include <linux/kernel.h>
188 +#include <linux/init.h>
189 +#include <linux/moduleparam.h>
190 +#include <linux/spinlock.h>
191 +#include <linux/usb.h>
192 +#include <linux/platform_device.h>
193 +
194 +#include <asm/io.h>
195 +#include <asm/irq.h>
196 +#include <asm/arch/dma.h>
197 +#include <asm/arch/io_interface_mux.h>
198 +
199 +#include "../core/hcd.h"
200 +#include "../core/hub.h"
201 +#include "hc-crisv10.h"
202 +#include "hc-cris-dbg.h"
203 +
204 +
205 +/***************************************************************************/
206 +/***************************************************************************/
207 +/* Host Controller settings                                                */
208 +/***************************************************************************/
209 +/***************************************************************************/
210 +
211 +#define VERSION                        "1.00 hinko.4"
212 +#define COPYRIGHT              "(c) 2005, 2006 Axis Communications AB"
213 +#define DESCRIPTION     "ETRAX 100LX USB Host Controller (2.6.25-rc9 port)"
214 +
215 +#define ETRAX_USB_HC_IRQ USB_HC_IRQ_NBR
216 +#define ETRAX_USB_RX_IRQ USB_DMA_RX_IRQ_NBR
217 +#define ETRAX_USB_TX_IRQ USB_DMA_TX_IRQ_NBR
218 +
219 +/* Number of physical ports in Etrax 100LX */
220 +#define USB_ROOT_HUB_PORTS 2
221 +
222 +const char hc_name[] = "hc-crisv10";
223 +const char product_desc[] = DESCRIPTION;
224 +
225 +/* The number of epids is, among other things, used for pre-allocating
226 +   ctrl, bulk and isoc EP descriptors (one for each epid).
227 +   Assumed to be > 1 when initiating the DMA lists. */
228 +#define NBR_OF_EPIDS       32
229 +
230 +/* Support interrupt traffic intervals up to 128 ms. */
231 +#define MAX_INTR_INTERVAL  128
232 +
233 +/* If periodic traffic (intr or isoc) is to be used, then one entry in the EP
234 +   table must be "invalid". By this we mean that we shouldn't care about epid
235 +   attentions for this epid, or at least handle them differently from epid
236 +   attentions for "valid" epids. This define determines which one to use
237 +   (don't change it). */
238 +#define INVALID_EPID       31
239 +/* A special epid for the bulk dummys. */
240 +#define DUMMY_EPID         30
241 +
242 +/* Module settings */
243 +
244 +MODULE_DESCRIPTION(DESCRIPTION);
245 +MODULE_LICENSE("GPL");
246 +MODULE_AUTHOR("Konrad Eriksson <konrad.eriksson@axis.se>");
247 +
248 +
249 +/* Module parameters */
250 +
251 +/* 0 = No ports enabled
252 +   1 = Only port 1 enabled (on board ethernet on devboard)
253 +   2 = Only port 2 enabled (external connector on devboard)
254 +   3 = Both ports enabled
255 +*/
256 +static unsigned int ports = 3;
257 +module_param(ports, uint, S_IRUGO);
258 +MODULE_PARM_DESC(ports, "Bitmask indicating USB ports to use");
259 +
260 +
261 +/***************************************************************************/
262 +/***************************************************************************/
263 +/* Shared global variables for this module                                 */
264 +/***************************************************************************/
265 +/***************************************************************************/
266 +
267 +/* EP descriptor lists for non period transfers. Must be 32-bit aligned. */
268 +static volatile struct USB_EP_Desc TxBulkEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
269 +
270 +static volatile struct USB_EP_Desc TxCtrlEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
271 +
272 +/* EP descriptor lists for period transfers. Must be 32-bit aligned. */
273 +static volatile struct USB_EP_Desc TxIntrEPList[MAX_INTR_INTERVAL] __attribute__ ((aligned (4)));
274 +static volatile struct USB_SB_Desc TxIntrSB_zout __attribute__ ((aligned (4)));
275 +
276 +static volatile struct USB_EP_Desc TxIsocEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
277 +static volatile struct USB_SB_Desc TxIsocSB_zout __attribute__ ((aligned (4)));
278 +
279 +//static volatile struct USB_SB_Desc TxIsocSBList[NBR_OF_EPIDS] __attribute__ ((aligned (4))); 
280 +
281 +/* After each enabled bulk EP IN we put two disabled EP descriptors with the eol flag set,
282 +   causing the DMA to stop the DMA channel. The first of these two has the intr flag set, which
283 +   gives us a dma8_sub0_descr interrupt. When we receive this, we advance the DMA one step in the
284 +   EP list and then restart the bulk channel, thus forcing a switch between bulk EP descriptors
285 +   in each frame. */
286 +static volatile struct USB_EP_Desc TxBulkDummyEPList[NBR_OF_EPIDS][2] __attribute__ ((aligned (4)));
287 +
288 +/* List of URB pointers, where each points to the active URB for a epid.
289 +   For Bulk, Ctrl and Intr this means which URB that currently is added to
290 +   DMA lists (Isoc URBs are all directly added to DMA lists). As soon as
291 +   URB has completed is the queue examined and the first URB in queue is
292 +   removed and moved to the activeUrbList while its state change to STARTED and
293 +   its transfer(s) gets added to DMA list (exception Isoc where URBs enter
294 +   state STARTED directly and added transfers added to DMA lists). */
295 +static struct urb *activeUrbList[NBR_OF_EPIDS];
296 +
297 +/* Additional software state info for each epid */
298 +static struct etrax_epid epid_state[NBR_OF_EPIDS];
299 +
300 +/* Timer handles for bulk traffic timer used to avoid DMA bug where DMA stops
301 +   even if there is new data waiting to be processed */
302 +static struct timer_list bulk_start_timer = TIMER_INITIALIZER(NULL, 0, 0);
303 +static struct timer_list bulk_eot_timer = TIMER_INITIALIZER(NULL, 0, 0);
304 +
305 +/* We want the start timer to expire before the eot timer, because the former
306 +   might start traffic, thus making it unnecessary for the latter to time
307 +   out. */
308 +#define BULK_START_TIMER_INTERVAL (HZ/50) /* 20 ms */
309 +#define BULK_EOT_TIMER_INTERVAL (HZ/16) /* 60 ms */
310 +
311 +/* Delay before a URB completion happen when it's scheduled to be delayed */
312 +#define LATER_TIMER_DELAY (HZ/50) /* 20 ms */
313 +
314 +/* Simplifying macros for checking software state info of a epid */
315 +/* ----------------------------------------------------------------------- */
316 +#define epid_inuse(epid)       epid_state[epid].inuse
317 +#define epid_out_traffic(epid) epid_state[epid].out_traffic
318 +#define epid_isoc(epid)   (epid_state[epid].type == PIPE_ISOCHRONOUS ? 1 : 0)
319 +#define epid_intr(epid)   (epid_state[epid].type == PIPE_INTERRUPT ? 1 : 0)
320 +
321 +
322 +/***************************************************************************/
323 +/***************************************************************************/
324 +/* DEBUG FUNCTIONS                                                         */
325 +/***************************************************************************/
326 +/***************************************************************************/
327 +/* Note that these functions are always available in their "__" variants,
328 +   for use in error situations. The "__" missing variants are controlled by
329 +   the USB_DEBUG_DESC/USB_DEBUG_URB macros. */
330 +static void __dump_urb(struct urb* purb)
331 +{
332 +  struct crisv10_urb_priv *urb_priv = purb->hcpriv;
333 +  int urb_num = -1;
334 +  if(urb_priv) {
335 +    urb_num = urb_priv->urb_num;
336 +  }
337 +  printk("\nURB:0x%x[%d]\n", (unsigned int)purb, urb_num);
338 +  printk("dev                   :0x%08lx\n", (unsigned long)purb->dev);
339 +  printk("pipe                  :0x%08x\n", purb->pipe);
340 +  printk("status                :%d\n", purb->status);
341 +  printk("transfer_flags        :0x%08x\n", purb->transfer_flags);
342 +  printk("transfer_buffer       :0x%08lx\n", (unsigned long)purb->transfer_buffer);
343 +  printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
344 +  printk("actual_length         :%d\n", purb->actual_length);
345 +  printk("setup_packet          :0x%08lx\n", (unsigned long)purb->setup_packet);
346 +  printk("start_frame           :%d\n", purb->start_frame);
347 +  printk("number_of_packets     :%d\n", purb->number_of_packets);
348 +  printk("interval              :%d\n", purb->interval);
349 +  printk("error_count           :%d\n", purb->error_count);
350 +  printk("context               :0x%08lx\n", (unsigned long)purb->context);
351 +  printk("complete              :0x%08lx\n\n", (unsigned long)purb->complete);
352 +}
353 +
354 +static void __dump_in_desc(volatile struct USB_IN_Desc *in)
355 +{
356 +  printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
357 +  printk("  sw_len  : 0x%04x (%d)\n", in->sw_len, in->sw_len);
358 +  printk("  command : 0x%04x\n", in->command);
359 +  printk("  next    : 0x%08lx\n", in->next);
360 +  printk("  buf     : 0x%08lx\n", in->buf);
361 +  printk("  hw_len  : 0x%04x (%d)\n", in->hw_len, in->hw_len);
362 +  printk("  status  : 0x%04x\n\n", in->status);
363 +}
364 +
365 +static void __dump_sb_desc(volatile struct USB_SB_Desc *sb)
366 +{
367 +  char tt = (sb->command & 0x30) >> 4;
368 +  char *tt_string;
369 +
370 +  switch (tt) {
371 +  case 0:
372 +    tt_string = "zout";
373 +    break;
374 +  case 1:
375 +    tt_string = "in";
376 +    break;
377 +  case 2:
378 +    tt_string = "out";
379 +    break;
380 +  case 3:
381 +    tt_string = "setup";
382 +    break;
383 +  default:
384 +    tt_string = "unknown (weird)";
385 +  }
386 +
387 +  printk(" USB_SB_Desc at 0x%08lx ", (unsigned long)sb);
388 +  printk(" command:0x%04x (", sb->command);
389 +  printk("rem:%d ", (sb->command & 0x3f00) >> 8);
390 +  printk("full:%d ", (sb->command & 0x40) >> 6);
391 +  printk("tt:%d(%s) ", tt, tt_string);
392 +  printk("intr:%d ", (sb->command & 0x8) >> 3);
393 +  printk("eot:%d ", (sb->command & 0x2) >> 1);
394 +  printk("eol:%d)", sb->command & 0x1);
395 +  printk(" sw_len:0x%04x(%d)", sb->sw_len, sb->sw_len);
396 +  printk(" next:0x%08lx", sb->next);
397 +  printk(" buf:0x%08lx\n", sb->buf);
398 +}
399 +
400 +
401 +static void __dump_ep_desc(volatile struct USB_EP_Desc *ep)
402 +{
403 +  printk("USB_EP_Desc at 0x%08lx ", (unsigned long)ep);
404 +  printk(" command:0x%04x (", ep->command);
405 +  printk("ep_id:%d ", (ep->command & 0x1f00) >> 8);
406 +  printk("enable:%d ", (ep->command & 0x10) >> 4);
407 +  printk("intr:%d ", (ep->command & 0x8) >> 3);
408 +  printk("eof:%d ", (ep->command & 0x2) >> 1);
409 +  printk("eol:%d)", ep->command & 0x1);
410 +  printk(" hw_len:0x%04x(%d)", ep->hw_len, ep->hw_len);
411 +  printk(" next:0x%08lx", ep->next);
412 +  printk(" sub:0x%08lx\n", ep->sub);
413 +}
414 +
415 +static inline void __dump_ep_list(int pipe_type)
416 +{
417 +  volatile struct USB_EP_Desc *ep;
418 +  volatile struct USB_EP_Desc *first_ep;
419 +  volatile struct USB_SB_Desc *sb;
420 +
421 +  switch (pipe_type)
422 +    {
423 +    case PIPE_BULK:
424 +      first_ep = &TxBulkEPList[0];
425 +      break;
426 +    case PIPE_CONTROL:
427 +      first_ep = &TxCtrlEPList[0];
428 +      break;
429 +    case PIPE_INTERRUPT:
430 +      first_ep = &TxIntrEPList[0];
431 +      break;
432 +    case PIPE_ISOCHRONOUS:
433 +      first_ep = &TxIsocEPList[0];
434 +      break;
435 +    default:
436 +      warn("Cannot dump unknown traffic type");
437 +      return;
438 +    }
439 +  ep = first_ep;
440 +
441 +  printk("\n\nDumping EP list...\n\n");
442 +
443 +  do {
444 +    __dump_ep_desc(ep);
445 +    /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
446 +    sb = ep->sub ? phys_to_virt(ep->sub) : 0;
447 +    while (sb) {
448 +      __dump_sb_desc(sb);
449 +      sb = sb->next ? phys_to_virt(sb->next) : 0;
450 +    }
451 +    ep = (volatile struct USB_EP_Desc *)(phys_to_virt(ep->next));
452 +
453 +  } while (ep != first_ep);
454 +}
455 +
456 +static inline void __dump_ept_data(int epid)
457 +{
458 +  unsigned long flags;
459 +  __u32 r_usb_ept_data;
460 +
461 +  if (epid < 0 || epid > 31) {
462 +    printk("Cannot dump ept data for invalid epid %d\n", epid);
463 +    return;
464 +  }
465 +
466 +  local_irq_save(flags);
467 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
468 +  nop();
469 +  r_usb_ept_data = *R_USB_EPT_DATA;
470 +  local_irq_restore(flags);
471 +
472 +  printk(" R_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
473 +  if (r_usb_ept_data == 0) {
474 +    /* No need for more detailed printing. */
475 +    return;
476 +  }
477 +  printk("  valid           : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
478 +  printk("  hold            : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
479 +  printk("  error_count_in  : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
480 +  printk("  t_in            : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
481 +  printk("  low_speed       : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
482 +  printk("  port            : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
483 +  printk("  error_code      : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
484 +  printk("  t_out           : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
485 +  printk("  error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
486 +  printk("  max_len         : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
487 +  printk("  ep              : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
488 +  printk("  dev             : %d\n", (r_usb_ept_data & 0x0000003f));
489 +}
490 +
491 +static inline void __dump_ept_data_iso(int epid)
492 +{
493 +  unsigned long flags;
494 +  __u32 ept_data;
495 +
496 +  if (epid < 0 || epid > 31) {
497 +    printk("Cannot dump ept data for invalid epid %d\n", epid);
498 +    return;
499 +  }
500 +
501 +  local_irq_save(flags);
502 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
503 +  nop();
504 +  ept_data = *R_USB_EPT_DATA_ISO;
505 +  local_irq_restore(flags);
506 +
507 +  printk(" R_USB_EPT_DATA = 0x%x for epid %d :\n", ept_data, epid);
508 +  if (ept_data == 0) {
509 +    /* No need for more detailed printing. */
510 +    return;
511 +  }
512 +  printk("  valid           : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, valid,
513 +                                               ept_data));
514 +  printk("  port            : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, port,
515 +                                               ept_data));
516 +  printk("  error_code      : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code,
517 +                                               ept_data));
518 +  printk("  max_len         : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len,
519 +                                               ept_data));
520 +  printk("  ep              : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, ep,
521 +                                               ept_data));
522 +  printk("  dev             : %d\n", IO_EXTRACT(R_USB_EPT_DATA_ISO, dev,
523 +                                               ept_data));
524 +}
525 +
526 +static inline void __dump_ept_data_list(void)
527 +{
528 +  int i;
529 +
530 +  printk("Dumping the whole R_USB_EPT_DATA list\n");
531 +
532 +  for (i = 0; i < 32; i++) {
533 +    __dump_ept_data(i);
534 +  }
535 +}
536 +
537 +static void debug_epid(int epid) {
538 +  int i;
539 +  
540 +  if(epid_isoc(epid)) {
541 +    __dump_ept_data_iso(epid);
542 +  } else {
543 +    __dump_ept_data(epid);
544 +  }
545 +
546 +  printk("Bulk:\n");
547 +  for(i = 0; i < 32; i++) {
548 +    if(IO_EXTRACT(USB_EP_command, epid, TxBulkEPList[i].command) ==
549 +       epid) {
550 +      printk("%d: ", i); __dump_ep_desc(&(TxBulkEPList[i]));
551 +    }
552 +  }
553 +
554 +  printk("Ctrl:\n");
555 +  for(i = 0; i < 32; i++) {
556 +    if(IO_EXTRACT(USB_EP_command, epid, TxCtrlEPList[i].command) ==
557 +       epid) {
558 +      printk("%d: ", i); __dump_ep_desc(&(TxCtrlEPList[i]));
559 +    }
560 +  }
561 +
562 +  printk("Intr:\n");
563 +  for(i = 0; i < MAX_INTR_INTERVAL; i++) {
564 +    if(IO_EXTRACT(USB_EP_command, epid, TxIntrEPList[i].command) ==
565 +       epid) {
566 +      printk("%d: ", i); __dump_ep_desc(&(TxIntrEPList[i]));
567 +    }
568 +  }
569 +  
570 +  printk("Isoc:\n");
571 +  for(i = 0; i < 32; i++) {
572 +    if(IO_EXTRACT(USB_EP_command, epid, TxIsocEPList[i].command) ==
573 +       epid) {
574 +      printk("%d: ", i); __dump_ep_desc(&(TxIsocEPList[i]));
575 +    }
576 +  }
577 +
578 +  __dump_ept_data_list();
579 +  __dump_ep_list(PIPE_INTERRUPT);
580 +  printk("\n\n");
581 +}
582 +
583 +
584 +
585 +char* hcd_status_to_str(__u8 bUsbStatus) {
586 +  static char hcd_status_str[128];
587 +  hcd_status_str[0] = '\0';
588 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, ourun, yes)) {
589 +    strcat(hcd_status_str, "ourun ");
590 +  }
591 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, perror, yes)) {
592 +    strcat(hcd_status_str, "perror ");
593 +  }
594 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, device_mode, yes)) {
595 +    strcat(hcd_status_str, "device_mode ");
596 +  }
597 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, host_mode, yes)) {
598 +    strcat(hcd_status_str, "host_mode ");
599 +  }
600 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, started, yes)) {
601 +    strcat(hcd_status_str, "started ");
602 +  }
603 +  if(bUsbStatus & IO_STATE(R_USB_STATUS, running, yes)) {
604 +    strcat(hcd_status_str, "running ");
605 +  }
606 +  return hcd_status_str;
607 +}
608 +
609 +
610 +char* sblist_to_str(struct USB_SB_Desc* sb_desc) {
611 +  static char sblist_to_str_buff[128];
612 +  char tmp[32], tmp2[32];
613 +  sblist_to_str_buff[0] = '\0';
614 +  while(sb_desc != NULL) {
615 +    switch(IO_EXTRACT(USB_SB_command, tt, sb_desc->command)) {
616 +    case 0: sprintf(tmp, "zout");  break;
617 +    case 1: sprintf(tmp, "in");    break;
618 +    case 2: sprintf(tmp, "out");   break;
619 +    case 3: sprintf(tmp, "setup"); break;
620 +    }
621 +    sprintf(tmp2, "(%s %d)", tmp, sb_desc->sw_len);
622 +    strcat(sblist_to_str_buff, tmp2);
623 +    if(sb_desc->next != 0) {
624 +      sb_desc = phys_to_virt(sb_desc->next);
625 +    } else {
626 +      sb_desc = NULL;
627 +    }
628 +  }
629 +  return sblist_to_str_buff;
630 +}
631 +
632 +char* port_status_to_str(__u16 wPortStatus) {
633 +  static char port_status_str[128];
634 +  port_status_str[0] = '\0';
635 +  if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) {
636 +    strcat(port_status_str, "connected ");
637 +  }
638 +  if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes)) {
639 +    strcat(port_status_str, "enabled ");
640 +  }
641 +  if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, suspended, yes)) {
642 +    strcat(port_status_str, "suspended ");
643 +  }
644 +  if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes)) {
645 +    strcat(port_status_str, "reset ");
646 +  }
647 +  if(wPortStatus & IO_STATE(R_USB_RH_PORT_STATUS_1, speed, full)) {
648 +    strcat(port_status_str, "full-speed ");
649 +  } else {
650 +    strcat(port_status_str, "low-speed ");
651 +  }
652 +  return port_status_str;
653 +}
654 +
655 +
656 +char* endpoint_to_str(struct usb_endpoint_descriptor *ed) {
657 +  static char endpoint_to_str_buff[128];
658 +  char tmp[32];
659 +  int epnum = ed->bEndpointAddress & 0x0F;
660 +  int dir = ed->bEndpointAddress & 0x80;
661 +  int type = ed->bmAttributes & 0x03;
662 +  endpoint_to_str_buff[0] = '\0';
663 +  sprintf(endpoint_to_str_buff, "ep:%d ", epnum);
664 +  switch(type) {
665 +  case 0:
666 +    sprintf(tmp, " ctrl");
667 +    break;
668 +  case 1:
669 +    sprintf(tmp, " isoc");
670 +    break;
671 +  case 2:
672 +    sprintf(tmp, " bulk");
673 +    break;
674 +  case 3:
675 +    sprintf(tmp, " intr");
676 +    break;
677 +  }
678 +  strcat(endpoint_to_str_buff, tmp);
679 +  if(dir) {
680 +    sprintf(tmp, " in");
681 +  } else {
682 +    sprintf(tmp, " out");
683 +  }
684 +  strcat(endpoint_to_str_buff, tmp);
685 +
686 +  return endpoint_to_str_buff;
687 +}
688 +
689 +/* Debug helper functions for Transfer Controller */
690 +char* pipe_to_str(unsigned int pipe) {
691 +  static char pipe_to_str_buff[128];
692 +  char tmp[64];
693 +  sprintf(pipe_to_str_buff, "dir:%s", str_dir(pipe));
694 +  sprintf(tmp, " type:%s", str_type(pipe));
695 +  strcat(pipe_to_str_buff, tmp);
696 +
697 +  sprintf(tmp, " dev:%d", usb_pipedevice(pipe));
698 +  strcat(pipe_to_str_buff, tmp);
699 +  sprintf(tmp, " ep:%d", usb_pipeendpoint(pipe));
700 +  strcat(pipe_to_str_buff, tmp);
701 +  return pipe_to_str_buff;
702 +}
703 +
704 +
705 +#define USB_DEBUG_DESC 1
706 +
707 +#ifdef USB_DEBUG_DESC
708 +#define dump_in_desc(x) __dump_in_desc(x)
709 +#define dump_sb_desc(...) __dump_sb_desc(...)
710 +#define dump_ep_desc(x) __dump_ep_desc(x)
711 +#define dump_ept_data(x) __dump_ept_data(x)
712 +#else
713 +#define dump_in_desc(...) do {} while (0)
714 +#define dump_sb_desc(...) do {} while (0)
715 +#define dump_ep_desc(...) do {} while (0)
716 +#endif
717 +
718 +
719 +/* Uncomment this to enable massive function call trace
720 +   #define USB_DEBUG_TRACE */
721 +//#define USB_DEBUG_TRACE 1
722 +
723 +#ifdef USB_DEBUG_TRACE
724 +#define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
725 +#define DBFEXIT  (printk(": Exiting:  %s\n", __FUNCTION__))
726 +#else
727 +#define DBFENTER do {} while (0)
728 +#define DBFEXIT  do {} while (0)
729 +#endif
730 +
731 +#define CHECK_ALIGN(x) if (((__u32)(x)) & 0x00000003) \
732 +{panic("Alignment check (DWORD) failed at %s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__);}
733 +
734 +/* Most helpful debugging aid */
735 +#define ASSERT(expr) ((void) ((expr) ? 0 : (err("assert failed at: %s %d",__FUNCTION__, __LINE__))))
736 +
737 +
738 +/***************************************************************************/
739 +/***************************************************************************/
740 +/* Forward declarations                                                    */
741 +/***************************************************************************/
742 +/***************************************************************************/
743 +void crisv10_hcd_epid_attn_irq(struct crisv10_irq_reg *reg);
744 +void crisv10_hcd_port_status_irq(struct crisv10_irq_reg *reg);
745 +void crisv10_hcd_ctl_status_irq(struct crisv10_irq_reg *reg);
746 +void crisv10_hcd_isoc_eof_irq(struct crisv10_irq_reg *reg);
747 +
748 +void rh_port_status_change(__u16[]);
749 +int  rh_clear_port_feature(__u8, __u16);
750 +int  rh_set_port_feature(__u8, __u16);
751 +static void rh_disable_port(unsigned int port);
752 +
753 +static void check_finished_bulk_tx_epids(struct usb_hcd *hcd,
754 +                                        int timer);
755 +
756 +//static int  tc_setup_epid(struct usb_host_endpoint *ep, struct urb *urb,
757 +//                      int mem_flags);
758 +static int tc_setup_epid(struct urb *urb, int mem_flags);
759 +static void tc_free_epid(struct usb_host_endpoint *ep);
760 +static int  tc_allocate_epid(void);
761 +static void tc_finish_urb(struct usb_hcd *hcd, struct urb *urb, int status);
762 +static void tc_finish_urb_later(struct usb_hcd *hcd, struct urb *urb,
763 +                               int status);
764 +
765 +static int  urb_priv_create(struct usb_hcd *hcd, struct urb *urb, int epid,
766 +                          int mem_flags);
767 +static void urb_priv_free(struct usb_hcd *hcd, struct urb *urb);
768 +
769 +static inline struct urb *urb_list_first(int epid);
770 +static inline void        urb_list_add(struct urb *urb, int epid,
771 +                                     int mem_flags);
772 +static inline urb_entry_t *urb_list_entry(struct urb *urb, int epid);
773 +static inline void        urb_list_del(struct urb *urb, int epid);
774 +static inline void        urb_list_move_last(struct urb *urb, int epid);
775 +static inline struct urb *urb_list_next(struct urb *urb, int epid);
776 +
777 +int create_sb_for_urb(struct urb *urb, int mem_flags);
778 +int init_intr_urb(struct urb *urb, int mem_flags);
779 +
780 +static inline void  etrax_epid_set(__u8 index, __u32 data);
781 +static inline void  etrax_epid_clear_error(__u8 index);
782 +static inline void  etrax_epid_set_toggle(__u8 index, __u8 dirout,
783 +                                             __u8 toggle);
784 +static inline __u8  etrax_epid_get_toggle(__u8 index, __u8 dirout);
785 +static inline __u32 etrax_epid_get(__u8 index);
786 +
787 +/* We're accessing the same register position in Etrax so
788 +   when we do full access the internal difference doesn't matter */
789 +#define etrax_epid_iso_set(index, data) etrax_epid_set(index, data)
790 +#define etrax_epid_iso_get(index) etrax_epid_get(index)
791 +
792 +
793 +//static void        tc_dma_process_isoc_urb(struct urb *urb);
794 +static void        tc_dma_process_queue(int epid);
795 +static void        tc_dma_unlink_intr_urb(struct urb *urb);
796 +static irqreturn_t tc_dma_tx_interrupt(int irq, void *vhc);
797 +static irqreturn_t tc_dma_rx_interrupt(int irq, void *vhc);
798 +
799 +static void tc_bulk_start_timer_func(unsigned long dummy);
800 +static void tc_bulk_eot_timer_func(unsigned long dummy);
801 +
802 +
803 +/*************************************************************/
804 +/*************************************************************/
805 +/* Host Controler Driver block                               */
806 +/*************************************************************/
807 +/*************************************************************/
808 +
809 +/* HCD operations */
810 +static irqreturn_t crisv10_hcd_top_irq(int irq, void*);
811 +static int crisv10_hcd_reset(struct usb_hcd *);
812 +static int crisv10_hcd_start(struct usb_hcd *);
813 +static void crisv10_hcd_stop(struct usb_hcd *);
814 +#ifdef CONFIG_PM
815 +static int crisv10_hcd_suspend(struct device *, u32, u32);
816 +static int crisv10_hcd_resume(struct device *, u32);
817 +#endif /* CONFIG_PM */
818 +static int crisv10_hcd_get_frame(struct usb_hcd *);
819 +
820 +//static int  tc_urb_enqueue(struct usb_hcd *, struct usb_host_endpoint *ep, struct urb *, gfp_t mem_flags);
821 +static int tc_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags);
822 +//static int  tc_urb_dequeue(struct usb_hcd *, struct urb *);
823 +static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
824 +static void tc_endpoint_disable(struct usb_hcd *, struct usb_host_endpoint *ep);
825 +
826 +static int rh_status_data_request(struct usb_hcd *, char *);
827 +static int rh_control_request(struct usb_hcd *, u16, u16, u16, char*, u16);
828 +
829 +#ifdef CONFIG_PM
830 +static int crisv10_hcd_hub_suspend(struct usb_hcd *);
831 +static int crisv10_hcd_hub_resume(struct usb_hcd *);
832 +#endif /* CONFIG_PM */
833 +#ifdef CONFIG_USB_OTG
834 +static int crisv10_hcd_start_port_reset(struct usb_hcd *, unsigned);
835 +#endif /* CONFIG_USB_OTG */
836 +
837 +/* host controller driver interface */
838 +static const struct hc_driver crisv10_hc_driver = 
839 +  {
840 +    .description =     hc_name,
841 +    .product_desc =    product_desc,
842 +    .hcd_priv_size =   sizeof(struct crisv10_hcd),
843 +
844 +    /* Attaching IRQ handler manualy in probe() */
845 +    /* .irq =          crisv10_hcd_irq, */
846 +
847 +    .flags =           HCD_USB11,
848 +
849 +    /* called to init HCD and root hub */
850 +    .reset =           crisv10_hcd_reset,
851 +    .start =           crisv10_hcd_start,      
852 +
853 +    /* cleanly make HCD stop writing memory and doing I/O */
854 +    .stop =            crisv10_hcd_stop,
855 +
856 +    /* return current frame number */
857 +    .get_frame_number =        crisv10_hcd_get_frame,
858 +
859 +
860 +    /* Manage i/o requests via the Transfer Controller */
861 +    .urb_enqueue =     tc_urb_enqueue,
862 +    .urb_dequeue =     tc_urb_dequeue,
863 +
864 +    /* hw synch, freeing endpoint resources that urb_dequeue can't */
865 +    .endpoint_disable = tc_endpoint_disable,
866 +
867 +
868 +    /* Root Hub support */
869 +    .hub_status_data = rh_status_data_request,
870 +    .hub_control =     rh_control_request,
871 +#ifdef CONFIG_PM
872 +    .hub_suspend =     rh_suspend_request,
873 +    .hub_resume =      rh_resume_request,
874 +#endif /* CONFIG_PM */
875 +#ifdef CONFIG_USB_OTG
876 +    .start_port_reset =        crisv10_hcd_start_port_reset,
877 +#endif /* CONFIG_USB_OTG */
878 +  };
879 +
880 +
881 +/*
882 + * conversion between pointers to a hcd and the corresponding
883 + * crisv10_hcd 
884 + */
885 +
886 +static inline struct crisv10_hcd *hcd_to_crisv10_hcd(struct usb_hcd *hcd)
887 +{
888 +       return (struct crisv10_hcd *) hcd->hcd_priv;
889 +}
890 +
891 +static inline struct usb_hcd *crisv10_hcd_to_hcd(struct crisv10_hcd *hcd)
892 +{
893 +       return container_of((void *) hcd, struct usb_hcd, hcd_priv);
894 +}
895 +
896 +/* check if specified port is in use */
897 +static inline int port_in_use(unsigned int port)
898 +{
899 +       return ports & (1 << port);
900 +}
901 +
902 +/* number of ports in use */
903 +static inline unsigned int num_ports(void)
904 +{
905 +       unsigned int i, num = 0;
906 +       for (i = 0; i < USB_ROOT_HUB_PORTS; i++)
907 +               if (port_in_use(i))
908 +                       num++;
909 +       return num;
910 +}
911 +
912 +/* map hub port number to the port number used internally by the HC */
913 +static inline unsigned int map_port(unsigned int port)
914 +{
915 +  unsigned int i, num = 0;
916 +  for (i = 0; i < USB_ROOT_HUB_PORTS; i++)
917 +    if (port_in_use(i))
918 +      if (++num == port)
919 +       return i;
920 +  return -1;
921 +}
922 +
923 +/* size of descriptors in slab cache */
924 +#ifndef MAX
925 +#define MAX(x, y)              ((x) > (y) ? (x) : (y))
926 +#endif
927 +
928 +
929 +/******************************************************************/
930 +/* Hardware Interrupt functions                                   */
931 +/******************************************************************/
932 +
933 +/* Fast interrupt handler for HC */
934 +static irqreturn_t crisv10_hcd_top_irq(int irq, void *vcd)
935 +{
936 +  struct usb_hcd *hcd = vcd;
937 +  struct crisv10_irq_reg reg;
938 +  __u32 irq_mask;
939 +  unsigned long flags;
940 +
941 +  DBFENTER;
942 +
943 +  ASSERT(hcd != NULL);
944 +  reg.hcd = hcd;
945 +
946 +  /* Turn of other interrupts while handling these sensitive cases */
947 +  local_irq_save(flags);
948 +  
949 +  /* Read out which interrupts that are flaged */
950 +  irq_mask = *R_USB_IRQ_MASK_READ;
951 +  reg.r_usb_irq_mask_read = irq_mask;
952 +
953 +  /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that
954 +     R_USB_STATUS must be read before R_USB_EPID_ATTN since reading the latter
955 +     clears the ourun and perror fields of R_USB_STATUS. */
956 +  reg.r_usb_status = *R_USB_STATUS;
957 +  
958 +  /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn
959 +     interrupts. */
960 +  reg.r_usb_epid_attn = *R_USB_EPID_ATTN;
961 +  
962 +  /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
963 +     port_status interrupt. */
964 +  reg.r_usb_rh_port_status_1 = *R_USB_RH_PORT_STATUS_1;
965 +  reg.r_usb_rh_port_status_2 = *R_USB_RH_PORT_STATUS_2;
966 +  
967 +  /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
968 +  /* Note: the lower 11 bits contain the actual frame number, sent with each
969 +     sof. */
970 +  reg.r_usb_fm_number = *R_USB_FM_NUMBER;
971 +
972 +  /* Interrupts are handled in order of priority. */
973 +  if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
974 +    crisv10_hcd_port_status_irq(&reg);
975 +  }
976 +  if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
977 +    crisv10_hcd_epid_attn_irq(&reg);
978 +  }
979 +  if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
980 +    crisv10_hcd_ctl_status_irq(&reg);
981 +  }
982 +  if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
983 +    crisv10_hcd_isoc_eof_irq(&reg);
984 +  }
985 +  if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
986 +    /* Update/restart the bulk start timer since obviously the channel is
987 +       running. */
988 +    mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
989 +    /* Update/restart the bulk eot timer since we just received an bulk eot
990 +       interrupt. */
991 +    mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
992 +
993 +    /* Check for finished bulk transfers on epids */
994 +    check_finished_bulk_tx_epids(hcd, 0);
995 +  }
996 +  local_irq_restore(flags);
997 +
998 +  DBFEXIT;
999 +  return IRQ_HANDLED;
1000 +}
1001 +
1002 +
1003 +void crisv10_hcd_epid_attn_irq(struct crisv10_irq_reg *reg) {
1004 +  struct usb_hcd *hcd = reg->hcd;
1005 +  struct crisv10_urb_priv *urb_priv;
1006 +  int epid;
1007 +  DBFENTER;
1008 +
1009 +  for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
1010 +    if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
1011 +      struct urb *urb;
1012 +      __u32 ept_data;
1013 +      int error_code;
1014 +
1015 +      if (epid == DUMMY_EPID || epid == INVALID_EPID) {
1016 +       /* We definitely don't care about these ones. Besides, they are
1017 +          always disabled, so any possible disabling caused by the
1018 +          epid attention interrupt is irrelevant. */
1019 +       warn("Got epid_attn for INVALID_EPID or DUMMY_EPID (%d).", epid);
1020 +       continue;
1021 +      }
1022 +
1023 +      if(!epid_inuse(epid)) {
1024 +       irq_err("Epid attention on epid:%d that isn't in use\n", epid);
1025 +       printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
1026 +       debug_epid(epid);
1027 +       continue;
1028 +      }
1029 +
1030 +      /* Note that although there are separate R_USB_EPT_DATA and
1031 +        R_USB_EPT_DATA_ISO registers, they are located at the same address and
1032 +        are of the same size. In other words, this read should be ok for isoc
1033 +        also. */
1034 +      ept_data = etrax_epid_get(epid);
1035 +      error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, ept_data);
1036 +
1037 +      /* Get the active URB for this epid. We blatantly assume
1038 +        that only this URB could have caused the epid attention. */
1039 +      urb = activeUrbList[epid];
1040 +      if (urb == NULL) {
1041 +       irq_err("Attention on epid:%d error:%d with no active URB.\n",
1042 +               epid, error_code);
1043 +       printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
1044 +       debug_epid(epid);
1045 +       continue;
1046 +      }
1047 +
1048 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
1049 +      ASSERT(urb_priv);
1050 +
1051 +      /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
1052 +      if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
1053 +
1054 +       /* Isoc traffic doesn't have error_count_in/error_count_out. */
1055 +       if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
1056 +           (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, ept_data) == 3 ||
1057 +            IO_EXTRACT(R_USB_EPT_DATA, error_count_out, ept_data) == 3)) {
1058 +         /* Check if URB allready is marked for late-finish, we can get
1059 +            several 3rd error for Intr traffic when a device is unplugged */
1060 +         if(urb_priv->later_data == NULL) {
1061 +           /* 3rd error. */
1062 +           irq_warn("3rd error for epid:%d (%s %s) URB:0x%x[%d]\n", epid,
1063 +                    str_dir(urb->pipe), str_type(urb->pipe),
1064 +                    (unsigned int)urb, urb_priv->urb_num);
1065 +         
1066 +           tc_finish_urb_later(hcd, urb, -EPROTO);
1067 +         }
1068 +
1069 +       } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
1070 +         irq_warn("Perror for epid:%d\n", epid);
1071 +         printk("FM_NUMBER: %d\n", reg->r_usb_fm_number & 0x7ff);
1072 +         printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
1073 +         __dump_urb(urb);
1074 +         debug_epid(epid);
1075 +
1076 +         if (!(ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
1077 +           /* invalid ep_id */
1078 +           panic("Perror because of invalid epid."
1079 +                 " Deconfigured too early?");
1080 +         } else {
1081 +           /* past eof1, near eof, zout transfer, setup transfer */
1082 +           /* Dump the urb and the relevant EP descriptor. */
1083 +           panic("Something wrong with DMA descriptor contents."
1084 +                 " Too much traffic inserted?");
1085 +         }
1086 +       } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
1087 +         /* buffer ourun */
1088 +         printk("FM_NUMBER: %d\n", reg->r_usb_fm_number & 0x7ff);
1089 +         printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
1090 +         __dump_urb(urb);
1091 +         debug_epid(epid);
1092 +
1093 +         panic("Buffer overrun/underrun for epid:%d. DMA too busy?", epid);
1094 +       } else {
1095 +         irq_warn("Attention on epid:%d (%s %s) with no error code\n", epid,
1096 +                  str_dir(urb->pipe), str_type(urb->pipe));
1097 +         printk("R_USB_STATUS: 0x%x\n", reg->r_usb_status);
1098 +         __dump_urb(urb);
1099 +         debug_epid(epid);
1100 +       }
1101 +
1102 +      } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
1103 +                                             stall)) {
1104 +       /* Not really a protocol error, just says that the endpoint gave
1105 +          a stall response. Note that error_code cannot be stall for isoc. */
1106 +       if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1107 +         panic("Isoc traffic cannot stall");
1108 +       }
1109 +
1110 +       tc_dbg("Stall for epid:%d (%s %s) URB:0x%x\n", epid,
1111 +              str_dir(urb->pipe), str_type(urb->pipe), (unsigned int)urb);
1112 +       tc_finish_urb(hcd, urb, -EPIPE);
1113 +
1114 +      } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
1115 +                                             bus_error)) {
1116 +       /* Two devices responded to a transaction request. Must be resolved
1117 +          by software. FIXME: Reset ports? */
1118 +       panic("Bus error for epid %d."
1119 +             " Two devices responded to transaction request\n",
1120 +             epid);
1121 +
1122 +      } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code,
1123 +                                             buffer_error)) {
1124 +       /* DMA overrun or underrun. */
1125 +       irq_warn("Buffer overrun/underrun for epid:%d (%s %s)\n", epid,
1126 +                str_dir(urb->pipe), str_type(urb->pipe));
1127 +
1128 +       /* It seems that error_code = buffer_error in
1129 +          R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
1130 +          are the same error. */
1131 +       tc_finish_urb(hcd, urb, -EPROTO);
1132 +      } else {
1133 +         irq_warn("Unknown attention on epid:%d (%s %s)\n", epid,
1134 +                  str_dir(urb->pipe), str_type(urb->pipe));
1135 +         dump_ept_data(epid);
1136 +      }
1137 +    }
1138 +  }
1139 +  DBFEXIT;
1140 +}
1141 +
1142 +void crisv10_hcd_port_status_irq(struct crisv10_irq_reg *reg)
1143 +{
1144 +  __u16 port_reg[USB_ROOT_HUB_PORTS];
1145 +  DBFENTER;
1146 +  port_reg[0] = reg->r_usb_rh_port_status_1;
1147 +  port_reg[1] = reg->r_usb_rh_port_status_2;
1148 +  rh_port_status_change(port_reg);
1149 +  DBFEXIT;
1150 +}
1151 +
1152 +void crisv10_hcd_isoc_eof_irq(struct crisv10_irq_reg *reg)
1153 +{
1154 +  int epid;
1155 +  struct urb *urb;
1156 +  struct crisv10_urb_priv *urb_priv;
1157 +
1158 +  DBFENTER;
1159 +
1160 +  for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1161 +
1162 +    /* Only check epids that are in use, is valid and has SB list */
1163 +    if (!epid_inuse(epid) || epid == INVALID_EPID ||
1164 +       TxIsocEPList[epid].sub == 0 || epid == DUMMY_EPID) {
1165 +      /* Nothing here to see. */
1166 +      continue;
1167 +    }
1168 +    ASSERT(epid_isoc(epid));
1169 +
1170 +    /* Get the active URB for this epid (if any). */
1171 +    urb = activeUrbList[epid];
1172 +    if (urb == 0) {
1173 +      isoc_warn("Ignoring NULL urb for epid:%d\n", epid);
1174 +      continue;
1175 +    }
1176 +    if(!epid_out_traffic(epid)) {
1177 +      /* Sanity check. */
1178 +      ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
1179 +
1180 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
1181 +      ASSERT(urb_priv);
1182 +
1183 +      if (urb_priv->urb_state == NOT_STARTED) {
1184 +       /* If ASAP is not set and urb->start_frame is the current frame,
1185 +          start the transfer. */
1186 +       if (!(urb->transfer_flags & URB_ISO_ASAP) &&
1187 +           (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
1188 +         /* EP should not be enabled if we're waiting for start_frame */
1189 +         ASSERT((TxIsocEPList[epid].command &
1190 +                 IO_STATE(USB_EP_command, enable, yes)) == 0);
1191 +
1192 +         isoc_warn("Enabling isoc IN EP descr for epid %d\n", epid);
1193 +         TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1194 +
1195 +         /* This urb is now active. */
1196 +         urb_priv->urb_state = STARTED;
1197 +         continue;
1198 +       }
1199 +      }
1200 +    }
1201 +  }
1202 +
1203 +  DBFEXIT;
1204 +}
1205 +
1206 +void crisv10_hcd_ctl_status_irq(struct crisv10_irq_reg *reg)
1207 +{
1208 +  struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(reg->hcd);
1209 +
1210 +  DBFENTER;
1211 +  ASSERT(crisv10_hcd);
1212 +
1213 +  irq_dbg("ctr_status_irq, controller status: %s\n",
1214 +         hcd_status_to_str(reg->r_usb_status));
1215 +  
1216 +  /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
1217 +     list for the corresponding epid? */
1218 +  if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
1219 +    panic("USB controller got ourun.");
1220 +  }
1221 +  if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
1222 +    
1223 +    /* Before, etrax_usb_do_intr_recover was called on this epid if it was
1224 +       an interrupt pipe. I don't see how re-enabling all EP descriptors
1225 +       will help if there was a programming error. */
1226 +    panic("USB controller got perror.");
1227 +  }
1228 +
1229 +  /* Keep track of USB Controller, if it's running or not */
1230 +  if(reg->r_usb_status & IO_STATE(R_USB_STATUS, running, yes)) {
1231 +    crisv10_hcd->running = 1;
1232 +  } else {
1233 +    crisv10_hcd->running = 0;
1234 +  }
1235 +  
1236 +  if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
1237 +    /* We should never operate in device mode. */
1238 +    panic("USB controller in device mode.");
1239 +  }
1240 +
1241 +  /* Set the flag to avoid getting "Unlink after no-IRQ? Controller is probably
1242 +     using the wrong IRQ" from hcd_unlink_urb() in drivers/usb/core/hcd.c */
1243 +  set_bit(HCD_FLAG_SAW_IRQ, &reg->hcd->flags);
1244 +  
1245 +  DBFEXIT;
1246 +}
1247 +
1248 +
1249 +/******************************************************************/
1250 +/* Host Controller interface functions                            */
1251 +/******************************************************************/
1252 +
1253 +static inline void crisv10_ready_wait(void) {
1254 +  volatile int timeout = 10000;
1255 +  /* Check the busy bit of USB controller in Etrax */
1256 +  while((*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy)) &&
1257 +       (timeout-- > 0));
1258 +  if(timeout == 0) {
1259 +    warn("Timeout while waiting for USB controller to be idle\n");
1260 +  }
1261 +}
1262 +
1263 +/* reset host controller */
1264 +static int crisv10_hcd_reset(struct usb_hcd *hcd)
1265 +{
1266 +  DBFENTER;
1267 +  hcd_dbg(hcd, "reset\n");
1268 +
1269 +
1270 +  /* Reset the USB interface. */
1271 +  /*
1272 +  *R_USB_COMMAND =
1273 +    IO_STATE(R_USB_COMMAND, port_sel, nop) |
1274 +    IO_STATE(R_USB_COMMAND, port_cmd, reset) |
1275 +    IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
1276 +  nop();
1277 +  */
1278 +  DBFEXIT;
1279 +  return 0;
1280 +}
1281 +
1282 +/* start host controller */
1283 +static int crisv10_hcd_start(struct usb_hcd *hcd)
1284 +{
1285 +  DBFENTER;
1286 +  hcd_dbg(hcd, "start\n");
1287 +
1288 +  crisv10_ready_wait();
1289 +
1290 +  /* Start processing of USB traffic. */
1291 +  *R_USB_COMMAND =
1292 +    IO_STATE(R_USB_COMMAND, port_sel, nop) |
1293 +    IO_STATE(R_USB_COMMAND, port_cmd, reset) |
1294 +    IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
1295 +
1296 +  nop();
1297 +
1298 +  hcd->state = HC_STATE_RUNNING;
1299 +
1300 +  DBFEXIT;
1301 +  return 0;
1302 +}
1303 +
1304 +/* stop host controller */
1305 +static void crisv10_hcd_stop(struct usb_hcd *hcd)
1306 +{
1307 +  DBFENTER;
1308 +  hcd_dbg(hcd, "stop\n");
1309 +  crisv10_hcd_reset(hcd);
1310 +  DBFEXIT;
1311 +}
1312 +
1313 +/* return the current frame number */
1314 +static int crisv10_hcd_get_frame(struct usb_hcd *hcd)
1315 +{
1316 +  DBFENTER;
1317 +  DBFEXIT;
1318 +  return (*R_USB_FM_NUMBER & 0x7ff);
1319 +}
1320 +
1321 +#ifdef CONFIG_USB_OTG
1322 +
1323 +static int crisv10_hcd_start_port_reset(struct usb_hcd *hcd, unsigned port)
1324 +{
1325 +  return 0; /* no-op for now */
1326 +}
1327 +
1328 +#endif /* CONFIG_USB_OTG */
1329 +
1330 +
1331 +/******************************************************************/
1332 +/* Root Hub functions                                             */
1333 +/******************************************************************/
1334 +
1335 +/* root hub status */
1336 +static const struct usb_hub_status rh_hub_status = 
1337 +  {
1338 +    .wHubStatus =              0,
1339 +    .wHubChange =              0,
1340 +  };
1341 +
1342 +/* root hub descriptor */
1343 +static const u8 rh_hub_descr[] =
1344 +  {
1345 +    0x09,                      /* bDescLength         */
1346 +    0x29,                      /* bDescriptorType     */
1347 +    USB_ROOT_HUB_PORTS,         /* bNbrPorts          */
1348 +    0x00,                      /* wHubCharacteristics */
1349 +    0x00,               
1350 +    0x01,                      /* bPwrOn2pwrGood      */
1351 +    0x00,                      /* bHubContrCurrent    */
1352 +    0x00,                      /* DeviceRemovable     */
1353 +    0xff                       /* PortPwrCtrlMask     */
1354 +  };
1355 +
1356 +/* Actual holder of root hub status*/
1357 +struct crisv10_rh rh;
1358 +
1359 +/* Initialize root hub data structures (called from dvdrv_hcd_probe()) */
1360 +int rh_init(void) {
1361 +  int i;
1362 +  /* Reset port status flags */
1363 +  for (i = 0; i < USB_ROOT_HUB_PORTS; i++) {
1364 +    rh.wPortChange[i] = 0;
1365 +    rh.wPortStatusPrev[i] = 0;
1366 +  }
1367 +  return 0;
1368 +}
1369 +
1370 +#define RH_FEAT_MASK ((1<<USB_PORT_FEAT_CONNECTION)|\
1371 +                     (1<<USB_PORT_FEAT_ENABLE)|\
1372 +                     (1<<USB_PORT_FEAT_SUSPEND)|\
1373 +                     (1<<USB_PORT_FEAT_RESET))
1374 +
1375 +/* Handle port status change interrupt (called from bottom part interrupt) */
1376 +void rh_port_status_change(__u16 port_reg[]) {
1377 +  int i;
1378 +  __u16 wChange;
1379 +
1380 +  for(i = 0; i < USB_ROOT_HUB_PORTS; i++) {
1381 +    /* Xor out changes since last read, masked for important flags */
1382 +    wChange = (port_reg[i] & RH_FEAT_MASK) ^ rh.wPortStatusPrev[i];
1383 +    /* Or changes together with (if any) saved changes */
1384 +    rh.wPortChange[i] |= wChange;
1385 +    /* Save new status */
1386 +    rh.wPortStatusPrev[i] = port_reg[i];
1387 +
1388 +    if(wChange) {
1389 +      rh_dbg("Interrupt port_status change port%d: %s  Current-status:%s\n", i+1,
1390 +            port_status_to_str(wChange),
1391 +            port_status_to_str(port_reg[i]));
1392 +    }
1393 +  }
1394 +}
1395 +
1396 +/* Construct port status change bitmap for the root hub */
1397 +static int rh_status_data_request(struct usb_hcd *hcd, char *buf)
1398 +{
1399 +  struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
1400 +  unsigned int i;
1401 +
1402 +//  DBFENTER;
1403 +  
1404 +  /*
1405 +   * corresponds to hub status change EP (USB 2.0 spec section 11.13.4)
1406 +   * return bitmap indicating ports with status change
1407 +   */
1408 +  *buf = 0;
1409 +  spin_lock(&crisv10_hcd->lock);
1410 +  for (i = 1; i <= crisv10_hcd->num_ports; i++) {
1411 +    if (rh.wPortChange[map_port(i)]) {
1412 +      *buf |= (1 << i);
1413 +      rh_dbg("rh_status_data_request, change on port %d: %s  Current Status: %s\n", i,
1414 +            port_status_to_str(rh.wPortChange[map_port(i)]),
1415 +            port_status_to_str(rh.wPortStatusPrev[map_port(i)]));
1416 +    }
1417 +  }
1418 +  spin_unlock(&crisv10_hcd->lock);
1419 +
1420 +// DBFEXIT;
1421 +
1422 +  return *buf == 0 ? 0 : 1;
1423 +}
1424 +
1425 +/* Handle a control request for the root hub (called from hcd_driver) */
1426 +static int rh_control_request(struct usb_hcd *hcd, 
1427 +                             u16 typeReq, 
1428 +                             u16 wValue, 
1429 +                             u16 wIndex,
1430 +                             char *buf, 
1431 +                             u16 wLength) {
1432 +
1433 +  struct crisv10_hcd *crisv10_hcd = hcd_to_crisv10_hcd(hcd);
1434 +  int retval = 0;
1435 +  int len;
1436 +  DBFENTER;
1437 +
1438 +  switch (typeReq) {
1439 +  case GetHubDescriptor:
1440 +    rh_dbg("GetHubDescriptor\n");
1441 +    len = min_t(unsigned int, sizeof rh_hub_descr, wLength);
1442 +    memcpy(buf, rh_hub_descr, len);
1443 +    buf[2] = crisv10_hcd->num_ports;
1444 +    break;
1445 +  case GetHubStatus:
1446 +    rh_dbg("GetHubStatus\n");
1447 +    len = min_t(unsigned int, sizeof rh_hub_status, wLength);
1448 +    memcpy(buf, &rh_hub_status, len);
1449 +    break;
1450 +  case GetPortStatus:
1451 +    if (!wIndex || wIndex > crisv10_hcd->num_ports)
1452 +      goto error;
1453 +    rh_dbg("GetportStatus, port:%d change:%s  status:%s\n", wIndex,
1454 +          port_status_to_str(rh.wPortChange[map_port(wIndex)]),
1455 +          port_status_to_str(rh.wPortStatusPrev[map_port(wIndex)]));
1456 +    *(u16 *) buf = cpu_to_le16(rh.wPortStatusPrev[map_port(wIndex)]);
1457 +    *(u16 *) (buf + 2) = cpu_to_le16(rh.wPortChange[map_port(wIndex)]);
1458 +    break;
1459 +  case SetHubFeature:
1460 +    rh_dbg("SetHubFeature\n");
1461 +  case ClearHubFeature:
1462 +    rh_dbg("ClearHubFeature\n");
1463 +    switch (wValue) {
1464 +    case C_HUB_OVER_CURRENT:
1465 +    case C_HUB_LOCAL_POWER:
1466 +      rh_warn("Not implemented hub request:%d \n", typeReq);
1467 +      /* not implemented */
1468 +      break;
1469 +    default:
1470 +      goto error;
1471 +    }
1472 +    break;
1473 +  case SetPortFeature:
1474 +    if (!wIndex || wIndex > crisv10_hcd->num_ports)
1475 +      goto error;
1476 +    if(rh_set_port_feature(map_port(wIndex), wValue))
1477 +      goto error;
1478 +    break;
1479 +  case ClearPortFeature:
1480 +    if (!wIndex || wIndex > crisv10_hcd->num_ports)
1481 +      goto error;
1482 +    if(rh_clear_port_feature(map_port(wIndex), wValue))
1483 +      goto error;
1484 +    break;
1485 +  default:
1486 +    rh_warn("Unknown hub request: %d\n", typeReq);
1487 +  error:
1488 +    retval = -EPIPE;
1489 +  }
1490 +  DBFEXIT;
1491 +  return retval;
1492 +}
1493 +
1494 +int rh_set_port_feature(__u8 bPort, __u16 wFeature) {
1495 +  __u8 bUsbCommand = 0;
1496 +  switch(wFeature) {
1497 +  case USB_PORT_FEAT_RESET:
1498 +    rh_dbg("SetPortFeature: reset\n");
1499 +    bUsbCommand |= IO_STATE(R_USB_COMMAND, port_cmd, reset);
1500 +    goto set;
1501 +    break;
1502 +  case USB_PORT_FEAT_SUSPEND:
1503 +    rh_dbg("SetPortFeature: suspend\n");
1504 +    bUsbCommand |= IO_STATE(R_USB_COMMAND, port_cmd, suspend);
1505 +    goto set;
1506 +    break;
1507 +  case USB_PORT_FEAT_POWER:
1508 +    rh_dbg("SetPortFeature: power\n");
1509 +    break;
1510 +  case USB_PORT_FEAT_C_CONNECTION:
1511 +    rh_dbg("SetPortFeature: c_connection\n");
1512 +    break;
1513 +  case USB_PORT_FEAT_C_RESET:
1514 +    rh_dbg("SetPortFeature: c_reset\n");
1515 +    break;
1516 +  case USB_PORT_FEAT_C_OVER_CURRENT:
1517 +    rh_dbg("SetPortFeature: c_over_current\n");
1518 +    break;
1519 +
1520 +  set:
1521 +    /* Select which port via the port_sel field */
1522 +    bUsbCommand |= IO_FIELD(R_USB_COMMAND, port_sel, bPort+1);
1523 +
1524 +    /* Make sure the controller isn't busy. */
1525 +    crisv10_ready_wait();
1526 +    /* Send out the actual command to the USB controller */
1527 +    *R_USB_COMMAND = bUsbCommand;
1528 +
1529 +    /* If port reset then also bring USB controller into running state */
1530 +    if(wFeature == USB_PORT_FEAT_RESET) {
1531 +      /* Wait a while for controller to first become started after port reset */
1532 +      udelay(12000); /* 12ms blocking wait */
1533 +      
1534 +      /* Make sure the controller isn't busy. */
1535 +      crisv10_ready_wait();
1536 +
1537 +      /* If all enabled ports were disabled the host controller goes down into
1538 +        started mode, so we need to bring it back into the running state.
1539 +        (This is safe even if it's already in the running state.) */
1540 +      *R_USB_COMMAND =
1541 +       IO_STATE(R_USB_COMMAND, port_sel, nop) |
1542 +       IO_STATE(R_USB_COMMAND, port_cmd, reset) |
1543 +       IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
1544 +    }
1545 +
1546 +    break;
1547 +  default:
1548 +    rh_dbg("SetPortFeature: unknown feature\n");
1549 +    return -1;
1550 +  }
1551 +  return 0;
1552 +}
1553 +
1554 +int rh_clear_port_feature(__u8 bPort, __u16 wFeature) {
1555 +  switch(wFeature) {
1556 +  case USB_PORT_FEAT_ENABLE:
1557 +    rh_dbg("ClearPortFeature: enable\n");
1558 +    rh_disable_port(bPort);
1559 +    break;
1560 +  case USB_PORT_FEAT_SUSPEND:
1561 +    rh_dbg("ClearPortFeature: suspend\n");
1562 +    break;
1563 +  case USB_PORT_FEAT_POWER:
1564 +    rh_dbg("ClearPortFeature: power\n");
1565 +    break;
1566 +
1567 +  case USB_PORT_FEAT_C_ENABLE:
1568 +    rh_dbg("ClearPortFeature: c_enable\n");
1569 +    goto clear;
1570 +  case USB_PORT_FEAT_C_SUSPEND:
1571 +    rh_dbg("ClearPortFeature: c_suspend\n");
1572 +    goto clear;
1573 +  case USB_PORT_FEAT_C_CONNECTION:
1574 +    rh_dbg("ClearPortFeature: c_connection\n");
1575 +    goto clear;
1576 +  case USB_PORT_FEAT_C_OVER_CURRENT:
1577 +    rh_dbg("ClearPortFeature: c_over_current\n");
1578 +    goto clear;
1579 +  case USB_PORT_FEAT_C_RESET:
1580 +    rh_dbg("ClearPortFeature: c_reset\n");
1581 +    goto clear;
1582 +  clear:
1583 +    rh.wPortChange[bPort] &= ~(1 << (wFeature - 16));
1584 +    break;
1585 +  default:
1586 +    rh_dbg("ClearPortFeature: unknown feature\n");
1587 +    return -1;
1588 +  }
1589 +  return 0;
1590 +}
1591 +
1592 +
1593 +#ifdef CONFIG_PM
1594 +/* Handle a suspend request for the root hub (called from hcd_driver) */
1595 +static int rh_suspend_request(struct usb_hcd *hcd)
1596 +{
1597 +  return 0; /* no-op for now */
1598 +}
1599 +
1600 +/* Handle a resume request for the root hub (called from hcd_driver) */
1601 +static int rh_resume_request(struct usb_hcd *hcd)
1602 +{
1603 +  return 0; /* no-op for now */
1604 +}
1605 +#endif /* CONFIG_PM */
1606 +
1607 +
1608 +
1609 +/* Wrapper function for workaround port disable registers in USB controller  */
1610 +static void rh_disable_port(unsigned int port) {
1611 +  volatile int timeout = 10000;
1612 +  volatile char* usb_portx_disable;
1613 +  switch(port) {
1614 +  case 0:
1615 +    usb_portx_disable = R_USB_PORT1_DISABLE;
1616 +    break;
1617 +  case 1:
1618 +    usb_portx_disable = R_USB_PORT2_DISABLE;
1619 +    break;
1620 +  default:
1621 +    /* Invalid port index */
1622 +    return;
1623 +  }
1624 +  /* Set disable flag in special register  */
1625 +  *usb_portx_disable = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
1626 +  /* Wait until not enabled anymore */
1627 +  while((rh.wPortStatusPrev[port] &
1628 +       IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes)) &&
1629 +       (timeout-- > 0));
1630 +  if(timeout == 0) {
1631 +    warn("Timeout while waiting for port %d to become disabled\n", port);
1632 +  }
1633 +  /* clear disable flag in special register  */
1634 +  *usb_portx_disable = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
1635 +  rh_info("Physical port %d disabled\n", port+1);
1636 +}
1637 +
1638 +
1639 +/******************************************************************/
1640 +/* Transfer Controller (TC) functions                             */
1641 +/******************************************************************/
1642 +
1643 +/* FIXME: Should RX_BUF_SIZE be a config option, or maybe we should adjust it
1644 +   dynamically?
1645 +   To adjust it dynamically we would have to get an interrupt when we reach
1646 +   the end of the rx descriptor list, or when we get close to the end, and
1647 +   then allocate more descriptors. */
1648 +#define NBR_OF_RX_DESC     512
1649 +#define RX_DESC_BUF_SIZE   1024
1650 +#define RX_BUF_SIZE        (NBR_OF_RX_DESC * RX_DESC_BUF_SIZE)
1651 +
1652 +
1653 +/* Local variables for Transfer Controller */
1654 +/* --------------------------------------- */
1655 +
1656 +/* This is a circular (double-linked) list of the active urbs for each epid.
1657 +   The head is never removed, and new urbs are linked onto the list as
1658 +   urb_entry_t elements. Don't reference urb_list directly; use the wrapper
1659 +   functions instead (which includes spin_locks) */
1660 +static struct list_head urb_list[NBR_OF_EPIDS];
1661 +
1662 +/* Read about the need and usage of this lock in submit_ctrl_urb. */
1663 +/* Lock for URB lists for each EPID */
1664 +static spinlock_t urb_list_lock;
1665 +
1666 +/* Lock for EPID array register (R_USB_EPT_x) in Etrax */
1667 +static spinlock_t etrax_epid_lock;
1668 +
1669 +/* Lock for dma8 sub0 handling */
1670 +static spinlock_t etrax_dma8_sub0_lock;
1671 +
1672 +/* DMA IN cache bug. Align the DMA IN buffers to 32 bytes, i.e. a cache line.
1673 +   Since RX_DESC_BUF_SIZE is 1024 is a multiple of 32, all rx buffers will be
1674 +   cache aligned. */
1675 +static volatile unsigned char RxBuf[RX_BUF_SIZE] __attribute__ ((aligned (32)));
1676 +static volatile struct USB_IN_Desc RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned (4)));
1677 +
1678 +/* Pointers into RxDescList. */
1679 +static volatile struct USB_IN_Desc *myNextRxDesc;
1680 +static volatile struct USB_IN_Desc *myLastRxDesc;
1681 +
1682 +/* A zout transfer makes a memory access at the address of its buf pointer,
1683 +   which means that setting this buf pointer to 0 will cause an access to the
1684 +   flash. In addition to this, setting sw_len to 0 results in a 16/32 bytes
1685 +   (depending on DMA burst size) transfer.
1686 +   Instead, we set it to 1, and point it to this buffer. */
1687 +static int zout_buffer[4] __attribute__ ((aligned (4)));
1688 +
1689 +/* Cache for allocating new EP and SB descriptors. */
1690 +//static kmem_cache_t *usb_desc_cache;
1691 +static struct kmem_cache *usb_desc_cache;
1692 +
1693 +/* Cache for the data allocated in the isoc descr top half. */
1694 +//static kmem_cache_t *isoc_compl_cache;
1695 +static struct kmem_cache *isoc_compl_cache;
1696 +
1697 +/* Cache for the data allocated when delayed finishing of URBs */
1698 +//static kmem_cache_t *later_data_cache;
1699 +static struct kmem_cache *later_data_cache;
1700 +
1701 +/* Counter to keep track of how many Isoc EP we have sat up. Used to enable
1702 +   and disable iso_eof interrupt. We only need these interrupts when we have
1703 +   Isoc data endpoints (consumes CPU cycles).
1704 +   FIXME: This could be more fine granular, so this interrupt is only enabled
1705 +   when we have a In Isoc URB not URB_ISO_ASAP flaged queued. */
1706 +static int isoc_epid_counter;
1707 +
1708 +/* Protecting wrapper functions for R_USB_EPT_x */
1709 +/* -------------------------------------------- */
1710 +static inline void etrax_epid_set(__u8 index, __u32 data) {
1711 +  unsigned long flags;
1712 +  spin_lock_irqsave(&etrax_epid_lock, flags);
1713 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
1714 +  nop();
1715 +  *R_USB_EPT_DATA = data;
1716 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
1717 +}
1718 +
1719 +static inline void etrax_epid_clear_error(__u8 index) {
1720 +  unsigned long flags;
1721 +  spin_lock_irqsave(&etrax_epid_lock, flags);
1722 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
1723 +  nop();
1724 +  *R_USB_EPT_DATA &=
1725 +    ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
1726 +      IO_MASK(R_USB_EPT_DATA, error_count_out) |
1727 +      IO_MASK(R_USB_EPT_DATA, error_code));
1728 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
1729 +}
1730 +
1731 +static inline void etrax_epid_set_toggle(__u8 index, __u8 dirout,
1732 +                                             __u8 toggle) {
1733 +  unsigned long flags;
1734 +  spin_lock_irqsave(&etrax_epid_lock, flags);
1735 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
1736 +  nop();
1737 +  if(dirout) {
1738 +    *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
1739 +    *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
1740 +  } else {
1741 +    *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
1742 +    *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
1743 +  }
1744 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
1745 +}
1746 +
1747 +static inline __u8 etrax_epid_get_toggle(__u8 index, __u8 dirout) {
1748 +  unsigned long flags;
1749 +  __u8 toggle;
1750 +  spin_lock_irqsave(&etrax_epid_lock, flags);
1751 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
1752 +  nop();
1753 +  if (dirout) {
1754 +    toggle = IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
1755 +  } else {
1756 +    toggle = IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
1757 +  }
1758 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
1759 +  return toggle;
1760 +}
1761 +
1762 +
1763 +static inline __u32 etrax_epid_get(__u8 index) {
1764 +  unsigned long flags;
1765 +  __u32 data;
1766 +  spin_lock_irqsave(&etrax_epid_lock, flags);
1767 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, index);
1768 +  nop();
1769 +  data = *R_USB_EPT_DATA;
1770 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
1771 +  return data;
1772 +}
1773 +
1774 +
1775 +
1776 +
1777 +/* Main functions for Transfer Controller */
1778 +/* -------------------------------------- */
1779 +
1780 +/* Init structs, memories and lists used by Transfer Controller */
1781 +int tc_init(struct usb_hcd *hcd) {
1782 +  int i;
1783 +  /* Clear software state info for all epids */
1784 +  memset(epid_state, 0, sizeof(struct etrax_epid) * NBR_OF_EPIDS);
1785 +
1786 +  /* Set Invalid and Dummy as being in use and disabled */
1787 +  epid_state[INVALID_EPID].inuse = 1;
1788 +  epid_state[DUMMY_EPID].inuse = 1;
1789 +  epid_state[INVALID_EPID].disabled = 1;
1790 +  epid_state[DUMMY_EPID].disabled = 1;
1791 +
1792 +  /* Clear counter for how many Isoc epids we have sat up */
1793 +  isoc_epid_counter = 0;
1794 +
1795 +  /* Initialize the urb list by initiating a head for each list.
1796 +     Also reset list hodling active URB for each epid */
1797 +  for (i = 0; i < NBR_OF_EPIDS; i++) {
1798 +    INIT_LIST_HEAD(&urb_list[i]);
1799 +    activeUrbList[i] = NULL;
1800 +  }
1801 +
1802 +  /* Init lock for URB lists */
1803 +  spin_lock_init(&urb_list_lock);
1804 +  /* Init lock for Etrax R_USB_EPT register */
1805 +  spin_lock_init(&etrax_epid_lock);
1806 +  /* Init lock for Etrax dma8 sub0 handling */
1807 +  spin_lock_init(&etrax_dma8_sub0_lock);
1808 +
1809 +  /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
1810 +
1811 +  /* Note that we specify sizeof(struct USB_EP_Desc) as the size, but also
1812 +     allocate SB descriptors from this cache. This is ok since
1813 +     sizeof(struct USB_EP_Desc) == sizeof(struct USB_SB_Desc). */
1814 +//  usb_desc_cache = kmem_cache_create("usb_desc_cache",
1815 +//                                  sizeof(struct USB_EP_Desc), 0,
1816 +//                                  SLAB_HWCACHE_ALIGN, 0, 0);
1817 +  usb_desc_cache = kmem_cache_create(
1818 +                                 "usb_desc_cache",
1819 +                                 sizeof(struct USB_EP_Desc),
1820 +                                 0,
1821 +                                 SLAB_HWCACHE_ALIGN,
1822 +                                 NULL);
1823 +  if(usb_desc_cache == NULL) {
1824 +    return -ENOMEM;
1825 +  }
1826 +
1827 +  /* Create slab cache for speedy allocation of memory for isoc bottom-half
1828 +     interrupt handling */
1829 +//  isoc_compl_cache =
1830 +//    kmem_cache_create("isoc_compl_cache",
1831 +//                   sizeof(struct crisv10_isoc_complete_data),
1832 +//                   0, SLAB_HWCACHE_ALIGN, 0, 0);
1833 +  isoc_compl_cache = kmem_cache_create(
1834 +                                 "isoc_compl_cache",
1835 +                                 sizeof(struct crisv10_isoc_complete_data),
1836 +                                 0,
1837 +                                 SLAB_HWCACHE_ALIGN,
1838 +                                 NULL
1839 +                                 );
1840 +
1841 +  if(isoc_compl_cache == NULL) {
1842 +    return -ENOMEM;
1843 +  }
1844 +
1845 +  /* Create slab cache for speedy allocation of memory for later URB finish
1846 +     struct */
1847 +//  later_data_cache =
1848 +//    kmem_cache_create("later_data_cache",
1849 +//                   sizeof(struct urb_later_data),
1850 +//                   0, SLAB_HWCACHE_ALIGN, 0, 0);
1851 +
1852 +  later_data_cache = kmem_cache_create(
1853 +                                 "later_data_cache",
1854 +                                 sizeof(struct urb_later_data),
1855 +                                 0,
1856 +                                 SLAB_HWCACHE_ALIGN,
1857 +                                 NULL
1858 +                                 );
1859 +  
1860 +  if(later_data_cache == NULL) {
1861 +    return -ENOMEM;
1862 +  }
1863 +
1864 +
1865 +  /* Initiate the bulk start timer. */
1866 +  init_timer(&bulk_start_timer);
1867 +  bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
1868 +  bulk_start_timer.function = tc_bulk_start_timer_func;
1869 +  add_timer(&bulk_start_timer);
1870 +
1871 +
1872 +  /* Initiate the bulk eot timer. */
1873 +  init_timer(&bulk_eot_timer);
1874 +  bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
1875 +  bulk_eot_timer.function = tc_bulk_eot_timer_func;
1876 +  bulk_eot_timer.data = (unsigned long)hcd;
1877 +  add_timer(&bulk_eot_timer);
1878 +
1879 +  return 0;
1880 +}
1881 +
1882 +/* Uninitialize all resources used by Transfer Controller */
1883 +void tc_destroy(void) {
1884 +
1885 +  /* Destroy all slab cache */
1886 +  kmem_cache_destroy(usb_desc_cache);
1887 +  kmem_cache_destroy(isoc_compl_cache);
1888 +  kmem_cache_destroy(later_data_cache);
1889 +
1890 +  /* Remove timers */
1891 +  del_timer(&bulk_start_timer);
1892 +  del_timer(&bulk_eot_timer);
1893 +}
1894 +
1895 +static void restart_dma8_sub0(void) {
1896 +  unsigned long flags;
1897 +  spin_lock_irqsave(&etrax_dma8_sub0_lock, flags);
1898 +  /* Verify that the dma is not running */
1899 +  if ((*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd)) == 0) {
1900 +    struct USB_EP_Desc *ep = (struct USB_EP_Desc *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
1901 +    while (DUMMY_EPID == IO_EXTRACT(USB_EP_command, epid, ep->command)) {
1902 +      ep = (struct USB_EP_Desc *)phys_to_virt(ep->next);
1903 +    }
1904 +    /* Advance the DMA to the next EP descriptor that is not a DUMMY_EPID.
1905 +        * ep->next is already a physical address. virt_to_phys is needed, see
1906 +        * http://mhonarc.axis.se/dev-etrax/msg08630.html
1907 +        */
1908 +    //*R_DMA_CH8_SUB0_EP = ep->next;
1909 +       *R_DMA_CH8_SUB0_EP = virt_to_phys(ep);
1910 +    /* Restart the DMA */
1911 +    *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1912 +  }
1913 +  spin_unlock_irqrestore(&etrax_dma8_sub0_lock, flags);
1914 +}
1915 +
1916 +/* queue an URB with the transfer controller (called from hcd_driver) */
1917 +//static int tc_urb_enqueue(struct usb_hcd *hcd, 
1918 +//                       struct usb_host_endpoint *ep,
1919 +//                       struct urb *urb, 
1920 +//                       gfp_t mem_flags) {
1921 +static int tc_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1922 +{
1923 +  int epid;
1924 +  int retval;
1925 +//  int bustime = 0;
1926 +  int maxpacket;
1927 +  unsigned long flags;
1928 +  struct crisv10_urb_priv *urb_priv;
1929 +  struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
1930 +  DBFENTER;
1931 +
1932 +  if(!(crisv10_hcd->running)) {
1933 +    /* The USB Controller is not running, probably because no device is 
1934 +       attached. No idea to enqueue URBs then */
1935 +    tc_warn("Rejected enqueueing of URB:0x%x because no dev attached\n",
1936 +           (unsigned int)urb);
1937 +    return -ENOENT;
1938 +  }
1939 +
1940 +  maxpacket = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1941 +  
1942 +  /* hinko ignore usb_pipeisoc */
1943 +#if 0
1944 +  /* Special case check for In Isoc transfers. Specification states that each
1945 +     In Isoc transfer consists of one packet and therefore it should fit into
1946 +     the transfer-buffer of an URB.
1947 +     We do the check here to be sure (an invalid scenario can be produced with
1948 +     parameters to the usbtest suite) */
1949 +  if(usb_pipeisoc(urb->pipe) && usb_pipein(urb->pipe) &&
1950 +     (urb->transfer_buffer_length < maxpacket)) {
1951 +    tc_err("Submit In Isoc URB with buffer length:%d to pipe with maxpacketlen: %d\n", urb->transfer_buffer_length, maxpacket);
1952 +    return -EMSGSIZE;
1953 +  }
1954 +
1955 +  /* Check if there is enough bandwidth for periodic transfer  */
1956 +  if(usb_pipeint(urb->pipe) || usb_pipeisoc(urb->pipe)) {
1957 +    /* only check (and later claim) if not already claimed */
1958 +    if (urb->bandwidth == 0) {
1959 +      bustime = usb_check_bandwidth(urb->dev, urb);
1960 +      if (bustime < 0) {
1961 +       tc_err("Not enough periodic bandwidth\n");
1962 +       return -ENOSPC;
1963 +      }
1964 +    }
1965 +  }
1966 +#endif
1967 +  
1968 +  /* Check if there is a epid for URBs destination, if not this function
1969 +     set up one. */
1970 +  //epid = tc_setup_epid(ep, urb, mem_flags);
1971 +  epid = tc_setup_epid(urb, mem_flags);
1972 +  if (epid < 0) {
1973 +    tc_err("Failed setup epid:%d for URB:0x%x\n", epid, (unsigned int)urb);
1974 +    DBFEXIT;
1975 +    return -ENOMEM;
1976 +  }
1977 +
1978 +  if(urb == activeUrbList[epid]) {
1979 +    tc_err("Resubmition of allready active URB:0x%x\n", (unsigned int)urb);
1980 +    return -ENXIO;
1981 +  }
1982 +
1983 +  if(urb_list_entry(urb, epid)) {
1984 +    tc_err("Resubmition of allready queued URB:0x%x\n", (unsigned int)urb);
1985 +    return -ENXIO;
1986 +  }
1987 +
1988 +  /* If we actively have flaged endpoint as disabled then refuse submition */
1989 +  if(epid_state[epid].disabled) {
1990 +    return -ENOENT;
1991 +  }
1992 +
1993 +  /* Allocate and init HC-private data for URB */
1994 +  if(urb_priv_create(hcd, urb, epid, mem_flags) != 0) {
1995 +    DBFEXIT;
1996 +    return -ENOMEM;
1997 +  }
1998 +  urb_priv = urb->hcpriv;
1999 +
2000 +  tc_dbg("Enqueue URB:0x%x[%d] epid:%d (%s) bufflen:%d\n",
2001 +        (unsigned int)urb, urb_priv->urb_num, epid,
2002 +        pipe_to_str(urb->pipe), urb->transfer_buffer_length);
2003 +
2004 +  /* Create and link SBs required for this URB */
2005 +  retval = create_sb_for_urb(urb, mem_flags);
2006 +  if(retval != 0) {
2007 +    tc_err("Failed to create SBs for URB:0x%x[%d]\n", (unsigned int)urb,
2008 +          urb_priv->urb_num);
2009 +    urb_priv_free(hcd, urb);
2010 +    DBFEXIT;
2011 +    return retval;
2012 +  }
2013 +
2014 +  /* Init intr EP pool if this URB is a INTR transfer. This pool is later
2015 +     used when inserting EPs in the TxIntrEPList. We do the alloc here
2016 +     so we can't run out of memory later */
2017 +  if(usb_pipeint(urb->pipe)) {
2018 +    retval = init_intr_urb(urb, mem_flags);
2019 +    if(retval != 0) {
2020 +      tc_warn("Failed to init Intr URB\n");
2021 +      urb_priv_free(hcd, urb);
2022 +      DBFEXIT;
2023 +      return retval;
2024 +    }
2025 +  }
2026 +
2027 +  /* Disable other access when inserting USB */
2028 +
2029 +  /* BUG on sleeping inside int disabled if using local_irq_save/local_irq_restore
2030 +   * her - because urb_list_add() and tc_dma_process_queue() save irqs again !??!
2031 +   */
2032 +//  local_irq_save(flags);
2033 +
2034 +  /* hinko ignore usb_pipeisoc */
2035 +#if 0
2036 +  /* Claim bandwidth, if needed */
2037 +  if(bustime) {
2038 +    usb_claim_bandwidth(urb->dev, urb, bustime, 0);
2039 +  }
2040 +  
2041 +  /* Add URB to EP queue */
2042 +  urb_list_add(urb, epid, mem_flags);
2043 +
2044 +  if(usb_pipeisoc(urb->pipe)) {
2045 +    /* Special processing of Isoc URBs. */
2046 +    tc_dma_process_isoc_urb(urb);
2047 +  } else {
2048 +    /* Process EP queue for rest of the URB types (Bulk, Ctrl, Intr) */
2049 +    tc_dma_process_queue(epid);
2050 +  }
2051 +#endif
2052 +  /* Add URB to EP queue */
2053 +  urb_list_add(urb, epid, mem_flags);
2054 +
2055 +  /*hinko link/unlink urb -> ep */
2056 +  spin_lock_irqsave(&crisv10_hcd->lock, flags);
2057 +  //spin_lock(&crisv10_hcd->lock);
2058 +  retval = usb_hcd_link_urb_to_ep(hcd, urb);
2059 +  if (retval) {
2060 +    spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
2061 +    tc_warn("Failed to link urb to ep\n");
2062 +    urb_priv_free(hcd, urb);
2063 +    DBFEXIT;
2064 +    return retval;
2065 +  }
2066 +  spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
2067 +  //spin_unlock(&crisv10_hcd->lock);
2068 +  
2069 +  /* Process EP queue for rest of the URB types (Bulk, Ctrl, Intr) */
2070 +  tc_dma_process_queue(epid);
2071 +
2072 +//  local_irq_restore(flags);
2073 +
2074 +  DBFEXIT;
2075 +  return 0;
2076 +}
2077 +
2078 +/* remove an URB from the transfer controller queues (called from hcd_driver)*/
2079 +//static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
2080 +static int tc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2081 +{
2082 +  struct crisv10_urb_priv *urb_priv;
2083 +  unsigned long flags;
2084 +  int epid;
2085 +
2086 +  DBFENTER;
2087 +  /* Disable interrupts here since a descriptor interrupt for the isoc epid
2088 +     will modify the sb list.  This could possibly be done more granular, but
2089 +     urb_dequeue should not be used frequently anyway.
2090 +  */
2091 +  local_irq_save(flags);
2092 +
2093 +  urb_priv = urb->hcpriv;
2094 +
2095 +  if (!urb_priv) {
2096 +    /* This happens if a device driver calls unlink on an urb that
2097 +       was never submitted (lazy driver) or if the urb was completed
2098 +       while dequeue was being called. */
2099 +    tc_warn("Dequeing of not enqueued URB:0x%x\n", (unsigned int)urb);
2100 +    local_irq_restore(flags);
2101 +    return 0;
2102 +  }
2103 +  epid = urb_priv->epid;
2104 +
2105 +  tc_warn("Dequeing %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
2106 +         (urb == activeUrbList[epid]) ? "active" : "queued",
2107 +         (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
2108 +         str_type(urb->pipe), epid, urb->status,
2109 +         (urb_priv->later_data) ? "later-sched" : "");
2110 +
2111 +  /* For Bulk, Ctrl and Intr are only one URB active at a time. So any URB
2112 +     that isn't active can be dequeued by just removing it from the queue */
2113 +  if(usb_pipebulk(urb->pipe) || usb_pipecontrol(urb->pipe) ||
2114 +     usb_pipeint(urb->pipe)) {
2115 +
2116 +    /* Check if URB haven't gone further than the queue */
2117 +    if(urb != activeUrbList[epid]) {
2118 +      ASSERT(urb_priv->later_data == NULL);
2119 +      tc_warn("Dequeing URB:0x%x[%d] (%s %s epid:%d) from queue"
2120 +             " (not active)\n", (unsigned int)urb, urb_priv->urb_num,
2121 +             str_dir(urb->pipe), str_type(urb->pipe), epid);
2122 +      
2123 +      /* Finish the URB with error status from USB core */
2124 +      tc_finish_urb(hcd, urb, urb->status);
2125 +      local_irq_restore(flags);
2126 +      return 0;
2127 +    }
2128 +  }
2129 +
2130 +  /* Set URB status to Unlink for handling when interrupt comes. */
2131 +  urb_priv->urb_state = UNLINK;
2132 +
2133 +  /* Differentiate dequeing of Bulk and Ctrl from Isoc and Intr */
2134 +  switch(usb_pipetype(urb->pipe)) {
2135 +  case PIPE_BULK:
2136 +    /* Check if EP still is enabled */
2137 +    if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2138 +      /* The EP was enabled, disable it. */
2139 +      TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2140 +    }
2141 +    /* Kicking dummy list out of the party. */
2142 +    TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2143 +    break;
2144 +  case PIPE_CONTROL:
2145 +    /* Check if EP still is enabled */
2146 +    if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2147 +      /* The EP was enabled, disable it. */
2148 +      TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2149 +    }
2150 +    break;
2151 +  case PIPE_ISOCHRONOUS:
2152 +    /* Disabling, busy-wait and unlinking of Isoc SBs will be done in
2153 +       finish_isoc_urb(). Because there might the case when URB is dequeued
2154 +       but there are other valid URBs waiting */
2155 +
2156 +    /* Check if In Isoc EP still is enabled */
2157 +    if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2158 +      /* The EP was enabled, disable it. */
2159 +      TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2160 +    }
2161 +    break;
2162 +  case PIPE_INTERRUPT:
2163 +    /* Special care is taken for interrupt URBs. EPs are unlinked in
2164 +       tc_finish_urb */
2165 +    break;
2166 +  default:
2167 +    break;
2168 +  }
2169 +
2170 +  /* Asynchronous unlink, finish the URB later from scheduled or other
2171 +     event (data finished, error) */
2172 +  tc_finish_urb_later(hcd, urb, urb->status);
2173 +
2174 +  local_irq_restore(flags);
2175 +  DBFEXIT;
2176 +  return 0;
2177 +}
2178 +
2179 +
2180 +static void tc_sync_finish_epid(struct usb_hcd *hcd, int epid) {
2181 +  volatile int timeout = 10000;
2182 +  struct urb* urb;
2183 +  struct crisv10_urb_priv* urb_priv;
2184 +  unsigned long flags;
2185 +  
2186 +  volatile struct USB_EP_Desc *first_ep;  /* First EP in the list. */
2187 +  volatile struct USB_EP_Desc *curr_ep;   /* Current EP, the iterator. */
2188 +  volatile struct USB_EP_Desc *next_ep;   /* The EP after current. */
2189 +
2190 +  int type = epid_state[epid].type;
2191 +
2192 +  /* Setting this flag will cause enqueue() to return -ENOENT for new
2193 +     submitions on this endpoint and finish_urb() wont process queue further */
2194 +  epid_state[epid].disabled = 1;
2195 +
2196 +  switch(type) {
2197 +  case PIPE_BULK:
2198 +    /* Check if EP still is enabled */
2199 +    if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2200 +      /* The EP was enabled, disable it. */
2201 +      TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2202 +      tc_warn("sync_finish: Disabling EP for epid:%d\n", epid);
2203 +
2204 +      /* Do busy-wait until DMA not using this EP descriptor anymore */
2205 +      while((*R_DMA_CH8_SUB0_EP ==
2206 +            virt_to_phys(&TxBulkEPList[epid])) &&
2207 +           (timeout-- > 0));
2208 +      if(timeout == 0) {
2209 +       warn("Timeout while waiting for DMA-TX-Bulk to leave EP for"
2210 +            " epid:%d\n", epid);
2211 +      }
2212 +    }
2213 +    break;
2214 +
2215 +  case PIPE_CONTROL:
2216 +    /* Check if EP still is enabled */
2217 +    if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2218 +      /* The EP was enabled, disable it. */
2219 +      TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2220 +      tc_warn("sync_finish: Disabling EP for epid:%d\n", epid);
2221 +
2222 +      /* Do busy-wait until DMA not using this EP descriptor anymore */
2223 +      while((*R_DMA_CH8_SUB1_EP ==
2224 +            virt_to_phys(&TxCtrlEPList[epid])) &&
2225 +           (timeout-- > 0));
2226 +      if(timeout == 0) {
2227 +       warn("Timeout while waiting for DMA-TX-Ctrl to leave EP for"
2228 +            " epid:%d\n", epid);
2229 +      }
2230 +    }
2231 +    break;
2232 +
2233 +  case PIPE_INTERRUPT:
2234 +    local_irq_save(flags);
2235 +    /* Disable all Intr EPs belonging to epid */
2236 +    first_ep = &TxIntrEPList[0];
2237 +    curr_ep = first_ep;
2238 +    do {
2239 +      next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
2240 +      if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
2241 +       /* Disable EP */
2242 +       next_ep->command &= ~IO_MASK(USB_EP_command, enable);
2243 +      }
2244 +      curr_ep = phys_to_virt(curr_ep->next);
2245 +    } while (curr_ep != first_ep);
2246 +
2247 +    local_irq_restore(flags);
2248 +    break;
2249 +
2250 +  case PIPE_ISOCHRONOUS:
2251 +    /* Check if EP still is enabled */
2252 +    if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2253 +      tc_warn("sync_finish: Disabling Isoc EP for epid:%d\n", epid);
2254 +      /* The EP was enabled, disable it. */
2255 +      TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2256 +      
2257 +      while((*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid])) &&
2258 +           (timeout-- > 0));
2259 +      if(timeout == 0) {
2260 +       warn("Timeout while waiting for DMA-TX-Isoc to leave EP for"
2261 +            " epid:%d\n", epid);
2262 +      }
2263 +    }
2264 +    break;
2265 +  }
2266 +
2267 +  local_irq_save(flags);
2268 +
2269 +  /* Finish if there is active URB for this endpoint */
2270 +  if(activeUrbList[epid] != NULL) {
2271 +    urb = activeUrbList[epid];
2272 +    urb_priv = urb->hcpriv;
2273 +    ASSERT(urb_priv);
2274 +    tc_warn("Sync finish %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
2275 +           (urb == activeUrbList[epid]) ? "active" : "queued",
2276 +           (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
2277 +           str_type(urb->pipe), epid, urb->status,
2278 +           (urb_priv->later_data) ? "later-sched" : "");
2279 +
2280 +    tc_finish_urb(hcd, activeUrbList[epid], -ENOENT);
2281 +    ASSERT(activeUrbList[epid] == NULL);
2282 +  }
2283 +
2284 +  /* Finish any queued URBs for this endpoint. There won't be any resubmitions
2285 +     because epid_disabled causes enqueue() to fail for this endpoint */
2286 +  while((urb = urb_list_first(epid)) != NULL) {
2287 +    urb_priv = urb->hcpriv;
2288 +    ASSERT(urb_priv);
2289 +
2290 +    tc_warn("Sync finish %s URB:0x%x[%d] (%s %s epid:%d) status:%d %s\n",
2291 +           (urb == activeUrbList[epid]) ? "active" : "queued",
2292 +           (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
2293 +           str_type(urb->pipe), epid, urb->status,
2294 +           (urb_priv->later_data) ? "later-sched" : "");
2295 +
2296 +    tc_finish_urb(hcd, urb, -ENOENT);
2297 +  }
2298 +  epid_state[epid].disabled = 0;
2299 +  local_irq_restore(flags);
2300 +}
2301 +
2302 +/* free resources associated with an endpoint (called from hcd_driver) */
2303 +static void tc_endpoint_disable(struct usb_hcd *hcd, 
2304 +                               struct usb_host_endpoint *ep) {
2305 +  DBFENTER;
2306 +  /* Only free epid if it has been allocated. We get two endpoint_disable
2307 +     requests for ctrl endpoints so ignore the second one */
2308 +  if(ep->hcpriv != NULL) {
2309 +    struct crisv10_ep_priv *ep_priv = ep->hcpriv;
2310 +    int epid = ep_priv->epid;
2311 +    tc_warn("endpoint_disable ep:0x%x ep-priv:0x%x (%s) (epid:%d freed)\n",
2312 +          (unsigned int)ep, (unsigned int)ep->hcpriv,
2313 +          endpoint_to_str(&(ep->desc)), epid);
2314 +
2315 +    tc_sync_finish_epid(hcd, epid);
2316 +
2317 +    ASSERT(activeUrbList[epid] == NULL);
2318 +    ASSERT(list_empty(&urb_list[epid]));
2319 +
2320 +    tc_free_epid(ep);
2321 +  } else {
2322 +    tc_dbg("endpoint_disable ep:0x%x ep-priv:0x%x (%s)\n", (unsigned int)ep,
2323 +          (unsigned int)ep->hcpriv, endpoint_to_str(&(ep->desc)));
2324 +  }
2325 +  DBFEXIT;
2326 +}
2327 +
2328 +//static void tc_finish_urb_later_proc(void *data) {
2329 +static void tc_finish_urb_later_proc(struct work_struct *work) {
2330 +  unsigned long flags;
2331 +  //struct urb_later_data* uld = (struct urb_later_data*)data;
2332 +  struct urb_later_data* uld = container_of(work, struct urb_later_data, ws.work);
2333 +  local_irq_save(flags);
2334 +  if(uld->urb == NULL) {
2335 +    late_dbg("Later finish of URB = NULL (allready finished)\n");
2336 +  } else {
2337 +    struct crisv10_urb_priv* urb_priv = uld->urb->hcpriv;
2338 +    ASSERT(urb_priv);
2339 +    if(urb_priv->urb_num == uld->urb_num) {
2340 +      late_dbg("Later finish of URB:0x%x[%d]\n", (unsigned int)(uld->urb),
2341 +              urb_priv->urb_num);
2342 +      if(uld->status != uld->urb->status) {
2343 +       errno_dbg("Later-finish URB with status:%d, later-status:%d\n",
2344 +                 uld->urb->status, uld->status);
2345 +      }
2346 +      if(uld != urb_priv->later_data) {
2347 +       panic("Scheduled uld not same as URBs uld\n");
2348 +      }
2349 +      tc_finish_urb(uld->hcd, uld->urb, uld->status);
2350 +    } else {
2351 +      late_warn("Ignoring later finish of URB:0x%x[%d]"
2352 +               ", urb_num doesn't match current URB:0x%x[%d]",
2353 +               (unsigned int)(uld->urb), uld->urb_num,
2354 +               (unsigned int)(uld->urb), urb_priv->urb_num);
2355 +    }
2356 +  }
2357 +  local_irq_restore(flags);
2358 +  kmem_cache_free(later_data_cache, uld);
2359 +}
2360 +
2361 +static void tc_finish_urb_later(struct usb_hcd *hcd, struct urb *urb,
2362 +                               int status) {
2363 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
2364 +  struct urb_later_data* uld;
2365 +
2366 +  ASSERT(urb_priv);
2367 +
2368 +  if(urb_priv->later_data != NULL) {
2369 +    /* Later-finish allready scheduled for this URB, just update status to
2370 +       return when finishing later */
2371 +    errno_dbg("Later-finish schedule change URB status:%d with new"
2372 +             " status:%d\n", urb_priv->later_data->status, status);
2373 +    
2374 +    urb_priv->later_data->status = status;
2375 +    return;
2376 +  }
2377 +
2378 +  uld = kmem_cache_alloc(later_data_cache, GFP_ATOMIC);
2379 +  ASSERT(uld);
2380 +
2381 +  uld->hcd = hcd;
2382 +  uld->urb = urb;
2383 +  uld->urb_num = urb_priv->urb_num;
2384 +  uld->status = status;
2385 +
2386 +  //INIT_WORK(&uld->ws, tc_finish_urb_later_proc, uld);
2387 +  INIT_DELAYED_WORK(&uld->ws, tc_finish_urb_later_proc);
2388 +  urb_priv->later_data = uld;
2389 +
2390 +  /* Schedule the finishing of the URB to happen later */
2391 +  schedule_delayed_work(&uld->ws, LATER_TIMER_DELAY);
2392 +}
2393 +
2394 +  /* hinko ignore usb_pipeisoc */
2395 +#if 0
2396 +static void tc_finish_isoc_urb(struct usb_hcd *hcd, struct urb *urb,
2397 +                              int status);
2398 +#endif
2399 +
2400 +static void tc_finish_urb(struct usb_hcd *hcd, struct urb *urb, int status) {
2401 +  struct crisv10_hcd* crisv10_hcd = hcd_to_crisv10_hcd(hcd);
2402 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
2403 +  int epid;
2404 +  char toggle;
2405 +  int urb_num;
2406 +
2407 +  DBFENTER;
2408 +  ASSERT(urb_priv != NULL);
2409 +  epid = urb_priv->epid;
2410 +  urb_num = urb_priv->urb_num;
2411 +
2412 +  if(urb != activeUrbList[epid]) {
2413 +    if(urb_list_entry(urb, epid)) {
2414 +      /* Remove this URB from the list. Only happens when URB are finished
2415 +        before having been processed (dequeing) */
2416 +      urb_list_del(urb, epid);
2417 +    } else {
2418 +      tc_warn("Finishing of URB:0x%x[%d] neither active or in queue for"
2419 +             " epid:%d\n", (unsigned int)urb, urb_num, epid);
2420 +    }
2421 +  }
2422 +
2423 +  /* Cancel any pending later-finish of this URB */
2424 +  if(urb_priv->later_data) {
2425 +    urb_priv->later_data->urb = NULL;
2426 +  }
2427 +
2428 +  /* For an IN pipe, we always set the actual length, regardless of whether
2429 +     there was an error or not (which means the device driver can use the data
2430 +     if it wants to). */
2431 +  if(usb_pipein(urb->pipe)) {
2432 +    urb->actual_length = urb_priv->rx_offset;
2433 +  } else {
2434 +    /* Set actual_length for OUT urbs also; the USB mass storage driver seems
2435 +       to want that. */
2436 +    if (status == 0 && urb->status == -EINPROGRESS) {
2437 +      urb->actual_length = urb->transfer_buffer_length;
2438 +    } else {
2439 +      /*  We wouldn't know of any partial writes if there was an error. */
2440 +      urb->actual_length = 0;
2441 +    }
2442 +  }
2443 +
2444 +
2445 +  /* URB status mangling */
2446 +  if(urb->status == -EINPROGRESS) {
2447 +    /* The USB core hasn't changed the status, let's set our finish status */
2448 +    urb->status = status;
2449 +
2450 +    if ((status == 0) && (urb->transfer_flags & URB_SHORT_NOT_OK) &&
2451 +       usb_pipein(urb->pipe) &&
2452 +       (urb->actual_length != urb->transfer_buffer_length)) {
2453 +      /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's
2454 +        max length) is to be treated as an error. */
2455 +      errno_dbg("Finishing URB:0x%x[%d] with SHORT_NOT_OK flag and short"
2456 +               " data:%d\n", (unsigned int)urb, urb_num,
2457 +               urb->actual_length);
2458 +      urb->status = -EREMOTEIO;
2459 +    }
2460 +
2461 +    if(urb_priv->urb_state == UNLINK) {
2462 +      /* URB has been requested to be unlinked asynchronously */
2463 +      urb->status = -ECONNRESET;
2464 +      errno_dbg("Fixing unlink status of URB:0x%x[%d] to:%d\n",
2465 +               (unsigned int)urb, urb_num, urb->status);
2466 +    }
2467 +  } else {
2468 +    /* The USB Core wants to signal some error via the URB, pass it through */
2469 +  }
2470 +
2471 +  /* hinko ignore usb_pipeisoc */
2472 +#if 0
2473 +  /* use completely different finish function for Isoc URBs */
2474 +  if(usb_pipeisoc(urb->pipe)) {
2475 +    tc_finish_isoc_urb(hcd, urb, status);
2476 +    return;
2477 +  }
2478 +#endif
2479 +
2480 +  /* Do special unlinking of EPs for Intr traffic */
2481 +  if(usb_pipeint(urb->pipe)) {
2482 +    tc_dma_unlink_intr_urb(urb);
2483 +  }
2484 +
2485 +  /* hinko ignore usb_pipeisoc */
2486 +#if 0
2487 +  /* Release allocated bandwidth for periodic transfers */
2488 +  if(usb_pipeint(urb->pipe) || usb_pipeisoc(urb->pipe))
2489 +    usb_release_bandwidth(urb->dev, urb, 0);
2490 +#endif
2491 +  
2492 +  /* This URB is active on EP */
2493 +  if(urb == activeUrbList[epid]) {
2494 +    /* We need to fiddle with the toggle bits because the hardware doesn't do
2495 +       it for us. */
2496 +    toggle = etrax_epid_get_toggle(epid, usb_pipeout(urb->pipe));
2497 +    usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2498 +                 usb_pipeout(urb->pipe), toggle);
2499 +
2500 +    /* Checks for Ctrl and Bulk EPs */
2501 +    switch(usb_pipetype(urb->pipe)) {
2502 +    case PIPE_BULK:
2503 +      /* Check so Bulk EP realy is disabled before finishing active URB  */
2504 +      ASSERT((TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) ==
2505 +            IO_STATE(USB_EP_command, enable, no));
2506 +      /* Disable sub-pointer for EP to avoid next tx_interrupt() to
2507 +        process Bulk EP. */
2508 +      TxBulkEPList[epid].sub = 0;
2509 +      /* No need to wait for the DMA before changing the next pointer.
2510 +        The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
2511 +        the last one (INVALID_EPID) for actual traffic. */
2512 +      TxBulkEPList[epid].next = 
2513 +       virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2514 +      break;
2515 +    case PIPE_CONTROL:
2516 +      /* Check so Ctrl EP realy is disabled before finishing active URB  */
2517 +      ASSERT((TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) ==
2518 +            IO_STATE(USB_EP_command, enable, no));
2519 +      /* Disable sub-pointer for EP to avoid next tx_interrupt() to
2520 +        process Ctrl EP. */
2521 +      TxCtrlEPList[epid].sub = 0;
2522 +      break;
2523 +    }
2524 +  }
2525 +
2526 +  /* Free HC-private URB data*/
2527 +  urb_priv_free(hcd, urb);
2528 +
2529 +  if(urb->status) {
2530 +    errno_dbg("finish_urb (URB:0x%x[%d] %s %s) (data:%d) status:%d\n",
2531 +             (unsigned int)urb, urb_num, str_dir(urb->pipe),
2532 +             str_type(urb->pipe), urb->actual_length, urb->status);
2533 +  } else {
2534 +    tc_dbg("finish_urb (URB:0x%x[%d] %s %s) (data:%d) status:%d\n",
2535 +          (unsigned int)urb, urb_num, str_dir(urb->pipe),
2536 +          str_type(urb->pipe), urb->actual_length, urb->status);
2537 +  }
2538 +
2539 +  /* If we just finished an active URB, clear active pointer. */
2540 +  if (urb == activeUrbList[epid]) {
2541 +    /* Make URB not active on EP anymore */
2542 +    activeUrbList[epid] = NULL;
2543 +
2544 +    if(urb->status == 0) {
2545 +      /* URB finished sucessfully, process queue to see if there are any more
2546 +        URBs waiting before we call completion function.*/
2547 +      if(crisv10_hcd->running) {
2548 +       /* Only process queue if USB controller is running */
2549 +       tc_dma_process_queue(epid);
2550 +      } else {
2551 +       tc_warn("No processing of queue for epid:%d, USB Controller not"
2552 +               " running\n", epid);
2553 +      }
2554 +    }
2555 +  }
2556 +
2557 +  /*  Hand the URB from HCD to its USB device driver, using its completion
2558 +      functions */
2559 +//  usb_hcd_giveback_urb (hcd, urb);
2560 +       /**
2561 +        * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
2562 +        * @hcd: host controller to which @urb was submitted
2563 +        * @urb: URB being unlinked
2564 +        *
2565 +        * Host controller drivers should call this routine before calling
2566 +        * usb_hcd_giveback_urb().  The HCD's private spinlock must be held and
2567 +        * interrupts must be disabled.  The actions carried out here are required
2568 +        * for URB completion.
2569 +        */
2570 +  
2571 +  /*hinko link/unlink urb -> ep */
2572 +  //spin_lock(&crisv10_hcd->lock);
2573 +  unsigned long flags;
2574 +  spin_lock_irqsave(&crisv10_hcd->lock, flags);
2575 +  usb_hcd_unlink_urb_from_ep(hcd, urb);
2576 +  usb_hcd_giveback_urb(hcd, urb, status);
2577 +  //spin_unlock(&crisv10_hcd->lock);
2578 +  spin_unlock_irqrestore(&crisv10_hcd->lock, flags);
2579 +
2580 +  /* Check the queue once more if the URB returned with error, because we
2581 +     didn't do it before the completion function because the specification
2582 +     states that the queue should not restart until all it's unlinked
2583 +     URBs have been fully retired, with the completion functions run */
2584 +  if(crisv10_hcd->running) {
2585 +    /* Only process queue if USB controller is running */
2586 +    tc_dma_process_queue(epid);
2587 +  } else {
2588 +    tc_warn("No processing of queue for epid:%d, USB Controller not running\n",
2589 +           epid);
2590 +  }
2591 +
2592 +  DBFEXIT;
2593 +}
2594 +
2595 +  /* hinko ignore usb_pipeisoc */
2596 +#if 0
2597 +static void tc_finish_isoc_urb(struct usb_hcd *hcd, struct urb *urb,
2598 +                              int status) {
2599 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
2600 +  int epid, i;
2601 +  volatile int timeout = 10000;
2602 +
2603 +  ASSERT(urb_priv);
2604 +  epid = urb_priv->epid;
2605 +
2606 +  ASSERT(usb_pipeisoc(urb->pipe));
2607 +
2608 +  /* Set that all isoc packets have status and length set before
2609 +     completing the urb. */
2610 +  for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++){
2611 +    urb->iso_frame_desc[i].actual_length = 0;
2612 +    urb->iso_frame_desc[i].status = -EPROTO;
2613 +  }
2614 +
2615 +  /* Check if the URB is currently active (done or error) */
2616 +  if(urb == activeUrbList[epid]) {
2617 +    /* Check if there are another In Isoc URB queued for this epid */
2618 +    if (!list_empty(&urb_list[epid])&& !epid_state[epid].disabled) {
2619 +      /* Move it from queue to active and mark it started so Isoc transfers
2620 +        won't be interrupted.
2621 +        All Isoc URBs data transfers are already added to DMA lists so we
2622 +        don't have to insert anything in DMA lists here. */
2623 +      activeUrbList[epid] = urb_list_first(epid);
2624 +      ((struct crisv10_urb_priv *)(activeUrbList[epid]->hcpriv))->urb_state =
2625 +       STARTED;
2626 +      urb_list_del(activeUrbList[epid], epid);
2627 +
2628 +      if(urb->status) {
2629 +       errno_dbg("finish_isoc_urb (URB:0x%x[%d] %s %s) (%d of %d packets)"
2630 +                 " status:%d, new waiting URB:0x%x[%d]\n",
2631 +                 (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
2632 +                 str_type(urb->pipe), urb_priv->isoc_packet_counter,
2633 +                 urb->number_of_packets, urb->status,
2634 +                 (unsigned int)activeUrbList[epid],
2635 +                 ((struct crisv10_urb_priv *)(activeUrbList[epid]->hcpriv))->urb_num);
2636 +      }
2637 +
2638 +    } else { /* No other URB queued for this epid */
2639 +      if(urb->status) {
2640 +       errno_dbg("finish_isoc_urb (URB:0x%x[%d] %s %s) (%d of %d packets)"
2641 +                 " status:%d, no new URB waiting\n",
2642 +                 (unsigned int)urb, urb_priv->urb_num, str_dir(urb->pipe),
2643 +                 str_type(urb->pipe), urb_priv->isoc_packet_counter,
2644 +                 urb->number_of_packets, urb->status);
2645 +      }
2646 +
2647 +      /* Check if EP is still enabled, then shut it down. */
2648 +      if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
2649 +       isoc_dbg("Isoc EP enabled for epid:%d, disabling it\n", epid);
2650 +
2651 +       /* Should only occur for In Isoc EPs where SB isn't consumed. */
2652 +       ASSERT(usb_pipein(urb->pipe));
2653 +
2654 +       /* Disable it and wait for it to stop */
2655 +       TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
2656 +       
2657 +       /* Ah, the luxury of busy-wait. */
2658 +       while((*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid])) &&
2659 +             (timeout-- > 0));
2660 +       if(timeout == 0) {
2661 +         warn("Timeout while waiting for DMA-TX-Isoc to leave EP for epid:%d\n", epid);
2662 +       }
2663 +      }
2664 +
2665 +      /* Unlink SB to say that epid is finished. */
2666 +      TxIsocEPList[epid].sub = 0;
2667 +      TxIsocEPList[epid].hw_len = 0;
2668 +
2669 +      /* No URB active for EP anymore */
2670 +      activeUrbList[epid] = NULL;
2671 +    }
2672 +  } else { /* Finishing of not active URB (queued up with SBs thought) */
2673 +    isoc_warn("finish_isoc_urb (URB:0x%x %s) (%d of %d packets) status:%d,"
2674 +             " SB queued but not active\n",
2675 +             (unsigned int)urb, str_dir(urb->pipe),
2676 +             urb_priv->isoc_packet_counter, urb->number_of_packets,
2677 +             urb->status);
2678 +    if(usb_pipeout(urb->pipe)) {
2679 +      /* Finishing of not yet active Out Isoc URB needs unlinking of SBs. */
2680 +      struct USB_SB_Desc *iter_sb, *prev_sb, *next_sb;
2681 +
2682 +      iter_sb = TxIsocEPList[epid].sub ?
2683 +       phys_to_virt(TxIsocEPList[epid].sub) : 0;
2684 +      prev_sb = 0;
2685 +
2686 +      /* SB that is linked before this URBs first SB */
2687 +      while (iter_sb && (iter_sb != urb_priv->first_sb)) {
2688 +       prev_sb = iter_sb;
2689 +       iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
2690 +      }
2691 +
2692 +      if (iter_sb == 0) {
2693 +       /* Unlink of the URB currently being transmitted. */
2694 +       prev_sb = 0;
2695 +       iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
2696 +      }
2697 +
2698 +      while (iter_sb && (iter_sb != urb_priv->last_sb)) {
2699 +       iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
2700 +      }
2701 +
2702 +      if (iter_sb) {
2703 +       next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
2704 +      } else {
2705 +       /* This should only happen if the DMA has completed
2706 +          processing the SB list for this EP while interrupts
2707 +          are disabled. */
2708 +       isoc_dbg("Isoc urb not found, already sent?\n");
2709 +       next_sb = 0;
2710 +      }
2711 +      if (prev_sb) {
2712 +       prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
2713 +      } else {
2714 +       TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
2715 +      }
2716 +    }
2717 +  }
2718 +
2719 +  /* Free HC-private URB data*/
2720 +  urb_priv_free(hcd, urb);
2721 +
2722 +  usb_release_bandwidth(urb->dev, urb, 0);
2723 +
2724 +  /*  Hand the URB from HCD to its USB device driver, using its completion
2725 +      functions */
2726 +  usb_hcd_giveback_urb (hcd, urb);
2727 +}
2728 +#endif
2729 +
2730 +static __u32 urb_num = 0;
2731 +
2732 +/* allocate and initialize URB private data */
2733 +static int urb_priv_create(struct usb_hcd *hcd, struct urb *urb, int epid,
2734 +                          int mem_flags) {
2735 +  struct crisv10_urb_priv *urb_priv;
2736 +  
2737 +  urb_priv = kmalloc(sizeof *urb_priv, mem_flags);
2738 +  if (!urb_priv)
2739 +    return -ENOMEM;
2740 +  memset(urb_priv, 0, sizeof *urb_priv);
2741 +
2742 +  urb_priv->epid = epid;
2743 +  urb_priv->urb_state = NOT_STARTED;
2744 +
2745 +  urb->hcpriv = urb_priv;
2746 +  /* Assign URB a sequence number, and increment counter */
2747 +  urb_priv->urb_num = urb_num;
2748 +  urb_num++;
2749 +  return 0;
2750 +}
2751 +
2752 +/* free URB private data */
2753 +static void urb_priv_free(struct usb_hcd *hcd, struct urb *urb) {
2754 +  int i;
2755 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
2756 +  ASSERT(urb_priv != 0);
2757 +
2758 +  /* Check it has any SBs linked that needs to be freed*/
2759 +  if(urb_priv->first_sb != NULL) {
2760 +    struct USB_SB_Desc *next_sb, *first_sb, *last_sb;
2761 +    int i = 0;
2762 +    first_sb = urb_priv->first_sb;
2763 +    last_sb = urb_priv->last_sb;
2764 +    ASSERT(last_sb);
2765 +    while(first_sb != last_sb) {
2766 +      next_sb = (struct USB_SB_Desc *)phys_to_virt(first_sb->next);
2767 +      kmem_cache_free(usb_desc_cache, first_sb);
2768 +      first_sb = next_sb;
2769 +      i++;
2770 +    }
2771 +    kmem_cache_free(usb_desc_cache, last_sb);
2772 +    i++;
2773 +  }
2774 +
2775 +  /* Check if it has any EPs in its Intr pool that also needs to be freed */
2776 +  if(urb_priv->intr_ep_pool_length > 0) {
2777 +    for(i = 0; i < urb_priv->intr_ep_pool_length; i++) {
2778 +      kfree(urb_priv->intr_ep_pool[i]);
2779 +    }
2780 +    /*
2781 +    tc_dbg("Freed %d EPs from URB:0x%x EP pool\n",
2782 +            urb_priv->intr_ep_pool_length, (unsigned int)urb);
2783 +    */
2784 +  }
2785 +
2786 +  kfree(urb_priv);
2787 +  urb->hcpriv = NULL;
2788 +}
2789 +
2790 +static int ep_priv_create(struct usb_host_endpoint *ep, int mem_flags) {
2791 +  struct crisv10_ep_priv *ep_priv;
2792 +  
2793 +  ep_priv = kmalloc(sizeof *ep_priv, mem_flags);
2794 +  if (!ep_priv)
2795 +    return -ENOMEM;
2796 +  memset(ep_priv, 0, sizeof *ep_priv);
2797 +
2798 +  ep->hcpriv = ep_priv;
2799 +  return 0;
2800 +}
2801 +
2802 +static void ep_priv_free(struct usb_host_endpoint *ep) {
2803 +  struct crisv10_ep_priv *ep_priv = ep->hcpriv;
2804 +  ASSERT(ep_priv);
2805 +  kfree(ep_priv);
2806 +  ep->hcpriv = NULL;
2807 +}
2808 +
2809 +/* EPID handling functions, managing EP-list in Etrax through wrappers */
2810 +/* ------------------------------------------------------------------- */
2811 +
2812 +/* Sets up a new EPID for an endpoint or returns existing if found */
2813 +//static int tc_setup_epid(struct usb_host_endpoint *ep, struct urb *urb,
2814 +//                      int mem_flags) {
2815 +static int tc_setup_epid(struct urb *urb, int mem_flags)
2816 +{
2817 +  int epid;
2818 +  char devnum, endpoint, out_traffic, slow;
2819 +  int maxlen;
2820 +  __u32 epid_data;
2821 +  struct usb_host_endpoint *ep = urb->ep;
2822 +  struct crisv10_ep_priv *ep_priv = ep->hcpriv;
2823 +  
2824 +  DBFENTER;
2825 +  
2826 +  /* Check if a valid epid already is setup for this endpoint */
2827 +  if(ep_priv != NULL) {
2828 +    return ep_priv->epid;
2829 +  }
2830 +
2831 +  /* We must find and initiate a new epid for this urb. */
2832 +  epid = tc_allocate_epid();
2833 +  
2834 +  if (epid == -1) {
2835 +    /* Failed to allocate a new epid. */
2836 +    DBFEXIT;
2837 +    return epid;
2838 +  }
2839 +  
2840 +  /* We now have a new epid to use. Claim it. */
2841 +  epid_state[epid].inuse = 1;
2842 +  
2843 +  /* Init private data for new endpoint */
2844 +  if(ep_priv_create(ep, mem_flags) != 0) {
2845 +    return -ENOMEM;
2846 +  }
2847 +  ep_priv = ep->hcpriv;
2848 +  ep_priv->epid = epid;
2849 +
2850 +  devnum = usb_pipedevice(urb->pipe);
2851 +  endpoint = usb_pipeendpoint(urb->pipe);
2852 +  slow = (urb->dev->speed == USB_SPEED_LOW);
2853 +  maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2854 +
2855 +  if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
2856 +    /* We want both IN and OUT control traffic to be put on the same
2857 +       EP/SB list. */
2858 +    out_traffic = 1;
2859 +  } else {
2860 +    out_traffic = usb_pipeout(urb->pipe);
2861 +  }
2862 +    
2863 +  if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2864 +    epid_data = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
2865 +      /* FIXME: Change any to the actual port? */
2866 +      IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
2867 +      IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
2868 +      IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
2869 +      IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
2870 +    etrax_epid_iso_set(epid, epid_data);
2871 +  } else {
2872 +    epid_data = IO_STATE(R_USB_EPT_DATA, valid, yes) |
2873 +      IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
2874 +      /* FIXME: Change any to the actual port? */
2875 +      IO_STATE(R_USB_EPT_DATA, port, any) |
2876 +      IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
2877 +      IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
2878 +      IO_FIELD(R_USB_EPT_DATA, dev, devnum);
2879 +    etrax_epid_set(epid, epid_data);
2880 +  }
2881 +  
2882 +  epid_state[epid].out_traffic = out_traffic;
2883 +  epid_state[epid].type = usb_pipetype(urb->pipe);
2884 +
2885 +  tc_warn("Setting up ep:0x%x epid:%d (addr:%d endp:%d max_len:%d %s %s %s)\n",
2886 +         (unsigned int)ep, epid, devnum, endpoint, maxlen,
2887 +         str_type(urb->pipe), out_traffic ? "out" : "in",
2888 +         slow ? "low" : "full");
2889 +
2890 +  /* Enable Isoc eof interrupt if we set up the first Isoc epid */
2891 +  if(usb_pipeisoc(urb->pipe)) {
2892 +    isoc_epid_counter++;
2893 +    if(isoc_epid_counter == 1) {
2894 +      isoc_warn("Enabled Isoc eof interrupt\n");
2895 +      *R_USB_IRQ_MASK_SET |= IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set);
2896 +    }
2897 +  }
2898 +
2899 +  DBFEXIT;
2900 +  return epid;
2901 +}
2902 +
2903 +static void tc_free_epid(struct usb_host_endpoint *ep) {
2904 +  unsigned long flags;
2905 +  struct crisv10_ep_priv *ep_priv = ep->hcpriv;
2906 +  int epid;
2907 +  volatile int timeout = 10000;
2908 +
2909 +  DBFENTER;
2910 +
2911 +  if (ep_priv == NULL) {
2912 +    tc_warn("Trying to free unused epid on ep:0x%x\n", (unsigned int)ep);
2913 +    DBFEXIT;
2914 +    return;
2915 +  }
2916 +
2917 +  epid = ep_priv->epid;
2918 +
2919 +  /* Disable Isoc eof interrupt if we free the last Isoc epid */
2920 +  if(epid_isoc(epid)) {
2921 +    ASSERT(isoc_epid_counter > 0);
2922 +    isoc_epid_counter--;
2923 +    if(isoc_epid_counter == 0) {
2924 +      *R_USB_IRQ_MASK_SET &= ~IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set);
2925 +      isoc_warn("Disabled Isoc eof interrupt\n");
2926 +    }
2927 +  }
2928 +
2929 +  /* Take lock manualy instead of in epid_x_x wrappers,
2930 +     because we need to be polling here */
2931 +  spin_lock_irqsave(&etrax_epid_lock, flags);
2932 +  
2933 +  *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2934 +  nop();
2935 +  while((*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) &&
2936 +       (timeout-- > 0));
2937 +  if(timeout == 0) {
2938 +    warn("Timeout while waiting for epid:%d to drop hold\n", epid);
2939 +  }
2940 +  /* This will, among other things, set the valid field to 0. */
2941 +  *R_USB_EPT_DATA = 0;
2942 +  spin_unlock_irqrestore(&etrax_epid_lock, flags);
2943 +  
2944 +  /* Free resource in software state info list */
2945 +  epid_state[epid].inuse = 0;
2946 +
2947 +  /* Free private endpoint data */
2948 +  ep_priv_free(ep);
2949 +  
2950 +  DBFEXIT;
2951 +}
2952 +
2953 +static int tc_allocate_epid(void) {
2954 +  int i;
2955 +  DBFENTER;
2956 +  for (i = 0; i < NBR_OF_EPIDS; i++) {
2957 +    if (!epid_inuse(i)) {
2958 +      DBFEXIT;
2959 +      return i;
2960 +    }
2961 +  }
2962 +  
2963 +  tc_warn("Found no free epids\n");
2964 +  DBFEXIT;
2965 +  return -1;
2966 +}
2967 +
2968 +
2969 +/* Wrappers around the list functions (include/linux/list.h). */
2970 +/* ---------------------------------------------------------- */
2971 +static inline int __urb_list_empty(int epid) {
2972 +  int retval;
2973 +  retval = list_empty(&urb_list[epid]);
2974 +  return retval;
2975 +}
2976 +
2977 +/* Returns first urb for this epid, or NULL if list is empty. */
2978 +static inline struct urb *urb_list_first(int epid) {
2979 +  unsigned long flags;
2980 +  struct urb *first_urb = 0;
2981 +  spin_lock_irqsave(&urb_list_lock, flags);
2982 +  if (!__urb_list_empty(epid)) {
2983 +    /* Get the first urb (i.e. head->next). */
2984 +    urb_entry_t *urb_entry = list_entry((&urb_list[epid])->next, urb_entry_t, list);
2985 +    first_urb = urb_entry->urb;
2986 +  }
2987 +  spin_unlock_irqrestore(&urb_list_lock, flags);
2988 +  return first_urb;
2989 +}
2990 +
2991 +/* Adds an urb_entry last in the list for this epid. */
2992 +static inline void urb_list_add(struct urb *urb, int epid, int mem_flags) {
2993 +  unsigned long flags;
2994 +  urb_entry_t *urb_entry = (urb_entry_t *)kmalloc(sizeof(urb_entry_t), mem_flags);
2995 +  ASSERT(urb_entry);
2996 +  
2997 +  urb_entry->urb = urb;
2998 +  spin_lock_irqsave(&urb_list_lock, flags);
2999 +  list_add_tail(&urb_entry->list, &urb_list[epid]);
3000 +  spin_unlock_irqrestore(&urb_list_lock, flags);
3001 +}
3002 +
3003 +/* Search through the list for an element that contains this urb. (The list
3004 +   is expected to be short and the one we are about to delete will often be
3005 +   the first in the list.)
3006 +   Should be protected by spin_locks in calling function */
3007 +static inline urb_entry_t *__urb_list_entry(struct urb *urb, int epid) {
3008 +  struct list_head *entry;
3009 +  struct list_head *tmp;
3010 +  urb_entry_t *urb_entry;
3011 +  
3012 +  list_for_each_safe(entry, tmp, &urb_list[epid]) {
3013 +    urb_entry = list_entry(entry, urb_entry_t, list);
3014 +    ASSERT(urb_entry);
3015 +    ASSERT(urb_entry->urb);
3016 +    
3017 +    if (urb_entry->urb == urb) {
3018 +      return urb_entry;
3019 +    }
3020 +  }
3021 +  return 0;
3022 +}
3023 +
3024 +/* Same function as above but for global use. Protects list by spinlock */
3025 +static inline urb_entry_t *urb_list_entry(struct urb *urb, int epid) {
3026 +  unsigned long flags;
3027 +  urb_entry_t *urb_entry;
3028 +  spin_lock_irqsave(&urb_list_lock, flags);
3029 +  urb_entry = __urb_list_entry(urb, epid);
3030 +  spin_unlock_irqrestore(&urb_list_lock, flags);
3031 +  return (urb_entry);
3032 +}
3033 +
3034 +/* Delete an urb from the list. */
3035 +static inline void urb_list_del(struct urb *urb, int epid) {
3036 +  unsigned long flags;
3037 +  urb_entry_t *urb_entry;
3038 +
3039 +  /* Delete entry and free. */
3040 +  spin_lock_irqsave(&urb_list_lock, flags);
3041 +  urb_entry = __urb_list_entry(urb, epid);
3042 +  ASSERT(urb_entry);
3043 +
3044 +  list_del(&urb_entry->list);
3045 +  spin_unlock_irqrestore(&urb_list_lock, flags);
3046 +  kfree(urb_entry);
3047 +}
3048 +
3049 +/* Move an urb to the end of the list. */
3050 +static inline void urb_list_move_last(struct urb *urb, int epid) {
3051 +  unsigned long flags;
3052 +  urb_entry_t *urb_entry;
3053 +  
3054 +  spin_lock_irqsave(&urb_list_lock, flags);
3055 +  urb_entry = __urb_list_entry(urb, epid);
3056 +  ASSERT(urb_entry);
3057 +
3058 +  list_del(&urb_entry->list);
3059 +  list_add_tail(&urb_entry->list, &urb_list[epid]);
3060 +  spin_unlock_irqrestore(&urb_list_lock, flags);
3061 +}
3062 +
3063 +/* Get the next urb in the list. */
3064 +static inline struct urb *urb_list_next(struct urb *urb, int epid) {
3065 +  unsigned long flags;
3066 +  urb_entry_t *urb_entry;
3067 +
3068 +  spin_lock_irqsave(&urb_list_lock, flags);
3069 +  urb_entry = __urb_list_entry(urb, epid);
3070 +  ASSERT(urb_entry);
3071 +
3072 +  if (urb_entry->list.next != &urb_list[epid]) {
3073 +    struct list_head *elem = urb_entry->list.next;
3074 +    urb_entry = list_entry(elem, urb_entry_t, list);
3075 +    spin_unlock_irqrestore(&urb_list_lock, flags);
3076 +    return urb_entry->urb;
3077 +  } else {
3078 +    spin_unlock_irqrestore(&urb_list_lock, flags);
3079 +    return NULL;
3080 +  }
3081 +}
3082 +
3083 +struct USB_EP_Desc* create_ep(int epid, struct USB_SB_Desc* sb_desc,
3084 +                             int mem_flags) {
3085 +  struct USB_EP_Desc *ep_desc;
3086 +  ep_desc = (struct USB_EP_Desc *) kmem_cache_alloc(usb_desc_cache, mem_flags);
3087 +  if(ep_desc == NULL)
3088 +    return NULL;
3089 +  memset(ep_desc, 0, sizeof(struct USB_EP_Desc));
3090 +
3091 +  ep_desc->hw_len = 0;
3092 +  ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
3093 +                     IO_STATE(USB_EP_command, enable, yes));
3094 +  if(sb_desc == NULL) {
3095 +    ep_desc->sub = 0;
3096 +  } else {
3097 +    ep_desc->sub = virt_to_phys(sb_desc);
3098 +  }
3099 +  return ep_desc;
3100 +}
3101 +
3102 +#define TT_ZOUT  0
3103 +#define TT_IN    1
3104 +#define TT_OUT   2
3105 +#define TT_SETUP 3
3106 +
3107 +#define CMD_EOL  IO_STATE(USB_SB_command, eol, yes)
3108 +#define CMD_INTR IO_STATE(USB_SB_command, intr, yes)
3109 +#define CMD_FULL IO_STATE(USB_SB_command, full, yes)
3110 +
3111 +/* Allocation and setup of a generic SB. Used to create SETUP, OUT and ZOUT
3112 +   SBs. Also used by create_sb_in() to avoid same allocation procedure at two
3113 +   places */
3114 +struct USB_SB_Desc* create_sb(struct USB_SB_Desc* sb_prev, int tt, void* data,
3115 +                             int datalen, int mem_flags) {
3116 +  struct USB_SB_Desc *sb_desc;
3117 +  sb_desc = (struct USB_SB_Desc*)kmem_cache_alloc(usb_desc_cache, mem_flags);
3118 +  if(sb_desc == NULL)
3119 +    return NULL;
3120 +  memset(sb_desc, 0, sizeof(struct USB_SB_Desc));
3121 +
3122 +  sb_desc->command = IO_FIELD(USB_SB_command, tt, tt) |
3123 +                     IO_STATE(USB_SB_command, eot, yes);
3124 +
3125 +  sb_desc->sw_len = datalen;
3126 +  if(data != NULL) {
3127 +    sb_desc->buf = virt_to_phys(data);
3128 +  } else {
3129 +    sb_desc->buf = 0;
3130 +  }
3131 +  if(sb_prev != NULL) {
3132 +    sb_prev->next = virt_to_phys(sb_desc);
3133 +  }
3134 +  return sb_desc;
3135 +}
3136 +
3137 +/* Creates a copy of an existing SB by allocation space for it and copy
3138 +   settings */
3139 +struct USB_SB_Desc* create_sb_copy(struct USB_SB_Desc* sb_orig, int mem_flags) {
3140 +  struct USB_SB_Desc *sb_desc;
3141 +  sb_desc = (struct USB_SB_Desc*)kmem_cache_alloc(usb_desc_cache, mem_flags);
3142 +  if(sb_desc == NULL)
3143 +    return NULL;
3144 +
3145 +  memcpy(sb_desc, sb_orig, sizeof(struct USB_SB_Desc));
3146 +  return sb_desc;
3147 +}
3148 +
3149 +/* A specific create_sb function for creation of in SBs. This is due to
3150 +   that datalen in In SBs shows how many packets we are expecting. It also
3151 +   sets up the rem field to show if how many bytes we expect in last packet
3152 +   if it's not a full one */
3153 +struct USB_SB_Desc* create_sb_in(struct USB_SB_Desc* sb_prev, int datalen,
3154 +                                int maxlen, int mem_flags) {
3155 +  struct USB_SB_Desc *sb_desc;
3156 +  sb_desc = create_sb(sb_prev, TT_IN, NULL,
3157 +                     datalen ? (datalen - 1) / maxlen + 1 : 0, mem_flags);
3158 +  if(sb_desc == NULL)
3159 +    return NULL;
3160 +  sb_desc->command |= IO_FIELD(USB_SB_command, rem, datalen % maxlen);
3161 +  return sb_desc;
3162 +}
3163 +
3164 +void set_sb_cmds(struct USB_SB_Desc *sb_desc, __u16 flags) {
3165 +  sb_desc->command |= flags;
3166 +}
3167 +
3168 +int create_sb_for_urb(struct urb *urb, int mem_flags) {
3169 +  int is_out = !usb_pipein(urb->pipe);
3170 +  int type = usb_pipetype(urb->pipe);
3171 +  int maxlen = usb_maxpacket(urb->dev, urb->pipe, is_out);
3172 +  int buf_len = urb->transfer_buffer_length;
3173 +  void *buf = buf_len > 0 ? urb->transfer_buffer : NULL;
3174 +  struct USB_SB_Desc *sb_desc = NULL;
3175 +
3176 +  struct crisv10_urb_priv *urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
3177 +  ASSERT(urb_priv != NULL);
3178 +
3179 +  switch(type) {
3180 +  case PIPE_CONTROL:
3181 +    /* Setup stage */
3182 +    sb_desc = create_sb(NULL, TT_SETUP, urb->setup_packet, 8, mem_flags);
3183 +    if(sb_desc == NULL)
3184 +      return -ENOMEM;
3185 +    set_sb_cmds(sb_desc, CMD_FULL);
3186 +
3187 +    /* Attach first SB to URB */
3188 +    urb_priv->first_sb = sb_desc;    
3189 +
3190 +    if (is_out) { /* Out Control URB */
3191 +      /* If this Control OUT transfer has an optional data stage we add
3192 +        an OUT token before the mandatory IN (status) token */
3193 +      if ((buf_len > 0) && buf) {
3194 +       sb_desc = create_sb(sb_desc, TT_OUT, buf, buf_len, mem_flags);
3195 +       if(sb_desc == NULL)
3196 +         return -ENOMEM;
3197 +       set_sb_cmds(sb_desc, CMD_FULL);
3198 +      }
3199 +
3200 +      /* Status stage */
3201 +      /* The data length has to be exactly 1. This is due to a requirement
3202 +         of the USB specification that a host must be prepared to receive
3203 +         data in the status phase */
3204 +      sb_desc = create_sb(sb_desc, TT_IN, NULL, 1, mem_flags);
3205 +      if(sb_desc == NULL)
3206 +       return -ENOMEM;
3207 +    } else { /* In control URB */
3208 +      /* Data stage */
3209 +      sb_desc = create_sb_in(sb_desc, buf_len, maxlen, mem_flags);
3210 +      if(sb_desc == NULL)
3211 +       return -ENOMEM;
3212 +
3213 +      /* Status stage */
3214 +      /* Read comment at zout_buffer declaration for an explanation to this. */
3215 +      sb_desc = create_sb(sb_desc, TT_ZOUT, &zout_buffer[0], 1, mem_flags);
3216 +      if(sb_desc == NULL)
3217 +       return -ENOMEM;
3218 +      /* Set descriptor interrupt flag for in URBs so we can finish URB after
3219 +         zout-packet has been sent */
3220 +      set_sb_cmds(sb_desc, CMD_INTR | CMD_FULL);
3221 +    }
3222 +    /* Set end-of-list flag in last SB */
3223 +    set_sb_cmds(sb_desc, CMD_EOL);
3224 +    /* Attach last SB to URB */
3225 +    urb_priv->last_sb = sb_desc;
3226 +    break;
3227 +
3228 +  case PIPE_BULK:
3229 +    if (is_out) { /* Out Bulk URB */
3230 +      sb_desc = create_sb(NULL, TT_OUT, buf, buf_len, mem_flags);
3231 +      if(sb_desc == NULL)
3232 +       return -ENOMEM;
3233 +      /* The full field is set to yes, even if we don't actually check that
3234 +        this is a full-length transfer (i.e., that transfer_buffer_length %
3235 +        maxlen = 0).
3236 +        Setting full prevents the USB controller from sending an empty packet
3237 +        in that case.  However, if URB_ZERO_PACKET was set we want that. */
3238 +      if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
3239 +       set_sb_cmds(sb_desc, CMD_FULL);
3240 +      }
3241 +    } else { /* In Bulk URB */
3242 +      sb_desc = create_sb_in(NULL, buf_len, maxlen, mem_flags);
3243 +      if(sb_desc == NULL)
3244 +       return -ENOMEM;
3245 +    }
3246 +    /* Set end-of-list flag for last SB */
3247 +    set_sb_cmds(sb_desc, CMD_EOL);
3248 +
3249 +    /* Attach SB to URB */
3250 +    urb_priv->first_sb = sb_desc;
3251 +    urb_priv->last_sb = sb_desc;
3252 +    break;
3253 +
3254 +  case PIPE_INTERRUPT:
3255 +    if(is_out) { /* Out Intr URB */
3256 +      sb_desc = create_sb(NULL, TT_OUT, buf, buf_len, mem_flags);
3257 +      if(sb_desc == NULL)
3258 +       return -ENOMEM;
3259 +
3260 +      /* The full field is set to yes, even if we don't actually check that
3261 +        this is a full-length transfer (i.e., that transfer_buffer_length %
3262 +        maxlen = 0).
3263 +        Setting full prevents the USB controller from sending an empty packet
3264 +        in that case.  However, if URB_ZERO_PACKET was set we want that. */
3265 +      if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
3266 +       set_sb_cmds(sb_desc, CMD_FULL);
3267 +      }
3268 +      /* Only generate TX interrupt if it's a Out URB*/
3269 +      set_sb_cmds(sb_desc, CMD_INTR);
3270 +
3271 +    } else { /* In Intr URB */
3272 +      sb_desc = create_sb_in(NULL, buf_len, maxlen, mem_flags);
3273 +      if(sb_desc == NULL)
3274 +       return -ENOMEM;
3275 +    }
3276 +    /* Set end-of-list flag for last SB */
3277 +    set_sb_cmds(sb_desc, CMD_EOL);
3278 +
3279 +    /* Attach SB to URB */
3280 +    urb_priv->first_sb = sb_desc;
3281 +    urb_priv->last_sb = sb_desc;
3282 +
3283 +    break;
3284 +  case PIPE_ISOCHRONOUS:
3285 +    if(is_out) { /* Out Isoc URB */
3286 +      int i;
3287 +      if(urb->number_of_packets == 0) {
3288 +       tc_err("Can't create SBs for Isoc URB with zero packets\n");
3289 +       return -EPIPE;
3290 +      }
3291 +      /* Create one SB descriptor for each packet and link them together. */
3292 +      for(i = 0; i < urb->number_of_packets; i++) {
3293 +       if (urb->iso_frame_desc[i].length > 0) {
3294 +
3295 +         sb_desc = create_sb(sb_desc, TT_OUT, urb->transfer_buffer +
3296 +                             urb->iso_frame_desc[i].offset,
3297 +                             urb->iso_frame_desc[i].length, mem_flags);
3298 +         if(sb_desc == NULL)
3299 +           return -ENOMEM;
3300 +
3301 +         /* Check if it's a full length packet */
3302 +         if (urb->iso_frame_desc[i].length ==
3303 +             usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
3304 +           set_sb_cmds(sb_desc, CMD_FULL);
3305 +         }
3306 +         
3307 +       } else { /* zero length packet */
3308 +         sb_desc = create_sb(sb_desc, TT_ZOUT, &zout_buffer[0], 1, mem_flags);
3309 +         if(sb_desc == NULL)
3310 +           return -ENOMEM;
3311 +         set_sb_cmds(sb_desc, CMD_FULL);
3312 +       }
3313 +       /* Attach first SB descriptor to URB */
3314 +       if (i == 0) {
3315 +         urb_priv->first_sb = sb_desc;
3316 +       }
3317 +      }
3318 +      /* Set interrupt and end-of-list flags in last SB */
3319 +      set_sb_cmds(sb_desc, CMD_INTR | CMD_EOL);
3320 +      /* Attach last SB descriptor to URB */
3321 +      urb_priv->last_sb = sb_desc;
3322 +      tc_dbg("Created %d out SBs for Isoc URB:0x%x\n",
3323 +              urb->number_of_packets, (unsigned int)urb);
3324 +    } else { /* In Isoc URB */
3325 +      /* Actual number of packets is not relevant for periodic in traffic as
3326 +        long as it is more than zero.  Set to 1 always. */
3327 +      sb_desc = create_sb(sb_desc, TT_IN, NULL, 1, mem_flags);
3328 +      if(sb_desc == NULL)
3329 +       return -ENOMEM;
3330 +      /* Set end-of-list flags for SB */
3331 +      set_sb_cmds(sb_desc, CMD_EOL);
3332 +
3333 +      /* Attach SB to URB */
3334 +      urb_priv->first_sb = sb_desc;
3335 +      urb_priv->last_sb = sb_desc;
3336 +    }
3337 +    break;
3338 +  default:
3339 +    tc_err("Unknown pipe-type\n");
3340 +    return -EPIPE;
3341 +    break;
3342 +  }
3343 +  return 0;
3344 +}
3345 +
3346 +int init_intr_urb(struct urb *urb, int mem_flags) {
3347 +  struct crisv10_urb_priv *urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
3348 +  struct USB_EP_Desc* ep_desc;
3349 +  int interval;
3350 +  int i;
3351 +  int ep_count;
3352 +
3353 +  ASSERT(urb_priv != NULL);
3354 +  ASSERT(usb_pipeint(urb->pipe));
3355 +  /* We can't support interval longer than amount of eof descriptors in
3356 +     TxIntrEPList */
3357 +  if(urb->interval > MAX_INTR_INTERVAL) {
3358 +    tc_err("Interrupt interval %dms too big (max: %dms)\n", urb->interval,
3359 +          MAX_INTR_INTERVAL);
3360 +    return -EINVAL;
3361 +  }
3362 +
3363 +  /* We assume that the SB descriptors already have been setup */
3364 +  ASSERT(urb_priv->first_sb != NULL);
3365 +
3366 +  /* Round of the interval to 2^n, it is obvious that this code favours
3367 +     smaller numbers, but that is actually a good thing */
3368 +  /* FIXME: The "rounding error" for larger intervals will be quite
3369 +     large. For in traffic this shouldn't be a problem since it will only
3370 +     mean that we "poll" more often. */
3371 +  interval = urb->interval;
3372 +  for (i = 0; interval; i++) {
3373 +    interval = interval >> 1;
3374 +  }
3375 +  urb_priv->interval = 1 << (i - 1);
3376 +
3377 +  /* We can only have max interval for Out Interrupt due to that we can only
3378 +     handle one linked in EP for a certain epid in the Intr descr array at the
3379 +     time. The USB Controller in the Etrax 100LX continues to process Intr EPs
3380 +     so we have no way of knowing which one that caused the actual transfer if
3381 +     we have several linked in. */
3382 +  if(usb_pipeout(urb->pipe)) {
3383 +    urb_priv->interval = MAX_INTR_INTERVAL;
3384 +  }
3385 +
3386 +  /* Calculate amount of EPs needed */
3387 +  ep_count = MAX_INTR_INTERVAL / urb_priv->interval;
3388 +
3389 +  for(i = 0; i < ep_count; i++) {
3390 +    ep_desc = create_ep(urb_priv->epid, urb_priv->first_sb, mem_flags);
3391 +    if(ep_desc == NULL) {
3392 +      /* Free any descriptors that we may have allocated before failure */
3393 +      while(i > 0) {
3394 +       i--;
3395 +       kfree(urb_priv->intr_ep_pool[i]);
3396 +      }
3397 +      return -ENOMEM;
3398 +    }
3399 +    urb_priv->intr_ep_pool[i] = ep_desc;
3400 +  }
3401 +  urb_priv->intr_ep_pool_length = ep_count;
3402 +  return 0;
3403 +}
3404 +
3405 +/* DMA RX/TX functions */
3406 +/* ----------------------- */
3407 +
3408 +static void tc_dma_init_rx_list(void) {
3409 +  int i;
3410 +
3411 +  /* Setup descriptor list except last one */
3412 +  for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
3413 +    RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
3414 +    RxDescList[i].command = 0;
3415 +    RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
3416 +    RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
3417 +    RxDescList[i].hw_len = 0;
3418 +    RxDescList[i].status = 0;
3419 +    
3420 +    /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as
3421 +       USB_IN_Desc for the relevant fields.) */
3422 +    prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
3423 +    
3424 +  }
3425 +  /* Special handling of last descriptor */
3426 +  RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
3427 +  RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
3428 +  RxDescList[i].next = virt_to_phys(&RxDescList[0]);
3429 +  RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
3430 +  RxDescList[i].hw_len = 0;
3431 +  RxDescList[i].status = 0;
3432 +  
3433 +  /* Setup list pointers that show progress in list */
3434 +  myNextRxDesc = &RxDescList[0];
3435 +  myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
3436 +  
3437 +  flush_etrax_cache();
3438 +  /* Point DMA to first descriptor in list and start it */
3439 +  *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
3440 +  *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
3441 +}
3442 +
3443 +
3444 +static void tc_dma_init_tx_bulk_list(void) {
3445 +  int i;
3446 +  volatile struct USB_EP_Desc *epDescr;
3447 +
3448 +  for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
3449 +    epDescr = &(TxBulkEPList[i]);
3450 +    CHECK_ALIGN(epDescr);
3451 +    epDescr->hw_len = 0;
3452 +    epDescr->command = IO_FIELD(USB_EP_command, epid, i);
3453 +    epDescr->sub = 0;
3454 +    epDescr->next = virt_to_phys(&TxBulkEPList[i + 1]);
3455 +
3456 +    /* Initiate two EPs, disabled and with the eol flag set. No need for any
3457 +       preserved epid. */
3458 +    
3459 +    /* The first one has the intr flag set so we get an interrupt when the DMA
3460 +       channel is about to become disabled. */
3461 +    CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
3462 +    TxBulkDummyEPList[i][0].hw_len = 0;
3463 +    TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
3464 +                                      IO_STATE(USB_EP_command, eol, yes) |
3465 +                                      IO_STATE(USB_EP_command, intr, yes));
3466 +    TxBulkDummyEPList[i][0].sub = 0;
3467 +    TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
3468 +    
3469 +    /* The second one. */
3470 +    CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
3471 +    TxBulkDummyEPList[i][1].hw_len = 0;
3472 +    TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
3473 +                                      IO_STATE(USB_EP_command, eol, yes));
3474 +    TxBulkDummyEPList[i][1].sub = 0;
3475 +    /* The last dummy's next pointer is the same as the current EP's next pointer. */
3476 +    TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
3477 +  }
3478 +
3479 +  /* Special handling of last descr in list, make list circular */
3480 +  epDescr = &TxBulkEPList[i];
3481 +  CHECK_ALIGN(epDescr);
3482 +  epDescr->hw_len = 0;
3483 +  epDescr->command = IO_STATE(USB_EP_command, eol, yes) |
3484 +    IO_FIELD(USB_EP_command, epid, i);
3485 +  epDescr->sub = 0;
3486 +  epDescr->next = virt_to_phys(&TxBulkEPList[0]);
3487 +  
3488 +  /* Init DMA sub-channel pointers to last item in each list */
3489 +  *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
3490 +  /* No point in starting the bulk channel yet.
3491 +   *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
3492 +}
3493 +
3494 +static void tc_dma_init_tx_ctrl_list(void) {
3495 +  int i;
3496 +  volatile struct USB_EP_Desc *epDescr;
3497 +
3498 +  for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
3499 +    epDescr = &(TxCtrlEPList[i]);
3500 +    CHECK_ALIGN(epDescr);
3501 +    epDescr->hw_len = 0;
3502 +    epDescr->command = IO_FIELD(USB_EP_command, epid, i);
3503 +    epDescr->sub = 0;
3504 +    epDescr->next = virt_to_phys(&TxCtrlEPList[i + 1]);
3505 +  }
3506 +  /* Special handling of last descr in list, make list circular */
3507 +  epDescr = &TxCtrlEPList[i];
3508 +  CHECK_ALIGN(epDescr);
3509 +  epDescr->hw_len = 0;
3510 +  epDescr->command = IO_STATE(USB_EP_command, eol, yes) |
3511 +    IO_FIELD(USB_EP_command, epid, i);
3512 +  epDescr->sub = 0;
3513 +  epDescr->next = virt_to_phys(&TxCtrlEPList[0]);
3514 +  
3515 +  /* Init DMA sub-channel pointers to last item in each list */
3516 +  *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[i]);
3517 +  /* No point in starting the ctrl channel yet.
3518 +   *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
3519 +}
3520 +
3521 +
3522 +static void tc_dma_init_tx_intr_list(void) {
3523 +  int i;
3524 +
3525 +  TxIntrSB_zout.sw_len = 1;
3526 +  TxIntrSB_zout.next = 0;
3527 +  TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
3528 +  TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
3529 +                          IO_STATE(USB_SB_command, tt, zout) |
3530 +                          IO_STATE(USB_SB_command, full, yes) |
3531 +                          IO_STATE(USB_SB_command, eot, yes) |
3532 +                          IO_STATE(USB_SB_command, eol, yes));
3533 +  
3534 +  for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
3535 +    CHECK_ALIGN(&TxIntrEPList[i]);
3536 +    TxIntrEPList[i].hw_len = 0;
3537 +    TxIntrEPList[i].command =
3538 +      (IO_STATE(USB_EP_command, eof, yes) |
3539 +       IO_STATE(USB_EP_command, enable, yes) |
3540 +       IO_FIELD(USB_EP_command, epid, INVALID_EPID));
3541 +    TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
3542 +    TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
3543 +  }
3544 +
3545 +  /* Special handling of last descr in list, make list circular */
3546 +  CHECK_ALIGN(&TxIntrEPList[i]);
3547 +  TxIntrEPList[i].hw_len = 0;
3548 +  TxIntrEPList[i].command =
3549 +    (IO_STATE(USB_EP_command, eof, yes) |
3550 +     IO_STATE(USB_EP_command, eol, yes) |
3551 +     IO_STATE(USB_EP_command, enable, yes) |
3552 +     IO_FIELD(USB_EP_command, epid, INVALID_EPID));
3553 +  TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
3554 +  TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
3555 +
3556 +  intr_dbg("Initiated Intr EP descriptor list\n");
3557 +
3558 +
3559 +  /* Connect DMA 8 sub-channel 2 to first in list */
3560 +  *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
3561 +}
3562 +
3563 +static void tc_dma_init_tx_isoc_list(void) {
3564 +  int i;
3565 +
3566 +  DBFENTER;
3567 +
3568 +  /* Read comment at zout_buffer declaration for an explanation to this. */
3569 +  TxIsocSB_zout.sw_len = 1;
3570 +  TxIsocSB_zout.next = 0;
3571 +  TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
3572 +  TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
3573 +                          IO_STATE(USB_SB_command, tt, zout) |
3574 +                          IO_STATE(USB_SB_command, full, yes) |
3575 +                          IO_STATE(USB_SB_command, eot, yes) |
3576 +                          IO_STATE(USB_SB_command, eol, yes));
3577 +
3578 +  /* The last isochronous EP descriptor is a dummy. */
3579 +  for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
3580 +    CHECK_ALIGN(&TxIsocEPList[i]);
3581 +    TxIsocEPList[i].hw_len = 0;
3582 +    TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
3583 +    TxIsocEPList[i].sub = 0;
3584 +    TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
3585 +  }
3586 +
3587 +  CHECK_ALIGN(&TxIsocEPList[i]);
3588 +  TxIsocEPList[i].hw_len = 0;
3589 +
3590 +  /* Must enable the last EP descr to get eof interrupt. */
3591 +  TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
3592 +                            IO_STATE(USB_EP_command, eof, yes) |
3593 +                            IO_STATE(USB_EP_command, eol, yes) |
3594 +                            IO_FIELD(USB_EP_command, epid, INVALID_EPID));
3595 +  TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
3596 +  TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
3597 +
3598 +  *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
3599 +  *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3600 +}
3601 +
3602 +static int tc_dma_init(struct usb_hcd *hcd) {
3603 +  tc_dma_init_rx_list();
3604 +  tc_dma_init_tx_bulk_list();
3605 +  tc_dma_init_tx_ctrl_list();
3606 +  tc_dma_init_tx_intr_list();
3607 +  tc_dma_init_tx_isoc_list();
3608 +
3609 +  if (cris_request_dma(USB_TX_DMA_NBR,
3610 +                      "ETRAX 100LX built-in USB (Tx)",
3611 +                      DMA_VERBOSE_ON_ERROR,
3612 +                      dma_usb)) {
3613 +    err("Could not allocate DMA ch 8 for USB");
3614 +    return -EBUSY;
3615 +  }
3616 +       
3617 +  if (cris_request_dma(USB_RX_DMA_NBR,
3618 +                      "ETRAX 100LX built-in USB (Rx)",
3619 +                      DMA_VERBOSE_ON_ERROR,
3620 +                      dma_usb)) {
3621 +    err("Could not allocate DMA ch 9 for USB");
3622 +    return -EBUSY;
3623 +  }
3624 +
3625 +  *R_IRQ_MASK2_SET =
3626 +    /* Note that these interrupts are not used. */
3627 +    IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
3628 +    /* Sub channel 1 (ctrl) descr. interrupts are used. */
3629 +    IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
3630 +    IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
3631 +    /* Sub channel 3 (isoc) descr. interrupts are used. */
3632 +    IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
3633 +  
3634 +  /* Note that the dma9_descr interrupt is not used. */
3635 +  *R_IRQ_MASK2_SET =
3636 +    IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
3637 +    IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
3638 +
3639 +  if (request_irq(ETRAX_USB_RX_IRQ, tc_dma_rx_interrupt, 0,
3640 +                 "ETRAX 100LX built-in USB (Rx)", hcd)) {
3641 +    err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
3642 +    return -EBUSY;
3643 +  }
3644 +  
3645 +  if (request_irq(ETRAX_USB_TX_IRQ, tc_dma_tx_interrupt, 0,
3646 +                 "ETRAX 100LX built-in USB (Tx)", hcd)) {
3647 +    err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
3648 +    return -EBUSY;
3649 +  }
3650 +
3651 +  return 0;
3652 +}
3653 +
3654 +static void tc_dma_destroy(void) {
3655 +  free_irq(ETRAX_USB_RX_IRQ, NULL);
3656 +  free_irq(ETRAX_USB_TX_IRQ, NULL);
3657 +
3658 +  cris_free_dma(USB_TX_DMA_NBR, "ETRAX 100LX built-in USB (Tx)");
3659 +  cris_free_dma(USB_RX_DMA_NBR, "ETRAX 100LX built-in USB (Rx)");
3660 +
3661 +}
3662 +
3663 +static void tc_dma_link_intr_urb(struct urb *urb);
3664 +
3665 +/* Handle processing of Bulk, Ctrl and Intr queues */
3666 +static void tc_dma_process_queue(int epid) {
3667 +  struct urb *urb;
3668 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
3669 +  unsigned long flags;
3670 +  char toggle;
3671 +
3672 +  if(epid_state[epid].disabled) {
3673 +    /* Don't process any URBs on a disabled endpoint */
3674 +    return;
3675 +  }
3676 +
3677 +  /* Do not disturb us while fiddling with EPs and epids */
3678 +  local_irq_save(flags);
3679 +
3680 +  /* For bulk, Ctrl and Intr can we only have one URB active at a time for
3681 +     a specific EP. */
3682 +  if(activeUrbList[epid] != NULL) {
3683 +    /* An URB is already active on EP, skip checking queue */
3684 +    local_irq_restore(flags);
3685 +    return;
3686 +  }
3687 +
3688 +  urb = urb_list_first(epid);
3689 +  if(urb == NULL) {
3690 +    /* No URB waiting in EP queue. Nothing do to */
3691 +    local_irq_restore(flags);
3692 +    return;
3693 +  }
3694 +
3695 +  urb_priv = urb->hcpriv;
3696 +  ASSERT(urb_priv != NULL);
3697 +  ASSERT(urb_priv->urb_state == NOT_STARTED);
3698 +  ASSERT(!usb_pipeisoc(urb->pipe));
3699 +
3700 +  /* Remove this URB from the queue and move it to active */
3701 +  activeUrbList[epid] = urb;
3702 +  urb_list_del(urb, epid);
3703 +
3704 +  urb_priv->urb_state = STARTED;
3705 +
3706 +  /* Reset error counters (regardless of which direction this traffic is). */
3707 +  etrax_epid_clear_error(epid);
3708 +
3709 +  /* Special handling of Intr EP lists */
3710 +  if(usb_pipeint(urb->pipe)) {
3711 +    tc_dma_link_intr_urb(urb);
3712 +    local_irq_restore(flags);
3713 +    return;
3714 +  }
3715 +
3716 +  /* Software must preset the toggle bits for Bulk and Ctrl */
3717 +  if(usb_pipecontrol(urb->pipe)) {
3718 +    /* Toggle bits are initialized only during setup transaction in a
3719 +       CTRL transfer */
3720 +    etrax_epid_set_toggle(epid, 0, 0);
3721 +    etrax_epid_set_toggle(epid, 1, 0);
3722 +  } else {
3723 +    toggle = usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
3724 +                          usb_pipeout(urb->pipe));
3725 +    etrax_epid_set_toggle(epid, usb_pipeout(urb->pipe), toggle);
3726 +  }
3727 +
3728 +  tc_dbg("Added SBs from (URB:0x%x %s %s) to epid %d: %s\n",
3729 +        (unsigned int)urb, str_dir(urb->pipe), str_type(urb->pipe), epid,
3730 +        sblist_to_str(urb_priv->first_sb));
3731 +
3732 +  /* We start the DMA sub channel without checking if it's running or not,
3733 +     because:
3734 +     1) If it's already running, issuing the start command is a nop.
3735 +     2) We avoid a test-and-set race condition. */
3736 +  switch(usb_pipetype(urb->pipe)) {
3737 +  case PIPE_BULK:
3738 +    /* Assert that the EP descriptor is disabled. */
3739 +    ASSERT(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
3740 +
3741 +    /* Set up and enable the EP descriptor. */
3742 +    TxBulkEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3743 +    TxBulkEPList[epid].hw_len = 0;
3744 +    TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3745 +
3746 +    /* Check if the dummy list is already with us (if several urbs were queued). */
3747 +    if (usb_pipein(urb->pipe) && (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0]))) {
3748 +      tc_dbg("Inviting dummy list to the party for urb 0x%lx, epid %d", 
3749 +            (unsigned long)urb, epid);
3750 +      
3751 +      /* We don't need to check if the DMA is at this EP or not before changing the
3752 +        next pointer, since we will do it in one 32-bit write (EP descriptors are
3753 +        32-bit aligned). */
3754 +      TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
3755 +    }
3756 +
3757 +    restart_dma8_sub0();
3758 +
3759 +    /* Update/restart the bulk start timer since we just started the channel.*/
3760 +    mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
3761 +    /* Update/restart the bulk eot timer since we just inserted traffic. */
3762 +    mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3763 +    break;
3764 +  case PIPE_CONTROL:
3765 +    /* Assert that the EP descriptor is disabled. */
3766 +    ASSERT(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
3767 +
3768 +    /* Set up and enable the EP descriptor. */
3769 +    TxCtrlEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3770 +    TxCtrlEPList[epid].hw_len = 0;
3771 +    TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3772 +
3773 +    *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
3774 +    break;
3775 +  }
3776 +  local_irq_restore(flags);
3777 +}
3778 +
3779 +static void tc_dma_link_intr_urb(struct urb *urb) {
3780 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
3781 +  volatile struct USB_EP_Desc *tmp_ep;
3782 +  struct USB_EP_Desc *ep_desc;
3783 +  int i = 0, epid;
3784 +  int pool_idx = 0;
3785 +
3786 +  ASSERT(urb_priv != NULL);
3787 +  epid = urb_priv->epid;
3788 +  ASSERT(urb_priv->interval > 0);
3789 +  ASSERT(urb_priv->intr_ep_pool_length > 0);
3790 +
3791 +  tmp_ep = &TxIntrEPList[0];
3792 +
3793 +  /* Only insert one EP descriptor in list for Out Intr URBs.
3794 +     We can only handle Out Intr with interval of 128ms because
3795 +     it's not possible to insert several Out Intr EPs because they
3796 +     are not consumed by the DMA. */
3797 +  if(usb_pipeout(urb->pipe)) {
3798 +    ep_desc = urb_priv->intr_ep_pool[0];
3799 +    ASSERT(ep_desc);
3800 +    ep_desc->next = tmp_ep->next;
3801 +    tmp_ep->next = virt_to_phys(ep_desc);
3802 +    i++;
3803 +  } else {
3804 +    /* Loop through Intr EP descriptor list and insert EP for URB at
3805 +       specified interval */
3806 +    do {
3807 +      /* Each EP descriptor with eof flag sat signals a new frame */
3808 +      if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
3809 +       /* Insert a EP from URBs EP pool at correct interval */
3810 +       if ((i % urb_priv->interval) == 0) {
3811 +         ep_desc = urb_priv->intr_ep_pool[pool_idx];
3812 +         ASSERT(ep_desc);
3813 +         ep_desc->next = tmp_ep->next;
3814 +         tmp_ep->next = virt_to_phys(ep_desc);
3815 +         pool_idx++;
3816 +         ASSERT(pool_idx <= urb_priv->intr_ep_pool_length);
3817 +       }
3818 +       i++;
3819 +      }
3820 +      tmp_ep = (struct USB_EP_Desc *)phys_to_virt(tmp_ep->next);
3821 +    } while(tmp_ep != &TxIntrEPList[0]);
3822 +  }
3823 +
3824 +  intr_dbg("Added SBs to intr epid %d: %s interval:%d (%d EP)\n", epid,
3825 +          sblist_to_str(urb_priv->first_sb), urb_priv->interval, pool_idx);
3826 +
3827 +  /* We start the DMA sub channel without checking if it's running or not,
3828 +     because:
3829 +     1) If it's already running, issuing the start command is a nop.
3830 +     2) We avoid a test-and-set race condition. */
3831 +  *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
3832 +}
3833 +
3834 +  /* hinko ignore usb_pipeisoc */
3835 +#if 0
3836 +static void tc_dma_process_isoc_urb(struct urb *urb) {
3837 +  unsigned long flags;
3838 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
3839 +  int epid;
3840 +
3841 +  /* Do not disturb us while fiddling with EPs and epids */
3842 +  local_irq_save(flags);
3843 +
3844 +  ASSERT(urb_priv);
3845 +  ASSERT(urb_priv->first_sb);
3846 +  epid = urb_priv->epid;
3847 +
3848 +  if(activeUrbList[epid] == NULL) {
3849 +    /* EP is idle, so make this URB active */
3850 +    activeUrbList[epid] = urb;
3851 +    urb_list_del(urb, epid);
3852 +    ASSERT(TxIsocEPList[epid].sub == 0);
3853 +    ASSERT(!(TxIsocEPList[epid].command &
3854 +            IO_STATE(USB_EP_command, enable, yes)));
3855 +
3856 +    /* Differentiate between In and Out Isoc. Because In SBs are not consumed*/
3857 +    if(usb_pipein(urb->pipe)) {
3858 +    /* Each EP for In Isoc will have only one SB descriptor, setup when
3859 +       submitting the first active urb. We do it here by copying from URBs
3860 +       pre-allocated SB. */
3861 +      memcpy((void *)&(TxIsocSBList[epid]), urb_priv->first_sb,
3862 +            sizeof(TxIsocSBList[epid]));
3863 +      TxIsocEPList[epid].hw_len = 0;
3864 +      TxIsocEPList[epid].sub = virt_to_phys(&(TxIsocSBList[epid]));
3865 +    } else {
3866 +      /* For Out Isoc we attach the pre-allocated list of SBs for the URB */
3867 +      TxIsocEPList[epid].hw_len = 0;
3868 +      TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3869 +
3870 +      isoc_dbg("Attached first URB:0x%x[%d] to epid:%d first_sb:0x%x"
3871 +              " last_sb::0x%x\n",
3872 +              (unsigned int)urb, urb_priv->urb_num, epid,
3873 +              (unsigned int)(urb_priv->first_sb),
3874 +              (unsigned int)(urb_priv->last_sb));
3875 +    }
3876 +
3877 +    if (urb->transfer_flags & URB_ISO_ASAP) {
3878 +      /* The isoc transfer should be started as soon as possible. The
3879 +        start_frame field is a return value if URB_ISO_ASAP was set. Comparing
3880 +        R_USB_FM_NUMBER with a USB Chief trace shows that the first isoc IN
3881 +        token is sent 2 frames later. I'm not sure how this affects usage of
3882 +        the start_frame field by the device driver, or how it affects things
3883 +        when USB_ISO_ASAP is not set, so therefore there's no compensation for
3884 +        the 2 frame "lag" here. */
3885 +      urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3886 +      TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3887 +      urb_priv->urb_state = STARTED;
3888 +      isoc_dbg("URB_ISO_ASAP set, urb->start_frame set to %d\n",
3889 +              urb->start_frame);
3890 +    } else {
3891 +      /* Not started yet. */
3892 +      urb_priv->urb_state = NOT_STARTED;
3893 +      isoc_warn("urb_priv->urb_state set to NOT_STARTED for URB:0x%x\n",
3894 +               (unsigned int)urb);
3895 +    }
3896 +
3897 +  } else {
3898 +    /* An URB is already active on the EP. Leave URB in queue and let
3899 +       finish_isoc_urb process it after current active URB */
3900 +    ASSERT(TxIsocEPList[epid].sub != 0);
3901 +
3902 +    if(usb_pipein(urb->pipe)) {
3903 +      /* Because there already is a active In URB on this epid we do nothing
3904 +         and the finish_isoc_urb() function will handle switching to next URB*/
3905 +
3906 +    } else { /* For Out Isoc, insert new URBs traffic last in SB-list. */
3907 +      struct USB_SB_Desc *temp_sb_desc;
3908 +
3909 +      /* Set state STARTED to all Out Isoc URBs added to SB list because we
3910 +         don't know how many of them that are finished before descr interrupt*/
3911 +      urb_priv->urb_state = STARTED;
3912 +
3913 +      /* Find end of current SB list by looking for SB with eol flag sat */
3914 +      temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
3915 +      while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
3916 +            IO_STATE(USB_SB_command, eol, yes)) {
3917 +       ASSERT(temp_sb_desc->next);
3918 +       temp_sb_desc = phys_to_virt(temp_sb_desc->next);
3919 +      }
3920 +
3921 +      isoc_dbg("Appended URB:0x%x[%d] (first:0x%x last:0x%x) to epid:%d"
3922 +              " sub:0x%x eol:0x%x\n",
3923 +              (unsigned int)urb, urb_priv->urb_num,
3924 +              (unsigned int)(urb_priv->first_sb),
3925 +              (unsigned int)(urb_priv->last_sb), epid,
3926 +              (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
3927 +              (unsigned int)temp_sb_desc);
3928 +
3929 +      /* Next pointer must be set before eol is removed. */
3930 +      temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
3931 +      /* Clear the previous end of list flag since there is a new in the
3932 +        added SB descriptor list. */
3933 +      temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
3934 +
3935 +      if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
3936 +       __u32 epid_data;
3937 +       /* 8.8.5 in Designer's Reference says we should check for and correct
3938 +          any errors in the EP here.  That should not be necessary if
3939 +          epid_attn is handled correctly, so we assume all is ok. */
3940 +       epid_data = etrax_epid_iso_get(epid);
3941 +       if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) !=
3942 +           IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3943 +         isoc_err("Disabled Isoc EP with error:%d on epid:%d when appending"
3944 +                  " URB:0x%x[%d]\n",
3945 +                  IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data), epid,
3946 +                  (unsigned int)urb, urb_priv->urb_num);
3947 +       }
3948 +
3949 +       /* The SB list was exhausted. */
3950 +       if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
3951 +         /* The new sublist did not get processed before the EP was
3952 +            disabled.  Setup the EP again. */
3953 +
3954 +         if(virt_to_phys(temp_sb_desc) == TxIsocEPList[epid].sub) {
3955 +           isoc_dbg("EP for epid:%d stoped at SB:0x%x before newly inserted"
3956 +                    ", restarting from this URBs SB:0x%x\n",
3957 +                    epid, (unsigned int)temp_sb_desc,
3958 +                    (unsigned int)(urb_priv->first_sb));
3959 +           TxIsocEPList[epid].hw_len = 0;
3960 +           TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3961 +           urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3962 +           /* Enable the EP again so data gets processed this time */
3963 +           TxIsocEPList[epid].command |=
3964 +             IO_STATE(USB_EP_command, enable, yes);
3965 +
3966 +         } else {
3967 +           /* The EP has been disabled but not at end this URB (god knows
3968 +              where). This should generate an epid_attn so we should not be
3969 +              here */
3970 +           isoc_warn("EP was disabled on sb:0x%x before SB list for"
3971 +                    " URB:0x%x[%d] got processed\n",
3972 +                    (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
3973 +                    (unsigned int)urb, urb_priv->urb_num);
3974 +         }
3975 +       } else {
3976 +         /* This might happend if we are slow on this function and isn't
3977 +            an error. */
3978 +         isoc_dbg("EP was disabled and finished with SBs from appended"
3979 +                  " URB:0x%x[%d]\n", (unsigned int)urb, urb_priv->urb_num);
3980 +       }
3981 +      }
3982 +    }
3983 +  }
3984 +  
3985 +  /* Start the DMA sub channel */
3986 +  *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3987 +
3988 +  local_irq_restore(flags);
3989 +}
3990 +#endif
3991 +
3992 +static void tc_dma_unlink_intr_urb(struct urb *urb) {
3993 +  struct crisv10_urb_priv *urb_priv = urb->hcpriv;
3994 +  volatile struct USB_EP_Desc *first_ep;  /* First EP in the list. */
3995 +  volatile struct USB_EP_Desc *curr_ep;   /* Current EP, the iterator. */
3996 +  volatile struct USB_EP_Desc *next_ep;   /* The EP after current. */
3997 +  volatile struct USB_EP_Desc *unlink_ep; /* The one we should remove from
3998 +                                            the list. */
3999 +  int count = 0;
4000 +  volatile int timeout = 10000;
4001 +  int epid;
4002 +
4003 +  /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the
4004 +     List". */
4005 +  ASSERT(urb_priv);
4006 +  ASSERT(urb_priv->intr_ep_pool_length > 0);
4007 +  epid = urb_priv->epid;
4008 +
4009 +  /* First disable all Intr EPs belonging to epid for this URB */
4010 +  first_ep = &TxIntrEPList[0];
4011 +  curr_ep = first_ep;
4012 +  do {
4013 +    next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
4014 +    if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
4015 +      /* Disable EP */
4016 +      next_ep->command &= ~IO_MASK(USB_EP_command, enable);
4017 +    }
4018 +    curr_ep = phys_to_virt(curr_ep->next);
4019 +  } while (curr_ep != first_ep);
4020 +
4021 +
4022 +  /* Now unlink all EPs belonging to this epid from Descr list */
4023 +  first_ep = &TxIntrEPList[0];
4024 +  curr_ep = first_ep;
4025 +  do {
4026 +    next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
4027 +    if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
4028 +      /* This is the one we should unlink. */
4029 +      unlink_ep = next_ep;
4030 +
4031 +      /* Actually unlink the EP from the DMA list. */
4032 +      curr_ep->next = unlink_ep->next;
4033 +
4034 +      /* Wait until the DMA is no longer at this descriptor. */
4035 +      while((*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep)) &&
4036 +           (timeout-- > 0));
4037 +      if(timeout == 0) {
4038 +       warn("Timeout while waiting for DMA-TX-Intr to leave unlink EP\n");
4039 +      }
4040 +      
4041 +      count++;
4042 +    }
4043 +    curr_ep = phys_to_virt(curr_ep->next);
4044 +  } while (curr_ep != first_ep);
4045 +
4046 +  if(count != urb_priv->intr_ep_pool_length) {
4047 +    intr_warn("Unlinked %d of %d Intr EPs for URB:0x%x[%d]\n", count,
4048 +             urb_priv->intr_ep_pool_length, (unsigned int)urb,
4049 +             urb_priv->urb_num);
4050 +  } else {
4051 +    intr_dbg("Unlinked %d of %d interrupt EPs for URB:0x%x\n", count,
4052 +            urb_priv->intr_ep_pool_length, (unsigned int)urb);
4053 +  }
4054 +}
4055 +
4056 +static void check_finished_bulk_tx_epids(struct usb_hcd *hcd,
4057 +                                                   int timer) {
4058 +  unsigned long flags;
4059 +  int epid;
4060 +  struct urb *urb;
4061 +  struct crisv10_urb_priv * urb_priv;
4062 +  __u32 epid_data;
4063 +
4064 +  /* Protect TxEPList */
4065 +  local_irq_save(flags);
4066 +
4067 +  for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
4068 +    /* A finished EP descriptor is disabled and has a valid sub pointer */
4069 +    if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
4070 +       (TxBulkEPList[epid].sub != 0)) {
4071 +
4072 +      /* Get the active URB for this epid */
4073 +      urb = activeUrbList[epid];
4074 +      /* Sanity checks */
4075 +      ASSERT(urb);
4076 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4077 +      ASSERT(urb_priv);
4078 +      
4079 +      /* Only handle finished out Bulk EPs here,
4080 +        and let RX interrupt take care of the rest */
4081 +      if(!epid_out_traffic(epid)) {
4082 +       continue;
4083 +      }
4084 +
4085 +      if(timer) {
4086 +       tc_warn("Found finished %s Bulk epid:%d URB:0x%x[%d] from timeout\n",
4087 +               epid_out_traffic(epid) ? "Out" : "In", epid, (unsigned int)urb,
4088 +               urb_priv->urb_num);
4089 +      } else {
4090 +       tc_dbg("Found finished %s Bulk epid:%d URB:0x%x[%d] from interrupt\n",
4091 +              epid_out_traffic(epid) ? "Out" : "In", epid, (unsigned int)urb,
4092 +              urb_priv->urb_num);
4093 +      }
4094 +
4095 +      if(urb_priv->urb_state == UNLINK) {
4096 +       /* This Bulk URB is requested to be unlinked, that means that the EP
4097 +          has been disabled and we might not have sent all data */
4098 +       tc_finish_urb(hcd, urb, urb->status);
4099 +       continue;
4100 +      }
4101 +
4102 +      ASSERT(urb_priv->urb_state == STARTED);
4103 +      if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
4104 +       tc_err("Endpoint got disabled before reaching last sb\n");
4105 +      }
4106 +       
4107 +      epid_data = etrax_epid_get(epid);
4108 +      if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) ==
4109 +         IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
4110 +       /* This means that the endpoint has no error, is disabled
4111 +          and had inserted traffic, i.e. transfer successfully completed. */
4112 +       tc_finish_urb(hcd, urb, 0);
4113 +      } else {
4114 +       /* Shouldn't happen. We expect errors to be caught by epid
4115 +          attention. */
4116 +       tc_err("Found disabled bulk EP desc (epid:%d error:%d)\n",
4117 +              epid, IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data));
4118 +      }
4119 +    } else {
4120 +      tc_dbg("Ignoring In Bulk epid:%d, let RX interrupt handle it\n", epid);
4121 +    }
4122 +  }
4123 +
4124 +  local_irq_restore(flags);
4125 +}
4126 +
4127 +static void check_finished_ctrl_tx_epids(struct usb_hcd *hcd) {
4128 +  unsigned long flags;
4129 +  int epid;
4130 +  struct urb *urb;
4131 +  struct crisv10_urb_priv * urb_priv;
4132 +  __u32 epid_data;
4133 +
4134 +  /* Protect TxEPList */
4135 +  local_irq_save(flags);
4136 +
4137 +  for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
4138 +    if(epid == DUMMY_EPID)
4139 +      continue;
4140 +
4141 +    /* A finished EP descriptor is disabled and has a valid sub pointer */
4142 +    if (!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
4143 +       (TxCtrlEPList[epid].sub != 0)) {
4144 +      
4145 +      /* Get the active URB for this epid */
4146 +      urb = activeUrbList[epid];
4147 +
4148 +      if(urb == NULL) {
4149 +       tc_warn("Found finished Ctrl epid:%d with no active URB\n", epid);
4150 +       continue;
4151 +      }
4152 +      
4153 +      /* Sanity checks */
4154 +      ASSERT(usb_pipein(urb->pipe));
4155 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4156 +      ASSERT(urb_priv);
4157 +      if (phys_to_virt(TxCtrlEPList[epid].sub) != urb_priv->last_sb) {
4158 +       tc_err("Endpoint got disabled before reaching last sb\n");
4159 +      }
4160 +
4161 +      epid_data = etrax_epid_get(epid);
4162 +      if (IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data) ==
4163 +         IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
4164 +       /* This means that the endpoint has no error, is disabled
4165 +          and had inserted traffic, i.e. transfer successfully completed. */
4166 +
4167 +       /* Check if RX-interrupt for In Ctrl has been processed before
4168 +          finishing the URB */
4169 +       if(urb_priv->ctrl_rx_done) {
4170 +         tc_dbg("Finishing In Ctrl URB:0x%x[%d] in tx_interrupt\n",
4171 +                (unsigned int)urb, urb_priv->urb_num);
4172 +         tc_finish_urb(hcd, urb, 0);
4173 +       } else {
4174 +         /* If we get zout descriptor interrupt before RX was done for a
4175 +            In Ctrl transfer, then we flag that and it will be finished
4176 +            in the RX-Interrupt */
4177 +         urb_priv->ctrl_zout_done = 1;
4178 +         tc_dbg("Got zout descr interrupt before RX interrupt\n");
4179 +       }
4180 +      } else {
4181 +       /* Shouldn't happen. We expect errors to be caught by epid
4182 +          attention. */
4183 +       tc_err("Found disabled Ctrl EP desc (epid:%d URB:0x%x[%d]) error_code:%d\n", epid, (unsigned int)urb, urb_priv->urb_num, IO_EXTRACT(R_USB_EPT_DATA, error_code, epid_data));
4184 +       __dump_ep_desc(&(TxCtrlEPList[epid]));
4185 +       __dump_ept_data(epid);
4186 +      }      
4187 +    }
4188 +  }
4189 +  local_irq_restore(flags);
4190 +}
4191 +
4192 +  /* hinko ignore usb_pipeisoc */
4193 +#if 0
4194 +/* This function goes through all epids that are setup for Out Isoc transfers
4195 +   and marks (isoc_out_done) all queued URBs that the DMA has finished
4196 +   transfer for.
4197 +   No URB completetion is done here to make interrupt routine return quickly.
4198 +   URBs are completed later with help of complete_isoc_bottom_half() that
4199 +   becomes schedules when this functions is finished. */
4200 +static void check_finished_isoc_tx_epids(void) {
4201 +  unsigned long flags;
4202 +  int epid;
4203 +  struct urb *urb;
4204 +  struct crisv10_urb_priv * urb_priv;
4205 +  struct USB_SB_Desc* sb_desc;
4206 +  int epid_done;
4207 +
4208 +  /* Protect TxIsocEPList */
4209 +  local_irq_save(flags);
4210 +
4211 +  for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
4212 +    if (TxIsocEPList[epid].sub == 0 || epid == INVALID_EPID ||
4213 +       !epid_out_traffic(epid)) {
4214 +      /* Nothing here to see. */
4215 +      continue;
4216 +    }
4217 +    ASSERT(epid_inuse(epid));
4218 +    ASSERT(epid_isoc(epid));
4219 +
4220 +    sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
4221 +    /* Find the last descriptor of the currently active URB for this ep.
4222 +       This is the first descriptor in the sub list marked for a descriptor
4223 +       interrupt. */
4224 +    while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
4225 +      sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
4226 +    }
4227 +    ASSERT(sb_desc);
4228 +
4229 +    isoc_dbg("Descr IRQ checking epid:%d sub:0x%x intr:0x%x\n",
4230 +            epid, (unsigned int)phys_to_virt(TxIsocEPList[epid].sub),
4231 +            (unsigned int)sb_desc);
4232 +
4233 +    urb = activeUrbList[epid];
4234 +    if(urb == NULL) {
4235 +      isoc_err("Isoc Descr irq on epid:%d with no active URB\n", epid);
4236 +      continue;
4237 +    }
4238 +
4239 +    epid_done = 0;
4240 +    while(urb && !epid_done) {
4241 +      /* Sanity check. */
4242 +      ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
4243 +      ASSERT(usb_pipeout(urb->pipe));
4244 +      
4245 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4246 +      ASSERT(urb_priv);
4247 +      ASSERT(urb_priv->urb_state == STARTED ||
4248 +            urb_priv->urb_state == UNLINK);
4249 +      
4250 +      if (sb_desc != urb_priv->last_sb) {
4251 +       /* This urb has been sent. */
4252 +       urb_priv->isoc_out_done = 1;
4253 +
4254 +      } else { /* Found URB that has last_sb as the interrupt reason */
4255 +
4256 +       /* Check if EP has been disabled, meaning that all transfers are done*/
4257 +       if(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
4258 +         ASSERT((sb_desc->command & IO_MASK(USB_SB_command, eol)) ==
4259 +                IO_STATE(USB_SB_command, eol, yes));
4260 +         ASSERT(sb_desc->next == 0);
4261 +         urb_priv->isoc_out_done = 1;
4262 +       } else {
4263 +         isoc_dbg("Skipping URB:0x%x[%d] because EP not disabled yet\n",
4264 +                  (unsigned int)urb, urb_priv->urb_num);
4265 +       }
4266 +       /* Stop looking any further in queue */
4267 +       epid_done = 1;  
4268 +      }
4269 +
4270 +      if (!epid_done) {
4271 +       if(urb == activeUrbList[epid]) {
4272 +         urb = urb_list_first(epid);
4273 +       } else {
4274 +         urb = urb_list_next(urb, epid);
4275 +       }
4276 +      }
4277 +    } /* END: while(urb && !epid_done) */
4278 +  }
4279 +
4280 +  local_irq_restore(flags);
4281 +}
4282 +
4283 +
4284 +/* This is where the Out Isoc URBs are realy completed. This function is
4285 +   scheduled from tc_dma_tx_interrupt() when one or more Out Isoc transfers
4286 +   are done. This functions completes all URBs earlier marked with
4287 +   isoc_out_done by fast interrupt routine check_finished_isoc_tx_epids() */
4288 +
4289 +static void complete_isoc_bottom_half(void *data) {
4290 +  struct crisv10_isoc_complete_data *comp_data;
4291 +  struct usb_iso_packet_descriptor *packet;
4292 +  struct crisv10_urb_priv * urb_priv;
4293 +  unsigned long flags;
4294 +  struct urb* urb;
4295 +  int epid_done;
4296 +  int epid;
4297 +  int i;
4298 +
4299 +  comp_data = (struct crisv10_isoc_complete_data*)data;
4300 +
4301 +  local_irq_save(flags);
4302 +
4303 +  for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
4304 +    if(!epid_inuse(epid) || !epid_isoc(epid) || !epid_out_traffic(epid) || epid == DUMMY_EPID) {
4305 +      /* Only check valid Out Isoc epids */
4306 +      continue;
4307 +    }
4308 +
4309 +    isoc_dbg("Isoc bottom-half checking epid:%d, sub:0x%x\n", epid,
4310 +            (unsigned int)phys_to_virt(TxIsocEPList[epid].sub));
4311 +
4312 +    /* The descriptor interrupt handler has marked all transmitted Out Isoc
4313 +       URBs with isoc_out_done.  Now we traverse all epids and for all that
4314 +       have out Isoc traffic we traverse its URB list and complete the
4315 +       transmitted URBs. */
4316 +    epid_done = 0;
4317 +    while (!epid_done) {
4318 +
4319 +      /* Get the active urb (if any) */
4320 +      urb = activeUrbList[epid];
4321 +      if (urb == 0) {
4322 +       isoc_dbg("No active URB on epid:%d anymore\n", epid);
4323 +       epid_done = 1;
4324 +       continue;
4325 +      }
4326 +
4327 +      /* Sanity check. */
4328 +      ASSERT(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
4329 +      ASSERT(usb_pipeout(urb->pipe));
4330 +
4331 +      urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4332 +      ASSERT(urb_priv);
4333 +
4334 +      if (!(urb_priv->isoc_out_done)) {
4335 +       /* We have reached URB that isn't flaged done yet, stop traversing. */
4336 +       isoc_dbg("Stoped traversing Out Isoc URBs on epid:%d"
4337 +                " before not yet flaged URB:0x%x[%d]\n",
4338 +                epid, (unsigned int)urb, urb_priv->urb_num);
4339 +       epid_done = 1;
4340 +       continue;
4341 +      }
4342 +
4343 +      /* This urb has been sent. */
4344 +      isoc_dbg("Found URB:0x%x[%d] that is flaged isoc_out_done\n",
4345 +              (unsigned int)urb, urb_priv->urb_num);
4346 +
4347 +      /* Set ok on transfered packets for this URB and finish it */
4348 +      for (i = 0; i < urb->number_of_packets; i++) {
4349 +       packet = &urb->iso_frame_desc[i];
4350 +       packet->status = 0;
4351 +       packet->actual_length = packet->length;
4352 +      }
4353 +      urb_priv->isoc_packet_counter = urb->number_of_packets;
4354 +      tc_finish_urb(comp_data->hcd, urb, 0);
4355 +
4356 +    } /* END: while(!epid_done) */
4357 +  } /* END: for(epid...) */
4358 +
4359 +  local_irq_restore(flags);
4360 +  kmem_cache_free(isoc_compl_cache, comp_data);
4361 +}
4362 +#endif
4363 +
4364 +static void check_finished_intr_tx_epids(struct usb_hcd *hcd) {
4365 +  unsigned long flags;
4366 +  int epid;
4367 +  struct urb *urb;
4368 +  struct crisv10_urb_priv * urb_priv;
4369 +  volatile struct USB_EP_Desc *curr_ep;   /* Current EP, the iterator. */
4370 +  volatile struct USB_EP_Desc *next_ep;   /* The EP after current. */
4371 +
4372 +  /* Protect TxintrEPList */
4373 +  local_irq_save(flags);
4374 +
4375 +  for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
4376 +    if(!epid_inuse(epid) || !epid_intr(epid) || !epid_out_traffic(epid)) {
4377 +      /* Nothing to see on this epid. Only check valid Out Intr epids */
4378 +      continue;
4379 +    }
4380 +
4381 +    urb = activeUrbList[epid];
4382 +    if(urb == 0) {
4383 +      intr_warn("Found Out Intr epid:%d with no active URB\n", epid);
4384 +      continue;
4385 +    }
4386 +
4387 +    /* Sanity check. */
4388 +    ASSERT(usb_pipetype(urb->pipe) == PIPE_INTERRUPT);
4389 +    ASSERT(usb_pipeout(urb->pipe));
4390 +    
4391 +    urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4392 +    ASSERT(urb_priv);
4393 +
4394 +    /* Go through EPs between first and second sof-EP. It's here Out Intr EPs
4395 +       are inserted.*/
4396 +    curr_ep = &TxIntrEPList[0];
4397 +    do {
4398 +      next_ep = (struct USB_EP_Desc *)phys_to_virt(curr_ep->next);
4399 +      if(next_ep == urb_priv->intr_ep_pool[0]) {
4400 +       /* We found the Out Intr EP for this epid */
4401 +       
4402 +       /* Disable it so it doesn't get processed again */
4403 +       next_ep->command &= ~IO_MASK(USB_EP_command, enable);
4404 +
4405 +       /* Finish the active Out Intr URB with status OK */
4406 +       tc_finish_urb(hcd, urb, 0);
4407 +      }
4408 +      curr_ep = phys_to_virt(curr_ep->next);
4409 +    } while (curr_ep != &TxIntrEPList[1]);
4410 +
4411 +  }
4412 +  local_irq_restore(flags);
4413 +}
4414 +
4415 +/* Interrupt handler for DMA8/IRQ24 with subchannels (called from hardware intr) */
4416 +static irqreturn_t tc_dma_tx_interrupt(int irq, void *vhc) {
4417 +  struct usb_hcd *hcd = (struct usb_hcd*)vhc;
4418 +  ASSERT(hcd);
4419 +
4420 +  if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
4421 +    /* Clear this interrupt */
4422 +    *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
4423 +    restart_dma8_sub0();
4424 +  }
4425 +
4426 +  if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
4427 +    /* Clear this interrupt */
4428 +    *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
4429 +    check_finished_ctrl_tx_epids(hcd);
4430 +  }
4431 +
4432 +  if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
4433 +    /* Clear this interrupt */
4434 +    *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
4435 +    check_finished_intr_tx_epids(hcd);
4436 +  }
4437 +
4438 +  /* hinko ignore usb_pipeisoc */
4439 +#if 0
4440 +  if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
4441 +    struct crisv10_isoc_complete_data* comp_data;
4442 +
4443 +    /* Flag done Out Isoc for later completion */
4444 +    check_finished_isoc_tx_epids();
4445 +
4446 +    /* Clear this interrupt */
4447 +    *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
4448 +    /* Schedule bottom half of Out Isoc completion function. This function
4449 +       finishes the URBs marked with isoc_out_done */
4450 +    comp_data = (struct crisv10_isoc_complete_data*)
4451 +      kmem_cache_alloc(isoc_compl_cache, GFP_ATOMIC);
4452 +    ASSERT(comp_data != NULL);
4453 +    comp_data ->hcd = hcd;
4454 +
4455 +    //INIT_WORK(&comp_data->usb_bh, complete_isoc_bottom_half, comp_data);
4456 +    INIT_WORK(&comp_data->usb_bh, complete_isoc_bottom_half);
4457 +    schedule_work(&comp_data->usb_bh);
4458 +  }
4459 +#endif
4460 +
4461 +  return IRQ_HANDLED;
4462 +}
4463 +
4464 +/* Interrupt handler for DMA9/IRQ25 (called from hardware intr) */
4465 +static irqreturn_t tc_dma_rx_interrupt(int irq, void *vhc) {
4466 +  unsigned long flags;
4467 +  struct urb *urb;
4468 +  struct usb_hcd *hcd = (struct usb_hcd*)vhc;
4469 +  struct crisv10_urb_priv *urb_priv;
4470 +  int epid = 0;
4471 +  int real_error;
4472 +
4473 +  ASSERT(hcd);
4474 +
4475 +  /* Clear this interrupt. */
4476 +  *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
4477 +
4478 +  /* Custom clear interrupt for this interrupt */
4479 +  /* The reason we cli here is that we call the driver's callback functions. */
4480 +  local_irq_save(flags);
4481 +
4482 +  /* Note that this while loop assumes that all packets span only
4483 +     one rx descriptor. */
4484 +  while(myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
4485 +    epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
4486 +    /* Get the active URB for this epid */
4487 +    urb = activeUrbList[epid];
4488 +
4489 +    ASSERT(epid_inuse(epid));
4490 +    if (!urb) {
4491 +      dma_err("No urb for epid %d in rx interrupt\n", epid);
4492 +      goto skip_out;
4493 +    }
4494 +
4495 +    /* Check if any errors on epid */
4496 +    real_error = 0;
4497 +    if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
4498 +      __u32 r_usb_ept_data;
4499 +
4500 +      if (usb_pipeisoc(urb->pipe)) {
4501 +       r_usb_ept_data = etrax_epid_iso_get(epid);
4502 +       if((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
4503 +          (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
4504 +          (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
4505 +         /* Not an error, just a failure to receive an expected iso
4506 +            in packet in this frame.  This is not documented
4507 +            in the designers reference. Continue processing.
4508 +         */
4509 +       } else real_error = 1;
4510 +      } else real_error = 1;
4511 +    }
4512 +
4513 +    if(real_error) {
4514 +      dma_err("Error in RX descr on epid:%d for URB 0x%x",
4515 +             epid, (unsigned int)urb);
4516 +      dump_ept_data(epid);
4517 +      dump_in_desc(myNextRxDesc);
4518 +      goto skip_out;
4519 +    }
4520 +
4521 +    urb_priv = (struct crisv10_urb_priv *)urb->hcpriv;
4522 +    ASSERT(urb_priv);
4523 +    ASSERT(urb_priv->urb_state == STARTED ||
4524 +          urb_priv->urb_state == UNLINK);
4525 +
4526 +    if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
4527 +       (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
4528 +       (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
4529 +
4530 +      /* We get nodata for empty data transactions, and the rx descriptor's
4531 +        hw_len field is not valid in that case. No data to copy in other
4532 +        words. */
4533 +      if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
4534 +       /* No data to copy */
4535 +      } else {
4536 +       /*
4537 +       dma_dbg("Processing RX for URB:0x%x epid:%d (data:%d ofs:%d)\n",
4538 +               (unsigned int)urb, epid, myNextRxDesc->hw_len,
4539 +               urb_priv->rx_offset);
4540 +       */
4541 +       /* Only copy data if URB isn't flaged to be unlinked*/
4542 +       if(urb_priv->urb_state != UNLINK) {
4543 +         /* Make sure the data fits in the buffer. */
4544 +         if(urb_priv->rx_offset + myNextRxDesc->hw_len
4545 +            <= urb->transfer_buffer_length) {
4546 +
4547 +           /* Copy the data to URBs buffer */
4548 +           memcpy(urb->transfer_buffer + urb_priv->rx_offset,
4549 +                  phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
4550 +           urb_priv->rx_offset += myNextRxDesc->hw_len;
4551 +         } else {
4552 +           /* Signal overflow when returning URB */
4553 +           urb->status = -EOVERFLOW;
4554 +           tc_finish_urb_later(hcd, urb, urb->status);
4555 +         }
4556 +       }
4557 +      }
4558 +
4559 +      /* Check if it was the last packet in the transfer */
4560 +      if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
4561 +       /* Special handling for In Ctrl URBs. */
4562 +       if(usb_pipecontrol(urb->pipe) && usb_pipein(urb->pipe) &&
4563 +          !(urb_priv->ctrl_zout_done)) {
4564 +         /* Flag that RX part of Ctrl transfer is done. Because zout descr
4565 +            interrupt hasn't happend yet will the URB be finished in the
4566 +            TX-Interrupt. */
4567 +         urb_priv->ctrl_rx_done = 1;
4568 +         tc_dbg("Not finishing In Ctrl URB:0x%x from rx_interrupt, waiting"
4569 +                " for zout\n", (unsigned int)urb);
4570 +       } else {
4571 +         tc_finish_urb(hcd, urb, 0);
4572 +       }
4573 +      }
4574 +    } else { /* ISOC RX */
4575 +      /*
4576 +      isoc_dbg("Processing RX for epid:%d (URB:0x%x) ISOC pipe\n",
4577 +              epid, (unsigned int)urb);
4578 +      */
4579 +
4580 +      struct usb_iso_packet_descriptor *packet;
4581 +
4582 +      if (urb_priv->urb_state == UNLINK) {
4583 +       isoc_warn("Ignoring Isoc Rx data for urb being unlinked.\n");
4584 +       goto skip_out;
4585 +      } else if (urb_priv->urb_state == NOT_STARTED) {
4586 +       isoc_err("What? Got Rx data for Isoc urb that isn't started?\n");
4587 +       goto skip_out;
4588 +      }
4589 +
4590 +      packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
4591 +      ASSERT(packet);
4592 +      packet->status = 0;
4593 +
4594 +      if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
4595 +       /* We get nodata for empty data transactions, and the rx descriptor's
4596 +          hw_len field is not valid in that case. We copy 0 bytes however to
4597 +          stay in synch. */
4598 +       packet->actual_length = 0;
4599 +      } else {
4600 +       packet->actual_length = myNextRxDesc->hw_len;
4601 +       /* Make sure the data fits in the buffer. */
4602 +       ASSERT(packet->actual_length <= packet->length);
4603 +       memcpy(urb->transfer_buffer + packet->offset,
4604 +              phys_to_virt(myNextRxDesc->buf), packet->actual_length);
4605 +       if(packet->actual_length > 0)
4606 +         isoc_dbg("Copied %d bytes, packet %d for URB:0x%x[%d]\n",
4607 +                  packet->actual_length, urb_priv->isoc_packet_counter,
4608 +                  (unsigned int)urb, urb_priv->urb_num);
4609 +      }
4610 +
4611 +      /* Increment the packet counter. */
4612 +      urb_priv->isoc_packet_counter++;
4613 +
4614 +      /* Note that we don't care about the eot field in the rx descriptor's
4615 +        status. It will always be set for isoc traffic. */
4616 +      if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
4617 +       /* Complete the urb with status OK. */
4618 +       tc_finish_urb(hcd, urb, 0);
4619 +      }
4620 +    }
4621 +
4622 +  skip_out:
4623 +    myNextRxDesc->status = 0;
4624 +    myNextRxDesc->command |= IO_MASK(USB_IN_command, eol);
4625 +    myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
4626 +    myLastRxDesc = myNextRxDesc;
4627 +    myNextRxDesc = phys_to_virt(myNextRxDesc->next);
4628 +    flush_etrax_cache();
4629 +    *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, restart);
4630 +  }
4631 +
4632 +  local_irq_restore(flags);
4633 +
4634 +  return IRQ_HANDLED;
4635 +}
4636 +
4637 +static void tc_bulk_start_timer_func(unsigned long dummy) {
4638 +  /* We might enable an EP descriptor behind the current DMA position when
4639 +     it's about to decide that there are no more bulk traffic and it should
4640 +     stop the bulk channel.
4641 +     Therefore we periodically check if the bulk channel is stopped and there
4642 +     is an enabled bulk EP descriptor, in which case we start the bulk
4643 +     channel. */
4644 +  
4645 +  if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
4646 +    int epid;
4647 +
4648 +    timer_dbg("bulk_start_timer: Bulk DMA channel not running.\n");
4649 +
4650 +    for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
4651 +      if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
4652 +       timer_warn("Found enabled EP for epid %d, starting bulk channel.\n",
4653 +                  epid);
4654 +       restart_dma8_sub0();
4655 +
4656 +       /* Restart the bulk eot timer since we just started the bulk channel.*/
4657 +       mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
4658 +
4659 +       /* No need to search any further. */
4660 +       break;
4661 +      }
4662 +    }
4663 +  } else {
4664 +    timer_dbg("bulk_start_timer: Bulk DMA channel running.\n");
4665 +  }
4666 +}
4667 +
4668 +static void tc_bulk_eot_timer_func(unsigned long dummy) {
4669 +  struct usb_hcd *hcd = (struct usb_hcd*)dummy;
4670 +  ASSERT(hcd);
4671 +  /* Because of a race condition in the top half, we might miss a bulk eot.
4672 +     This timer "simulates" a bulk eot if we don't get one for a while,
4673 +     hopefully correcting the situation. */
4674 +  timer_dbg("bulk_eot_timer timed out.\n");
4675 +  check_finished_bulk_tx_epids(hcd, 1);
4676 +}
4677 +
4678 +
4679 +/*************************************************************/
4680 +/*************************************************************/
4681 +/* Device driver block                                       */
4682 +/*************************************************************/
4683 +/*************************************************************/
4684 +
4685 +/* Forward declarations for device driver functions */
4686 +static int devdrv_hcd_probe(struct device *);
4687 +static int devdrv_hcd_remove(struct device *);
4688 +#ifdef CONFIG_PM
4689 +static int devdrv_hcd_suspend(struct device *, u32, u32);
4690 +static int devdrv_hcd_resume(struct device *, u32);
4691 +#endif /* CONFIG_PM */
4692 +
4693 +/* the device */
4694 +static struct platform_device *devdrv_hc_platform_device;
4695 +
4696 +/* device driver interface */
4697 +static struct device_driver devdrv_hc_device_driver = {
4698 +  .name =                      (char *) hc_name,
4699 +  .bus =                       &platform_bus_type,
4700 +
4701 +  .probe =             devdrv_hcd_probe,
4702 +  .remove =            devdrv_hcd_remove,
4703 +
4704 +#ifdef CONFIG_PM
4705 +  .suspend =           devdrv_hcd_suspend,
4706 +  .resume =            devdrv_hcd_resume,
4707 +#endif /* CONFIG_PM */
4708 +};
4709 +
4710 +/* initialize the host controller and driver  */
4711 +static int __init_or_module devdrv_hcd_probe(struct device *dev)
4712 +{
4713 +  struct usb_hcd *hcd;
4714 +  struct crisv10_hcd *crisv10_hcd;
4715 +  int retval;
4716 +
4717 +  /* Check DMA burst length */
4718 +  if(IO_EXTRACT(R_BUS_CONFIG, dma_burst, *R_BUS_CONFIG) !=
4719 +     IO_STATE(R_BUS_CONFIG, dma_burst, burst32)) {
4720 +    devdrv_err("Invalid DMA burst length in Etrax 100LX,"
4721 +              " needs to be 32\n");
4722 +    return -EPERM;
4723 +  }
4724 +
4725 +  hcd = usb_create_hcd(&crisv10_hc_driver, dev, dev->bus_id);
4726 +  if (!hcd)
4727 +    return -ENOMEM;
4728 +
4729 +  crisv10_hcd = hcd_to_crisv10_hcd(hcd);
4730 +  spin_lock_init(&crisv10_hcd->lock);
4731 +  crisv10_hcd->num_ports = num_ports();
4732 +  crisv10_hcd->running = 0;
4733 +
4734 +  dev_set_drvdata(dev, crisv10_hcd);
4735 +
4736 +  devdrv_dbg("ETRAX USB IRQs HC:%d  RX:%d  TX:%d\n", ETRAX_USB_HC_IRQ,
4737 +         ETRAX_USB_RX_IRQ, ETRAX_USB_TX_IRQ);
4738 +
4739 +  /* Print out chip version read from registers */
4740 +  int rev_maj = *R_USB_REVISION & IO_MASK(R_USB_REVISION, major);
4741 +  int rev_min = *R_USB_REVISION & IO_MASK(R_USB_REVISION, minor);
4742 +  if(rev_min == 0) {
4743 +    devdrv_info("Etrax 100LX USB Revision %d v1,2\n", rev_maj);
4744 +  } else {
4745 +    devdrv_info("Etrax 100LX USB Revision %d v%d\n", rev_maj, rev_min);
4746 +  }
4747 +
4748 +  devdrv_info("Bulk timer interval, start:%d eot:%d\n",
4749 +             BULK_START_TIMER_INTERVAL,
4750 +             BULK_EOT_TIMER_INTERVAL);
4751 +
4752 +
4753 +  /* Init root hub data structures */
4754 +  if(rh_init()) {
4755 +    devdrv_err("Failed init data for Root Hub\n");
4756 +    retval = -ENOMEM;
4757 +  }
4758 +
4759 +  if(port_in_use(0)) {
4760 +    if (cris_request_io_interface(if_usb_1, "ETRAX100LX USB-HCD")) {
4761 +      printk(KERN_CRIT "usb-host: request IO interface usb1 failed");
4762 +      retval = -EBUSY;
4763 +      goto out;
4764 +    }
4765 +    devdrv_info("Claimed interface for USB physical port 1\n");
4766 +  }
4767 +  if(port_in_use(1)) {
4768 +    if (cris_request_io_interface(if_usb_2, "ETRAX100LX USB-HCD")) {
4769 +      /* Free first interface if second failed to be claimed */
4770 +      if(port_in_use(0)) {
4771 +       cris_free_io_interface(if_usb_1);
4772 +      }
4773 +      printk(KERN_CRIT "usb-host: request IO interface usb2 failed");
4774 +      retval = -EBUSY;
4775 +      goto out;
4776 +    }
4777 +    devdrv_info("Claimed interface for USB physical port 2\n");
4778 +  }
4779 +  
4780 +  /* Init transfer controller structs and locks */
4781 +  if((retval = tc_init(hcd)) != 0) {
4782 +    goto out;
4783 +  }
4784 +
4785 +  /* Attach interrupt functions for DMA and init DMA controller */
4786 +  if((retval = tc_dma_init(hcd)) != 0) {
4787 +    goto out;
4788 +  }
4789 +
4790 +  /* Attach the top IRQ handler for USB controller interrupts */
4791 +  if (request_irq(ETRAX_USB_HC_IRQ, crisv10_hcd_top_irq, 0,
4792 +                 "ETRAX 100LX built-in USB (HC)", hcd)) {
4793 +    err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
4794 +    retval = -EBUSY;
4795 +    goto out;
4796 +  }
4797 +
4798 +  /* iso_eof is only enabled when isoc traffic is running. */
4799 +  *R_USB_IRQ_MASK_SET =
4800 +    /* IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) | */
4801 +    IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
4802 +    IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
4803 +    IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
4804 +    IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
4805 +
4806 +
4807 +  crisv10_ready_wait();
4808 +  /* Reset the USB interface. */
4809 +  *R_USB_COMMAND =
4810 +    IO_STATE(R_USB_COMMAND, port_sel, nop) |
4811 +    IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4812 +    IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
4813 +
4814 +  /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to
4815 +     0x2A30 (10800), to guarantee that control traffic gets 10% of the
4816 +     bandwidth, and periodic transfer may allocate the rest (90%).
4817 +     This doesn't work though.
4818 +     The value 11960 is chosen to be just after the SOF token, with a couple
4819 +     of bit times extra for possible bit stuffing. */
4820 +  *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
4821 +
4822 +  crisv10_ready_wait();
4823 +  /* Configure the USB interface as a host controller. */
4824 +  *R_USB_COMMAND =
4825 +    IO_STATE(R_USB_COMMAND, port_sel, nop) |
4826 +    IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4827 +    IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
4828 +
4829 +
4830 +  /* Check so controller not busy before enabling ports */
4831 +  crisv10_ready_wait();
4832 +
4833 +  /* Enable selected USB ports */
4834 +  if(port_in_use(0)) {
4835 +    *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
4836 +  } else {
4837 +    *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
4838 +  }
4839 +  if(port_in_use(1)) {
4840 +    *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
4841 +  } else {
4842 +    *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
4843 +  }
4844 +
4845 +  crisv10_ready_wait();
4846 +  /* Start processing of USB traffic. */
4847 +  *R_USB_COMMAND =
4848 +    IO_STATE(R_USB_COMMAND, port_sel, nop) |
4849 +    IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4850 +    IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4851 +
4852 +  /* Do not continue probing initialization before USB interface is done */
4853 +  crisv10_ready_wait();
4854 +
4855 +  /* Register our Host Controller to USB Core
4856 +   * Finish the remaining parts of generic HCD initialization: allocate the
4857 +   * buffers of consistent memory, register the bus
4858 +   * and call the driver's reset() and start() routines. */
4859 +  retval = usb_add_hcd(hcd, ETRAX_USB_HC_IRQ, IRQF_DISABLED);
4860 +  if (retval != 0) {
4861 +    devdrv_err("Failed registering HCD driver\n");
4862 +    goto out;
4863 +  }
4864 +
4865 +  return 0;
4866 +
4867 + out:
4868 +  devdrv_hcd_remove(dev);
4869 +  return retval;
4870 +}
4871 +
4872 +
4873 +/* cleanup after the host controller and driver */
4874 +static int __init_or_module devdrv_hcd_remove(struct device *dev)
4875 +{
4876 +  struct crisv10_hcd *crisv10_hcd = dev_get_drvdata(dev);
4877 +  struct usb_hcd *hcd;
4878 +
4879 +  if (!crisv10_hcd)
4880 +    return 0;
4881 +  hcd = crisv10_hcd_to_hcd(crisv10_hcd);
4882 +
4883 +
4884 +  /* Stop USB Controller in Etrax 100LX */
4885 +  crisv10_hcd_reset(hcd);
4886 +
4887 +  usb_remove_hcd(hcd);
4888 +  devdrv_dbg("Removed HCD from USB Core\n");
4889 +
4890 +  /* Free USB Controller IRQ */
4891 +  free_irq(ETRAX_USB_HC_IRQ, NULL);
4892 +
4893 +  /* Free resources */
4894 +  tc_dma_destroy();
4895 +  tc_destroy();
4896 +
4897 +
4898 +  if(port_in_use(0)) {
4899 +    cris_free_io_interface(if_usb_1);
4900 +  }
4901 +  if(port_in_use(1)) {
4902 +    cris_free_io_interface(if_usb_2);
4903 +  }
4904 +
4905 +  devdrv_dbg("Freed all claimed resources\n");
4906 +
4907 +  return 0;
4908 +}
4909 +
4910 +
4911 +#ifdef CONFIG_PM
4912 +
4913 +static int devdrv_hcd_suspend(struct usb_hcd *hcd, u32 state, u32 level)
4914 +{
4915 +  return 0; /* no-op for now */
4916 +}
4917 +
4918 +static int devdrv_hcd_resume(struct usb_hcd *hcd, u32 level)
4919 +{
4920 +  return 0; /* no-op for now */
4921 +}
4922 +
4923 +#endif /* CONFIG_PM */
4924 +
4925 +
4926 +
4927 +/*************************************************************/
4928 +/*************************************************************/
4929 +/* Module block                                              */
4930 +/*************************************************************/
4931 +/*************************************************************/
4932
4933 +/* register driver */
4934 +static int __init module_hcd_init(void) 
4935 +{
4936 +  
4937 +  if (usb_disabled())
4938 +    return -ENODEV;
4939 +
4940 +  /* Here we select enabled ports by following defines created from
4941 +     menuconfig */
4942 +#ifndef CONFIG_ETRAX_USB_HOST_PORT1
4943 +  ports &= ~(1<<0);
4944 +#endif
4945 +#ifndef CONFIG_ETRAX_USB_HOST_PORT2
4946 +  ports &= ~(1<<1);
4947 +#endif
4948 +
4949 +  printk(KERN_INFO "%s version "VERSION" "COPYRIGHT"\n", product_desc);
4950 +
4951 +  devdrv_hc_platform_device =
4952 +    platform_device_register_simple((char *) hc_name, 0, NULL, 0);
4953 +
4954 +  if (IS_ERR(devdrv_hc_platform_device))
4955 +    return PTR_ERR(devdrv_hc_platform_device);
4956 +  return driver_register(&devdrv_hc_device_driver);
4957 +  /* 
4958 +   * Note that we do not set the DMA mask for the device,
4959 +   * i.e. we pretend that we will use PIO, since no specific
4960 +   * allocation routines are needed for DMA buffers. This will
4961 +   * cause the HCD buffer allocation routines to fall back to
4962 +   * kmalloc().
4963 +   */
4964 +}
4965 +
4966 +/* unregister driver */
4967 +static void __exit module_hcd_exit(void) 
4968 +{      
4969 +  driver_unregister(&devdrv_hc_device_driver);
4970 +}
4971 +
4972 +
4973 +/* Module hooks */
4974 +module_init(module_hcd_init);
4975 +module_exit(module_hcd_exit);
4976 Index: linux-2.6.25.4/drivers/usb/host/hc-crisv10.h
4977 ===================================================================
4978 --- /dev/null
4979 +++ linux-2.6.25.4/drivers/usb/host/hc-crisv10.h
4980 @@ -0,0 +1,331 @@
4981 +#ifndef __LINUX_ETRAX_USB_H
4982 +#define __LINUX_ETRAX_USB_H
4983 +
4984 +#include <linux/types.h>
4985 +#include <linux/list.h>
4986 +
4987 +struct USB_IN_Desc {
4988 +  volatile __u16 sw_len;
4989 +  volatile __u16 command;
4990 +  volatile unsigned long next;
4991 +  volatile unsigned long buf;
4992 +  volatile __u16 hw_len;
4993 +  volatile __u16 status;
4994 +};
4995 +
4996 +struct USB_SB_Desc {
4997 +  volatile __u16 sw_len;
4998 +  volatile __u16 command;
4999 +  volatile unsigned long next;
5000 +  volatile unsigned long buf;
5001 +};
5002 +
5003 +struct USB_EP_Desc {
5004 +  volatile __u16 hw_len;
5005 +  volatile __u16 command;
5006 +  volatile unsigned long sub;
5007 +  volatile unsigned long next;
5008 +};
5009 +
5010 +
5011 +/* Root Hub port status struct */
5012 +struct crisv10_rh {
5013 +  volatile __u16 wPortChange[2];
5014 +  volatile __u16 wPortStatusPrev[2];
5015 +};
5016 +
5017 +/* HCD description */
5018 +struct crisv10_hcd {
5019 +  spinlock_t           lock;
5020 +  __u8                 num_ports;
5021 +  __u8                  running;
5022 +};
5023 +
5024 +
5025 +/* Endpoint HC private data description */
5026 +struct crisv10_ep_priv {
5027 +  int epid;
5028 +};
5029 +
5030 +/* Additional software state info for a USB Controller epid */
5031 +struct etrax_epid {
5032 +  __u8 inuse;       /* !0 = setup in Etrax and used for a endpoint */
5033 +  __u8 disabled;    /* !0 = Temporarly disabled to avoid resubmission */
5034 +  __u8 type;        /* Setup as: PIPE_BULK, PIPE_CONTROL ... */
5035 +  __u8 out_traffic; /* !0 = This epid is for out traffic */
5036 +};
5037 +
5038 +/* Struct to hold information of scheduled later URB completion */
5039 +struct urb_later_data {
5040 +//  struct work_struct ws;
5041 +  struct delayed_work ws;
5042 +  struct usb_hcd *hcd;
5043 +  struct urb *urb;
5044 +  int urb_num;
5045 +  int status;
5046 +};
5047 +
5048 +
5049 +typedef enum {
5050 +  STARTED,
5051 +  NOT_STARTED,
5052 +  UNLINK,
5053 +} crisv10_urb_state_t;
5054 +
5055 +
5056 +struct crisv10_urb_priv {
5057 +  /* Sequence number for this URB. Every new submited URB gets this from
5058 +     a incrementing counter. Used when a URB is scheduled for later finish to
5059 +     be sure that the intended URB hasn't already been completed (device
5060 +     drivers has a tendency to reuse URBs once they are completed, causing us
5061 +     to not be able to single old ones out only based on the URB pointer.) */
5062 +  __u32 urb_num;
5063 +
5064 +  /* The first_sb field is used for freeing all SB descriptors belonging
5065 +     to an urb. The corresponding ep descriptor's sub pointer cannot be
5066 +     used for this since the DMA advances the sub pointer as it processes
5067 +     the sb list. */
5068 +  struct USB_SB_Desc *first_sb;
5069 +
5070 +  /* The last_sb field referes to the last SB descriptor that belongs to
5071 +     this urb. This is important to know so we can free the SB descriptors
5072 +     that ranges between first_sb and last_sb. */
5073 +  struct USB_SB_Desc *last_sb;
5074 +  
5075 +  /* The rx_offset field is used in ctrl and bulk traffic to keep track
5076 +     of the offset in the urb's transfer_buffer where incoming data should be
5077 +     copied to. */
5078 +  __u32 rx_offset;
5079 +  
5080 +  /* Counter used in isochronous transfers to keep track of the
5081 +     number of packets received/transmitted.  */
5082 +  __u32 isoc_packet_counter;
5083 +
5084 +  /* Flag that marks if this Isoc Out URB has finished it's transfer. Used
5085 +     because several URBs can be finished before list is processed */
5086 +  __u8  isoc_out_done;
5087 +  
5088 +  /* This field is used to pass information about the urb's current state
5089 +     between the various interrupt handlers (thus marked volatile). */
5090 +  volatile crisv10_urb_state_t urb_state;
5091 +  
5092 +  /* In Ctrl transfers consist of (at least) 3 packets: SETUP, IN and ZOUT.
5093 +     When DMA8 sub-channel 2 has processed the SB list for this sequence we
5094 +     get a interrupt. We also get a interrupt for In transfers and which
5095 +     one of these interrupts that comes first depends of data size and device.
5096 +     To be sure that we have got both interrupts before we complete the URB
5097 +     we have these to flags that shows which part that has completed.
5098 +     We can then check when we get one of the interrupts that if the other has
5099 +     occured it's safe for us to complete the URB, otherwise we set appropriate
5100 +     flag and do the completion when we get the other interrupt. */
5101 +  volatile unsigned char ctrl_zout_done;
5102 +  volatile unsigned char ctrl_rx_done;
5103 +
5104 +  /* Connection between the submitted urb and ETRAX epid number */
5105 +  __u8 epid;
5106 +  
5107 +  /* The rx_data_list field is used for periodic traffic, to hold
5108 +     received data for later processing in the the complete_urb functions,
5109 +     where the data us copied to the urb's transfer_buffer. Basically, we
5110 +     use this intermediate storage because we don't know when it's safe to
5111 +     reuse the transfer_buffer (FIXME?). */
5112 +  struct list_head rx_data_list;
5113 +
5114 +
5115 +  /* The interval time rounded up to closest 2^N */
5116 +  int interval;
5117 +
5118 +  /* Pool of EP descriptors needed if it's a INTR transfer.
5119 +     Amount of EPs in pool correspons to how many INTR that should
5120 +     be inserted in TxIntrEPList (max 128, defined by MAX_INTR_INTERVAL) */
5121 +  struct USB_EP_Desc* intr_ep_pool[128];
5122 +
5123 +  /* The mount of EPs allocated for this INTR URB */
5124 +  int intr_ep_pool_length;
5125 +
5126 +  /* Pointer to info struct if URB is scheduled to be finished later */
5127 +  struct urb_later_data* later_data;
5128 +};
5129 +
5130 +
5131 +/* This struct is for passing data from the top half to the bottom half irq
5132 +   handlers */
5133 +struct crisv10_irq_reg {
5134 +  struct usb_hcd* hcd;
5135 +  __u32 r_usb_epid_attn;
5136 +  __u8 r_usb_status;
5137 +  __u16 r_usb_rh_port_status_1;
5138 +  __u16 r_usb_rh_port_status_2;
5139 +  __u32 r_usb_irq_mask_read;
5140 +  __u32 r_usb_fm_number;
5141 +  struct work_struct usb_bh;
5142 +};
5143 +
5144 +
5145 +/* This struct is for passing data from the isoc top half to the isoc bottom
5146 +   half. */
5147 +struct crisv10_isoc_complete_data {
5148 +  struct usb_hcd *hcd;
5149 +  struct urb *urb;
5150 +  struct work_struct usb_bh;
5151 +};
5152 +
5153 +/* Entry item for URB lists for each endpint */
5154 +typedef struct urb_entry
5155 +{
5156 +       struct urb *urb;
5157 +       struct list_head list;
5158 +} urb_entry_t;
5159 +
5160 +/* ---------------------------------------------------------------------------
5161 +   Virtual Root HUB
5162 +   ------------------------------------------------------------------------- */
5163 +/* destination of request */
5164 +#define RH_INTERFACE               0x01
5165 +#define RH_ENDPOINT                0x02
5166 +#define RH_OTHER                   0x03
5167 +
5168 +#define RH_CLASS                   0x20
5169 +#define RH_VENDOR                  0x40
5170 +
5171 +/* Requests: bRequest << 8 | bmRequestType */
5172 +#define RH_GET_STATUS           0x0080
5173 +#define RH_CLEAR_FEATURE        0x0100
5174 +#define RH_SET_FEATURE          0x0300
5175 +#define RH_SET_ADDRESS         0x0500
5176 +#define RH_GET_DESCRIPTOR      0x0680
5177 +#define RH_SET_DESCRIPTOR       0x0700
5178 +#define RH_GET_CONFIGURATION   0x0880
5179 +#define RH_SET_CONFIGURATION   0x0900
5180 +#define RH_GET_STATE            0x0280
5181 +#define RH_GET_INTERFACE        0x0A80
5182 +#define RH_SET_INTERFACE        0x0B00
5183 +#define RH_SYNC_FRAME           0x0C80
5184 +/* Our Vendor Specific Request */
5185 +#define RH_SET_EP               0x2000
5186 +
5187 +
5188 +/* Hub port features */
5189 +#define RH_PORT_CONNECTION         0x00
5190 +#define RH_PORT_ENABLE             0x01
5191 +#define RH_PORT_SUSPEND            0x02
5192 +#define RH_PORT_OVER_CURRENT       0x03
5193 +#define RH_PORT_RESET              0x04
5194 +#define RH_PORT_POWER              0x08
5195 +#define RH_PORT_LOW_SPEED          0x09
5196 +#define RH_C_PORT_CONNECTION       0x10
5197 +#define RH_C_PORT_ENABLE           0x11
5198 +#define RH_C_PORT_SUSPEND          0x12
5199 +#define RH_C_PORT_OVER_CURRENT     0x13
5200 +#define RH_C_PORT_RESET            0x14
5201 +
5202 +/* Hub features */
5203 +#define RH_C_HUB_LOCAL_POWER       0x00
5204 +#define RH_C_HUB_OVER_CURRENT      0x01
5205 +
5206 +#define RH_DEVICE_REMOTE_WAKEUP    0x00
5207 +#define RH_ENDPOINT_STALL          0x01
5208 +
5209 +/* Our Vendor Specific feature */
5210 +#define RH_REMOVE_EP               0x00
5211 +
5212 +
5213 +#define RH_ACK                     0x01
5214 +#define RH_REQ_ERR                 -1
5215 +#define RH_NACK                    0x00
5216 +
5217 +/* Field definitions for */
5218 +
5219 +#define USB_IN_command__eol__BITNR      0 /* command macros */
5220 +#define USB_IN_command__eol__WIDTH      1
5221 +#define USB_IN_command__eol__no         0
5222 +#define USB_IN_command__eol__yes        1
5223 +
5224 +#define USB_IN_command__intr__BITNR     3
5225 +#define USB_IN_command__intr__WIDTH     1
5226 +#define USB_IN_command__intr__no        0
5227 +#define USB_IN_command__intr__yes       1
5228 +
5229 +#define USB_IN_status__eop__BITNR       1 /* status macros. */
5230 +#define USB_IN_status__eop__WIDTH       1
5231 +#define USB_IN_status__eop__no          0
5232 +#define USB_IN_status__eop__yes         1
5233 +
5234 +#define USB_IN_status__eot__BITNR       5
5235 +#define USB_IN_status__eot__WIDTH       1
5236 +#define USB_IN_status__eot__no          0
5237 +#define USB_IN_status__eot__yes         1
5238 +
5239 +#define USB_IN_status__error__BITNR     6
5240 +#define USB_IN_status__error__WIDTH     1
5241 +#define USB_IN_status__error__no        0
5242 +#define USB_IN_status__error__yes       1
5243 +
5244 +#define USB_IN_status__nodata__BITNR    7
5245 +#define USB_IN_status__nodata__WIDTH    1
5246 +#define USB_IN_status__nodata__no       0
5247 +#define USB_IN_status__nodata__yes      1
5248 +
5249 +#define USB_IN_status__epid__BITNR      8
5250 +#define USB_IN_status__epid__WIDTH      5
5251 +
5252 +#define USB_EP_command__eol__BITNR      0
5253 +#define USB_EP_command__eol__WIDTH      1
5254 +#define USB_EP_command__eol__no         0
5255 +#define USB_EP_command__eol__yes        1
5256 +
5257 +#define USB_EP_command__eof__BITNR      1
5258 +#define USB_EP_command__eof__WIDTH      1
5259 +#define USB_EP_command__eof__no         0
5260 +#define USB_EP_command__eof__yes        1
5261 +
5262 +#define USB_EP_command__intr__BITNR     3
5263 +#define USB_EP_command__intr__WIDTH     1
5264 +#define USB_EP_command__intr__no        0
5265 +#define USB_EP_command__intr__yes       1
5266 +
5267 +#define USB_EP_command__enable__BITNR   4
5268 +#define USB_EP_command__enable__WIDTH   1
5269 +#define USB_EP_command__enable__no      0
5270 +#define USB_EP_command__enable__yes     1
5271 +
5272 +#define USB_EP_command__hw_valid__BITNR 5
5273 +#define USB_EP_command__hw_valid__WIDTH 1
5274 +#define USB_EP_command__hw_valid__no    0
5275 +#define USB_EP_command__hw_valid__yes   1
5276 +
5277 +#define USB_EP_command__epid__BITNR     8
5278 +#define USB_EP_command__epid__WIDTH     5
5279 +
5280 +#define USB_SB_command__eol__BITNR      0 /* command macros. */
5281 +#define USB_SB_command__eol__WIDTH      1
5282 +#define USB_SB_command__eol__no         0
5283 +#define USB_SB_command__eol__yes        1
5284 +
5285 +#define USB_SB_command__eot__BITNR      1
5286 +#define USB_SB_command__eot__WIDTH      1
5287 +#define USB_SB_command__eot__no         0
5288 +#define USB_SB_command__eot__yes        1
5289 +
5290 +#define USB_SB_command__intr__BITNR     3
5291 +#define USB_SB_command__intr__WIDTH     1
5292 +#define USB_SB_command__intr__no        0
5293 +#define USB_SB_command__intr__yes       1
5294 +
5295 +#define USB_SB_command__tt__BITNR       4
5296 +#define USB_SB_command__tt__WIDTH       2
5297 +#define USB_SB_command__tt__zout        0
5298 +#define USB_SB_command__tt__in          1
5299 +#define USB_SB_command__tt__out         2
5300 +#define USB_SB_command__tt__setup       3
5301 +
5302 +
5303 +#define USB_SB_command__rem__BITNR      8
5304 +#define USB_SB_command__rem__WIDTH      6
5305 +
5306 +#define USB_SB_command__full__BITNR     6
5307 +#define USB_SB_command__full__WIDTH     1
5308 +#define USB_SB_command__full__no        0
5309 +#define USB_SB_command__full__yes       1
5310 +
5311 +#endif