Merge master.kernel.org:/home/rmk/linux-2.6-serial
[pandora-kernel.git] / drivers / usb / host / hc_crisv10.c
1 /*
2  * usb-host.c: ETRAX 100LX USB Host Controller Driver (HCD)
3  *
4  * Copyright (c) 2002, 2003 Axis Communications AB.
5  */
6
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/delay.h>
10 #include <linux/ioport.h>
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/unistd.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/spinlock.h>
19
20 #include <asm/uaccess.h>
21 #include <asm/io.h>
22 #include <asm/irq.h>
23 #include <asm/dma.h>
24 #include <asm/system.h>
25 #include <asm/arch/svinto.h>
26
27 #include <linux/usb.h>
28 /* Ugly include because we don't live with the other host drivers. */
29 #include <../drivers/usb/core/hcd.h>
30 #include <../drivers/usb/core/usb.h>
31
32 #include "hc_crisv10.h"
33
34 #define ETRAX_USB_HC_IRQ USB_HC_IRQ_NBR
35 #define ETRAX_USB_RX_IRQ USB_DMA_RX_IRQ_NBR
36 #define ETRAX_USB_TX_IRQ USB_DMA_TX_IRQ_NBR
37
38 static const char *usb_hcd_version = "$Revision: 1.2 $";
39
40 #undef KERN_DEBUG
41 #define KERN_DEBUG ""
42
43
44 #undef USB_DEBUG_RH
45 #undef USB_DEBUG_EPID
46 #undef USB_DEBUG_SB
47 #undef USB_DEBUG_DESC
48 #undef USB_DEBUG_URB
49 #undef USB_DEBUG_TRACE
50 #undef USB_DEBUG_BULK
51 #undef USB_DEBUG_CTRL
52 #undef USB_DEBUG_INTR
53 #undef USB_DEBUG_ISOC
54
55 #ifdef USB_DEBUG_RH
56 #define dbg_rh(format, arg...) printk(KERN_DEBUG __FILE__ ": (RH) " format "\n" , ## arg)
57 #else
58 #define dbg_rh(format, arg...) do {} while (0)
59 #endif
60
61 #ifdef USB_DEBUG_EPID
62 #define dbg_epid(format, arg...) printk(KERN_DEBUG __FILE__ ": (EPID) " format "\n" , ## arg)
63 #else
64 #define dbg_epid(format, arg...) do {} while (0)
65 #endif
66
67 #ifdef USB_DEBUG_SB
68 #define dbg_sb(format, arg...) printk(KERN_DEBUG __FILE__ ": (SB) " format "\n" , ## arg)
69 #else
70 #define dbg_sb(format, arg...) do {} while (0)
71 #endif
72
73 #ifdef USB_DEBUG_CTRL
74 #define dbg_ctrl(format, arg...) printk(KERN_DEBUG __FILE__ ": (CTRL) " format "\n" , ## arg)
75 #else
76 #define dbg_ctrl(format, arg...) do {} while (0)
77 #endif
78
79 #ifdef USB_DEBUG_BULK
80 #define dbg_bulk(format, arg...) printk(KERN_DEBUG __FILE__ ": (BULK) " format "\n" , ## arg)
81 #else
82 #define dbg_bulk(format, arg...) do {} while (0)
83 #endif
84
85 #ifdef USB_DEBUG_INTR
86 #define dbg_intr(format, arg...) printk(KERN_DEBUG __FILE__ ": (INTR) " format "\n" , ## arg)
87 #else
88 #define dbg_intr(format, arg...) do {} while (0)
89 #endif
90
91 #ifdef USB_DEBUG_ISOC
92 #define dbg_isoc(format, arg...) printk(KERN_DEBUG __FILE__ ": (ISOC) " format "\n" , ## arg)
93 #else
94 #define dbg_isoc(format, arg...) do {} while (0)
95 #endif
96
97 #ifdef USB_DEBUG_TRACE
98 #define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
99 #define DBFEXIT  (printk(": Exiting:  %s\n", __FUNCTION__))
100 #else
101 #define DBFENTER do {} while (0)
102 #define DBFEXIT  do {} while (0)
103 #endif
104
105 #define usb_pipeslow(pipe)      (((pipe) >> 26) & 1)
106
107 /*-------------------------------------------------------------------
108  Virtual Root Hub
109  -------------------------------------------------------------------*/
110
111 static __u8 root_hub_dev_des[] =
112 {
113         0x12,  /*  __u8  bLength; */
114         0x01,  /*  __u8  bDescriptorType; Device */
115         0x00,  /*  __le16 bcdUSB; v1.0 */
116         0x01,
117         0x09,  /*  __u8  bDeviceClass; HUB_CLASSCODE */
118         0x00,  /*  __u8  bDeviceSubClass; */
119         0x00,  /*  __u8  bDeviceProtocol; */
120         0x08,  /*  __u8  bMaxPacketSize0; 8 Bytes */
121         0x00,  /*  __le16 idVendor; */
122         0x00,
123         0x00,  /*  __le16 idProduct; */
124         0x00,
125         0x00,  /*  __le16 bcdDevice; */
126         0x00,
127         0x00,  /*  __u8  iManufacturer; */
128         0x02,  /*  __u8  iProduct; */
129         0x01,  /*  __u8  iSerialNumber; */
130         0x01   /*  __u8  bNumConfigurations; */
131 };
132
133 /* Configuration descriptor */
134 static __u8 root_hub_config_des[] =
135 {
136         0x09,  /*  __u8  bLength; */
137         0x02,  /*  __u8  bDescriptorType; Configuration */
138         0x19,  /*  __le16 wTotalLength; */
139         0x00,
140         0x01,  /*  __u8  bNumInterfaces; */
141         0x01,  /*  __u8  bConfigurationValue; */
142         0x00,  /*  __u8  iConfiguration; */
143         0x40,  /*  __u8  bmAttributes; Bit 7: Bus-powered */
144         0x00,  /*  __u8  MaxPower; */
145
146      /* interface */
147         0x09,  /*  __u8  if_bLength; */
148         0x04,  /*  __u8  if_bDescriptorType; Interface */
149         0x00,  /*  __u8  if_bInterfaceNumber; */
150         0x00,  /*  __u8  if_bAlternateSetting; */
151         0x01,  /*  __u8  if_bNumEndpoints; */
152         0x09,  /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
153         0x00,  /*  __u8  if_bInterfaceSubClass; */
154         0x00,  /*  __u8  if_bInterfaceProtocol; */
155         0x00,  /*  __u8  if_iInterface; */
156
157      /* endpoint */
158         0x07,  /*  __u8  ep_bLength; */
159         0x05,  /*  __u8  ep_bDescriptorType; Endpoint */
160         0x81,  /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
161         0x03,  /*  __u8  ep_bmAttributes; Interrupt */
162         0x08,  /*  __le16 ep_wMaxPacketSize; 8 Bytes */
163         0x00,
164         0xff   /*  __u8  ep_bInterval; 255 ms */
165 };
166
167 static __u8 root_hub_hub_des[] =
168 {
169         0x09,  /*  __u8  bLength; */
170         0x29,  /*  __u8  bDescriptorType; Hub-descriptor */
171         0x02,  /*  __u8  bNbrPorts; */
172         0x00,  /* __u16  wHubCharacteristics; */
173         0x00,
174         0x01,  /*  __u8  bPwrOn2pwrGood; 2ms */
175         0x00,  /*  __u8  bHubContrCurrent; 0 mA */
176         0x00,  /*  __u8  DeviceRemovable; *** 7 Ports max *** */
177         0xff   /*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
178 };
179
180 static DEFINE_TIMER(bulk_start_timer, NULL, 0, 0);
181 static DEFINE_TIMER(bulk_eot_timer, NULL, 0, 0);
182
183 /* We want the start timer to expire before the eot timer, because the former might start
184    traffic, thus making it unnecessary for the latter to time out. */
185 #define BULK_START_TIMER_INTERVAL (HZ/10) /* 100 ms */
186 #define BULK_EOT_TIMER_INTERVAL (HZ/10+2) /* 120 ms */
187
188 #define OK(x) len = (x); dbg_rh("OK(%d): line: %d", x, __LINE__); break
189 #define CHECK_ALIGN(x) if (((__u32)(x)) & 0x00000003) \
190 {panic("Alignment check (DWORD) failed at %s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__);}
191
192 #define SLAB_FLAG     (in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL)
193 #define KMALLOC_FLAG  (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
194
195 /* Most helpful debugging aid */
196 #define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
197
198 /* Alternative assert define which stops after a failed assert. */
199 /*
200 #define assert(expr)                                      \
201 {                                                         \
202         if (!(expr)) {                                    \
203                 err("assert failed at line %d",__LINE__); \
204                 while (1);                                \
205         }                                                 \
206 }
207 */
208
209
210 /* FIXME: Should RX_BUF_SIZE be a config option, or maybe we should adjust it dynamically?
211    To adjust it dynamically we would have to get an interrupt when we reach the end
212    of the rx descriptor list, or when we get close to the end, and then allocate more
213    descriptors. */
214
215 #define NBR_OF_RX_DESC     512
216 #define RX_DESC_BUF_SIZE   1024
217 #define RX_BUF_SIZE        (NBR_OF_RX_DESC * RX_DESC_BUF_SIZE)
218
219 /* The number of epids is, among other things, used for pre-allocating
220    ctrl, bulk and isoc EP descriptors (one for each epid).
221    Assumed to be > 1 when initiating the DMA lists. */
222 #define NBR_OF_EPIDS       32
223
224 /* Support interrupt traffic intervals up to 128 ms. */
225 #define MAX_INTR_INTERVAL 128
226
227 /* If periodic traffic (intr or isoc) is to be used, then one entry in the EP table
228    must be "invalid". By this we mean that we shouldn't care about epid attentions
229    for this epid, or at least handle them differently from epid attentions for "valid"
230    epids. This define determines which one to use (don't change it). */
231 #define INVALID_EPID     31
232 /* A special epid for the bulk dummys. */
233 #define DUMMY_EPID       30
234
235 /* This is just a software cache for the valid entries in R_USB_EPT_DATA. */
236 static __u32 epid_usage_bitmask;
237
238 /* A bitfield to keep information on in/out traffic is needed to uniquely identify
239    an endpoint on a device, since the most significant bit which indicates traffic
240    direction is lacking in the ep_id field (ETRAX epids can handle both in and
241    out traffic on endpoints that are otherwise identical). The USB framework, however,
242    relies on them to be handled separately.  For example, bulk IN and OUT urbs cannot
243    be queued in the same list, since they would block each other. */
244 static __u32 epid_out_traffic;
245
246 /* DMA IN cache bug. Align the DMA IN buffers to 32 bytes, i.e. a cache line.
247    Since RX_DESC_BUF_SIZE is 1024 is a multiple of 32, all rx buffers will be cache aligned. */
248 static volatile unsigned char RxBuf[RX_BUF_SIZE] __attribute__ ((aligned (32)));
249 static volatile USB_IN_Desc_t RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned (4)));
250
251 /* Pointers into RxDescList. */
252 static volatile USB_IN_Desc_t *myNextRxDesc;
253 static volatile USB_IN_Desc_t *myLastRxDesc;
254 static volatile USB_IN_Desc_t *myPrevRxDesc;
255
256 /* EP descriptors must be 32-bit aligned. */
257 static volatile USB_EP_Desc_t TxCtrlEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
258 static volatile USB_EP_Desc_t TxBulkEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
259 /* After each enabled bulk EP (IN or OUT) we put two disabled EP descriptors with the eol flag set,
260    causing the DMA to stop the DMA channel. The first of these two has the intr flag set, which
261    gives us a dma8_sub0_descr interrupt. When we receive this, we advance the DMA one step in the
262    EP list and then restart the bulk channel, thus forcing a switch between bulk EP descriptors
263    in each frame. */
264 static volatile USB_EP_Desc_t TxBulkDummyEPList[NBR_OF_EPIDS][2] __attribute__ ((aligned (4)));
265
266 static volatile USB_EP_Desc_t TxIsocEPList[NBR_OF_EPIDS] __attribute__ ((aligned (4)));
267 static volatile USB_SB_Desc_t TxIsocSB_zout __attribute__ ((aligned (4)));
268
269 static volatile USB_EP_Desc_t TxIntrEPList[MAX_INTR_INTERVAL] __attribute__ ((aligned (4)));
270 static volatile USB_SB_Desc_t TxIntrSB_zout __attribute__ ((aligned (4)));
271
272 /* A zout transfer makes a memory access at the address of its buf pointer, which means that setting
273    this buf pointer to 0 will cause an access to the flash. In addition to this, setting sw_len to 0
274    results in a 16/32 bytes (depending on DMA burst size) transfer. Instead, we set it to 1, and point
275    it to this buffer. */
276 static int zout_buffer[4] __attribute__ ((aligned (4)));
277
278 /* Cache for allocating new EP and SB descriptors. */
279 static kmem_cache_t *usb_desc_cache;
280
281 /* Cache for the registers allocated in the top half. */
282 static kmem_cache_t *top_half_reg_cache;
283
284 /* Cache for the data allocated in the isoc descr top half. */
285 static kmem_cache_t *isoc_compl_cache;
286
287 static struct usb_bus *etrax_usb_bus;
288
289 /* This is a circular (double-linked) list of the active urbs for each epid.
290    The head is never removed, and new urbs are linked onto the list as
291    urb_entry_t elements. Don't reference urb_list directly; use the wrapper
292    functions instead. Note that working with these lists might require spinlock
293    protection. */
294 static struct list_head urb_list[NBR_OF_EPIDS];
295
296 /* Read about the need and usage of this lock in submit_ctrl_urb. */
297 static spinlock_t urb_list_lock;
298
299 /* Used when unlinking asynchronously. */
300 static struct list_head urb_unlink_list;
301
302 /* for returning string descriptors in UTF-16LE */
303 static int ascii2utf (char *ascii, __u8 *utf, int utfmax)
304 {
305         int retval;
306
307         for (retval = 0; *ascii && utfmax > 1; utfmax -= 2, retval += 2) {
308                 *utf++ = *ascii++ & 0x7f;
309                 *utf++ = 0;
310         }
311         return retval;
312 }
313
314 static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
315 {
316         char buf [30];
317
318         // assert (len > (2 * (sizeof (buf) + 1)));
319         // assert (strlen (type) <= 8);
320
321         // language ids
322         if (id == 0) {
323                 *data++ = 4; *data++ = 3;       /* 4 bytes data */
324                 *data++ = 0; *data++ = 0;       /* some language id */
325                 return 4;
326
327         // serial number
328         } else if (id == 1) {
329                 sprintf (buf, "%x", serial);
330
331         // product description
332         } else if (id == 2) {
333                 sprintf (buf, "USB %s Root Hub", type);
334
335         // id 3 == vendor description
336
337         // unsupported IDs --> "stall"
338         } else
339             return 0;
340
341         data [0] = 2 + ascii2utf (buf, data + 2, len - 2);
342         data [1] = 3;
343         return data [0];
344 }
345
346 /* Wrappers around the list functions (include/linux/list.h). */
347
348 static inline int urb_list_empty(int epid)
349 {
350         return list_empty(&urb_list[epid]);
351 }
352
353 /* Returns first urb for this epid, or NULL if list is empty. */
354 static inline struct urb *urb_list_first(int epid)
355 {
356         struct urb *first_urb = 0;
357
358         if (!urb_list_empty(epid)) {
359                 /* Get the first urb (i.e. head->next). */
360                 urb_entry_t *urb_entry = list_entry((&urb_list[epid])->next, urb_entry_t, list);
361                 first_urb = urb_entry->urb;
362         }
363         return first_urb;
364 }
365
366 /* Adds an urb_entry last in the list for this epid. */
367 static inline void urb_list_add(struct urb *urb, int epid)
368 {
369         urb_entry_t *urb_entry = (urb_entry_t *)kmalloc(sizeof(urb_entry_t), KMALLOC_FLAG);
370         assert(urb_entry);
371
372         urb_entry->urb = urb;
373         list_add_tail(&urb_entry->list, &urb_list[epid]);
374 }
375
376 /* Search through the list for an element that contains this urb. (The list
377    is expected to be short and the one we are about to delete will often be
378    the first in the list.) */
379 static inline urb_entry_t *__urb_list_entry(struct urb *urb, int epid)
380 {
381         struct list_head *entry;
382         struct list_head *tmp;
383         urb_entry_t *urb_entry;
384
385         list_for_each_safe(entry, tmp, &urb_list[epid]) {
386                 urb_entry = list_entry(entry, urb_entry_t, list);
387                 assert(urb_entry);
388                 assert(urb_entry->urb);
389
390                 if (urb_entry->urb == urb) {
391                         return urb_entry;
392                 }
393         }
394         return 0;
395 }
396
397 /* Delete an urb from the list. */
398 static inline void urb_list_del(struct urb *urb, int epid)
399 {
400         urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
401         assert(urb_entry);
402
403         /* Delete entry and free. */
404         list_del(&urb_entry->list);
405         kfree(urb_entry);
406 }
407
408 /* Move an urb to the end of the list. */
409 static inline void urb_list_move_last(struct urb *urb, int epid)
410 {
411         urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
412         assert(urb_entry);
413
414         list_move_tail(&urb_entry->list, &urb_list[epid]);
415 }
416
417 /* Get the next urb in the list. */
418 static inline struct urb *urb_list_next(struct urb *urb, int epid)
419 {
420         urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
421
422         assert(urb_entry);
423
424         if (urb_entry->list.next != &urb_list[epid]) {
425                 struct list_head *elem = urb_entry->list.next;
426                 urb_entry = list_entry(elem, urb_entry_t, list);
427                 return urb_entry->urb;
428         } else {
429                 return NULL;
430         }
431 }
432
433
434
435 /* For debug purposes only. */
436 static inline void urb_list_dump(int epid)
437 {
438         struct list_head *entry;
439         struct list_head *tmp;
440         urb_entry_t *urb_entry;
441         int i = 0;
442
443         info("Dumping urb list for epid %d", epid);
444
445         list_for_each_safe(entry, tmp, &urb_list[epid]) {
446                 urb_entry = list_entry(entry, urb_entry_t, list);
447                 info("   entry %d, urb = 0x%lx", i, (unsigned long)urb_entry->urb);
448         }
449 }
450
451 static void init_rx_buffers(void);
452 static int etrax_rh_unlink_urb(struct urb *urb);
453 static void etrax_rh_send_irq(struct urb *urb);
454 static void etrax_rh_init_int_timer(struct urb *urb);
455 static void etrax_rh_int_timer_do(unsigned long ptr);
456
457 static int etrax_usb_setup_epid(struct urb *urb);
458 static int etrax_usb_lookup_epid(struct urb *urb);
459 static int etrax_usb_allocate_epid(void);
460 static void etrax_usb_free_epid(int epid);
461
462 static int etrax_remove_from_sb_list(struct urb *urb);
463
464 static void* etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size,
465         unsigned mem_flags, dma_addr_t *dma);
466 static void etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma);
467
468 static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid);
469 static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid);
470 static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid);
471 static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid);
472
473 static int etrax_usb_submit_bulk_urb(struct urb *urb);
474 static int etrax_usb_submit_ctrl_urb(struct urb *urb);
475 static int etrax_usb_submit_intr_urb(struct urb *urb);
476 static int etrax_usb_submit_isoc_urb(struct urb *urb);
477
478 static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags);
479 static int etrax_usb_unlink_urb(struct urb *urb, int status);
480 static int etrax_usb_get_frame_number(struct usb_device *usb_dev);
481
482 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs);
483 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs);
484 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs);
485 static void etrax_usb_hc_interrupt_bottom_half(void *data);
486
487 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data);
488
489
490 /* The following is a list of interrupt handlers for the host controller interrupts we use.
491    They are called from etrax_usb_hc_interrupt_bottom_half. */
492 static void etrax_usb_hc_isoc_eof_interrupt(void);
493 static void etrax_usb_hc_bulk_eot_interrupt(int timer_induced);
494 static void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg);
495 static void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg);
496 static void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg);
497
498 static int etrax_rh_submit_urb (struct urb *urb);
499
500 /* Forward declaration needed because they are used in the rx interrupt routine. */
501 static void etrax_usb_complete_urb(struct urb *urb, int status);
502 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status);
503 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status);
504 static void etrax_usb_complete_intr_urb(struct urb *urb, int status);
505 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status);
506
507 static int etrax_usb_hc_init(void);
508 static void etrax_usb_hc_cleanup(void);
509
510 static struct usb_operations etrax_usb_device_operations =
511 {
512         .get_frame_number = etrax_usb_get_frame_number,
513         .submit_urb = etrax_usb_submit_urb,
514         .unlink_urb = etrax_usb_unlink_urb,
515         .buffer_alloc = etrax_usb_buffer_alloc,
516         .buffer_free = etrax_usb_buffer_free
517 };
518
519 /* Note that these functions are always available in their "__" variants, for use in
520    error situations. The "__" missing variants are controlled by the USB_DEBUG_DESC/
521    USB_DEBUG_URB macros. */
522 static void __dump_urb(struct urb* purb)
523 {
524         printk("\nurb                  :0x%08lx\n", (unsigned long)purb);
525         printk("dev                   :0x%08lx\n", (unsigned long)purb->dev);
526         printk("pipe                  :0x%08x\n", purb->pipe);
527         printk("status                :%d\n", purb->status);
528         printk("transfer_flags        :0x%08x\n", purb->transfer_flags);
529         printk("transfer_buffer       :0x%08lx\n", (unsigned long)purb->transfer_buffer);
530         printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
531         printk("actual_length         :%d\n", purb->actual_length);
532         printk("setup_packet          :0x%08lx\n", (unsigned long)purb->setup_packet);
533         printk("start_frame           :%d\n", purb->start_frame);
534         printk("number_of_packets     :%d\n", purb->number_of_packets);
535         printk("interval              :%d\n", purb->interval);
536         printk("error_count           :%d\n", purb->error_count);
537         printk("context               :0x%08lx\n", (unsigned long)purb->context);
538         printk("complete              :0x%08lx\n\n", (unsigned long)purb->complete);
539 }
540
541 static void __dump_in_desc(volatile USB_IN_Desc_t *in)
542 {
543         printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
544         printk("  sw_len  : 0x%04x (%d)\n", in->sw_len, in->sw_len);
545         printk("  command : 0x%04x\n", in->command);
546         printk("  next    : 0x%08lx\n", in->next);
547         printk("  buf     : 0x%08lx\n", in->buf);
548         printk("  hw_len  : 0x%04x (%d)\n", in->hw_len, in->hw_len);
549         printk("  status  : 0x%04x\n\n", in->status);
550 }
551
552 static void __dump_sb_desc(volatile USB_SB_Desc_t *sb)
553 {
554         char tt = (sb->command & 0x30) >> 4;
555         char *tt_string;
556
557         switch (tt) {
558         case 0:
559                 tt_string = "zout";
560                 break;
561         case 1:
562                 tt_string = "in";
563                 break;
564         case 2:
565                 tt_string = "out";
566                 break;
567         case 3:
568                 tt_string = "setup";
569                 break;
570         default:
571                 tt_string = "unknown (weird)";
572         }
573
574         printk("\n   USB_SB_Desc at 0x%08lx\n", (unsigned long)sb);
575         printk("     command : 0x%04x\n", sb->command);
576         printk("        rem     : %d\n", (sb->command & 0x3f00) >> 8);
577         printk("        full    : %d\n", (sb->command & 0x40) >> 6);
578         printk("        tt      : %d (%s)\n", tt, tt_string);
579         printk("        intr    : %d\n", (sb->command & 0x8) >> 3);
580         printk("        eot     : %d\n", (sb->command & 0x2) >> 1);
581         printk("        eol     : %d\n", sb->command & 0x1);
582         printk("     sw_len  : 0x%04x (%d)\n", sb->sw_len, sb->sw_len);
583         printk("     next    : 0x%08lx\n", sb->next);
584         printk("     buf     : 0x%08lx\n\n", sb->buf);
585 }
586
587
588 static void __dump_ep_desc(volatile USB_EP_Desc_t *ep)
589 {
590         printk("\nUSB_EP_Desc at 0x%08lx\n", (unsigned long)ep);
591         printk("  command : 0x%04x\n", ep->command);
592         printk("     ep_id   : %d\n", (ep->command & 0x1f00) >> 8);
593         printk("     enable  : %d\n", (ep->command & 0x10) >> 4);
594         printk("     intr    : %d\n", (ep->command & 0x8) >> 3);
595         printk("     eof     : %d\n", (ep->command & 0x2) >> 1);
596         printk("     eol     : %d\n", ep->command & 0x1);
597         printk("  hw_len  : 0x%04x (%d)\n", ep->hw_len, ep->hw_len);
598         printk("  next    : 0x%08lx\n", ep->next);
599         printk("  sub     : 0x%08lx\n\n", ep->sub);
600 }
601
602 static inline void __dump_ep_list(int pipe_type)
603 {
604         volatile USB_EP_Desc_t *ep;
605         volatile USB_EP_Desc_t *first_ep;
606         volatile USB_SB_Desc_t *sb;
607
608         switch (pipe_type)
609         {
610         case PIPE_BULK:
611                 first_ep = &TxBulkEPList[0];
612                 break;
613         case PIPE_CONTROL:
614                 first_ep = &TxCtrlEPList[0];
615                 break;
616         case PIPE_INTERRUPT:
617                 first_ep = &TxIntrEPList[0];
618                 break;
619         case PIPE_ISOCHRONOUS:
620                 first_ep = &TxIsocEPList[0];
621                 break;
622         default:
623                 warn("Cannot dump unknown traffic type");
624                 return;
625         }
626         ep = first_ep;
627
628         printk("\n\nDumping EP list...\n\n");
629
630         do {
631                 __dump_ep_desc(ep);
632                 /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
633                 sb = ep->sub ? phys_to_virt(ep->sub) : 0;
634                 while (sb) {
635                         __dump_sb_desc(sb);
636                         sb = sb->next ? phys_to_virt(sb->next) : 0;
637                 }
638                 ep = (volatile USB_EP_Desc_t *)(phys_to_virt(ep->next));
639
640         } while (ep != first_ep);
641 }
642
643 static inline void __dump_ept_data(int epid)
644 {
645         unsigned long flags;
646         __u32 r_usb_ept_data;
647
648         if (epid < 0 || epid > 31) {
649                 printk("Cannot dump ept data for invalid epid %d\n", epid);
650                 return;
651         }
652
653         save_flags(flags);
654         cli();
655         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
656         nop();
657         r_usb_ept_data = *R_USB_EPT_DATA;
658         restore_flags(flags);
659
660         printk("\nR_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
661         if (r_usb_ept_data == 0) {
662                 /* No need for more detailed printing. */
663                 return;
664         }
665         printk("  valid           : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
666         printk("  hold            : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
667         printk("  error_count_in  : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
668         printk("  t_in            : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
669         printk("  low_speed       : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
670         printk("  port            : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
671         printk("  error_code      : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
672         printk("  t_out           : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
673         printk("  error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
674         printk("  max_len         : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
675         printk("  ep              : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
676         printk("  dev             : %d\n", (r_usb_ept_data & 0x0000003f));
677 }
678
679 static inline void __dump_ept_data_list(void)
680 {
681         int i;
682
683         printk("Dumping the whole R_USB_EPT_DATA list\n");
684
685         for (i = 0; i < 32; i++) {
686                 __dump_ept_data(i);
687         }
688 }
689 #ifdef USB_DEBUG_DESC
690 #define dump_in_desc(...) __dump_in_desc(...)
691 #define dump_sb_desc(...) __dump_sb_desc(...)
692 #define dump_ep_desc(...) __dump_ep_desc(...)
693 #else
694 #define dump_in_desc(...) do {} while (0)
695 #define dump_sb_desc(...) do {} while (0)
696 #define dump_ep_desc(...) do {} while (0)
697 #endif
698
699 #ifdef USB_DEBUG_URB
700 #define dump_urb(x)     __dump_urb(x)
701 #else
702 #define dump_urb(x)     do {} while (0)
703 #endif
704
705 static void init_rx_buffers(void)
706 {
707         int i;
708
709         DBFENTER;
710
711         for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
712                 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
713                 RxDescList[i].command = 0;
714                 RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
715                 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
716                 RxDescList[i].hw_len = 0;
717                 RxDescList[i].status = 0;
718
719                 /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as USB_IN_Desc
720                    for the relevant fields.) */
721                 prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
722
723         }
724
725         RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
726         RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
727         RxDescList[i].next = virt_to_phys(&RxDescList[0]);
728         RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
729         RxDescList[i].hw_len = 0;
730         RxDescList[i].status = 0;
731
732         myNextRxDesc = &RxDescList[0];
733         myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
734         myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
735
736         *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
737         *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
738
739         DBFEXIT;
740 }
741
742 static void init_tx_bulk_ep(void)
743 {
744         int i;
745
746         DBFENTER;
747
748         for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
749                 CHECK_ALIGN(&TxBulkEPList[i]);
750                 TxBulkEPList[i].hw_len = 0;
751                 TxBulkEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
752                 TxBulkEPList[i].sub = 0;
753                 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[i + 1]);
754
755                 /* Initiate two EPs, disabled and with the eol flag set. No need for any
756                    preserved epid. */
757
758                 /* The first one has the intr flag set so we get an interrupt when the DMA
759                    channel is about to become disabled. */
760                 CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
761                 TxBulkDummyEPList[i][0].hw_len = 0;
762                 TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
763                                                    IO_STATE(USB_EP_command, eol, yes) |
764                                                    IO_STATE(USB_EP_command, intr, yes));
765                 TxBulkDummyEPList[i][0].sub = 0;
766                 TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
767
768                 /* The second one. */
769                 CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
770                 TxBulkDummyEPList[i][1].hw_len = 0;
771                 TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
772                                                    IO_STATE(USB_EP_command, eol, yes));
773                 TxBulkDummyEPList[i][1].sub = 0;
774                 /* The last dummy's next pointer is the same as the current EP's next pointer. */
775                 TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
776         }
777
778         /* Configure the last one. */
779         CHECK_ALIGN(&TxBulkEPList[i]);
780         TxBulkEPList[i].hw_len = 0;
781         TxBulkEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
782                                    IO_FIELD(USB_EP_command, epid, i));
783         TxBulkEPList[i].sub = 0;
784         TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[0]);
785
786         /* No need configuring dummy EPs for the last one as it will never be used for
787            bulk traffic (i == INVALD_EPID at this point). */
788
789         /* Set up to start on the last EP so we will enable it when inserting traffic
790            for the first time (imitating the situation where the DMA has stopped
791            because there was no more traffic). */
792         *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
793         /* No point in starting the bulk channel yet.
794          *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
795         DBFEXIT;
796 }
797
798 static void init_tx_ctrl_ep(void)
799 {
800         int i;
801
802         DBFENTER;
803
804         for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
805                 CHECK_ALIGN(&TxCtrlEPList[i]);
806                 TxCtrlEPList[i].hw_len = 0;
807                 TxCtrlEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
808                 TxCtrlEPList[i].sub = 0;
809                 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[i + 1]);
810         }
811
812         CHECK_ALIGN(&TxCtrlEPList[i]);
813         TxCtrlEPList[i].hw_len = 0;
814         TxCtrlEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
815                                    IO_FIELD(USB_EP_command, epid, i));
816
817         TxCtrlEPList[i].sub = 0;
818         TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[0]);
819
820         *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[0]);
821         *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
822
823         DBFEXIT;
824 }
825
826
827 static void init_tx_intr_ep(void)
828 {
829         int i;
830
831         DBFENTER;
832
833         /* Read comment at zout_buffer declaration for an explanation to this. */
834         TxIntrSB_zout.sw_len = 1;
835         TxIntrSB_zout.next = 0;
836         TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
837         TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
838                                  IO_STATE(USB_SB_command, tt, zout) |
839                                  IO_STATE(USB_SB_command, full, yes) |
840                                  IO_STATE(USB_SB_command, eot, yes) |
841                                  IO_STATE(USB_SB_command, eol, yes));
842
843         for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
844                 CHECK_ALIGN(&TxIntrEPList[i]);
845                 TxIntrEPList[i].hw_len = 0;
846                 TxIntrEPList[i].command =
847                         (IO_STATE(USB_EP_command, eof, yes) |
848                          IO_STATE(USB_EP_command, enable, yes) |
849                          IO_FIELD(USB_EP_command, epid, INVALID_EPID));
850                 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
851                 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
852         }
853
854         CHECK_ALIGN(&TxIntrEPList[i]);
855         TxIntrEPList[i].hw_len = 0;
856         TxIntrEPList[i].command =
857                 (IO_STATE(USB_EP_command, eof, yes) |
858                  IO_STATE(USB_EP_command, eol, yes) |
859                  IO_STATE(USB_EP_command, enable, yes) |
860                  IO_FIELD(USB_EP_command, epid, INVALID_EPID));
861         TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
862         TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
863
864         *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
865         *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
866         DBFEXIT;
867 }
868
869 static void init_tx_isoc_ep(void)
870 {
871         int i;
872
873         DBFENTER;
874
875         /* Read comment at zout_buffer declaration for an explanation to this. */
876         TxIsocSB_zout.sw_len = 1;
877         TxIsocSB_zout.next = 0;
878         TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
879         TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
880                                  IO_STATE(USB_SB_command, tt, zout) |
881                                  IO_STATE(USB_SB_command, full, yes) |
882                                  IO_STATE(USB_SB_command, eot, yes) |
883                                  IO_STATE(USB_SB_command, eol, yes));
884
885         /* The last isochronous EP descriptor is a dummy. */
886
887         for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
888                 CHECK_ALIGN(&TxIsocEPList[i]);
889                 TxIsocEPList[i].hw_len = 0;
890                 TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
891                 TxIsocEPList[i].sub = 0;
892                 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
893         }
894
895         CHECK_ALIGN(&TxIsocEPList[i]);
896         TxIsocEPList[i].hw_len = 0;
897
898         /* Must enable the last EP descr to get eof interrupt. */
899         TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
900                                    IO_STATE(USB_EP_command, eof, yes) |
901                                    IO_STATE(USB_EP_command, eol, yes) |
902                                    IO_FIELD(USB_EP_command, epid, INVALID_EPID));
903         TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
904         TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
905
906         *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
907         *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
908
909         DBFEXIT;
910 }
911
912 static void etrax_usb_unlink_intr_urb(struct urb *urb)
913 {
914         volatile USB_EP_Desc_t *first_ep;  /* First EP in the list. */
915         volatile USB_EP_Desc_t *curr_ep;   /* Current EP, the iterator. */
916         volatile USB_EP_Desc_t *next_ep;   /* The EP after current. */
917         volatile USB_EP_Desc_t *unlink_ep; /* The one we should remove from the list. */
918
919         int epid;
920
921         /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the List". */
922
923         DBFENTER;
924
925         epid = ((etrax_urb_priv_t *)urb->hcpriv)->epid;
926
927         first_ep = &TxIntrEPList[0];
928         curr_ep = first_ep;
929
930
931         /* Note that this loop removes all EP descriptors with this epid. This assumes
932            that all EP descriptors belong to the one and only urb for this epid. */
933
934         do {
935                 next_ep = (USB_EP_Desc_t *)phys_to_virt(curr_ep->next);
936
937                 if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
938
939                         dbg_intr("Found EP to unlink for epid %d", epid);
940
941                         /* This is the one we should unlink. */
942                         unlink_ep = next_ep;
943
944                         /* Actually unlink the EP from the DMA list. */
945                         curr_ep->next = unlink_ep->next;
946
947                         /* Wait until the DMA is no longer at this descriptor. */
948                         while (*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep));
949
950                         /* Now we are free to remove it and its SB descriptor.
951                            Note that it is assumed here that there is only one sb in the
952                            sb list for this ep. */
953                         kmem_cache_free(usb_desc_cache, phys_to_virt(unlink_ep->sub));
954                         kmem_cache_free(usb_desc_cache, (USB_EP_Desc_t *)unlink_ep);
955                 }
956
957                 curr_ep = phys_to_virt(curr_ep->next);
958
959         } while (curr_ep != first_ep);
960         urb->hcpriv = NULL;
961 }
962
963 void etrax_usb_do_intr_recover(int epid)
964 {
965         USB_EP_Desc_t *first_ep, *tmp_ep;
966
967         DBFENTER;
968
969         first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
970         tmp_ep = first_ep;
971
972         /* What this does is simply to walk the list of interrupt
973            ep descriptors and enable those that are disabled. */
974
975         do {
976                 if (IO_EXTRACT(USB_EP_command, epid, tmp_ep->command) == epid &&
977                     !(tmp_ep->command & IO_MASK(USB_EP_command, enable))) {
978                         tmp_ep->command |= IO_STATE(USB_EP_command, enable, yes);
979                 }
980
981                 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
982
983         } while (tmp_ep != first_ep);
984
985
986         DBFEXIT;
987 }
988
989 static int etrax_rh_unlink_urb (struct urb *urb)
990 {
991         etrax_hc_t *hc;
992
993         DBFENTER;
994
995         hc = urb->dev->bus->hcpriv;
996
997         if (hc->rh.urb == urb) {
998                 hc->rh.send = 0;
999                 del_timer(&hc->rh.rh_int_timer);
1000         }
1001
1002         DBFEXIT;
1003         return 0;
1004 }
1005
1006 static void etrax_rh_send_irq(struct urb *urb)
1007 {
1008         __u16 data = 0;
1009         etrax_hc_t *hc = urb->dev->bus->hcpriv;
1010         DBFENTER;
1011
1012 /*
1013   dbg_rh("R_USB_FM_NUMBER   : 0x%08X", *R_USB_FM_NUMBER);
1014   dbg_rh("R_USB_FM_REMAINING: 0x%08X", *R_USB_FM_REMAINING);
1015 */
1016
1017         data |= (hc->rh.wPortChange_1) ? (1 << 1) : 0;
1018         data |= (hc->rh.wPortChange_2) ? (1 << 2) : 0;
1019
1020         *((__u16 *)urb->transfer_buffer) = cpu_to_le16(data);
1021         /* FIXME: Why is actual_length set to 1 when data is 2 bytes?
1022            Since only 1 byte is used, why not declare data as __u8? */
1023         urb->actual_length = 1;
1024         urb->status = 0;
1025
1026         if (hc->rh.send && urb->complete) {
1027                 dbg_rh("wPortChange_1: 0x%04X", hc->rh.wPortChange_1);
1028                 dbg_rh("wPortChange_2: 0x%04X", hc->rh.wPortChange_2);
1029
1030                 urb->complete(urb, NULL);
1031         }
1032
1033         DBFEXIT;
1034 }
1035
1036 static void etrax_rh_init_int_timer(struct urb *urb)
1037 {
1038         etrax_hc_t *hc;
1039
1040         DBFENTER;
1041
1042         hc = urb->dev->bus->hcpriv;
1043         hc->rh.interval = urb->interval;
1044         init_timer(&hc->rh.rh_int_timer);
1045         hc->rh.rh_int_timer.function = etrax_rh_int_timer_do;
1046         hc->rh.rh_int_timer.data = (unsigned long)urb;
1047         /* FIXME: Is the jiffies resolution enough? All intervals < 10 ms will be mapped
1048            to 0, and the rest to the nearest lower 10 ms. */
1049         hc->rh.rh_int_timer.expires = jiffies + ((HZ * hc->rh.interval) / 1000);
1050         add_timer(&hc->rh.rh_int_timer);
1051
1052         DBFEXIT;
1053 }
1054
1055 static void etrax_rh_int_timer_do(unsigned long ptr)
1056 {
1057         struct urb *urb;
1058         etrax_hc_t *hc;
1059
1060         DBFENTER;
1061
1062         urb = (struct urb*)ptr;
1063         hc = urb->dev->bus->hcpriv;
1064
1065         if (hc->rh.send) {
1066                 etrax_rh_send_irq(urb);
1067         }
1068
1069         DBFEXIT;
1070 }
1071
1072 static int etrax_usb_setup_epid(struct urb *urb)
1073 {
1074         int epid;
1075         char devnum, endpoint, out_traffic, slow;
1076         int maxlen;
1077         unsigned long flags;
1078
1079         DBFENTER;
1080
1081         epid = etrax_usb_lookup_epid(urb);
1082         if ((epid != -1)){
1083                 /* An epid that fits this urb has been found. */
1084                 DBFEXIT;
1085                 return epid;
1086         }
1087
1088         /* We must find and initiate a new epid for this urb. */
1089         epid = etrax_usb_allocate_epid();
1090
1091         if (epid == -1) {
1092                 /* Failed to allocate a new epid. */
1093                 DBFEXIT;
1094                 return epid;
1095         }
1096
1097         /* We now have a new epid to use. Initiate it. */
1098         set_bit(epid, (void *)&epid_usage_bitmask);
1099
1100         devnum = usb_pipedevice(urb->pipe);
1101         endpoint = usb_pipeendpoint(urb->pipe);
1102         slow = usb_pipeslow(urb->pipe);
1103         maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1104         if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1105                 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1106                 out_traffic = 1;
1107         } else {
1108                 out_traffic = usb_pipeout(urb->pipe);
1109         }
1110
1111         save_flags(flags);
1112         cli();
1113
1114         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1115         nop();
1116
1117         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1118                 *R_USB_EPT_DATA_ISO = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
1119                         /* FIXME: Change any to the actual port? */
1120                         IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
1121                         IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
1122                         IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
1123                         IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
1124         } else {
1125                 *R_USB_EPT_DATA = IO_STATE(R_USB_EPT_DATA, valid, yes) |
1126                         IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
1127                         /* FIXME: Change any to the actual port? */
1128                         IO_STATE(R_USB_EPT_DATA, port, any) |
1129                         IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
1130                         IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
1131                         IO_FIELD(R_USB_EPT_DATA, dev, devnum);
1132         }
1133
1134         restore_flags(flags);
1135
1136         if (out_traffic) {
1137                 set_bit(epid, (void *)&epid_out_traffic);
1138         } else {
1139                 clear_bit(epid, (void *)&epid_out_traffic);
1140         }
1141
1142         dbg_epid("Setting up epid %d with devnum %d, endpoint %d and max_len %d (%s)",
1143                  epid, devnum, endpoint, maxlen, out_traffic ? "OUT" : "IN");
1144
1145         DBFEXIT;
1146         return epid;
1147 }
1148
1149 static void etrax_usb_free_epid(int epid)
1150 {
1151         unsigned long flags;
1152
1153         DBFENTER;
1154
1155         if (!test_bit(epid, (void *)&epid_usage_bitmask)) {
1156                 warn("Trying to free unused epid %d", epid);
1157                 DBFEXIT;
1158                 return;
1159         }
1160
1161         save_flags(flags);
1162         cli();
1163
1164         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1165         nop();
1166         while (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold));
1167         /* This will, among other things, set the valid field to 0. */
1168         *R_USB_EPT_DATA = 0;
1169         restore_flags(flags);
1170
1171         clear_bit(epid, (void *)&epid_usage_bitmask);
1172
1173
1174         dbg_epid("Freed epid %d", epid);
1175
1176         DBFEXIT;
1177 }
1178
1179 static int etrax_usb_lookup_epid(struct urb *urb)
1180 {
1181         int i;
1182         __u32 data;
1183         char devnum, endpoint, slow, out_traffic;
1184         int maxlen;
1185         unsigned long flags;
1186
1187         DBFENTER;
1188
1189         devnum = usb_pipedevice(urb->pipe);
1190         endpoint = usb_pipeendpoint(urb->pipe);
1191         slow = usb_pipeslow(urb->pipe);
1192         maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1193         if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1194                 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1195                 out_traffic = 1;
1196         } else {
1197                 out_traffic = usb_pipeout(urb->pipe);
1198         }
1199
1200         /* Step through att epids. */
1201         for (i = 0; i < NBR_OF_EPIDS; i++) {
1202                 if (test_bit(i, (void *)&epid_usage_bitmask) &&
1203                     test_bit(i, (void *)&epid_out_traffic) == out_traffic) {
1204
1205                         save_flags(flags);
1206                         cli();
1207                         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, i);
1208                         nop();
1209
1210                         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1211                                 data = *R_USB_EPT_DATA_ISO;
1212                                 restore_flags(flags);
1213
1214                                 if ((IO_MASK(R_USB_EPT_DATA_ISO, valid) & data) &&
1215                                     (IO_EXTRACT(R_USB_EPT_DATA_ISO, dev, data) == devnum) &&
1216                                     (IO_EXTRACT(R_USB_EPT_DATA_ISO, ep, data) == endpoint) &&
1217                                     (IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len, data) == maxlen)) {
1218                                         dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1219                                                  i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1220                                         DBFEXIT;
1221                                         return i;
1222                                 }
1223                         } else {
1224                                 data = *R_USB_EPT_DATA;
1225                                 restore_flags(flags);
1226
1227                                 if ((IO_MASK(R_USB_EPT_DATA, valid) & data) &&
1228                                     (IO_EXTRACT(R_USB_EPT_DATA, dev, data) == devnum) &&
1229                                     (IO_EXTRACT(R_USB_EPT_DATA, ep, data) == endpoint) &&
1230                                     (IO_EXTRACT(R_USB_EPT_DATA, low_speed, data) == slow) &&
1231                                     (IO_EXTRACT(R_USB_EPT_DATA, max_len, data) == maxlen)) {
1232                                         dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1233                                                  i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1234                                         DBFEXIT;
1235                                         return i;
1236                                 }
1237                         }
1238                 }
1239         }
1240
1241         DBFEXIT;
1242         return -1;
1243 }
1244
1245 static int etrax_usb_allocate_epid(void)
1246 {
1247         int i;
1248
1249         DBFENTER;
1250
1251         for (i = 0; i < NBR_OF_EPIDS; i++) {
1252                 if (!test_bit(i, (void *)&epid_usage_bitmask)) {
1253                         dbg_epid("Found free epid %d", i);
1254                         DBFEXIT;
1255                         return i;
1256                 }
1257         }
1258
1259         dbg_epid("Found no free epids");
1260         DBFEXIT;
1261         return -1;
1262 }
1263
1264 static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags)
1265 {
1266         etrax_hc_t *hc;
1267         int ret = -EINVAL;
1268
1269         DBFENTER;
1270
1271         if (!urb->dev || !urb->dev->bus) {
1272                 return -ENODEV;
1273         }
1274         if (usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)) <= 0) {
1275                 info("Submit urb to pipe with maxpacketlen 0, pipe 0x%X\n", urb->pipe);
1276                 return -EMSGSIZE;
1277         }
1278
1279         if (urb->timeout) {
1280                 /* FIXME. */
1281                 warn("urb->timeout specified, ignoring.");
1282         }
1283
1284         hc = (etrax_hc_t*)urb->dev->bus->hcpriv;
1285
1286         if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1287                 /* This request is for the Virtual Root Hub. */
1288                 ret = etrax_rh_submit_urb(urb);
1289
1290         } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1291
1292                 ret = etrax_usb_submit_bulk_urb(urb);
1293
1294         } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1295
1296                 ret = etrax_usb_submit_ctrl_urb(urb);
1297
1298         } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1299                 int bustime;
1300
1301                 if (urb->bandwidth == 0) {
1302                         bustime = usb_check_bandwidth(urb->dev, urb);
1303                         if (bustime < 0) {
1304                                 ret = bustime;
1305                         } else {
1306                                 ret = etrax_usb_submit_intr_urb(urb);
1307                                 if (ret == 0)
1308                                         usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1309                         }
1310                 } else {
1311                         /* Bandwidth already set. */
1312                         ret = etrax_usb_submit_intr_urb(urb);
1313                 }
1314
1315         } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1316                 int bustime;
1317
1318                 if (urb->bandwidth == 0) {
1319                         bustime = usb_check_bandwidth(urb->dev, urb);
1320                         if (bustime < 0) {
1321                                 ret = bustime;
1322                         } else {
1323                                 ret = etrax_usb_submit_isoc_urb(urb);
1324                                 if (ret == 0)
1325                                         usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1326                         }
1327                 } else {
1328                         /* Bandwidth already set. */
1329                         ret = etrax_usb_submit_isoc_urb(urb);
1330                 }
1331         }
1332
1333         DBFEXIT;
1334
1335         if (ret != 0)
1336           printk("Submit URB error %d\n", ret);
1337
1338         return ret;
1339 }
1340
1341 static int etrax_usb_unlink_urb(struct urb *urb, int status)
1342 {
1343         etrax_hc_t *hc;
1344         etrax_urb_priv_t *urb_priv;
1345         int epid;
1346         unsigned int flags;
1347
1348         DBFENTER;
1349
1350         if (!urb) {
1351                 return -EINVAL;
1352         }
1353
1354         /* Disable interrupts here since a descriptor interrupt for the isoc epid
1355            will modify the sb list.  This could possibly be done more granular, but
1356            unlink_urb should not be used frequently anyway.
1357         */
1358
1359         save_flags(flags);
1360         cli();
1361
1362         if (!urb->dev || !urb->dev->bus) {
1363                 restore_flags(flags);
1364                 return -ENODEV;
1365         }
1366         if (!urb->hcpriv) {
1367                 /* This happens if a device driver calls unlink on an urb that
1368                    was never submitted (lazy driver) or if the urb was completed
1369                    while unlink was being called. */
1370                 restore_flags(flags);
1371                 return 0;
1372         }
1373         if (urb->transfer_flags & URB_ASYNC_UNLINK) {
1374                 /* FIXME. */
1375                 /* If URB_ASYNC_UNLINK is set:
1376                    unlink
1377                    move to a separate urb list
1378                    call complete at next sof with ECONNRESET
1379
1380                    If not:
1381                    wait 1 ms
1382                    unlink
1383                    call complete with ENOENT
1384                 */
1385                 warn("URB_ASYNC_UNLINK set, ignoring.");
1386         }
1387
1388         /* One might think that urb->status = -EINPROGRESS would be a requirement for unlinking,
1389            but that doesn't work for interrupt and isochronous traffic since they are completed
1390            repeatedly, and urb->status is set then. That may in itself be a bug though. */
1391
1392         hc = urb->dev->bus->hcpriv;
1393         urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1394         epid = urb_priv->epid;
1395
1396         /* Set the urb status (synchronous unlink). */
1397         urb->status = -ENOENT;
1398         urb_priv->urb_state = UNLINK;
1399
1400         if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1401                 int ret;
1402                 ret = etrax_rh_unlink_urb(urb);
1403                 DBFEXIT;
1404                 restore_flags(flags);
1405                 return ret;
1406
1407         } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1408
1409                 dbg_bulk("Unlink of bulk urb (0x%lx)", (unsigned long)urb);
1410
1411                 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1412                         /* The EP was enabled, disable it and wait. */
1413                         TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1414
1415                         /* Ah, the luxury of busy-wait. */
1416                         while (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[epid]));
1417                 }
1418                 /* Kicking dummy list out of the party. */
1419                 TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
1420
1421         } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1422
1423                 dbg_ctrl("Unlink of ctrl urb (0x%lx)", (unsigned long)urb);
1424
1425                 if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1426                         /* The EP was enabled, disable it and wait. */
1427                         TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1428
1429                         /* Ah, the luxury of busy-wait. */
1430                         while (*R_DMA_CH8_SUB1_EP == virt_to_phys(&TxCtrlEPList[epid]));
1431                 }
1432
1433         } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1434
1435                 dbg_intr("Unlink of intr urb (0x%lx)", (unsigned long)urb);
1436
1437                 /* Separate function because it's a tad more complicated. */
1438                 etrax_usb_unlink_intr_urb(urb);
1439
1440         } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1441
1442                 dbg_isoc("Unlink of isoc urb (0x%lx)", (unsigned long)urb);
1443
1444                 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1445                         /* The EP was enabled, disable it and wait. */
1446                         TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1447
1448                         /* Ah, the luxury of busy-wait. */
1449                         while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
1450                 }
1451         }
1452
1453         /* Note that we need to remove the urb from the urb list *before* removing its SB
1454            descriptors. (This means that the isoc eof handler might get a null urb when we
1455            are unlinking the last urb.) */
1456
1457         if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1458
1459                 urb_list_del(urb, epid);
1460                 TxBulkEPList[epid].sub = 0;
1461                 etrax_remove_from_sb_list(urb);
1462
1463         } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1464
1465                 urb_list_del(urb, epid);
1466                 TxCtrlEPList[epid].sub = 0;
1467                 etrax_remove_from_sb_list(urb);
1468
1469         } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1470
1471                 urb_list_del(urb, epid);
1472                 /* Sanity check (should never happen). */
1473                 assert(urb_list_empty(epid));
1474
1475                 /* Release allocated bandwidth. */
1476                 usb_release_bandwidth(urb->dev, urb, 0);
1477
1478         } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1479
1480                 if (usb_pipeout(urb->pipe)) {
1481
1482                         USB_SB_Desc_t *iter_sb, *prev_sb, *next_sb;
1483
1484                         if (__urb_list_entry(urb, epid)) {
1485
1486                                 urb_list_del(urb, epid);
1487                                 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1488                                 prev_sb = 0;
1489                                 while (iter_sb && (iter_sb != urb_priv->first_sb)) {
1490                                         prev_sb = iter_sb;
1491                                         iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1492                                 }
1493
1494                                 if (iter_sb == 0) {
1495                                         /* Unlink of the URB currently being transmitted. */
1496                                         prev_sb = 0;
1497                                         iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1498                                 }
1499
1500                                 while (iter_sb && (iter_sb != urb_priv->last_sb)) {
1501                                         iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1502                                 }
1503                                 if (iter_sb) {
1504                                         next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1505                                 } else {
1506                                         /* This should only happen if the DMA has completed
1507                                            processing the SB list for this EP while interrupts
1508                                            are disabled. */
1509                                         dbg_isoc("Isoc urb not found, already sent?");
1510                                         next_sb = 0;
1511                                 }
1512                                 if (prev_sb) {
1513                                         prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
1514                                 } else {
1515                                         TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
1516                                 }
1517
1518                                 etrax_remove_from_sb_list(urb);
1519                                 if (urb_list_empty(epid)) {
1520                                         TxIsocEPList[epid].sub = 0;
1521                                         dbg_isoc("Last isoc out urb epid %d", epid);
1522                                 } else if (next_sb || prev_sb) {
1523                                         dbg_isoc("Re-enable isoc out epid %d", epid);
1524
1525                                         TxIsocEPList[epid].hw_len = 0;
1526                                         TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1527                                 } else {
1528                                         TxIsocEPList[epid].sub = 0;
1529                                         dbg_isoc("URB list non-empty and no SB list, EP disabled");
1530                                 }
1531                         } else {
1532                                 dbg_isoc("Urb 0x%p not found, completed already?", urb);
1533                         }
1534                 } else {
1535
1536                         urb_list_del(urb, epid);
1537
1538                         /* For in traffic there is only one SB descriptor for each EP even
1539                            though there may be several urbs (all urbs point at the same SB). */
1540                         if (urb_list_empty(epid)) {
1541                                 /* No more urbs, remove the SB. */
1542                                 TxIsocEPList[epid].sub = 0;
1543                                 etrax_remove_from_sb_list(urb);
1544                         } else {
1545                                 TxIsocEPList[epid].hw_len = 0;
1546                                 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1547                         }
1548                 }
1549                 /* Release allocated bandwidth. */
1550                 usb_release_bandwidth(urb->dev, urb, 1);
1551         }
1552         /* Free the epid if urb list is empty. */
1553         if (urb_list_empty(epid)) {
1554                 etrax_usb_free_epid(epid);
1555         }
1556         restore_flags(flags);
1557
1558         /* Must be done before calling completion handler. */
1559         kfree(urb_priv);
1560         urb->hcpriv = 0;
1561
1562         if (urb->complete) {
1563                 urb->complete(urb, NULL);
1564         }
1565
1566         DBFEXIT;
1567         return 0;
1568 }
1569
1570 static int etrax_usb_get_frame_number(struct usb_device *usb_dev)
1571 {
1572         DBFENTER;
1573         DBFEXIT;
1574         return (*R_USB_FM_NUMBER & 0x7ff);
1575 }
1576
1577 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1578 {
1579         DBFENTER;
1580
1581         /* This interrupt handler could be used when unlinking EP descriptors. */
1582
1583         if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
1584                 USB_EP_Desc_t *ep;
1585
1586                 //dbg_bulk("dma8_sub0_descr (BULK) intr.");
1587
1588                 /* It should be safe clearing the interrupt here, since we don't expect to get a new
1589                    one until we restart the bulk channel. */
1590                 *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
1591
1592                 /* Wait while the DMA is running (though we don't expect it to be). */
1593                 while (*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd));
1594
1595                 /* Advance the DMA to the next EP descriptor. */
1596                 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
1597
1598                 //dbg_bulk("descr intr: DMA is at 0x%lx", (unsigned long)ep);
1599
1600                 /* ep->next is already a physical address; no need for a virt_to_phys. */
1601                 *R_DMA_CH8_SUB0_EP = ep->next;
1602
1603                 /* Start the DMA bulk channel again. */
1604                 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1605         }
1606         if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
1607                 struct urb *urb;
1608                 int epid;
1609                 etrax_urb_priv_t *urb_priv;
1610                 unsigned long int flags;
1611
1612                 dbg_ctrl("dma8_sub1_descr (CTRL) intr.");
1613                 *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
1614
1615                 /* The complete callback gets called so we cli. */
1616                 save_flags(flags);
1617                 cli();
1618
1619                 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1620                         if ((TxCtrlEPList[epid].sub == 0) ||
1621                             (epid == DUMMY_EPID) ||
1622                             (epid == INVALID_EPID)) {
1623                                 /* Nothing here to see. */
1624                                 continue;
1625                         }
1626
1627                         /* Get the first urb (if any). */
1628                         urb = urb_list_first(epid);
1629
1630                         if (urb) {
1631
1632                                 /* Sanity check. */
1633                                 assert(usb_pipetype(urb->pipe) == PIPE_CONTROL);
1634
1635                                 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1636                                 assert(urb_priv);
1637
1638                                 if (urb_priv->urb_state == WAITING_FOR_DESCR_INTR) {
1639                                         assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
1640
1641                                         etrax_usb_complete_urb(urb, 0);
1642                                 }
1643                         }
1644                 }
1645                 restore_flags(flags);
1646         }
1647         if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
1648                 dbg_intr("dma8_sub2_descr (INTR) intr.");
1649                 *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
1650         }
1651         if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
1652                 struct urb *urb;
1653                 int epid;
1654                 int epid_done;
1655                 etrax_urb_priv_t *urb_priv;
1656                 USB_SB_Desc_t *sb_desc;
1657
1658                 usb_isoc_complete_data_t *comp_data = NULL;
1659
1660                 /* One or more isoc out transfers are done. */
1661                 dbg_isoc("dma8_sub3_descr (ISOC) intr.");
1662
1663                 /* For each isoc out EP search for the first sb_desc with the intr flag
1664                    set.  This descriptor must be the last packet from an URB.  Then
1665                    traverse the URB list for the EP until the URB with urb_priv->last_sb
1666                    matching the intr-marked sb_desc is found.  All URBs before this have
1667                    been sent.
1668                 */
1669
1670                 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1671                         /* Skip past epids with no SB lists, epids used for in traffic,
1672                            and special (dummy, invalid) epids. */
1673                         if ((TxIsocEPList[epid].sub == 0) ||
1674                             (test_bit(epid, (void *)&epid_out_traffic) == 0) ||
1675                             (epid == DUMMY_EPID) ||
1676                             (epid == INVALID_EPID)) {
1677                                 /* Nothing here to see. */
1678                                 continue;
1679                         }
1680                         sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
1681
1682                         /* Find the last descriptor of the currently active URB for this ep.
1683                            This is the first descriptor in the sub list marked for a descriptor
1684                            interrupt. */
1685                         while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
1686                                 sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
1687                         }
1688                         assert(sb_desc);
1689
1690                         dbg_isoc("Check epid %d, sub 0x%p, SB 0x%p",
1691                                  epid,
1692                                  phys_to_virt(TxIsocEPList[epid].sub),
1693                                  sb_desc);
1694
1695                         epid_done = 0;
1696
1697                         /* Get the first urb (if any). */
1698                         urb = urb_list_first(epid);
1699                         assert(urb);
1700
1701                         while (urb && !epid_done) {
1702
1703                                 /* Sanity check. */
1704                                 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
1705
1706                                 if (!usb_pipeout(urb->pipe)) {
1707                                         /* descr interrupts are generated only for out pipes. */
1708                                         epid_done = 1;
1709                                         continue;
1710                                 }
1711
1712                                 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1713                                 assert(urb_priv);
1714
1715                                 if (sb_desc != urb_priv->last_sb) {
1716
1717                                         /* This urb has been sent. */
1718                                         dbg_isoc("out URB 0x%p sent", urb);
1719
1720                                         urb_priv->urb_state = TRANSFER_DONE;
1721
1722                                 } else if ((sb_desc == urb_priv->last_sb) &&
1723                                            !(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1724
1725                                         assert((sb_desc->command & IO_MASK(USB_SB_command, eol)) == IO_STATE(USB_SB_command, eol, yes));
1726                                         assert(sb_desc->next == 0);
1727
1728                                         dbg_isoc("out URB 0x%p last in list, epid disabled", urb);
1729                                         TxIsocEPList[epid].sub = 0;
1730                                         TxIsocEPList[epid].hw_len = 0;
1731                                         urb_priv->urb_state = TRANSFER_DONE;
1732
1733                                         epid_done = 1;
1734
1735                                 } else {
1736                                         epid_done = 1;
1737                                 }
1738                                 if (!epid_done) {
1739                                         urb = urb_list_next(urb, epid);
1740                                 }
1741                         }
1742
1743                 }
1744
1745                 *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
1746
1747                 comp_data = (usb_isoc_complete_data_t*)kmem_cache_alloc(isoc_compl_cache, SLAB_ATOMIC);
1748                 assert(comp_data != NULL);
1749
1750                 INIT_WORK(&comp_data->usb_bh, etrax_usb_isoc_descr_interrupt_bottom_half, comp_data);
1751                 schedule_work(&comp_data->usb_bh);
1752         }
1753
1754         DBFEXIT;
1755         return IRQ_HANDLED;
1756 }
1757
1758 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data)
1759 {
1760         usb_isoc_complete_data_t *comp_data = (usb_isoc_complete_data_t*)data;
1761
1762         struct urb *urb;
1763         int epid;
1764         int epid_done;
1765         etrax_urb_priv_t *urb_priv;
1766
1767         DBFENTER;
1768
1769         dbg_isoc("dma8_sub3_descr (ISOC) bottom half.");
1770
1771         for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1772                 unsigned long flags;
1773
1774                 save_flags(flags);
1775                 cli();
1776
1777                 epid_done = 0;
1778
1779                 /* The descriptor interrupt handler has marked all transmitted isoch. out
1780                    URBs with TRANSFER_DONE.  Now we traverse all epids and for all that
1781                    have isoch. out traffic traverse its URB list and complete the
1782                    transmitted URB.
1783                 */
1784
1785                 while (!epid_done) {
1786
1787                         /* Get the first urb (if any). */
1788                         urb = urb_list_first(epid);
1789                         if (urb == 0) {
1790                                 epid_done = 1;
1791                                 continue;
1792                         }
1793
1794                         if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
1795                                         epid_done = 1;
1796                                         continue;
1797                         }
1798
1799                         if (!usb_pipeout(urb->pipe)) {
1800                                 /* descr interrupts are generated only for out pipes. */
1801                                 epid_done = 1;
1802                                 continue;
1803                         }
1804
1805                         dbg_isoc("Check epid %d, SB 0x%p", epid, (char*)TxIsocEPList[epid].sub);
1806
1807                         urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1808                         assert(urb_priv);
1809
1810                         if (urb_priv->urb_state == TRANSFER_DONE) {
1811                                 int i;
1812                                 struct usb_iso_packet_descriptor *packet;
1813
1814                                 /* This urb has been sent. */
1815                                 dbg_isoc("Completing isoc out URB 0x%p", urb);
1816
1817                                 for (i = 0; i < urb->number_of_packets; i++) {
1818                                         packet = &urb->iso_frame_desc[i];
1819                                         packet->status = 0;
1820                                         packet->actual_length = packet->length;
1821                                 }
1822
1823                                 etrax_usb_complete_isoc_urb(urb, 0);
1824
1825                                 if (urb_list_empty(epid)) {
1826                                         etrax_usb_free_epid(epid);
1827                                         epid_done = 1;
1828                                 }
1829                         } else {
1830                                 epid_done = 1;
1831                         }
1832                 }
1833                 restore_flags(flags);
1834
1835         }
1836         kmem_cache_free(isoc_compl_cache, comp_data);
1837
1838         DBFEXIT;
1839 }
1840
1841
1842
1843 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1844 {
1845         struct urb *urb;
1846         etrax_urb_priv_t *urb_priv;
1847         int epid = 0;
1848         unsigned long flags;
1849
1850         /* Isoc diagnostics. */
1851         static int curr_fm = 0;
1852         static int prev_fm = 0;
1853
1854         DBFENTER;
1855
1856         /* Clear this interrupt. */
1857         *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
1858
1859         /* Note that this while loop assumes that all packets span only
1860            one rx descriptor. */
1861
1862         /* The reason we cli here is that we call the driver's callback functions. */
1863         save_flags(flags);
1864         cli();
1865
1866         while (myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
1867
1868                 epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
1869                 urb = urb_list_first(epid);
1870
1871                 //printk("eop for epid %d, first urb 0x%lx\n", epid, (unsigned long)urb);
1872
1873                 if (!urb) {
1874                         err("No urb for epid %d in rx interrupt", epid);
1875                         __dump_ept_data(epid);
1876                         goto skip_out;
1877                 }
1878
1879                 /* Note that we cannot indescriminately assert(usb_pipein(urb->pipe)) since
1880                    ctrl pipes are not. */
1881
1882                 if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
1883                         __u32 r_usb_ept_data;
1884                         int no_error = 0;
1885
1886                         assert(test_bit(epid, (void *)&epid_usage_bitmask));
1887
1888                         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1889                         nop();
1890                         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1891                                 r_usb_ept_data = *R_USB_EPT_DATA_ISO;
1892
1893                                 if ((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
1894                                     (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
1895                                     (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
1896                                         /* Not an error, just a failure to receive an expected iso
1897                                            in packet in this frame.  This is not documented
1898                                            in the designers reference.
1899                                         */
1900                                         no_error++;
1901                                 } else {
1902                                         warn("R_USB_EPT_DATA_ISO for epid %d = 0x%x", epid, r_usb_ept_data);
1903                                 }
1904                         } else {
1905                                 r_usb_ept_data = *R_USB_EPT_DATA;
1906                                 warn("R_USB_EPT_DATA for epid %d = 0x%x", epid, r_usb_ept_data);
1907                         }
1908
1909                         if (!no_error){
1910                                 warn("error in rx desc->status, epid %d, first urb = 0x%lx",
1911                                      epid, (unsigned long)urb);
1912                                 __dump_in_desc(myNextRxDesc);
1913
1914                                 warn("R_USB_STATUS = 0x%x", *R_USB_STATUS);
1915
1916                                 /* Check that ept was disabled when error occurred. */
1917                                 switch (usb_pipetype(urb->pipe)) {
1918                                 case PIPE_BULK:
1919                                         assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1920                                         break;
1921                                 case PIPE_CONTROL:
1922                                         assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1923                                         break;
1924                                 case PIPE_INTERRUPT:
1925                                         assert(!(TxIntrEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1926                                         break;
1927                                 case PIPE_ISOCHRONOUS:
1928                                         assert(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1929                                         break;
1930                                 default:
1931                                         warn("etrax_usb_rx_interrupt: bad pipetype %d in urb 0x%p",
1932                                              usb_pipetype(urb->pipe),
1933                                              urb);
1934                                 }
1935                                 etrax_usb_complete_urb(urb, -EPROTO);
1936                                 goto skip_out;
1937                         }
1938                 }
1939
1940                 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1941                 assert(urb_priv);
1942
1943                 if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
1944                     (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
1945                     (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1946
1947                         if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
1948                                 /* We get nodata for empty data transactions, and the rx descriptor's
1949                                    hw_len field is not valid in that case. No data to copy in other
1950                                    words. */
1951                         } else {
1952                                 /* Make sure the data fits in the buffer. */
1953                                 assert(urb_priv->rx_offset + myNextRxDesc->hw_len
1954                                        <= urb->transfer_buffer_length);
1955
1956                                 memcpy(urb->transfer_buffer + urb_priv->rx_offset,
1957                                        phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
1958                                 urb_priv->rx_offset += myNextRxDesc->hw_len;
1959                         }
1960
1961                         if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
1962                                 if ((usb_pipetype(urb->pipe) == PIPE_CONTROL) &&
1963                                     ((TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)) ==
1964                                      IO_STATE(USB_EP_command, enable, yes))) {
1965                                         /* The EP is still enabled, so the OUT packet used to ack
1966                                            the in data is probably not processed yet.  If the EP
1967                                            sub pointer has not moved beyond urb_priv->last_sb mark
1968                                            it for a descriptor interrupt and complete the urb in
1969                                            the descriptor interrupt handler.
1970                                         */
1971                                         USB_SB_Desc_t *sub = TxCtrlEPList[urb_priv->epid].sub ? phys_to_virt(TxCtrlEPList[urb_priv->epid].sub) : 0;
1972
1973                                         while ((sub != NULL) && (sub != urb_priv->last_sb)) {
1974                                                 sub = sub->next ? phys_to_virt(sub->next) : 0;
1975                                         }
1976                                         if (sub != NULL) {
1977                                                 /* The urb has not been fully processed. */
1978                                                 urb_priv->urb_state = WAITING_FOR_DESCR_INTR;
1979                                         } else {
1980                                                 warn("(CTRL) epid enabled and urb (0x%p) processed, ep->sub=0x%p", urb, (char*)TxCtrlEPList[urb_priv->epid].sub);
1981                                                 etrax_usb_complete_urb(urb, 0);
1982                                         }
1983                                 } else {
1984                                         etrax_usb_complete_urb(urb, 0);
1985                                 }
1986                         }
1987
1988                 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1989
1990                         struct usb_iso_packet_descriptor *packet;
1991
1992                         if (urb_priv->urb_state == UNLINK) {
1993                                 info("Ignoring rx data for urb being unlinked.");
1994                                 goto skip_out;
1995                         } else if (urb_priv->urb_state == NOT_STARTED) {
1996                                 info("What? Got rx data for urb that isn't started?");
1997                                 goto skip_out;
1998                         }
1999
2000                         packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
2001                         packet->status = 0;
2002
2003                         if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
2004                                 /* We get nodata for empty data transactions, and the rx descriptor's
2005                                    hw_len field is not valid in that case. We copy 0 bytes however to
2006                                    stay in synch. */
2007                                 packet->actual_length = 0;
2008                         } else {
2009                                 packet->actual_length = myNextRxDesc->hw_len;
2010                                 /* Make sure the data fits in the buffer. */
2011                                 assert(packet->actual_length <= packet->length);
2012                                 memcpy(urb->transfer_buffer + packet->offset,
2013                                        phys_to_virt(myNextRxDesc->buf), packet->actual_length);
2014                         }
2015
2016                         /* Increment the packet counter. */
2017                         urb_priv->isoc_packet_counter++;
2018
2019                         /* Note that we don't care about the eot field in the rx descriptor's status.
2020                            It will always be set for isoc traffic. */
2021                         if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
2022
2023                                 /* Out-of-synch diagnostics. */
2024                                 curr_fm = (*R_USB_FM_NUMBER & 0x7ff);
2025                                 if (((prev_fm + urb_priv->isoc_packet_counter) % (0x7ff + 1)) != curr_fm) {
2026                                         /* This test is wrong, if there is more than one isoc
2027                                            in endpoint active it will always calculate wrong
2028                                            since prev_fm is shared by all endpoints.
2029
2030                                            FIXME Make this check per URB using urb->start_frame.
2031                                         */
2032                                         dbg_isoc("Out of synch? Previous frame = %d, current frame = %d",
2033                                                  prev_fm, curr_fm);
2034
2035                                 }
2036                                 prev_fm = curr_fm;
2037
2038                                 /* Complete the urb with status OK. */
2039                                 etrax_usb_complete_isoc_urb(urb, 0);
2040                         }
2041                 }
2042
2043         skip_out:
2044
2045                 /* DMA IN cache bug. Flush the DMA IN buffer from the cache. (struct etrax_dma_descr
2046                    has the same layout as USB_IN_Desc for the relevant fields.) */
2047                 prepare_rx_descriptor((struct etrax_dma_descr*)myNextRxDesc);
2048
2049                 myPrevRxDesc = myNextRxDesc;
2050                 myPrevRxDesc->command |= IO_MASK(USB_IN_command, eol);
2051                 myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
2052                 myLastRxDesc = myPrevRxDesc;
2053
2054                 myNextRxDesc->status = 0;
2055                 myNextRxDesc = phys_to_virt(myNextRxDesc->next);
2056         }
2057
2058         restore_flags(flags);
2059
2060         DBFEXIT;
2061
2062         return IRQ_HANDLED;
2063 }
2064
2065
2066 /* This function will unlink the SB descriptors associated with this urb. */
2067 static int etrax_remove_from_sb_list(struct urb *urb)
2068 {
2069         USB_SB_Desc_t *next_sb, *first_sb, *last_sb;
2070         etrax_urb_priv_t *urb_priv;
2071         int i = 0;
2072
2073         DBFENTER;
2074
2075         urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2076         assert(urb_priv);
2077
2078         /* Just a sanity check. Since we don't fiddle with the DMA list the EP descriptor
2079            doesn't really need to be disabled, it's just that we expect it to be. */
2080         if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2081                 assert(!(TxBulkEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2082         } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
2083                 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2084         }
2085
2086         first_sb = urb_priv->first_sb;
2087         last_sb = urb_priv->last_sb;
2088
2089         assert(first_sb);
2090         assert(last_sb);
2091
2092         while (first_sb != last_sb) {
2093                 next_sb = (USB_SB_Desc_t *)phys_to_virt(first_sb->next);
2094                 kmem_cache_free(usb_desc_cache, first_sb);
2095                 first_sb = next_sb;
2096                 i++;
2097         }
2098         kmem_cache_free(usb_desc_cache, last_sb);
2099         i++;
2100         dbg_sb("%d SB descriptors freed", i);
2101         /* Compare i with urb->number_of_packets for Isoc traffic.
2102            Should be same when calling unlink_urb */
2103
2104         DBFEXIT;
2105
2106         return i;
2107 }
2108
2109 static int etrax_usb_submit_bulk_urb(struct urb *urb)
2110 {
2111         int epid;
2112         int empty;
2113         unsigned long flags;
2114         etrax_urb_priv_t *urb_priv;
2115
2116         DBFENTER;
2117
2118         /* Epid allocation, empty check and list add must be protected.
2119            Read about this in etrax_usb_submit_ctrl_urb. */
2120
2121         spin_lock_irqsave(&urb_list_lock, flags);
2122         epid = etrax_usb_setup_epid(urb);
2123         if (epid == -1) {
2124                 DBFEXIT;
2125                 spin_unlock_irqrestore(&urb_list_lock, flags);
2126                 return -ENOMEM;
2127         }
2128         empty = urb_list_empty(epid);
2129         urb_list_add(urb, epid);
2130         spin_unlock_irqrestore(&urb_list_lock, flags);
2131
2132         dbg_bulk("Adding bulk %s urb 0x%lx to %s list, epid %d",
2133                  usb_pipein(urb->pipe) ? "IN" : "OUT", (unsigned long)urb, empty ? "empty" : "", epid);
2134
2135         /* Mark the urb as being in progress. */
2136         urb->status = -EINPROGRESS;
2137
2138         /* Setup the hcpriv data. */
2139         urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2140         assert(urb_priv != NULL);
2141         /* This sets rx_offset to 0. */
2142         urb_priv->urb_state = NOT_STARTED;
2143         urb->hcpriv = urb_priv;
2144
2145         if (empty) {
2146                 etrax_usb_add_to_bulk_sb_list(urb, epid);
2147         }
2148
2149         DBFEXIT;
2150
2151         return 0;
2152 }
2153
2154 static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid)
2155 {
2156         USB_SB_Desc_t *sb_desc;
2157         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2158         unsigned long flags;
2159         char maxlen;
2160
2161         DBFENTER;
2162
2163         dbg_bulk("etrax_usb_add_to_bulk_sb_list, urb 0x%lx", (unsigned long)urb);
2164
2165         maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2166
2167         sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2168         assert(sb_desc != NULL);
2169         memset(sb_desc, 0, sizeof(USB_SB_Desc_t));
2170
2171
2172         if (usb_pipeout(urb->pipe)) {
2173
2174                 dbg_bulk("Grabbing bulk OUT, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2175
2176                 /* This is probably a sanity check of the bulk transaction length
2177                    not being larger than 64 kB. */
2178                 if (urb->transfer_buffer_length > 0xffff) {
2179                         panic("urb->transfer_buffer_length > 0xffff");
2180                 }
2181
2182                 sb_desc->sw_len = urb->transfer_buffer_length;
2183
2184                 /* The rem field is don't care if it's not a full-length transfer, so setting
2185                    it shouldn't hurt. Also, rem isn't used for OUT traffic. */
2186                 sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
2187                                     IO_STATE(USB_SB_command, tt, out) |
2188                                     IO_STATE(USB_SB_command, eot, yes) |
2189                                     IO_STATE(USB_SB_command, eol, yes));
2190
2191                 /* The full field is set to yes, even if we don't actually check that this is
2192                    a full-length transfer (i.e., that transfer_buffer_length % maxlen = 0).
2193                    Setting full prevents the USB controller from sending an empty packet in
2194                    that case.  However, if URB_ZERO_PACKET was set we want that. */
2195                 if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
2196                         sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
2197                 }
2198
2199                 sb_desc->buf = virt_to_phys(urb->transfer_buffer);
2200                 sb_desc->next = 0;
2201
2202         } else if (usb_pipein(urb->pipe)) {
2203
2204                 dbg_bulk("Grabbing bulk IN, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2205
2206                 sb_desc->sw_len = urb->transfer_buffer_length ?
2207                         (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2208
2209                 /* The rem field is don't care if it's not a full-length transfer, so setting
2210                    it shouldn't hurt. */
2211                 sb_desc->command =
2212                         (IO_FIELD(USB_SB_command, rem,
2213                                   urb->transfer_buffer_length % maxlen) |
2214                          IO_STATE(USB_SB_command, tt, in) |
2215                          IO_STATE(USB_SB_command, eot, yes) |
2216                          IO_STATE(USB_SB_command, eol, yes));
2217
2218                 sb_desc->buf = 0;
2219                 sb_desc->next = 0;
2220         }
2221
2222         urb_priv->first_sb = sb_desc;
2223         urb_priv->last_sb = sb_desc;
2224         urb_priv->epid = epid;
2225
2226         urb->hcpriv = urb_priv;
2227
2228         /* Reset toggle bits and reset error count. */
2229         save_flags(flags);
2230         cli();
2231
2232         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2233         nop();
2234
2235         /* FIXME: Is this a special case since the hold field is checked,
2236            or should we check hold in a lot of other cases as well? */
2237         if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2238                 panic("Hold was set in %s", __FUNCTION__);
2239         }
2240
2241         /* Reset error counters (regardless of which direction this traffic is). */
2242         *R_USB_EPT_DATA &=
2243                 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2244                   IO_MASK(R_USB_EPT_DATA, error_count_out));
2245
2246         /* Software must preset the toggle bits. */
2247         if (usb_pipeout(urb->pipe)) {
2248                 char toggle =
2249                         usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2250                 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
2251                 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
2252         } else {
2253                 char toggle =
2254                         usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2255                 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
2256                 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
2257         }
2258
2259         /* Assert that the EP descriptor is disabled. */
2260         assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2261
2262         /* The reason we set the EP's sub pointer directly instead of
2263            walking the SB list and linking it last in the list is that we only
2264            have one active urb at a time (the rest are queued). */
2265
2266         /* Note that we cannot have interrupts running when we have set the SB descriptor
2267            but the EP is not yet enabled.  If a bulk eot happens for another EP, we will
2268            find this EP disabled and with a SB != 0, which will make us think that it's done. */
2269         TxBulkEPList[epid].sub = virt_to_phys(sb_desc);
2270         TxBulkEPList[epid].hw_len = 0;
2271         /* Note that we don't have to fill in the ep_id field since this
2272            was done when we allocated the EP descriptors in init_tx_bulk_ep. */
2273
2274         /* Check if the dummy list is already with us (if several urbs were queued). */
2275         if (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0])) {
2276
2277                 dbg_bulk("Inviting dummy list to the party for urb 0x%lx, epid %d",
2278                          (unsigned long)urb, epid);
2279
2280                 /* The last EP in the dummy list already has its next pointer set to
2281                    TxBulkEPList[epid].next. */
2282
2283                 /* We don't need to check if the DMA is at this EP or not before changing the
2284                    next pointer, since we will do it in one 32-bit write (EP descriptors are
2285                    32-bit aligned). */
2286                 TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
2287         }
2288         /* Enable the EP descr. */
2289         dbg_bulk("Enabling bulk EP for urb 0x%lx, epid %d", (unsigned long)urb, epid);
2290         TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2291
2292         /* Everything is set up, safe to enable interrupts again. */
2293         restore_flags(flags);
2294
2295         /* If the DMA bulk channel isn't running, we need to restart it if it
2296            has stopped at the last EP descriptor (DMA stopped because there was
2297            no more traffic) or if it has stopped at a dummy EP with the intr flag
2298            set (DMA stopped because we were too slow in inserting new traffic). */
2299         if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
2300
2301                 USB_EP_Desc_t *ep;
2302                 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
2303                 dbg_bulk("DMA channel not running in add");
2304                 dbg_bulk("DMA is at 0x%lx", (unsigned long)ep);
2305
2306                 if (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[NBR_OF_EPIDS - 1]) ||
2307                     (ep->command & 0x8) >> 3) {
2308                         *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
2309                         /* Update/restart the bulk start timer since we just started the channel. */
2310                         mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
2311                         /* Update/restart the bulk eot timer since we just inserted traffic. */
2312                         mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
2313                 }
2314         }
2315
2316         DBFEXIT;
2317 }
2318
2319 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status)
2320 {
2321         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2322         int epid = urb_priv->epid;
2323         unsigned long flags;
2324
2325         DBFENTER;
2326
2327         if (status)
2328                 warn("Completing bulk urb with status %d.", status);
2329
2330         dbg_bulk("Completing bulk urb 0x%lx for epid %d", (unsigned long)urb, epid);
2331
2332         /* Update the urb list. */
2333         urb_list_del(urb, epid);
2334
2335         /* For an IN pipe, we always set the actual length, regardless of whether there was
2336            an error or not (which means the device driver can use the data if it wants to). */
2337         if (usb_pipein(urb->pipe)) {
2338                 urb->actual_length = urb_priv->rx_offset;
2339         } else {
2340                 /* Set actual_length for OUT urbs also; the USB mass storage driver seems
2341                    to want that. We wouldn't know of any partial writes if there was an error. */
2342                 if (status == 0) {
2343                         urb->actual_length = urb->transfer_buffer_length;
2344                 } else {
2345                         urb->actual_length = 0;
2346                 }
2347         }
2348
2349         /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2350            Like, maybe we shouldn't toggle the toggle bits, or maybe we shouldn't insert more traffic. */
2351
2352         save_flags(flags);
2353         cli();
2354
2355         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2356         nop();
2357
2358         /* We need to fiddle with the toggle bits because the hardware doesn't do it for us. */
2359         if (usb_pipeout(urb->pipe)) {
2360                 char toggle =
2361                         IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
2362                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2363                               usb_pipeout(urb->pipe), toggle);
2364         } else {
2365                 char toggle =
2366                         IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
2367                 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2368                               usb_pipeout(urb->pipe), toggle);
2369         }
2370         restore_flags(flags);
2371
2372         /* Remember to free the SBs. */
2373         etrax_remove_from_sb_list(urb);
2374         kfree(urb_priv);
2375         urb->hcpriv = 0;
2376
2377         /* If there are any more urb's in the list we'd better start sending */
2378         if (!urb_list_empty(epid)) {
2379
2380                 struct urb *new_urb;
2381
2382                 /* Get the first urb. */
2383                 new_urb = urb_list_first(epid);
2384                 assert(new_urb);
2385
2386                 dbg_bulk("More bulk for epid %d", epid);
2387
2388                 etrax_usb_add_to_bulk_sb_list(new_urb, epid);
2389         }
2390
2391         urb->status = status;
2392
2393         /* We let any non-zero status from the layer above have precedence. */
2394         if (status == 0) {
2395                 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2396                    is to be treated as an error. */
2397                 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2398                         if (usb_pipein(urb->pipe) &&
2399                             (urb->actual_length !=
2400                              usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2401                                 urb->status = -EREMOTEIO;
2402                         }
2403                 }
2404         }
2405
2406         if (urb->complete) {
2407                 urb->complete(urb, NULL);
2408         }
2409
2410         if (urb_list_empty(epid)) {
2411                 /* This means that this EP is now free, deconfigure it. */
2412                 etrax_usb_free_epid(epid);
2413
2414                 /* No more traffic; time to clean up.
2415                    Must set sub pointer to 0, since we look at the sub pointer when handling
2416                    the bulk eot interrupt. */
2417
2418                 dbg_bulk("No bulk for epid %d", epid);
2419
2420                 TxBulkEPList[epid].sub = 0;
2421
2422                 /* Unlink the dummy list. */
2423
2424                 dbg_bulk("Kicking dummy list out of party for urb 0x%lx, epid %d",
2425                          (unsigned long)urb, epid);
2426
2427                 /* No need to wait for the DMA before changing the next pointer.
2428                    The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
2429                    the last one (INVALID_EPID) for actual traffic. */
2430                 TxBulkEPList[epid].next =
2431                         virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2432         }
2433
2434         DBFEXIT;
2435 }
2436
2437 static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2438 {
2439         int epid;
2440         int empty;
2441         unsigned long flags;
2442         etrax_urb_priv_t *urb_priv;
2443
2444         DBFENTER;
2445
2446         /* FIXME: Return -ENXIO if there is already a queued urb for this endpoint? */
2447
2448         /* Epid allocation, empty check and list add must be protected.
2449
2450            Epid allocation because if we find an existing epid for this endpoint an urb might be
2451            completed (emptying the list) before we add the new urb to the list, causing the epid
2452            to be de-allocated. We would then start the transfer with an invalid epid -> epid attn.
2453
2454            Empty check and add because otherwise we might conclude that the list is not empty,
2455            after which it becomes empty before we add the new urb to the list, causing us not to
2456            insert the new traffic into the SB list. */
2457
2458         spin_lock_irqsave(&urb_list_lock, flags);
2459         epid = etrax_usb_setup_epid(urb);
2460         if (epid == -1) {
2461                 spin_unlock_irqrestore(&urb_list_lock, flags);
2462                 DBFEXIT;
2463                 return -ENOMEM;
2464         }
2465         empty = urb_list_empty(epid);
2466         urb_list_add(urb, epid);
2467         spin_unlock_irqrestore(&urb_list_lock, flags);
2468
2469         dbg_ctrl("Adding ctrl urb 0x%lx to %s list, epid %d",
2470                  (unsigned long)urb, empty ? "empty" : "", epid);
2471
2472         /* Mark the urb as being in progress. */
2473         urb->status = -EINPROGRESS;
2474
2475         /* Setup the hcpriv data. */
2476         urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2477         assert(urb_priv != NULL);
2478         /* This sets rx_offset to 0. */
2479         urb_priv->urb_state = NOT_STARTED;
2480         urb->hcpriv = urb_priv;
2481
2482         if (empty) {
2483                 etrax_usb_add_to_ctrl_sb_list(urb, epid);
2484         }
2485
2486         DBFEXIT;
2487
2488         return 0;
2489 }
2490
2491 static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid)
2492 {
2493         USB_SB_Desc_t *sb_desc_setup;
2494         USB_SB_Desc_t *sb_desc_data;
2495         USB_SB_Desc_t *sb_desc_status;
2496
2497         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2498
2499         unsigned long flags;
2500         char maxlen;
2501
2502         DBFENTER;
2503
2504         maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2505
2506         sb_desc_setup = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2507         assert(sb_desc_setup != NULL);
2508         sb_desc_status = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2509         assert(sb_desc_status != NULL);
2510
2511         /* Initialize the mandatory setup SB descriptor (used only in control transfers) */
2512         sb_desc_setup->sw_len = 8;
2513         sb_desc_setup->command = (IO_FIELD(USB_SB_command, rem, 0) |
2514                                   IO_STATE(USB_SB_command, tt, setup) |
2515                                   IO_STATE(USB_SB_command, full, yes) |
2516                                   IO_STATE(USB_SB_command, eot, yes));
2517
2518         sb_desc_setup->buf = virt_to_phys(urb->setup_packet);
2519
2520         if (usb_pipeout(urb->pipe)) {
2521                 dbg_ctrl("Transfer for epid %d is OUT", epid);
2522
2523                 /* If this Control OUT transfer has an optional data stage we add an OUT token
2524                    before the mandatory IN (status) token, hence the reordered SB list */
2525
2526                 sb_desc_setup->next = virt_to_phys(sb_desc_status);
2527                 if (urb->transfer_buffer) {
2528
2529                         dbg_ctrl("This OUT transfer has an extra data stage");
2530
2531                         sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2532                         assert(sb_desc_data != NULL);
2533
2534                         sb_desc_setup->next = virt_to_phys(sb_desc_data);
2535
2536                         sb_desc_data->sw_len = urb->transfer_buffer_length;
2537                         sb_desc_data->command = (IO_STATE(USB_SB_command, tt, out) |
2538                                                  IO_STATE(USB_SB_command, full, yes) |
2539                                                  IO_STATE(USB_SB_command, eot, yes));
2540                         sb_desc_data->buf = virt_to_phys(urb->transfer_buffer);
2541                         sb_desc_data->next = virt_to_phys(sb_desc_status);
2542                 }
2543
2544                 sb_desc_status->sw_len = 1;
2545                 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2546                                            IO_STATE(USB_SB_command, tt, in) |
2547                                            IO_STATE(USB_SB_command, eot, yes) |
2548                                            IO_STATE(USB_SB_command, intr, yes) |
2549                                            IO_STATE(USB_SB_command, eol, yes));
2550
2551                 sb_desc_status->buf = 0;
2552                 sb_desc_status->next = 0;
2553
2554         } else if (usb_pipein(urb->pipe)) {
2555
2556                 dbg_ctrl("Transfer for epid %d is IN", epid);
2557                 dbg_ctrl("transfer_buffer_length = %d", urb->transfer_buffer_length);
2558                 dbg_ctrl("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
2559
2560                 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2561                 assert(sb_desc_data != NULL);
2562
2563                 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2564
2565                 sb_desc_data->sw_len = urb->transfer_buffer_length ?
2566                         (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2567                 dbg_ctrl("sw_len got %d", sb_desc_data->sw_len);
2568
2569                 sb_desc_data->command =
2570                         (IO_FIELD(USB_SB_command, rem,
2571                                   urb->transfer_buffer_length % maxlen) |
2572                          IO_STATE(USB_SB_command, tt, in) |
2573                          IO_STATE(USB_SB_command, eot, yes));
2574
2575                 sb_desc_data->buf = 0;
2576                 sb_desc_data->next = virt_to_phys(sb_desc_status);
2577
2578                 /* Read comment at zout_buffer declaration for an explanation to this. */
2579                 sb_desc_status->sw_len = 1;
2580                 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2581                                            IO_STATE(USB_SB_command, tt, zout) |
2582                                            IO_STATE(USB_SB_command, full, yes) |
2583                                            IO_STATE(USB_SB_command, eot, yes) |
2584                                            IO_STATE(USB_SB_command, intr, yes) |
2585                                            IO_STATE(USB_SB_command, eol, yes));
2586
2587                 sb_desc_status->buf = virt_to_phys(&zout_buffer[0]);
2588                 sb_desc_status->next = 0;
2589         }
2590
2591         urb_priv->first_sb = sb_desc_setup;
2592         urb_priv->last_sb = sb_desc_status;
2593         urb_priv->epid = epid;
2594
2595         urb_priv->urb_state = STARTED;
2596
2597         /* Reset toggle bits and reset error count, remember to di and ei */
2598         /* Warning: it is possible that this locking doesn't work with bottom-halves */
2599
2600         save_flags(flags);
2601         cli();
2602
2603         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2604         nop();
2605         if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2606                 panic("Hold was set in %s", __FUNCTION__);
2607         }
2608
2609
2610         /* FIXME: Compare with etrax_usb_add_to_bulk_sb_list where the toggle bits
2611            are set to a specific value. Why the difference? Read "Transfer and Toggle Bits
2612            in Designer's Reference, p. 8 - 11. */
2613         *R_USB_EPT_DATA &=
2614                 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2615                   IO_MASK(R_USB_EPT_DATA, error_count_out) |
2616                   IO_MASK(R_USB_EPT_DATA, t_in) |
2617                   IO_MASK(R_USB_EPT_DATA, t_out));
2618
2619         /* Since we use the rx interrupt to complete ctrl urbs, we can enable interrupts now
2620            (i.e. we don't check the sub pointer on an eot interrupt like we do for bulk traffic). */
2621         restore_flags(flags);
2622
2623         /* Assert that the EP descriptor is disabled. */
2624         assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2625
2626         /* Set up and enable the EP descriptor. */
2627         TxCtrlEPList[epid].sub = virt_to_phys(sb_desc_setup);
2628         TxCtrlEPList[epid].hw_len = 0;
2629         TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2630
2631         /* We start the DMA sub channel without checking if it's running or not, because:
2632            1) If it's already running, issuing the start command is a nop.
2633            2) We avoid a test-and-set race condition. */
2634         *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
2635
2636         DBFEXIT;
2637 }
2638
2639 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status)
2640 {
2641         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2642         int epid = urb_priv->epid;
2643
2644         DBFENTER;
2645
2646         if (status)
2647                 warn("Completing ctrl urb with status %d.", status);
2648
2649         dbg_ctrl("Completing ctrl epid %d, urb 0x%lx", epid, (unsigned long)urb);
2650
2651         /* Remove this urb from the list. */
2652         urb_list_del(urb, epid);
2653
2654         /* For an IN pipe, we always set the actual length, regardless of whether there was
2655            an error or not (which means the device driver can use the data if it wants to). */
2656         if (usb_pipein(urb->pipe)) {
2657                 urb->actual_length = urb_priv->rx_offset;
2658         }
2659
2660         /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2661            Like, maybe we shouldn't insert more traffic. */
2662
2663         /* Remember to free the SBs. */
2664         etrax_remove_from_sb_list(urb);
2665         kfree(urb_priv);
2666         urb->hcpriv = 0;
2667
2668         /* If there are any more urbs in the list we'd better start sending. */
2669         if (!urb_list_empty(epid)) {
2670                 struct urb *new_urb;
2671
2672                 /* Get the first urb. */
2673                 new_urb = urb_list_first(epid);
2674                 assert(new_urb);
2675
2676                 dbg_ctrl("More ctrl for epid %d, first urb = 0x%lx", epid, (unsigned long)new_urb);
2677
2678                 etrax_usb_add_to_ctrl_sb_list(new_urb, epid);
2679         }
2680
2681         urb->status = status;
2682
2683         /* We let any non-zero status from the layer above have precedence. */
2684         if (status == 0) {
2685                 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2686                    is to be treated as an error. */
2687                 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2688                         if (usb_pipein(urb->pipe) &&
2689                             (urb->actual_length !=
2690                              usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2691                                 urb->status = -EREMOTEIO;
2692                         }
2693                 }
2694         }
2695
2696         if (urb->complete) {
2697                 urb->complete(urb, NULL);
2698         }
2699
2700         if (urb_list_empty(epid)) {
2701                 /* No more traffic. Time to clean up. */
2702                 etrax_usb_free_epid(epid);
2703                 /* Must set sub pointer to 0. */
2704                 dbg_ctrl("No ctrl for epid %d", epid);
2705                 TxCtrlEPList[epid].sub = 0;
2706         }
2707
2708         DBFEXIT;
2709 }
2710
2711 static int etrax_usb_submit_intr_urb(struct urb *urb)
2712 {
2713
2714         int epid;
2715
2716         DBFENTER;
2717
2718         if (usb_pipeout(urb->pipe)) {
2719                 /* Unsupported transfer type.
2720                    We don't support interrupt out traffic. (If we do, we can't support
2721                    intervals for neither in or out traffic, but are forced to schedule all
2722                    interrupt traffic in one frame.) */
2723                 return -EINVAL;
2724         }
2725
2726         epid = etrax_usb_setup_epid(urb);
2727         if (epid == -1) {
2728                 DBFEXIT;
2729                 return -ENOMEM;
2730         }
2731
2732         if (!urb_list_empty(epid)) {
2733                 /* There is already a queued urb for this endpoint. */
2734                 etrax_usb_free_epid(epid);
2735                 return -ENXIO;
2736         }
2737
2738         urb->status = -EINPROGRESS;
2739
2740         dbg_intr("Add intr urb 0x%lx, to list, epid %d", (unsigned long)urb, epid);
2741
2742         urb_list_add(urb, epid);
2743         etrax_usb_add_to_intr_sb_list(urb, epid);
2744
2745         return 0;
2746
2747         DBFEXIT;
2748 }
2749
2750 static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2751 {
2752
2753         volatile USB_EP_Desc_t *tmp_ep;
2754         volatile USB_EP_Desc_t *first_ep;
2755
2756         char maxlen;
2757         int interval;
2758         int i;
2759
2760         etrax_urb_priv_t *urb_priv;
2761
2762         DBFENTER;
2763
2764         maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2765         interval = urb->interval;
2766
2767         urb_priv = kzalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2768         assert(urb_priv != NULL);
2769         urb->hcpriv = urb_priv;
2770
2771         first_ep = &TxIntrEPList[0];
2772
2773         /* Round of the interval to 2^n, it is obvious that this code favours
2774            smaller numbers, but that is actually a good thing */
2775         /* FIXME: The "rounding error" for larger intervals will be quite
2776            large. For in traffic this shouldn't be a problem since it will only
2777            mean that we "poll" more often. */
2778         for (i = 0; interval; i++) {
2779                 interval = interval >> 1;
2780         }
2781         interval = 1 << (i - 1);
2782
2783         dbg_intr("Interval rounded to %d", interval);
2784
2785         tmp_ep = first_ep;
2786         i = 0;
2787         do {
2788                 if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
2789                         if ((i % interval) == 0) {
2790                                 /* Insert the traffic ep after tmp_ep */
2791                                 USB_EP_Desc_t *ep_desc;
2792                                 USB_SB_Desc_t *sb_desc;
2793
2794                                 dbg_intr("Inserting EP for epid %d", epid);
2795
2796                                 ep_desc = (USB_EP_Desc_t *)
2797                                         kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2798                                 sb_desc = (USB_SB_Desc_t *)
2799                                         kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2800                                 assert(ep_desc != NULL);
2801                                 CHECK_ALIGN(ep_desc);
2802                                 assert(sb_desc != NULL);
2803
2804                                 ep_desc->sub = virt_to_phys(sb_desc);
2805                                 ep_desc->hw_len = 0;
2806                                 ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
2807                                                     IO_STATE(USB_EP_command, enable, yes));
2808
2809
2810                                 /* Round upwards the number of packets of size maxlen
2811                                    that this SB descriptor should receive. */
2812                                 sb_desc->sw_len = urb->transfer_buffer_length ?
2813                                         (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2814                                 sb_desc->next = 0;
2815                                 sb_desc->buf = 0;
2816                                 sb_desc->command =
2817                                         (IO_FIELD(USB_SB_command, rem, urb->transfer_buffer_length % maxlen) |
2818                                          IO_STATE(USB_SB_command, tt, in) |
2819                                          IO_STATE(USB_SB_command, eot, yes) |
2820                                          IO_STATE(USB_SB_command, eol, yes));
2821
2822                                 ep_desc->next = tmp_ep->next;
2823                                 tmp_ep->next = virt_to_phys(ep_desc);
2824                         }
2825                         i++;
2826                 }
2827                 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
2828         } while (tmp_ep != first_ep);
2829
2830
2831         /* Note that first_sb/last_sb doesn't apply to interrupt traffic. */
2832         urb_priv->epid = epid;
2833
2834         /* We start the DMA sub channel without checking if it's running or not, because:
2835            1) If it's already running, issuing the start command is a nop.
2836            2) We avoid a test-and-set race condition. */
2837         *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
2838
2839         DBFEXIT;
2840 }
2841
2842
2843
2844 static void etrax_usb_complete_intr_urb(struct urb *urb, int status)
2845 {
2846         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2847         int epid = urb_priv->epid;
2848
2849         DBFENTER;
2850
2851         if (status)
2852                 warn("Completing intr urb with status %d.", status);
2853
2854         dbg_intr("Completing intr epid %d, urb 0x%lx", epid, (unsigned long)urb);
2855
2856         urb->status = status;
2857         urb->actual_length = urb_priv->rx_offset;
2858
2859         dbg_intr("interrupt urb->actual_length = %d", urb->actual_length);
2860
2861         /* We let any non-zero status from the layer above have precedence. */
2862         if (status == 0) {
2863                 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2864                    is to be treated as an error. */
2865                 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2866                         if (urb->actual_length !=
2867                             usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
2868                                 urb->status = -EREMOTEIO;
2869                         }
2870                 }
2871         }
2872
2873         /* The driver will resubmit the URB so we need to remove it first */
2874         etrax_usb_unlink_urb(urb, 0);
2875         if (urb->complete) {
2876                 urb->complete(urb, NULL);
2877         }
2878
2879         DBFEXIT;
2880 }
2881
2882
2883 static int etrax_usb_submit_isoc_urb(struct urb *urb)
2884 {
2885         int epid;
2886         unsigned long flags;
2887
2888         DBFENTER;
2889
2890         dbg_isoc("Submitting isoc urb = 0x%lx", (unsigned long)urb);
2891
2892         /* Epid allocation, empty check and list add must be protected.
2893            Read about this in etrax_usb_submit_ctrl_urb. */
2894
2895         spin_lock_irqsave(&urb_list_lock, flags);
2896         /* Is there an active epid for this urb ? */
2897         epid = etrax_usb_setup_epid(urb);
2898         if (epid == -1) {
2899                 DBFEXIT;
2900                 spin_unlock_irqrestore(&urb_list_lock, flags);
2901                 return -ENOMEM;
2902         }
2903
2904         /* Ok, now we got valid endpoint, lets insert some traffic */
2905
2906         urb->status = -EINPROGRESS;
2907
2908         /* Find the last urb in the URB_List and add this urb after that one.
2909            Also add the traffic, that is do an etrax_usb_add_to_isoc_sb_list.  This
2910            is important to make this in "real time" since isochronous traffic is
2911            time sensitive. */
2912
2913         dbg_isoc("Adding isoc urb to (possibly empty) list");
2914         urb_list_add(urb, epid);
2915         etrax_usb_add_to_isoc_sb_list(urb, epid);
2916         spin_unlock_irqrestore(&urb_list_lock, flags);
2917
2918         DBFEXIT;
2919
2920         return 0;
2921 }
2922
2923 static void etrax_usb_check_error_isoc_ep(const int epid)
2924 {
2925         unsigned long int flags;
2926         int error_code;
2927         __u32 r_usb_ept_data;
2928
2929         /* We can't read R_USB_EPID_ATTN here since it would clear the iso_eof,
2930            bulk_eot and epid_attn interrupts.  So we just check the status of
2931            the epid without testing if for it in R_USB_EPID_ATTN. */
2932
2933
2934         save_flags(flags);
2935         cli();
2936         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2937         nop();
2938         /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
2939            registers, they are located at the same address and are of the same size.
2940            In other words, this read should be ok for isoc also. */
2941         r_usb_ept_data = *R_USB_EPT_DATA;
2942         restore_flags(flags);
2943
2944         error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
2945
2946         if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
2947                 warn("Hold was set for epid %d.", epid);
2948                 return;
2949         }
2950
2951         if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, no_error)) {
2952
2953                 /* This indicates that the SB list of the ept was completed before
2954                    new data was appended to it.  This is not an error, but indicates
2955                    large system or USB load and could possibly cause trouble for
2956                    very timing sensitive USB device drivers so we log it.
2957                 */
2958                 info("Isoc. epid %d disabled with no error", epid);
2959                 return;
2960
2961         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, stall)) {
2962                 /* Not really a protocol error, just says that the endpoint gave
2963                    a stall response. Note that error_code cannot be stall for isoc. */
2964                 panic("Isoc traffic cannot stall");
2965
2966         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, bus_error)) {
2967                 /* Two devices responded to a transaction request. Must be resolved
2968                    by software. FIXME: Reset ports? */
2969                 panic("Bus error for epid %d."
2970                       " Two devices responded to transaction request",
2971                       epid);
2972
2973         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
2974                 /* DMA overrun or underrun. */
2975                 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
2976
2977                 /* It seems that error_code = buffer_error in
2978                    R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
2979                    are the same error. */
2980         }
2981 }
2982
2983
2984 static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
2985 {
2986
2987         int i = 0;
2988
2989         etrax_urb_priv_t *urb_priv;
2990         USB_SB_Desc_t *prev_sb_desc,  *next_sb_desc, *temp_sb_desc;
2991
2992         DBFENTER;
2993
2994         prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
2995
2996         urb_priv = kzalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
2997         assert(urb_priv != NULL);
2998
2999         urb->hcpriv = urb_priv;
3000         urb_priv->epid = epid;
3001
3002         if (usb_pipeout(urb->pipe)) {
3003
3004                 if (urb->number_of_packets == 0) panic("etrax_usb_add_to_isoc_sb_list 0 packets\n");
3005
3006                 dbg_isoc("Transfer for epid %d is OUT", epid);
3007                 dbg_isoc("%d packets in URB", urb->number_of_packets);
3008
3009                 /* Create one SB descriptor for each packet and link them together. */
3010                 for (i = 0; i < urb->number_of_packets; i++) {
3011                         if (!urb->iso_frame_desc[i].length)
3012                                 continue;
3013
3014                         next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3015                         assert(next_sb_desc != NULL);
3016
3017                         if (urb->iso_frame_desc[i].length > 0) {
3018
3019                                 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, out) |
3020                                                          IO_STATE(USB_SB_command, eot, yes));
3021
3022                                 next_sb_desc->sw_len = urb->iso_frame_desc[i].length;
3023                                 next_sb_desc->buf = virt_to_phys((char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset);
3024
3025                                 /* Check if full length transfer. */
3026                                 if (urb->iso_frame_desc[i].length ==
3027                                     usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
3028                                         next_sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
3029                                 }
3030                         } else {
3031                                 dbg_isoc("zero len packet");
3032                                 next_sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
3033                                                          IO_STATE(USB_SB_command, tt, zout) |
3034                                                          IO_STATE(USB_SB_command, eot, yes) |
3035                                                          IO_STATE(USB_SB_command, full, yes));
3036
3037                                 next_sb_desc->sw_len = 1;
3038                                 next_sb_desc->buf = virt_to_phys(&zout_buffer[0]);
3039                         }
3040
3041                         /* First SB descriptor that belongs to this urb */
3042                         if (i == 0)
3043                                 urb_priv->first_sb = next_sb_desc;
3044                         else
3045                                 prev_sb_desc->next = virt_to_phys(next_sb_desc);
3046
3047                         prev_sb_desc = next_sb_desc;
3048                 }
3049
3050                 next_sb_desc->command |= (IO_STATE(USB_SB_command, intr, yes) |
3051                                           IO_STATE(USB_SB_command, eol, yes));
3052                 next_sb_desc->next = 0;
3053                 urb_priv->last_sb = next_sb_desc;
3054
3055         } else if (usb_pipein(urb->pipe)) {
3056
3057                 dbg_isoc("Transfer for epid %d is IN", epid);
3058                 dbg_isoc("transfer_buffer_length = %d", urb->transfer_buffer_length);
3059                 dbg_isoc("rem is calculated to %d", urb->iso_frame_desc[urb->number_of_packets - 1].length);
3060
3061                 /* Note that in descriptors for periodic traffic are not consumed. This means that
3062                    the USB controller never propagates in the SB list. In other words, if there already
3063                    is an SB descriptor in the list for this EP we don't have to do anything. */
3064                 if (TxIsocEPList[epid].sub == 0) {
3065                         dbg_isoc("Isoc traffic not already running, allocating SB");
3066
3067                         next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3068                         assert(next_sb_desc != NULL);
3069
3070                         next_sb_desc->command = (IO_STATE(USB_SB_command, tt, in) |
3071                                                  IO_STATE(USB_SB_command, eot, yes) |
3072                                                  IO_STATE(USB_SB_command, eol, yes));
3073
3074                         next_sb_desc->next = 0;
3075                         next_sb_desc->sw_len = 1; /* Actual number of packets is not relevant
3076                                                      for periodic in traffic as long as it is more
3077                                                      than zero.  Set to 1 always. */
3078                         next_sb_desc->buf = 0;
3079
3080                         /* The rem field is don't care for isoc traffic, so we don't set it. */
3081
3082                         /* Only one SB descriptor that belongs to this urb. */
3083                         urb_priv->first_sb = next_sb_desc;
3084                         urb_priv->last_sb = next_sb_desc;
3085
3086                 } else {
3087
3088                         dbg_isoc("Isoc traffic already running, just setting first/last_sb");
3089
3090                         /* Each EP for isoc in will have only one SB descriptor, setup when submitting the
3091                            already active urb. Note that even though we may have several first_sb/last_sb
3092                            pointing at the same SB descriptor, they are freed only once (when the list has
3093                            become empty). */
3094                         urb_priv->first_sb = phys_to_virt(TxIsocEPList[epid].sub);
3095                         urb_priv->last_sb = phys_to_virt(TxIsocEPList[epid].sub);
3096                         return;
3097                 }
3098
3099         }
3100
3101         /* Find the spot to insert this urb and add it. */
3102         if (TxIsocEPList[epid].sub == 0) {
3103                 /* First SB descriptor inserted in this list (in or out). */
3104                 dbg_isoc("Inserting SB desc first in list");
3105                 TxIsocEPList[epid].hw_len = 0;
3106                 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3107
3108         } else {
3109                 /* Isochronous traffic is already running, insert new traffic last (only out). */
3110                 dbg_isoc("Inserting SB desc last in list");
3111                 temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
3112                 while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
3113                        IO_STATE(USB_SB_command, eol, yes)) {
3114                         assert(temp_sb_desc->next);
3115                         temp_sb_desc = phys_to_virt(temp_sb_desc->next);
3116                 }
3117                 dbg_isoc("Appending list on desc 0x%p", temp_sb_desc);
3118
3119                 /* Next pointer must be set before eol is removed. */
3120                 temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
3121                 /* Clear the previous end of list flag since there is a new in the
3122                    added SB descriptor list. */
3123                 temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
3124
3125                 if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
3126                         /* 8.8.5 in Designer's Reference says we should check for and correct
3127                            any errors in the EP here.  That should not be necessary if epid_attn
3128                            is handled correctly, so we assume all is ok. */
3129                         dbg_isoc("EP disabled");
3130                         etrax_usb_check_error_isoc_ep(epid);
3131
3132                         /* The SB list was exhausted. */
3133                         if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
3134                                 /* The new sublist did not get processed before the EP was
3135                                    disabled.  Setup the EP again. */
3136                                 dbg_isoc("Set EP sub to new list");
3137                                 TxIsocEPList[epid].hw_len = 0;
3138                                 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3139                         }
3140                 }
3141         }
3142
3143         if (urb->transfer_flags & URB_ISO_ASAP) {
3144                 /* The isoc transfer should be started as soon as possible. The start_frame
3145                    field is a return value if URB_ISO_ASAP was set. Comparing R_USB_FM_NUMBER
3146                    with a USB Chief trace shows that the first isoc IN token is sent 2 frames
3147                    later. I'm not sure how this affects usage of the start_frame field by the
3148                    device driver, or how it affects things when USB_ISO_ASAP is not set, so
3149                    therefore there's no compensation for the 2 frame "lag" here. */
3150                 urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3151                 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3152                 urb_priv->urb_state = STARTED;
3153                 dbg_isoc("URB_ISO_ASAP set, urb->start_frame set to %d", urb->start_frame);
3154         } else {
3155                 /* Not started yet. */
3156                 urb_priv->urb_state = NOT_STARTED;
3157                 dbg_isoc("urb_priv->urb_state set to NOT_STARTED");
3158         }
3159
3160        /* We start the DMA sub channel without checking if it's running or not, because:
3161           1) If it's already running, issuing the start command is a nop.
3162           2) We avoid a test-and-set race condition. */
3163         *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3164
3165         DBFEXIT;
3166 }
3167
3168 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status)
3169 {
3170         etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3171         int epid = urb_priv->epid;
3172         int auto_resubmit = 0;
3173
3174         DBFENTER;
3175         dbg_isoc("complete urb 0x%p, status %d", urb, status);
3176
3177         if (status)
3178                 warn("Completing isoc urb with status %d.", status);
3179
3180         if (usb_pipein(urb->pipe)) {
3181                 int i;
3182
3183                 /* Make that all isoc packets have status and length set before
3184                    completing the urb. */
3185                 for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++) {
3186                         urb->iso_frame_desc[i].actual_length = 0;
3187                         urb->iso_frame_desc[i].status = -EPROTO;
3188                 }
3189
3190                 urb_list_del(urb, epid);
3191
3192                 if (!list_empty(&urb_list[epid])) {
3193                         ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3194                 } else {
3195                         unsigned long int flags;
3196                         if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3197                                 /* The EP was enabled, disable it and wait. */
3198                                 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
3199
3200                                 /* Ah, the luxury of busy-wait. */
3201                                 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
3202                         }
3203
3204                         etrax_remove_from_sb_list(urb);
3205                         TxIsocEPList[epid].sub = 0;
3206                         TxIsocEPList[epid].hw_len = 0;
3207
3208                         save_flags(flags);
3209                         cli();
3210                         etrax_usb_free_epid(epid);
3211                         restore_flags(flags);
3212                 }
3213
3214                 urb->hcpriv = 0;
3215                 kfree(urb_priv);
3216
3217                 /* Release allocated bandwidth. */
3218                 usb_release_bandwidth(urb->dev, urb, 0);
3219         } else if (usb_pipeout(urb->pipe)) {
3220                 int freed_descr;
3221
3222                 dbg_isoc("Isoc out urb complete 0x%p", urb);
3223
3224                 /* Update the urb list. */
3225                 urb_list_del(urb, epid);
3226
3227                 freed_descr = etrax_remove_from_sb_list(urb);
3228                 dbg_isoc("freed %d descriptors of %d packets", freed_descr, urb->number_of_packets);
3229                 assert(freed_descr == urb->number_of_packets);
3230                 urb->hcpriv = 0;
3231                 kfree(urb_priv);
3232
3233                 /* Release allocated bandwidth. */
3234                 usb_release_bandwidth(urb->dev, urb, 0);
3235         }
3236
3237         urb->status = status;
3238         if (urb->complete) {
3239                 urb->complete(urb, NULL);
3240         }
3241
3242         if (auto_resubmit) {
3243                 /* Check that urb was not unlinked by the complete callback. */
3244                 if (__urb_list_entry(urb, epid)) {
3245                         /* Move this one down the list. */
3246                         urb_list_move_last(urb, epid);
3247
3248                         /* Mark the now first urb as started (may already be). */
3249                         ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3250
3251                         /* Must set this to 0 since this urb is still active after
3252                            completion. */
3253                         urb_priv->isoc_packet_counter = 0;
3254                 } else {
3255                         warn("(ISOC) automatic resubmit urb 0x%p removed by complete.", urb);
3256                 }
3257         }
3258
3259         DBFEXIT;
3260 }
3261
3262 static void etrax_usb_complete_urb(struct urb *urb, int status)
3263 {
3264         switch (usb_pipetype(urb->pipe)) {
3265         case PIPE_BULK:
3266                 etrax_usb_complete_bulk_urb(urb, status);
3267                 break;
3268         case PIPE_CONTROL:
3269                 etrax_usb_complete_ctrl_urb(urb, status);
3270                 break;
3271         case PIPE_INTERRUPT:
3272                 etrax_usb_complete_intr_urb(urb, status);
3273                 break;
3274         case PIPE_ISOCHRONOUS:
3275                 etrax_usb_complete_isoc_urb(urb, status);
3276                 break;
3277         default:
3278                 err("Unknown pipetype");
3279         }
3280 }
3281
3282
3283
3284 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs)
3285 {
3286         usb_interrupt_registers_t *reg;
3287         unsigned long flags;
3288         __u32 irq_mask;
3289         __u8 status;
3290         __u32 epid_attn;
3291         __u16 port_status_1;
3292         __u16 port_status_2;
3293         __u32 fm_number;
3294
3295         DBFENTER;
3296
3297         /* Read critical registers into local variables, do kmalloc afterwards. */
3298         save_flags(flags);
3299         cli();
3300
3301         irq_mask = *R_USB_IRQ_MASK_READ;
3302         /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that R_USB_STATUS
3303            must be read before R_USB_EPID_ATTN since reading the latter clears the
3304            ourun and perror fields of R_USB_STATUS. */
3305         status = *R_USB_STATUS;
3306
3307         /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn interrupts. */
3308         epid_attn = *R_USB_EPID_ATTN;
3309
3310         /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
3311            port_status interrupt. */
3312         port_status_1 = *R_USB_RH_PORT_STATUS_1;
3313         port_status_2 = *R_USB_RH_PORT_STATUS_2;
3314
3315         /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
3316         /* Note: the lower 11 bits contain the actual frame number, sent with each sof. */
3317         fm_number = *R_USB_FM_NUMBER;
3318
3319         restore_flags(flags);
3320
3321         reg = (usb_interrupt_registers_t *)kmem_cache_alloc(top_half_reg_cache, SLAB_ATOMIC);
3322
3323         assert(reg != NULL);
3324
3325         reg->hc = (etrax_hc_t *)vhc;
3326
3327         /* Now put register values into kmalloc'd area. */
3328         reg->r_usb_irq_mask_read = irq_mask;
3329         reg->r_usb_status = status;
3330         reg->r_usb_epid_attn = epid_attn;
3331         reg->r_usb_rh_port_status_1 = port_status_1;
3332         reg->r_usb_rh_port_status_2 = port_status_2;
3333         reg->r_usb_fm_number = fm_number;
3334
3335         INIT_WORK(&reg->usb_bh, etrax_usb_hc_interrupt_bottom_half, reg);
3336         schedule_work(&reg->usb_bh);
3337
3338         DBFEXIT;
3339
3340         return IRQ_HANDLED;
3341 }
3342
3343 static void etrax_usb_hc_interrupt_bottom_half(void *data)
3344 {
3345         usb_interrupt_registers_t *reg = (usb_interrupt_registers_t *)data;
3346         __u32 irq_mask = reg->r_usb_irq_mask_read;
3347
3348         DBFENTER;
3349
3350         /* Interrupts are handled in order of priority. */
3351         if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
3352                 etrax_usb_hc_epid_attn_interrupt(reg);
3353         }
3354         if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
3355                 etrax_usb_hc_port_status_interrupt(reg);
3356         }
3357         if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
3358                 etrax_usb_hc_ctl_status_interrupt(reg);
3359         }
3360         if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
3361                 etrax_usb_hc_isoc_eof_interrupt();
3362         }
3363         if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
3364                 /* Update/restart the bulk start timer since obviously the channel is running. */
3365                 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
3366                 /* Update/restart the bulk eot timer since we just received an bulk eot interrupt. */
3367                 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3368
3369                 etrax_usb_hc_bulk_eot_interrupt(0);
3370         }
3371
3372         kmem_cache_free(top_half_reg_cache, reg);
3373
3374         DBFEXIT;
3375 }
3376
3377
3378 void etrax_usb_hc_isoc_eof_interrupt(void)
3379 {
3380         struct urb *urb;
3381         etrax_urb_priv_t *urb_priv;
3382         int epid;
3383         unsigned long flags;
3384
3385         DBFENTER;
3386
3387         /* Do not check the invalid epid (it has a valid sub pointer). */
3388         for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
3389
3390                 /* Do not check the invalid epid (it has a valid sub pointer). */
3391                 if ((epid == DUMMY_EPID) || (epid == INVALID_EPID))
3392                         continue;
3393
3394                 /* Disable interrupts to block the isoc out descriptor interrupt handler
3395                    from being called while the isoc EPID list is being checked.
3396                 */
3397                 save_flags(flags);
3398                 cli();
3399
3400                 if (TxIsocEPList[epid].sub == 0) {
3401                         /* Nothing here to see. */
3402                         restore_flags(flags);
3403                         continue;
3404                 }
3405
3406                 /* Get the first urb (if any). */
3407                 urb = urb_list_first(epid);
3408                 if (urb == 0) {
3409                         warn("Ignoring NULL urb");
3410                         restore_flags(flags);
3411                         continue;
3412                 }
3413                 if (usb_pipein(urb->pipe)) {
3414
3415                         /* Sanity check. */
3416                         assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
3417
3418                         urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3419                         assert(urb_priv);
3420
3421                         if (urb_priv->urb_state == NOT_STARTED) {
3422
3423                                 /* If ASAP is not set and urb->start_frame is the current frame,
3424                                    start the transfer. */
3425                                 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3426                                     (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
3427
3428                                         dbg_isoc("Enabling isoc IN EP descr for epid %d", epid);
3429                                         TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3430
3431                                         /* This urb is now active. */
3432                                         urb_priv->urb_state = STARTED;
3433                                         continue;
3434                                 }
3435                         }
3436                 }
3437                 restore_flags(flags);
3438         }
3439
3440         DBFEXIT;
3441
3442 }
3443
3444 void etrax_usb_hc_bulk_eot_interrupt(int timer_induced)
3445 {
3446         int epid;
3447
3448         /* The technique is to run one urb at a time, wait for the eot interrupt at which
3449            point the EP descriptor has been disabled. */
3450
3451         DBFENTER;
3452         dbg_bulk("bulk eot%s", timer_induced ? ", called by timer" : "");
3453
3454         for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3455
3456                 if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
3457                     (TxBulkEPList[epid].sub != 0)) {
3458
3459                         struct urb *urb;
3460                         etrax_urb_priv_t *urb_priv;
3461                         unsigned long flags;
3462                         __u32 r_usb_ept_data;
3463
3464                         /* Found a disabled EP descriptor which has a non-null sub pointer.
3465                            Verify that this ctrl EP descriptor got disabled no errors.
3466                            FIXME: Necessary to check error_code? */
3467                         dbg_bulk("for epid %d?", epid);
3468
3469                         /* Get the first urb. */
3470                         urb = urb_list_first(epid);
3471
3472                         /* FIXME: Could this happen for valid reasons? Why did it disappear? Because of
3473                            wrong unlinking? */
3474                         if (!urb) {
3475                                 warn("NULL urb for epid %d", epid);
3476                                 continue;
3477                         }
3478
3479                         assert(urb);
3480                         urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3481                         assert(urb_priv);
3482
3483                         /* Sanity checks. */
3484                         assert(usb_pipetype(urb->pipe) == PIPE_BULK);
3485                         if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
3486                                 err("bulk endpoint got disabled before reaching last sb");
3487                         }
3488
3489                         /* For bulk IN traffic, there seems to be a race condition between
3490                            between the bulk eot and eop interrupts, or rather an uncertainty regarding
3491                            the order in which they happen. Normally we expect the eop interrupt from
3492                            DMA channel 9 to happen before the eot interrupt.
3493
3494                            Therefore, we complete the bulk IN urb in the rx interrupt handler instead. */
3495
3496                         if (usb_pipein(urb->pipe)) {
3497                                 dbg_bulk("in urb, continuing");
3498                                 continue;
3499                         }
3500
3501                         save_flags(flags);
3502                         cli();
3503                         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3504                         nop();
3505                         r_usb_ept_data = *R_USB_EPT_DATA;
3506                         restore_flags(flags);
3507
3508                         if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
3509                             IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3510                                 /* This means that the endpoint has no error, is disabled
3511                                    and had inserted traffic, i.e. transfer successfully completed. */
3512                                 etrax_usb_complete_bulk_urb(urb, 0);
3513                         } else {
3514                                 /* Shouldn't happen. We expect errors to be caught by epid attention. */
3515                                 err("Found disabled bulk EP desc, error_code != no_error");
3516                         }
3517                 }
3518         }
3519
3520         /* Normally, we should find (at least) one disabled EP descriptor with a valid sub pointer.
3521            However, because of the uncertainty in the deliverance of the eop/eot interrupts, we may
3522            not.  Also, we might find two disabled EPs when handling an eot interrupt, and then find
3523            none the next time. */
3524
3525         DBFEXIT;
3526
3527 }
3528
3529 void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg)
3530 {
3531         /* This function handles the epid attention interrupt.  There are a variety of reasons
3532            for this interrupt to happen (Designer's Reference, p. 8 - 22 for the details):
3533
3534            invalid ep_id  - Invalid epid in an EP (EP disabled).
3535            stall          - Not strictly an error condition (EP disabled).
3536            3rd error      - Three successive transaction errors  (EP disabled).
3537            buffer ourun   - Buffer overrun or underrun (EP disabled).
3538            past eof1      - Intr or isoc transaction proceeds past EOF1.
3539            near eof       - Intr or isoc transaction would not fit inside the frame.
3540            zout transfer  - If zout transfer for a bulk endpoint (EP disabled).
3541            setup transfer - If setup transfer for a non-ctrl endpoint (EP disabled). */
3542
3543         int epid;
3544
3545
3546         DBFENTER;
3547
3548         assert(reg != NULL);
3549
3550         /* Note that we loop through all epids. We still want to catch errors for
3551            the invalid one, even though we might handle them differently. */
3552         for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3553
3554                 if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
3555
3556                         struct urb *urb;
3557                         __u32 r_usb_ept_data;
3558                         unsigned long flags;
3559                         int error_code;
3560
3561                         save_flags(flags);
3562                         cli();
3563                         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3564                         nop();
3565                         /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
3566                            registers, they are located at the same address and are of the same size.
3567                            In other words, this read should be ok for isoc also. */
3568                         r_usb_ept_data = *R_USB_EPT_DATA;
3569                         restore_flags(flags);
3570
3571                         /* First some sanity checks. */
3572                         if (epid == INVALID_EPID) {
3573                                 /* FIXME: What if it became disabled? Could seriously hurt interrupt
3574                                    traffic. (Use do_intr_recover.) */
3575                                 warn("Got epid_attn for INVALID_EPID (%d).", epid);
3576                                 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3577                                 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3578                                 continue;
3579                         } else  if (epid == DUMMY_EPID) {
3580                                 /* We definitely don't care about these ones. Besides, they are
3581                                    always disabled, so any possible disabling caused by the
3582                                    epid attention interrupt is irrelevant. */
3583                                 warn("Got epid_attn for DUMMY_EPID (%d).", epid);
3584                                 continue;
3585                         }
3586
3587                         /* Get the first urb in the urb list for this epid. We blatantly assume
3588                            that only the first urb could have caused the epid attention.
3589                            (For bulk and ctrl, only one urb is active at any one time. For intr
3590                            and isoc we remove them once they are completed.) */
3591                         urb = urb_list_first(epid);
3592
3593                         if (urb == NULL) {
3594                                 err("Got epid_attn for epid %i with no urb.", epid);
3595                                 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3596                                 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3597                                 continue;
3598                         }
3599
3600                         switch (usb_pipetype(urb->pipe)) {
3601                         case PIPE_BULK:
3602                                 warn("Got epid attn for bulk endpoint, epid %d", epid);
3603                                 break;
3604                         case PIPE_CONTROL:
3605                                 warn("Got epid attn for control endpoint, epid %d", epid);
3606                                 break;
3607                         case PIPE_INTERRUPT:
3608                                 warn("Got epid attn for interrupt endpoint, epid %d", epid);
3609                                 break;
3610                         case PIPE_ISOCHRONOUS:
3611                                 warn("Got epid attn for isochronous endpoint, epid %d", epid);
3612                                 break;
3613                         }
3614
3615                         if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
3616                                 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
3617                                         warn("Hold was set for epid %d.", epid);
3618                                         continue;
3619                                 }
3620                         }
3621
3622                         /* Even though error_code occupies bits 22 - 23 in both R_USB_EPT_DATA and
3623                            R_USB_EPT_DATA_ISOC, we separate them here so we don't forget in other places. */
3624                         if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3625                                 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
3626                         } else {
3627                                 error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data);
3628                         }
3629
3630                         /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
3631                         if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3632
3633                                 /* Isoc traffic doesn't have error_count_in/error_count_out. */
3634                                 if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
3635                                     (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, r_usb_ept_data) == 3 ||
3636                                      IO_EXTRACT(R_USB_EPT_DATA, error_count_out, r_usb_ept_data) == 3)) {
3637                                         /* 3rd error. */
3638                                         warn("3rd error for epid %i", epid);
3639                                         etrax_usb_complete_urb(urb, -EPROTO);
3640
3641                                 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3642
3643                                         warn("Perror for epid %d", epid);
3644
3645                                         if (!(r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
3646                                                 /* invalid ep_id */
3647                                                 panic("Perror because of invalid epid."
3648                                                       " Deconfigured too early?");
3649                                         } else {
3650                                                 /* past eof1, near eof, zout transfer, setup transfer */
3651
3652                                                 /* Dump the urb and the relevant EP descriptor list. */
3653
3654                                                 __dump_urb(urb);
3655                                                 __dump_ept_data(epid);
3656                                                 __dump_ep_list(usb_pipetype(urb->pipe));
3657
3658                                                 panic("Something wrong with DMA descriptor contents."
3659                                                       " Too much traffic inserted?");
3660                                         }
3661                                 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3662                                         /* buffer ourun */
3663                                         panic("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3664                                 }
3665
3666                         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, stall)) {
3667                                 /* Not really a protocol error, just says that the endpoint gave
3668                                    a stall response. Note that error_code cannot be stall for isoc. */
3669                                 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3670                                         panic("Isoc traffic cannot stall");
3671                                 }
3672
3673                                 warn("Stall for epid %d", epid);
3674                                 etrax_usb_complete_urb(urb, -EPIPE);
3675
3676                         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, bus_error)) {
3677                                 /* Two devices responded to a transaction request. Must be resolved
3678                                    by software. FIXME: Reset ports? */
3679                                 panic("Bus error for epid %d."
3680                                       " Two devices responded to transaction request",
3681                                       epid);
3682
3683                         } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
3684                                 /* DMA overrun or underrun. */
3685                                 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3686
3687                                 /* It seems that error_code = buffer_error in
3688                                    R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
3689                                    are the same error. */
3690                                 etrax_usb_complete_urb(urb, -EPROTO);
3691                         }
3692                 }
3693         }
3694
3695         DBFEXIT;
3696
3697 }
3698
3699 void etrax_usb_bulk_start_timer_func(unsigned long dummy)
3700 {
3701
3702         /* We might enable an EP descriptor behind the current DMA position when it's about
3703            to decide that there are no more bulk traffic and it should stop the bulk channel.
3704            Therefore we periodically check if the bulk channel is stopped and there is an
3705            enabled bulk EP descriptor, in which case we start the bulk channel. */
3706         dbg_bulk("bulk_start_timer timed out.");
3707
3708         if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
3709                 int epid;
3710
3711                 dbg_bulk("Bulk DMA channel not running.");
3712
3713                 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3714                         if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3715                                 dbg_bulk("Found enabled EP for epid %d, starting bulk channel.\n",
3716                                          epid);
3717                                 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
3718
3719                                 /* Restart the bulk eot timer since we just started the bulk channel. */
3720                                 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3721
3722                                 /* No need to search any further. */
3723                                 break;
3724                         }
3725                 }
3726         } else {
3727                 dbg_bulk("Bulk DMA channel running.");
3728         }
3729 }
3730
3731 void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg)
3732 {
3733         etrax_hc_t *hc = reg->hc;
3734         __u16 r_usb_rh_port_status_1 = reg->r_usb_rh_port_status_1;
3735         __u16 r_usb_rh_port_status_2 = reg->r_usb_rh_port_status_2;
3736
3737         DBFENTER;
3738
3739         /* The Etrax RH does not include a wPortChange register, so this has to be handled in software
3740            (by saving the old port status value for comparison when the port status interrupt happens).
3741            See section 11.16.2.6.2 in the USB 1.1 spec for details. */
3742
3743         dbg_rh("hc->rh.prev_wPortStatus_1 = 0x%x", hc->rh.prev_wPortStatus_1);
3744         dbg_rh("hc->rh.prev_wPortStatus_2 = 0x%x", hc->rh.prev_wPortStatus_2);
3745         dbg_rh("r_usb_rh_port_status_1 = 0x%x", r_usb_rh_port_status_1);
3746         dbg_rh("r_usb_rh_port_status_2 = 0x%x", r_usb_rh_port_status_2);
3747
3748         /* C_PORT_CONNECTION is set on any transition. */
3749         hc->rh.wPortChange_1 |=
3750                 ((r_usb_rh_port_status_1 & (1 << RH_PORT_CONNECTION)) !=
3751                  (hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_CONNECTION))) ?
3752                 (1 << RH_PORT_CONNECTION) : 0;
3753
3754         hc->rh.wPortChange_2 |=
3755                 ((r_usb_rh_port_status_2 & (1 << RH_PORT_CONNECTION)) !=
3756                  (hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_CONNECTION))) ?
3757                 (1 << RH_PORT_CONNECTION) : 0;
3758
3759         /* C_PORT_ENABLE is _only_ set on a one to zero transition, i.e. when
3760            the port is disabled, not when it's enabled. */
3761         hc->rh.wPortChange_1 |=
3762                 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_ENABLE))
3763                  && !(r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
3764                 (1 << RH_PORT_ENABLE) : 0;
3765
3766         hc->rh.wPortChange_2 |=
3767                 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_ENABLE))
3768                  && !(r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
3769                 (1 << RH_PORT_ENABLE) : 0;
3770
3771         /* C_PORT_SUSPEND is set to one when the device has transitioned out
3772            of the suspended state, i.e. when suspend goes from one to zero. */
3773         hc->rh.wPortChange_1 |=
3774                 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_SUSPEND))
3775                  && !(r_usb_rh_port_status_1 & (1 << RH_PORT_SUSPEND))) ?
3776                 (1 << RH_PORT_SUSPEND) : 0;
3777
3778         hc->rh.wPortChange_2 |=
3779                 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_SUSPEND))
3780                  && !(r_usb_rh_port_status_2 & (1 << RH_PORT_SUSPEND))) ?
3781                 (1 << RH_PORT_SUSPEND) : 0;
3782
3783
3784         /* C_PORT_RESET is set when reset processing on this port is complete. */
3785         hc->rh.wPortChange_1 |=
3786                 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_RESET))
3787                  && !(r_usb_rh_port_status_1 & (1 << RH_PORT_RESET))) ?
3788                 (1 << RH_PORT_RESET) : 0;
3789
3790         hc->rh.wPortChange_2 |=
3791                 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_RESET))
3792                  && !(r_usb_rh_port_status_2 & (1 << RH_PORT_RESET))) ?
3793                 (1 << RH_PORT_RESET) : 0;
3794
3795         /* Save the new values for next port status change. */
3796         hc->rh.prev_wPortStatus_1 = r_usb_rh_port_status_1;
3797         hc->rh.prev_wPortStatus_2 = r_usb_rh_port_status_2;
3798
3799         dbg_rh("hc->rh.wPortChange_1 set to 0x%x", hc->rh.wPortChange_1);
3800         dbg_rh("hc->rh.wPortChange_2 set to 0x%x", hc->rh.wPortChange_2);
3801
3802         DBFEXIT;
3803
3804 }
3805
3806 void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg)
3807 {
3808         DBFENTER;
3809
3810         /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
3811            list for the corresponding epid? */
3812         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3813                 panic("USB controller got ourun.");
3814         }
3815         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3816
3817                 /* Before, etrax_usb_do_intr_recover was called on this epid if it was
3818                    an interrupt pipe. I don't see how re-enabling all EP descriptors
3819                    will help if there was a programming error. */
3820                 panic("USB controller got perror.");
3821         }
3822
3823         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
3824                 /* We should never operate in device mode. */
3825                 panic("USB controller in device mode.");
3826         }
3827
3828         /* These if-statements could probably be nested. */
3829         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, host_mode)) {
3830                 info("USB controller in host mode.");
3831         }
3832         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, started)) {
3833                 info("USB controller started.");
3834         }
3835         if (reg->r_usb_status & IO_MASK(R_USB_STATUS, running)) {
3836                 info("USB controller running.");
3837         }
3838
3839         DBFEXIT;
3840
3841 }
3842
3843
3844 static int etrax_rh_submit_urb(struct urb *urb)
3845 {
3846         struct usb_device *usb_dev = urb->dev;
3847         etrax_hc_t *hc = usb_dev->bus->hcpriv;
3848         unsigned int pipe = urb->pipe;
3849         struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
3850         void *data = urb->transfer_buffer;
3851         int leni = urb->transfer_buffer_length;
3852         int len = 0;
3853         int stat = 0;
3854
3855         __u16 bmRType_bReq;
3856         __u16 wValue;
3857         __u16 wIndex;
3858         __u16 wLength;
3859
3860         DBFENTER;
3861
3862         /* FIXME: What is this interrupt urb that is sent to the root hub? */
3863         if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
3864                 dbg_rh("Root-Hub submit IRQ: every %d ms", urb->interval);
3865                 hc->rh.urb = urb;
3866                 hc->rh.send = 1;
3867                 /* FIXME: We could probably remove this line since it's done
3868                    in etrax_rh_init_int_timer. (Don't remove it from
3869                    etrax_rh_init_int_timer though.) */
3870                 hc->rh.interval = urb->interval;
3871                 etrax_rh_init_int_timer(urb);
3872                 DBFEXIT;
3873
3874                 return 0;
3875         }
3876
3877         bmRType_bReq = cmd->bRequestType | (cmd->bRequest << 8);
3878         wValue = le16_to_cpu(cmd->wValue);
3879         wIndex = le16_to_cpu(cmd->wIndex);
3880         wLength = le16_to_cpu(cmd->wLength);
3881
3882         dbg_rh("bmRType_bReq : 0x%04x (%d)", bmRType_bReq, bmRType_bReq);
3883         dbg_rh("wValue       : 0x%04x (%d)", wValue, wValue);
3884         dbg_rh("wIndex       : 0x%04x (%d)", wIndex, wIndex);
3885         dbg_rh("wLength      : 0x%04x (%d)", wLength, wLength);
3886
3887         switch (bmRType_bReq) {
3888
3889                 /* Request Destination:
3890                    without flags: Device,
3891                    RH_INTERFACE: interface,
3892                    RH_ENDPOINT: endpoint,
3893                    RH_CLASS means HUB here,
3894                    RH_OTHER | RH_CLASS  almost ever means HUB_PORT here
3895                  */
3896
3897         case RH_GET_STATUS:
3898                 *(__u16 *) data = cpu_to_le16 (1);
3899                 OK (2);
3900
3901         case RH_GET_STATUS | RH_INTERFACE:
3902                 *(__u16 *) data = cpu_to_le16 (0);
3903                 OK (2);
3904
3905         case RH_GET_STATUS | RH_ENDPOINT:
3906                 *(__u16 *) data = cpu_to_le16 (0);
3907                 OK (2);
3908
3909         case RH_GET_STATUS | RH_CLASS:
3910                 *(__u32 *) data = cpu_to_le32 (0);
3911                 OK (4);         /* hub power ** */
3912
3913         case RH_GET_STATUS | RH_OTHER | RH_CLASS:
3914                 if (wIndex == 1) {
3915                         *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_1);
3916                         *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_1);
3917                 } else if (wIndex == 2) {
3918                         *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_2);
3919                         *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_2);
3920                 } else {
3921                         dbg_rh("RH_GET_STATUS whith invalid wIndex!");
3922                         OK(0);
3923                 }
3924
3925                 OK(4);
3926
3927         case RH_CLEAR_FEATURE | RH_ENDPOINT:
3928                 switch (wValue) {
3929                 case (RH_ENDPOINT_STALL):
3930                         OK (0);
3931                 }
3932                 break;
3933
3934         case RH_CLEAR_FEATURE | RH_CLASS:
3935                 switch (wValue) {
3936                 case (RH_C_HUB_OVER_CURRENT):
3937                         OK (0); /* hub power over current ** */
3938                 }
3939                 break;
3940
3941         case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
3942                 switch (wValue) {
3943                 case (RH_PORT_ENABLE):
3944                         if (wIndex == 1) {
3945
3946                                 dbg_rh("trying to do disable port 1");
3947
3948                                 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
3949
3950                                 while (hc->rh.prev_wPortStatus_1 &
3951                                        IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes));
3952                                 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
3953                                 dbg_rh("Port 1 is disabled");
3954
3955                         } else if (wIndex == 2) {
3956
3957                                 dbg_rh("trying to do disable port 2");
3958
3959                                 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
3960
3961                                 while (hc->rh.prev_wPortStatus_2 &
3962                                        IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes));
3963                                 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
3964                                 dbg_rh("Port 2 is disabled");
3965
3966                         } else {
3967                                 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_ENABLE "
3968                                        "with invalid wIndex == %d!", wIndex);
3969                         }
3970
3971                         OK (0);
3972                 case (RH_PORT_SUSPEND):
3973                         /* Opposite to suspend should be resume, so we'll do a resume. */
3974                         /* FIXME: USB 1.1, 11.16.2.2 says:
3975                            "Clearing the PORT_SUSPEND feature causes a host-initiated resume
3976                            on the specified port. If the port is not in the Suspended state,
3977                            the hub should treat this request as a functional no-operation."
3978                            Shouldn't we check if the port is in a suspended state before
3979                            resuming? */
3980
3981                         /* Make sure the controller isn't busy. */
3982                         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
3983
3984                         if (wIndex == 1) {
3985                                 *R_USB_COMMAND =
3986                                         IO_STATE(R_USB_COMMAND, port_sel, port1) |
3987                                         IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3988                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
3989                         } else if (wIndex == 2) {
3990                                 *R_USB_COMMAND =
3991                                         IO_STATE(R_USB_COMMAND, port_sel, port2) |
3992                                         IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3993                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
3994                         } else {
3995                                 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_SUSPEND "
3996                                        "with invalid wIndex == %d!", wIndex);
3997                         }
3998
3999                         OK (0);
4000                 case (RH_PORT_POWER):
4001                         OK (0); /* port power ** */
4002                 case (RH_C_PORT_CONNECTION):
4003                         if (wIndex == 1) {
4004                                 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_CONNECTION);
4005                         } else if (wIndex == 2) {
4006                                 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_CONNECTION);
4007                         } else {
4008                                 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_CONNECTION "
4009                                        "with invalid wIndex == %d!", wIndex);
4010                         }
4011
4012                         OK (0);
4013                 case (RH_C_PORT_ENABLE):
4014                         if (wIndex == 1) {
4015                                 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_ENABLE);
4016                         } else if (wIndex == 2) {
4017                                 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_ENABLE);
4018                         } else {
4019                                 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_ENABLE "
4020                                        "with invalid wIndex == %d!", wIndex);
4021                         }
4022                         OK (0);
4023                 case (RH_C_PORT_SUSPEND):
4024 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
4025                         OK (0);
4026                 case (RH_C_PORT_OVER_CURRENT):
4027                         OK (0); /* port power over current ** */
4028                 case (RH_C_PORT_RESET):
4029                         if (wIndex == 1) {
4030                                 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_RESET);
4031                         } else if (wIndex == 2) {
4032                                 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_RESET);
4033                         } else {
4034                                 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_RESET "
4035                                        "with invalid index == %d!", wIndex);
4036                         }
4037
4038                         OK (0);
4039
4040                 }
4041                 break;
4042
4043         case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
4044                 switch (wValue) {
4045                 case (RH_PORT_SUSPEND):
4046
4047                         /* Make sure the controller isn't busy. */
4048                         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4049
4050                         if (wIndex == 1) {
4051                                 *R_USB_COMMAND =
4052                                         IO_STATE(R_USB_COMMAND, port_sel, port1) |
4053                                         IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4054                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4055                         } else if (wIndex == 2) {
4056                                 *R_USB_COMMAND =
4057                                         IO_STATE(R_USB_COMMAND, port_sel, port2) |
4058                                         IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4059                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4060                         } else {
4061                                 dbg_rh("RH_SET_FEATURE->RH_PORT_SUSPEND "
4062                                        "with invalid wIndex == %d!", wIndex);
4063                         }
4064
4065                         OK (0);
4066                 case (RH_PORT_RESET):
4067                         if (wIndex == 1) {
4068
4069                         port_1_reset:
4070                                 dbg_rh("Doing reset of port 1");
4071
4072                                 /* Make sure the controller isn't busy. */
4073                                 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4074
4075                                 *R_USB_COMMAND =
4076                                         IO_STATE(R_USB_COMMAND, port_sel, port1) |
4077                                         IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4078                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4079
4080                                 /* We must wait at least 10 ms for the device to recover.
4081                                    15 ms should be enough. */
4082                                 udelay(15000);
4083
4084                                 /* Wait for reset bit to go low (should be done by now). */
4085                                 while (hc->rh.prev_wPortStatus_1 &
4086                                        IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes));
4087
4088                                 /* If the port status is
4089                                    1) connected and enabled then there is a device and everything is fine
4090                                    2) neither connected nor enabled then there is no device, also fine
4091                                    3) connected and not enabled then we try again
4092                                    (Yes, there are other port status combinations besides these.) */
4093
4094                                 if ((hc->rh.prev_wPortStatus_1 &
4095                                      IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4096                                     (hc->rh.prev_wPortStatus_1 &
4097                                      IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4098                                         dbg_rh("Connected device on port 1, but port not enabled?"
4099                                                " Trying reset again.");
4100                                         goto port_2_reset;
4101                                 }
4102
4103                                 /* Diagnostic printouts. */
4104                                 if ((hc->rh.prev_wPortStatus_1 &
4105                                      IO_STATE(R_USB_RH_PORT_STATUS_1, connected, no)) &&
4106                                     (hc->rh.prev_wPortStatus_1 &
4107                                      IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4108                                         dbg_rh("No connected device on port 1");
4109                                 } else if ((hc->rh.prev_wPortStatus_1 &
4110                                             IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4111                                            (hc->rh.prev_wPortStatus_1 &
4112                                             IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes))) {
4113                                         dbg_rh("Connected device on port 1, port 1 enabled");
4114                                 }
4115
4116                         } else if (wIndex == 2) {
4117
4118                         port_2_reset:
4119                                 dbg_rh("Doing reset of port 2");
4120
4121                                 /* Make sure the controller isn't busy. */
4122                                 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4123
4124                                 /* Issue the reset command. */
4125                                 *R_USB_COMMAND =
4126                                         IO_STATE(R_USB_COMMAND, port_sel, port2) |
4127                                         IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4128                                         IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4129
4130                                 /* We must wait at least 10 ms for the device to recover.
4131                                    15 ms should be enough. */
4132                                 udelay(15000);
4133
4134                                 /* Wait for reset bit to go low (should be done by now). */
4135                                 while (hc->rh.prev_wPortStatus_2 &
4136                                        IO_STATE(R_USB_RH_PORT_STATUS_2, reset, yes));
4137
4138                                 /* If the port status is
4139                                    1) connected and enabled then there is a device and everything is fine
4140                                    2) neither connected nor enabled then there is no device, also fine
4141                                    3) connected and not enabled then we try again
4142                                    (Yes, there are other port status combinations besides these.) */
4143
4144                                 if ((hc->rh.prev_wPortStatus_2 &
4145                                      IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4146                                     (hc->rh.prev_wPortStatus_2 &
4147                                      IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4148                                         dbg_rh("Connected device on port 2, but port not enabled?"
4149                                                " Trying reset again.");
4150                                         goto port_2_reset;
4151                                 }
4152
4153                                 /* Diagnostic printouts. */
4154                                 if ((hc->rh.prev_wPortStatus_2 &
4155                                      IO_STATE(R_USB_RH_PORT_STATUS_2, connected, no)) &&
4156                                     (hc->rh.prev_wPortStatus_2 &
4157                                      IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4158                                         dbg_rh("No connected device on port 2");
4159                                 } else if ((hc->rh.prev_wPortStatus_2 &
4160                                             IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4161                                            (hc->rh.prev_wPortStatus_2 &
4162                                             IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes))) {
4163                                         dbg_rh("Connected device on port 2, port 2 enabled");
4164                                 }
4165
4166                         } else {
4167                                 dbg_rh("RH_SET_FEATURE->RH_PORT_RESET with invalid wIndex = %d", wIndex);
4168                         }
4169
4170                         /* Make sure the controller isn't busy. */
4171                         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4172
4173                         /* If all enabled ports were disabled the host controller goes down into
4174                            started mode, so we need to bring it back into the running state.
4175                            (This is safe even if it's already in the running state.) */
4176                         *R_USB_COMMAND =
4177                                 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4178                                 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4179                                 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4180
4181                         dbg_rh("...Done");
4182                         OK(0);
4183
4184                 case (RH_PORT_POWER):
4185                         OK (0); /* port power ** */
4186                 case (RH_PORT_ENABLE):
4187                         /* There is no port enable command in the host controller, so if the
4188                            port is already enabled, we do nothing. If not, we reset the port
4189                            (with an ugly goto). */
4190
4191                         if (wIndex == 1) {
4192                                 if (hc->rh.prev_wPortStatus_1 &
4193                                     IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no)) {
4194                                         goto port_1_reset;
4195                                 }
4196                         } else if (wIndex == 2) {
4197                                 if (hc->rh.prev_wPortStatus_2 &
4198                                     IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no)) {
4199                                         goto port_2_reset;
4200                                 }
4201                         } else {
4202                                 dbg_rh("RH_SET_FEATURE->RH_GET_STATUS with invalid wIndex = %d", wIndex);
4203                         }
4204                         OK (0);
4205                 }
4206                 break;
4207
4208         case RH_SET_ADDRESS:
4209                 hc->rh.devnum = wValue;
4210                 dbg_rh("RH address set to: %d", hc->rh.devnum);
4211                 OK (0);
4212
4213         case RH_GET_DESCRIPTOR:
4214                 switch ((wValue & 0xff00) >> 8) {
4215                 case (0x01):    /* device descriptor */
4216                         len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_dev_des), wLength));
4217                         memcpy (data, root_hub_dev_des, len);
4218                         OK (len);
4219                 case (0x02):    /* configuration descriptor */
4220                         len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_config_des), wLength));
4221                         memcpy (data, root_hub_config_des, len);
4222                         OK (len);
4223                 case (0x03):    /* string descriptors */
4224                         len = usb_root_hub_string (wValue & 0xff,
4225                                                    0xff, "ETRAX 100LX",
4226                                                    data, wLength);
4227                         if (len > 0) {
4228                                 OK(min(leni, len));
4229                         } else {
4230                                 stat = -EPIPE;
4231                         }
4232
4233                 }
4234                 break;
4235
4236         case RH_GET_DESCRIPTOR | RH_CLASS:
4237                 root_hub_hub_des[2] = hc->rh.numports;
4238                 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
4239                 memcpy (data, root_hub_hub_des, len);
4240                 OK (len);
4241
4242         case RH_GET_CONFIGURATION:
4243                 *(__u8 *) data = 0x01;
4244                 OK (1);
4245
4246         case RH_SET_CONFIGURATION:
4247                 OK (0);
4248
4249         default:
4250                 stat = -EPIPE;
4251         }
4252
4253         urb->actual_length = len;
4254         urb->status = stat;
4255         urb->dev = NULL;
4256         if (urb->complete) {
4257                 urb->complete(urb, NULL);
4258         }
4259         DBFEXIT;
4260
4261         return 0;
4262 }
4263
4264 static void
4265 etrax_usb_bulk_eot_timer_func(unsigned long dummy)
4266 {
4267         /* Because of a race condition in the top half, we might miss a bulk eot.
4268            This timer "simulates" a bulk eot if we don't get one for a while, hopefully
4269            correcting the situation. */
4270         dbg_bulk("bulk_eot_timer timed out.");
4271         etrax_usb_hc_bulk_eot_interrupt(1);
4272 }
4273
4274 static void*
4275 etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size,
4276         unsigned mem_flags, dma_addr_t *dma)
4277 {
4278   return kmalloc(size, mem_flags);
4279 }
4280
4281 static void
4282 etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma)
4283 {
4284   kfree(addr);
4285 }
4286
4287
4288 static struct device fake_device;
4289
4290 static int __init etrax_usb_hc_init(void)
4291 {
4292         static etrax_hc_t *hc;
4293         struct usb_bus *bus;
4294         struct usb_device *usb_rh;
4295         int i;
4296
4297         DBFENTER;
4298
4299         info("ETRAX 100LX USB-HCD %s (c) 2001-2003 Axis Communications AB\n", usb_hcd_version);
4300
4301         hc = kmalloc(sizeof(etrax_hc_t), GFP_KERNEL);
4302         assert(hc != NULL);
4303
4304         /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
4305         /* Note that we specify sizeof(USB_EP_Desc_t) as the size, but also allocate
4306            SB descriptors from this cache. This is ok since sizeof(USB_EP_Desc_t) ==
4307            sizeof(USB_SB_Desc_t). */
4308
4309         usb_desc_cache = kmem_cache_create("usb_desc_cache", sizeof(USB_EP_Desc_t), 0,
4310                                            SLAB_HWCACHE_ALIGN, 0, 0);
4311         assert(usb_desc_cache != NULL);
4312
4313         top_half_reg_cache = kmem_cache_create("top_half_reg_cache",
4314                                                sizeof(usb_interrupt_registers_t),
4315                                                0, SLAB_HWCACHE_ALIGN, 0, 0);
4316         assert(top_half_reg_cache != NULL);
4317
4318         isoc_compl_cache = kmem_cache_create("isoc_compl_cache",
4319                                                 sizeof(usb_isoc_complete_data_t),
4320                                                 0, SLAB_HWCACHE_ALIGN, 0, 0);
4321         assert(isoc_compl_cache != NULL);
4322
4323         etrax_usb_bus = bus = usb_alloc_bus(&etrax_usb_device_operations);
4324         hc->bus = bus;
4325         bus->bus_name="ETRAX 100LX";
4326         bus->hcpriv = hc;
4327
4328         /* Initialize RH to the default address.
4329            And make sure that we have no status change indication */
4330         hc->rh.numports = 2;  /* The RH has two ports */
4331         hc->rh.devnum = 1;
4332         hc->rh.wPortChange_1 = 0;
4333         hc->rh.wPortChange_2 = 0;
4334
4335         /* Also initate the previous values to zero */
4336         hc->rh.prev_wPortStatus_1 = 0;
4337         hc->rh.prev_wPortStatus_2 = 0;
4338
4339         /* Initialize the intr-traffic flags */
4340         /* FIXME: This isn't used. (Besides, the error field isn't initialized.) */
4341         hc->intr.sleeping = 0;
4342         hc->intr.wq = NULL;
4343
4344         epid_usage_bitmask = 0;
4345         epid_out_traffic = 0;
4346
4347         /* Mark the invalid epid as being used. */
4348         set_bit(INVALID_EPID, (void *)&epid_usage_bitmask);
4349         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, INVALID_EPID);
4350         nop();
4351         /* The valid bit should still be set ('invalid' is in our world; not the hardware's). */
4352         *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, yes) |
4353                            IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4354
4355         /* Mark the dummy epid as being used. */
4356         set_bit(DUMMY_EPID, (void *)&epid_usage_bitmask);
4357         *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, DUMMY_EPID);
4358         nop();
4359         *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, no) |
4360                            IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4361
4362         /* Initialize the urb list by initiating a head for each list. */
4363         for (i = 0; i < NBR_OF_EPIDS; i++) {
4364                 INIT_LIST_HEAD(&urb_list[i]);
4365         }
4366         spin_lock_init(&urb_list_lock);
4367
4368         INIT_LIST_HEAD(&urb_unlink_list);
4369
4370
4371         /* Initiate the bulk start timer. */
4372         init_timer(&bulk_start_timer);
4373         bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
4374         bulk_start_timer.function = etrax_usb_bulk_start_timer_func;
4375         add_timer(&bulk_start_timer);
4376
4377
4378         /* Initiate the bulk eot timer. */
4379         init_timer(&bulk_eot_timer);
4380         bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
4381         bulk_eot_timer.function = etrax_usb_bulk_eot_timer_func;
4382         add_timer(&bulk_eot_timer);
4383
4384         /* Set up the data structures for USB traffic. Note that this must be done before
4385            any interrupt that relies on sane DMA list occurrs. */
4386         init_rx_buffers();
4387         init_tx_bulk_ep();
4388         init_tx_ctrl_ep();
4389         init_tx_intr_ep();
4390         init_tx_isoc_ep();
4391
4392         device_initialize(&fake_device);
4393         kobject_set_name(&fake_device.kobj, "etrax_usb");
4394         kobject_add(&fake_device.kobj);
4395         kobject_uevent(&fake_device.kobj, KOBJ_ADD);
4396         hc->bus->controller = &fake_device;
4397         usb_register_bus(hc->bus);
4398
4399         *R_IRQ_MASK2_SET =
4400                 /* Note that these interrupts are not used. */
4401                 IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
4402                 /* Sub channel 1 (ctrl) descr. interrupts are used. */
4403                 IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
4404                 IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
4405                 /* Sub channel 3 (isoc) descr. interrupts are used. */
4406                 IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
4407
4408         /* Note that the dma9_descr interrupt is not used. */
4409         *R_IRQ_MASK2_SET =
4410                 IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
4411                 IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
4412
4413         /* FIXME: Enable iso_eof only when isoc traffic is running. */
4414         *R_USB_IRQ_MASK_SET =
4415                 IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) |
4416                 IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
4417                 IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
4418                 IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
4419                 IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
4420
4421
4422         if (request_irq(ETRAX_USB_HC_IRQ, etrax_usb_hc_interrupt_top_half, 0,
4423                         "ETRAX 100LX built-in USB (HC)", hc)) {
4424                 err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
4425                 etrax_usb_hc_cleanup();
4426                 DBFEXIT;
4427                 return -1;
4428         }
4429
4430         if (request_irq(ETRAX_USB_RX_IRQ, etrax_usb_rx_interrupt, 0,
4431                         "ETRAX 100LX built-in USB (Rx)", hc)) {
4432                 err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
4433                 etrax_usb_hc_cleanup();
4434                 DBFEXIT;
4435                 return -1;
4436         }
4437
4438         if (request_irq(ETRAX_USB_TX_IRQ, etrax_usb_tx_interrupt, 0,
4439                         "ETRAX 100LX built-in USB (Tx)", hc)) {
4440                 err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
4441                 etrax_usb_hc_cleanup();
4442                 DBFEXIT;
4443                 return -1;
4444         }
4445
4446         /* R_USB_COMMAND:
4447            USB commands in host mode. The fields in this register should all be
4448            written to in one write. Do not read-modify-write one field at a time. A
4449            write to this register will trigger events in the USB controller and an
4450            incomplete command may lead to unpredictable results, and in worst case
4451            even to a deadlock in the controller.
4452            (Note however that the busy field is read-only, so no need to write to it.) */
4453
4454         /* Check the busy bit before writing to R_USB_COMMAND. */
4455
4456         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4457
4458         /* Reset the USB interface. */
4459         *R_USB_COMMAND =
4460                 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4461                 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4462                 IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
4463
4464         /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to 0x2A30 (10800),
4465            to guarantee that control traffic gets 10% of the bandwidth, and periodic transfer may
4466            allocate the rest (90%). This doesn't work though. Read on for a lenghty explanation.
4467
4468            While there is a difference between rev. 2 and rev. 3 of the ETRAX 100LX regarding the NAK
4469            behaviour, it doesn't solve this problem. What happens is that a control transfer will not
4470            be interrupted in its data stage when PSTART happens (the point at which periodic traffic
4471            is started). Thus, if PSTART is set to 10800 and its IN or OUT token is NAKed until just before
4472            PSTART happens, it will continue the IN/OUT transfer as long as it's ACKed. After it's done,
4473            there may be too little time left for an isochronous transfer, causing an epid attention
4474            interrupt due to perror. The work-around for this is to let the control transfers run at the
4475            end of the frame instead of at the beginning, and will be interrupted just fine if it doesn't
4476            fit into the frame. However, since there will *always* be a control transfer at the beginning
4477            of the frame, regardless of what we set PSTART to, that transfer might be a 64-byte transfer
4478            which consumes up to 15% of the frame, leaving only 85% for periodic traffic. The solution to
4479            this would be to 'dummy allocate' 5% of the frame with the usb_claim_bandwidth function to make
4480            sure that the periodic transfers that are inserted will always fit in the frame.
4481
4482            The idea was suggested that a control transfer could be split up into several 8 byte transfers,
4483            so that it would be interrupted by PSTART, but since this can't be done for an IN transfer this
4484            hasn't been implemented.
4485
4486            The value 11960 is chosen to be just after the SOF token, with a couple of bit times extra
4487            for possible bit stuffing. */
4488
4489         *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
4490
4491 #ifdef CONFIG_ETRAX_USB_HOST_PORT1
4492         *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
4493 #endif
4494
4495 #ifdef CONFIG_ETRAX_USB_HOST_PORT2
4496         *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
4497 #endif
4498
4499         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4500
4501         /* Configure the USB interface as a host controller. */
4502         *R_USB_COMMAND =
4503                 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4504                 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4505                 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
4506
4507         /* Note: Do not reset any ports here. Await the port status interrupts, to have a controlled
4508            sequence of resetting the ports. If we reset both ports now, and there are devices
4509            on both ports, we will get a bus error because both devices will answer the set address
4510            request. */
4511
4512         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4513
4514         /* Start processing of USB traffic. */
4515         *R_USB_COMMAND =
4516                 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4517                 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4518                 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4519
4520         while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4521
4522         usb_rh = usb_alloc_dev(NULL, hc->bus, 0);
4523         hc->bus->root_hub = usb_rh;
4524         usb_rh->state = USB_STATE_ADDRESS;
4525         usb_rh->speed = USB_SPEED_FULL;
4526         usb_rh->devnum = 1;
4527         hc->bus->devnum_next = 2;
4528         usb_rh->ep0.desc.wMaxPacketSize = __const_cpu_to_le16(64);
4529         usb_get_device_descriptor(usb_rh, USB_DT_DEVICE_SIZE);
4530         usb_new_device(usb_rh);
4531
4532         DBFEXIT;
4533
4534         return 0;
4535 }
4536
4537 static void etrax_usb_hc_cleanup(void)
4538 {
4539         DBFENTER;
4540
4541         free_irq(ETRAX_USB_HC_IRQ, NULL);
4542         free_irq(ETRAX_USB_RX_IRQ, NULL);
4543         free_irq(ETRAX_USB_TX_IRQ, NULL);
4544
4545         usb_deregister_bus(etrax_usb_bus);
4546
4547         /* FIXME: call kmem_cache_destroy here? */
4548
4549         DBFEXIT;
4550 }
4551
4552 module_init(etrax_usb_hc_init);
4553 module_exit(etrax_usb_hc_cleanup);