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