Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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