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