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