2 * usb-host.c: ETRAX 100LX USB Host Controller Driver (HCD)
4 * Copyright (c) 2002, 2003 Axis Communications AB.
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>
21 #include <asm/uaccess.h>
25 #include <asm/system.h>
26 #include <asm/arch/svinto.h>
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>
33 #include "hc_crisv10.h"
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
39 static const char *usb_hcd_version = "$Revision: 1.2 $";
50 #undef USB_DEBUG_TRACE
57 #define dbg_rh(format, arg...) printk(KERN_DEBUG __FILE__ ": (RH) " format "\n" , ## arg)
59 #define dbg_rh(format, arg...) do {} while (0)
63 #define dbg_epid(format, arg...) printk(KERN_DEBUG __FILE__ ": (EPID) " format "\n" , ## arg)
65 #define dbg_epid(format, arg...) do {} while (0)
69 #define dbg_sb(format, arg...) printk(KERN_DEBUG __FILE__ ": (SB) " format "\n" , ## arg)
71 #define dbg_sb(format, arg...) do {} while (0)
75 #define dbg_ctrl(format, arg...) printk(KERN_DEBUG __FILE__ ": (CTRL) " format "\n" , ## arg)
77 #define dbg_ctrl(format, arg...) do {} while (0)
81 #define dbg_bulk(format, arg...) printk(KERN_DEBUG __FILE__ ": (BULK) " format "\n" , ## arg)
83 #define dbg_bulk(format, arg...) do {} while (0)
87 #define dbg_intr(format, arg...) printk(KERN_DEBUG __FILE__ ": (INTR) " format "\n" , ## arg)
89 #define dbg_intr(format, arg...) do {} while (0)
93 #define dbg_isoc(format, arg...) printk(KERN_DEBUG __FILE__ ": (ISOC) " format "\n" , ## arg)
95 #define dbg_isoc(format, arg...) do {} while (0)
98 #ifdef USB_DEBUG_TRACE
99 #define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
100 #define DBFEXIT (printk(": Exiting: %s\n", __FUNCTION__))
102 #define DBFENTER do {} while (0)
103 #define DBFEXIT do {} while (0)
106 #define usb_pipeslow(pipe) (((pipe) >> 26) & 1)
108 /*-------------------------------------------------------------------
110 -------------------------------------------------------------------*/
112 static __u8 root_hub_dev_des[] =
114 0x12, /* __u8 bLength; */
115 0x01, /* __u8 bDescriptorType; Device */
116 0x00, /* __u16 bcdUSB; v1.0 */
118 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
119 0x00, /* __u8 bDeviceSubClass; */
120 0x00, /* __u8 bDeviceProtocol; */
121 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
122 0x00, /* __u16 idVendor; */
124 0x00, /* __u16 idProduct; */
126 0x00, /* __u16 bcdDevice; */
128 0x00, /* __u8 iManufacturer; */
129 0x02, /* __u8 iProduct; */
130 0x01, /* __u8 iSerialNumber; */
131 0x01 /* __u8 bNumConfigurations; */
134 /* Configuration descriptor */
135 static __u8 root_hub_config_des[] =
137 0x09, /* __u8 bLength; */
138 0x02, /* __u8 bDescriptorType; Configuration */
139 0x19, /* __u16 wTotalLength; */
141 0x01, /* __u8 bNumInterfaces; */
142 0x01, /* __u8 bConfigurationValue; */
143 0x00, /* __u8 iConfiguration; */
144 0x40, /* __u8 bmAttributes; Bit 7: Bus-powered */
145 0x00, /* __u8 MaxPower; */
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; */
159 0x07, /* __u8 ep_bLength; */
160 0x05, /* __u8 ep_bDescriptorType; Endpoint */
161 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
162 0x03, /* __u8 ep_bmAttributes; Interrupt */
163 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
165 0xff /* __u8 ep_bInterval; 255 ms */
168 static __u8 root_hub_hub_des[] =
170 0x09, /* __u8 bLength; */
171 0x29, /* __u8 bDescriptorType; Hub-descriptor */
172 0x02, /* __u8 bNbrPorts; */
173 0x00, /* __u16 wHubCharacteristics; */
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 *** */
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);
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 */
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__);}
193 #define SLAB_FLAG (in_interrupt() ? SLAB_ATOMIC : SLAB_KERNEL)
194 #define KMALLOC_FLAG (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL)
196 /* Most helpful debugging aid */
197 #define assert(expr) ((void) ((expr) ? 0 : (err("assert failed at line %d",__LINE__))))
199 /* Alternative assert define which stops after a failed assert. */
201 #define assert(expr) \
204 err("assert failed at line %d",__LINE__); \
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
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)
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
225 /* Support interrupt traffic intervals up to 128 ms. */
226 #define MAX_INTR_INTERVAL 128
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
236 /* This is just a software cache for the valid entries in R_USB_EPT_DATA. */
237 static __u32 epid_usage_bitmask;
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;
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)));
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;
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
265 static volatile USB_EP_Desc_t TxBulkDummyEPList[NBR_OF_EPIDS][2] __attribute__ ((aligned (4)));
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)));
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)));
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)));
279 /* Cache for allocating new EP and SB descriptors. */
280 static kmem_cache_t *usb_desc_cache;
282 /* Cache for the registers allocated in the top half. */
283 static kmem_cache_t *top_half_reg_cache;
285 /* Cache for the data allocated in the isoc descr top half. */
286 static kmem_cache_t *isoc_compl_cache;
288 static struct usb_bus *etrax_usb_bus;
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
295 static struct list_head urb_list[NBR_OF_EPIDS];
297 /* Read about the need and usage of this lock in submit_ctrl_urb. */
298 static spinlock_t urb_list_lock;
300 /* Used when unlinking asynchronously. */
301 static struct list_head urb_unlink_list;
303 /* for returning string descriptors in UTF-16LE */
304 static int ascii2utf (char *ascii, __u8 *utf, int utfmax)
308 for (retval = 0; *ascii && utfmax > 1; utfmax -= 2, retval += 2) {
309 *utf++ = *ascii++ & 0x7f;
315 static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
319 // assert (len > (2 * (sizeof (buf) + 1)));
320 // assert (strlen (type) <= 8);
324 *data++ = 4; *data++ = 3; /* 4 bytes data */
325 *data++ = 0; *data++ = 0; /* some language id */
329 } else if (id == 1) {
330 sprintf (buf, "%x", serial);
332 // product description
333 } else if (id == 2) {
334 sprintf (buf, "USB %s Root Hub", type);
336 // id 3 == vendor description
338 // unsupported IDs --> "stall"
342 data [0] = 2 + ascii2utf (buf, data + 2, len - 2);
347 /* Wrappers around the list functions (include/linux/list.h). */
349 static inline int urb_list_empty(int epid)
351 return list_empty(&urb_list[epid]);
354 /* Returns first urb for this epid, or NULL if list is empty. */
355 static inline struct urb *urb_list_first(int epid)
357 struct urb *first_urb = 0;
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;
367 /* Adds an urb_entry last in the list for this epid. */
368 static inline void urb_list_add(struct urb *urb, int epid)
370 urb_entry_t *urb_entry = (urb_entry_t *)kmalloc(sizeof(urb_entry_t), KMALLOC_FLAG);
373 urb_entry->urb = urb;
374 list_add_tail(&urb_entry->list, &urb_list[epid]);
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)
382 struct list_head *entry;
383 struct list_head *tmp;
384 urb_entry_t *urb_entry;
386 list_for_each_safe(entry, tmp, &urb_list[epid]) {
387 urb_entry = list_entry(entry, urb_entry_t, list);
389 assert(urb_entry->urb);
391 if (urb_entry->urb == urb) {
398 /* Delete an urb from the list. */
399 static inline void urb_list_del(struct urb *urb, int epid)
401 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
404 /* Delete entry and free. */
405 list_del(&urb_entry->list);
409 /* Move an urb to the end of the list. */
410 static inline void urb_list_move_last(struct urb *urb, int epid)
412 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
415 list_del(&urb_entry->list);
416 list_add_tail(&urb_entry->list, &urb_list[epid]);
419 /* Get the next urb in the list. */
420 static inline struct urb *urb_list_next(struct urb *urb, int epid)
422 urb_entry_t *urb_entry = __urb_list_entry(urb, epid);
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;
437 /* For debug purposes only. */
438 static inline void urb_list_dump(int epid)
440 struct list_head *entry;
441 struct list_head *tmp;
442 urb_entry_t *urb_entry;
445 info("Dumping urb list for epid %d", epid);
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);
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);
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);
464 static int etrax_remove_from_sb_list(struct urb *urb);
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);
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);
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);
479 static int etrax_usb_submit_urb(struct urb *urb, int mem_flags);
480 static int etrax_usb_unlink_urb(struct urb *urb, int status);
481 static int etrax_usb_get_frame_number(struct usb_device *usb_dev);
482 static int etrax_usb_allocate_dev(struct usb_device *usb_dev);
483 static int etrax_usb_deallocate_dev(struct usb_device *usb_dev);
485 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs);
486 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs);
487 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs);
488 static void etrax_usb_hc_interrupt_bottom_half(void *data);
490 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data);
493 /* The following is a list of interrupt handlers for the host controller interrupts we use.
494 They are called from etrax_usb_hc_interrupt_bottom_half. */
495 static void etrax_usb_hc_isoc_eof_interrupt(void);
496 static void etrax_usb_hc_bulk_eot_interrupt(int timer_induced);
497 static void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg);
498 static void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg);
499 static void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg);
501 static int etrax_rh_submit_urb (struct urb *urb);
503 /* Forward declaration needed because they are used in the rx interrupt routine. */
504 static void etrax_usb_complete_urb(struct urb *urb, int status);
505 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status);
506 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status);
507 static void etrax_usb_complete_intr_urb(struct urb *urb, int status);
508 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status);
510 static int etrax_usb_hc_init(void);
511 static void etrax_usb_hc_cleanup(void);
513 static struct usb_operations etrax_usb_device_operations =
515 .allocate = etrax_usb_allocate_dev,
516 .deallocate = etrax_usb_deallocate_dev,
517 .get_frame_number = etrax_usb_get_frame_number,
518 .submit_urb = etrax_usb_submit_urb,
519 .unlink_urb = etrax_usb_unlink_urb,
520 .buffer_alloc = etrax_usb_buffer_alloc,
521 .buffer_free = etrax_usb_buffer_free
524 /* Note that these functions are always available in their "__" variants, for use in
525 error situations. The "__" missing variants are controlled by the USB_DEBUG_DESC/
526 USB_DEBUG_URB macros. */
527 static void __dump_urb(struct urb* purb)
529 printk("\nurb :0x%08lx\n", (unsigned long)purb);
530 printk("dev :0x%08lx\n", (unsigned long)purb->dev);
531 printk("pipe :0x%08x\n", purb->pipe);
532 printk("status :%d\n", purb->status);
533 printk("transfer_flags :0x%08x\n", purb->transfer_flags);
534 printk("transfer_buffer :0x%08lx\n", (unsigned long)purb->transfer_buffer);
535 printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
536 printk("actual_length :%d\n", purb->actual_length);
537 printk("setup_packet :0x%08lx\n", (unsigned long)purb->setup_packet);
538 printk("start_frame :%d\n", purb->start_frame);
539 printk("number_of_packets :%d\n", purb->number_of_packets);
540 printk("interval :%d\n", purb->interval);
541 printk("error_count :%d\n", purb->error_count);
542 printk("context :0x%08lx\n", (unsigned long)purb->context);
543 printk("complete :0x%08lx\n\n", (unsigned long)purb->complete);
546 static void __dump_in_desc(volatile USB_IN_Desc_t *in)
548 printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
549 printk(" sw_len : 0x%04x (%d)\n", in->sw_len, in->sw_len);
550 printk(" command : 0x%04x\n", in->command);
551 printk(" next : 0x%08lx\n", in->next);
552 printk(" buf : 0x%08lx\n", in->buf);
553 printk(" hw_len : 0x%04x (%d)\n", in->hw_len, in->hw_len);
554 printk(" status : 0x%04x\n\n", in->status);
557 static void __dump_sb_desc(volatile USB_SB_Desc_t *sb)
559 char tt = (sb->command & 0x30) >> 4;
576 tt_string = "unknown (weird)";
579 printk("\n USB_SB_Desc at 0x%08lx\n", (unsigned long)sb);
580 printk(" command : 0x%04x\n", sb->command);
581 printk(" rem : %d\n", (sb->command & 0x3f00) >> 8);
582 printk(" full : %d\n", (sb->command & 0x40) >> 6);
583 printk(" tt : %d (%s)\n", tt, tt_string);
584 printk(" intr : %d\n", (sb->command & 0x8) >> 3);
585 printk(" eot : %d\n", (sb->command & 0x2) >> 1);
586 printk(" eol : %d\n", sb->command & 0x1);
587 printk(" sw_len : 0x%04x (%d)\n", sb->sw_len, sb->sw_len);
588 printk(" next : 0x%08lx\n", sb->next);
589 printk(" buf : 0x%08lx\n\n", sb->buf);
593 static void __dump_ep_desc(volatile USB_EP_Desc_t *ep)
595 printk("\nUSB_EP_Desc at 0x%08lx\n", (unsigned long)ep);
596 printk(" command : 0x%04x\n", ep->command);
597 printk(" ep_id : %d\n", (ep->command & 0x1f00) >> 8);
598 printk(" enable : %d\n", (ep->command & 0x10) >> 4);
599 printk(" intr : %d\n", (ep->command & 0x8) >> 3);
600 printk(" eof : %d\n", (ep->command & 0x2) >> 1);
601 printk(" eol : %d\n", ep->command & 0x1);
602 printk(" hw_len : 0x%04x (%d)\n", ep->hw_len, ep->hw_len);
603 printk(" next : 0x%08lx\n", ep->next);
604 printk(" sub : 0x%08lx\n\n", ep->sub);
607 static inline void __dump_ep_list(int pipe_type)
609 volatile USB_EP_Desc_t *ep;
610 volatile USB_EP_Desc_t *first_ep;
611 volatile USB_SB_Desc_t *sb;
616 first_ep = &TxBulkEPList[0];
619 first_ep = &TxCtrlEPList[0];
622 first_ep = &TxIntrEPList[0];
624 case PIPE_ISOCHRONOUS:
625 first_ep = &TxIsocEPList[0];
628 warn("Cannot dump unknown traffic type");
633 printk("\n\nDumping EP list...\n\n");
637 /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
638 sb = ep->sub ? phys_to_virt(ep->sub) : 0;
641 sb = sb->next ? phys_to_virt(sb->next) : 0;
643 ep = (volatile USB_EP_Desc_t *)(phys_to_virt(ep->next));
645 } while (ep != first_ep);
648 static inline void __dump_ept_data(int epid)
651 __u32 r_usb_ept_data;
653 if (epid < 0 || epid > 31) {
654 printk("Cannot dump ept data for invalid epid %d\n", epid);
660 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
662 r_usb_ept_data = *R_USB_EPT_DATA;
663 restore_flags(flags);
665 printk("\nR_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
666 if (r_usb_ept_data == 0) {
667 /* No need for more detailed printing. */
670 printk(" valid : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
671 printk(" hold : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
672 printk(" error_count_in : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
673 printk(" t_in : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
674 printk(" low_speed : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
675 printk(" port : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
676 printk(" error_code : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
677 printk(" t_out : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
678 printk(" error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
679 printk(" max_len : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
680 printk(" ep : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
681 printk(" dev : %d\n", (r_usb_ept_data & 0x0000003f));
684 static inline void __dump_ept_data_list(void)
688 printk("Dumping the whole R_USB_EPT_DATA list\n");
690 for (i = 0; i < 32; i++) {
694 #ifdef USB_DEBUG_DESC
695 #define dump_in_desc(...) __dump_in_desc(...)
696 #define dump_sb_desc(...) __dump_sb_desc(...)
697 #define dump_ep_desc(...) __dump_ep_desc(...)
699 #define dump_in_desc(...) do {} while (0)
700 #define dump_sb_desc(...) do {} while (0)
701 #define dump_ep_desc(...) do {} while (0)
705 #define dump_urb(x) __dump_urb(x)
707 #define dump_urb(x) do {} while (0)
710 static void init_rx_buffers(void)
716 for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
717 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
718 RxDescList[i].command = 0;
719 RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
720 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
721 RxDescList[i].hw_len = 0;
722 RxDescList[i].status = 0;
724 /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as USB_IN_Desc
725 for the relevant fields.) */
726 prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
730 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
731 RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
732 RxDescList[i].next = virt_to_phys(&RxDescList[0]);
733 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
734 RxDescList[i].hw_len = 0;
735 RxDescList[i].status = 0;
737 myNextRxDesc = &RxDescList[0];
738 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
739 myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
741 *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
742 *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
747 static void init_tx_bulk_ep(void)
753 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
754 CHECK_ALIGN(&TxBulkEPList[i]);
755 TxBulkEPList[i].hw_len = 0;
756 TxBulkEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
757 TxBulkEPList[i].sub = 0;
758 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[i + 1]);
760 /* Initiate two EPs, disabled and with the eol flag set. No need for any
763 /* The first one has the intr flag set so we get an interrupt when the DMA
764 channel is about to become disabled. */
765 CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
766 TxBulkDummyEPList[i][0].hw_len = 0;
767 TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
768 IO_STATE(USB_EP_command, eol, yes) |
769 IO_STATE(USB_EP_command, intr, yes));
770 TxBulkDummyEPList[i][0].sub = 0;
771 TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
773 /* The second one. */
774 CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
775 TxBulkDummyEPList[i][1].hw_len = 0;
776 TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
777 IO_STATE(USB_EP_command, eol, yes));
778 TxBulkDummyEPList[i][1].sub = 0;
779 /* The last dummy's next pointer is the same as the current EP's next pointer. */
780 TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
783 /* Configure the last one. */
784 CHECK_ALIGN(&TxBulkEPList[i]);
785 TxBulkEPList[i].hw_len = 0;
786 TxBulkEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
787 IO_FIELD(USB_EP_command, epid, i));
788 TxBulkEPList[i].sub = 0;
789 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[0]);
791 /* No need configuring dummy EPs for the last one as it will never be used for
792 bulk traffic (i == INVALD_EPID at this point). */
794 /* Set up to start on the last EP so we will enable it when inserting traffic
795 for the first time (imitating the situation where the DMA has stopped
796 because there was no more traffic). */
797 *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
798 /* No point in starting the bulk channel yet.
799 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
803 static void init_tx_ctrl_ep(void)
809 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
810 CHECK_ALIGN(&TxCtrlEPList[i]);
811 TxCtrlEPList[i].hw_len = 0;
812 TxCtrlEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
813 TxCtrlEPList[i].sub = 0;
814 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[i + 1]);
817 CHECK_ALIGN(&TxCtrlEPList[i]);
818 TxCtrlEPList[i].hw_len = 0;
819 TxCtrlEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
820 IO_FIELD(USB_EP_command, epid, i));
822 TxCtrlEPList[i].sub = 0;
823 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[0]);
825 *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[0]);
826 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
832 static void init_tx_intr_ep(void)
838 /* Read comment at zout_buffer declaration for an explanation to this. */
839 TxIntrSB_zout.sw_len = 1;
840 TxIntrSB_zout.next = 0;
841 TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
842 TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
843 IO_STATE(USB_SB_command, tt, zout) |
844 IO_STATE(USB_SB_command, full, yes) |
845 IO_STATE(USB_SB_command, eot, yes) |
846 IO_STATE(USB_SB_command, eol, yes));
848 for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
849 CHECK_ALIGN(&TxIntrEPList[i]);
850 TxIntrEPList[i].hw_len = 0;
851 TxIntrEPList[i].command =
852 (IO_STATE(USB_EP_command, eof, yes) |
853 IO_STATE(USB_EP_command, enable, yes) |
854 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
855 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
856 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
859 CHECK_ALIGN(&TxIntrEPList[i]);
860 TxIntrEPList[i].hw_len = 0;
861 TxIntrEPList[i].command =
862 (IO_STATE(USB_EP_command, eof, yes) |
863 IO_STATE(USB_EP_command, eol, yes) |
864 IO_STATE(USB_EP_command, enable, yes) |
865 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
866 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
867 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
869 *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
870 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
874 static void init_tx_isoc_ep(void)
880 /* Read comment at zout_buffer declaration for an explanation to this. */
881 TxIsocSB_zout.sw_len = 1;
882 TxIsocSB_zout.next = 0;
883 TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
884 TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
885 IO_STATE(USB_SB_command, tt, zout) |
886 IO_STATE(USB_SB_command, full, yes) |
887 IO_STATE(USB_SB_command, eot, yes) |
888 IO_STATE(USB_SB_command, eol, yes));
890 /* The last isochronous EP descriptor is a dummy. */
892 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
893 CHECK_ALIGN(&TxIsocEPList[i]);
894 TxIsocEPList[i].hw_len = 0;
895 TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
896 TxIsocEPList[i].sub = 0;
897 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
900 CHECK_ALIGN(&TxIsocEPList[i]);
901 TxIsocEPList[i].hw_len = 0;
903 /* Must enable the last EP descr to get eof interrupt. */
904 TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
905 IO_STATE(USB_EP_command, eof, yes) |
906 IO_STATE(USB_EP_command, eol, yes) |
907 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
908 TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
909 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
911 *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
912 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
917 static void etrax_usb_unlink_intr_urb(struct urb *urb)
919 volatile USB_EP_Desc_t *first_ep; /* First EP in the list. */
920 volatile USB_EP_Desc_t *curr_ep; /* Current EP, the iterator. */
921 volatile USB_EP_Desc_t *next_ep; /* The EP after current. */
922 volatile USB_EP_Desc_t *unlink_ep; /* The one we should remove from the list. */
926 /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the List". */
930 epid = ((etrax_urb_priv_t *)urb->hcpriv)->epid;
932 first_ep = &TxIntrEPList[0];
936 /* Note that this loop removes all EP descriptors with this epid. This assumes
937 that all EP descriptors belong to the one and only urb for this epid. */
940 next_ep = (USB_EP_Desc_t *)phys_to_virt(curr_ep->next);
942 if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
944 dbg_intr("Found EP to unlink for epid %d", epid);
946 /* This is the one we should unlink. */
949 /* Actually unlink the EP from the DMA list. */
950 curr_ep->next = unlink_ep->next;
952 /* Wait until the DMA is no longer at this descriptor. */
953 while (*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep));
955 /* Now we are free to remove it and its SB descriptor.
956 Note that it is assumed here that there is only one sb in the
957 sb list for this ep. */
958 kmem_cache_free(usb_desc_cache, phys_to_virt(unlink_ep->sub));
959 kmem_cache_free(usb_desc_cache, (USB_EP_Desc_t *)unlink_ep);
962 curr_ep = phys_to_virt(curr_ep->next);
964 } while (curr_ep != first_ep);
968 void etrax_usb_do_intr_recover(int epid)
970 USB_EP_Desc_t *first_ep, *tmp_ep;
974 first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
977 /* What this does is simply to walk the list of interrupt
978 ep descriptors and enable those that are disabled. */
981 if (IO_EXTRACT(USB_EP_command, epid, tmp_ep->command) == epid &&
982 !(tmp_ep->command & IO_MASK(USB_EP_command, enable))) {
983 tmp_ep->command |= IO_STATE(USB_EP_command, enable, yes);
986 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
988 } while (tmp_ep != first_ep);
994 static int etrax_rh_unlink_urb (struct urb *urb)
1000 hc = urb->dev->bus->hcpriv;
1002 if (hc->rh.urb == urb) {
1004 del_timer(&hc->rh.rh_int_timer);
1011 static void etrax_rh_send_irq(struct urb *urb)
1014 etrax_hc_t *hc = urb->dev->bus->hcpriv;
1018 dbg_rh("R_USB_FM_NUMBER : 0x%08X", *R_USB_FM_NUMBER);
1019 dbg_rh("R_USB_FM_REMAINING: 0x%08X", *R_USB_FM_REMAINING);
1022 data |= (hc->rh.wPortChange_1) ? (1 << 1) : 0;
1023 data |= (hc->rh.wPortChange_2) ? (1 << 2) : 0;
1025 *((__u16 *)urb->transfer_buffer) = cpu_to_le16(data);
1026 /* FIXME: Why is actual_length set to 1 when data is 2 bytes?
1027 Since only 1 byte is used, why not declare data as __u8? */
1028 urb->actual_length = 1;
1031 if (hc->rh.send && urb->complete) {
1032 dbg_rh("wPortChange_1: 0x%04X", hc->rh.wPortChange_1);
1033 dbg_rh("wPortChange_2: 0x%04X", hc->rh.wPortChange_2);
1035 urb->complete(urb, NULL);
1041 static void etrax_rh_init_int_timer(struct urb *urb)
1047 hc = urb->dev->bus->hcpriv;
1048 hc->rh.interval = urb->interval;
1049 init_timer(&hc->rh.rh_int_timer);
1050 hc->rh.rh_int_timer.function = etrax_rh_int_timer_do;
1051 hc->rh.rh_int_timer.data = (unsigned long)urb;
1052 /* FIXME: Is the jiffies resolution enough? All intervals < 10 ms will be mapped
1053 to 0, and the rest to the nearest lower 10 ms. */
1054 hc->rh.rh_int_timer.expires = jiffies + ((HZ * hc->rh.interval) / 1000);
1055 add_timer(&hc->rh.rh_int_timer);
1060 static void etrax_rh_int_timer_do(unsigned long ptr)
1067 urb = (struct urb*)ptr;
1068 hc = urb->dev->bus->hcpriv;
1071 etrax_rh_send_irq(urb);
1077 static int etrax_usb_setup_epid(struct urb *urb)
1080 char devnum, endpoint, out_traffic, slow;
1082 unsigned long flags;
1086 epid = etrax_usb_lookup_epid(urb);
1088 /* An epid that fits this urb has been found. */
1093 /* We must find and initiate a new epid for this urb. */
1094 epid = etrax_usb_allocate_epid();
1097 /* Failed to allocate a new epid. */
1102 /* We now have a new epid to use. Initiate it. */
1103 set_bit(epid, (void *)&epid_usage_bitmask);
1105 devnum = usb_pipedevice(urb->pipe);
1106 endpoint = usb_pipeendpoint(urb->pipe);
1107 slow = usb_pipeslow(urb->pipe);
1108 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1109 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1110 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1113 out_traffic = usb_pipeout(urb->pipe);
1119 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1122 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1123 *R_USB_EPT_DATA_ISO = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
1124 /* FIXME: Change any to the actual port? */
1125 IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
1126 IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
1127 IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
1128 IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
1130 *R_USB_EPT_DATA = IO_STATE(R_USB_EPT_DATA, valid, yes) |
1131 IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
1132 /* FIXME: Change any to the actual port? */
1133 IO_STATE(R_USB_EPT_DATA, port, any) |
1134 IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
1135 IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
1136 IO_FIELD(R_USB_EPT_DATA, dev, devnum);
1139 restore_flags(flags);
1142 set_bit(epid, (void *)&epid_out_traffic);
1144 clear_bit(epid, (void *)&epid_out_traffic);
1147 dbg_epid("Setting up epid %d with devnum %d, endpoint %d and max_len %d (%s)",
1148 epid, devnum, endpoint, maxlen, out_traffic ? "OUT" : "IN");
1154 static void etrax_usb_free_epid(int epid)
1156 unsigned long flags;
1160 if (!test_bit(epid, (void *)&epid_usage_bitmask)) {
1161 warn("Trying to free unused epid %d", epid);
1169 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1171 while (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold));
1172 /* This will, among other things, set the valid field to 0. */
1173 *R_USB_EPT_DATA = 0;
1174 restore_flags(flags);
1176 clear_bit(epid, (void *)&epid_usage_bitmask);
1179 dbg_epid("Freed epid %d", epid);
1184 static int etrax_usb_lookup_epid(struct urb *urb)
1188 char devnum, endpoint, slow, out_traffic;
1190 unsigned long flags;
1194 devnum = usb_pipedevice(urb->pipe);
1195 endpoint = usb_pipeendpoint(urb->pipe);
1196 slow = usb_pipeslow(urb->pipe);
1197 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1198 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1199 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1202 out_traffic = usb_pipeout(urb->pipe);
1205 /* Step through att epids. */
1206 for (i = 0; i < NBR_OF_EPIDS; i++) {
1207 if (test_bit(i, (void *)&epid_usage_bitmask) &&
1208 test_bit(i, (void *)&epid_out_traffic) == out_traffic) {
1212 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, i);
1215 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1216 data = *R_USB_EPT_DATA_ISO;
1217 restore_flags(flags);
1219 if ((IO_MASK(R_USB_EPT_DATA_ISO, valid) & data) &&
1220 (IO_EXTRACT(R_USB_EPT_DATA_ISO, dev, data) == devnum) &&
1221 (IO_EXTRACT(R_USB_EPT_DATA_ISO, ep, data) == endpoint) &&
1222 (IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len, data) == maxlen)) {
1223 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1224 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1229 data = *R_USB_EPT_DATA;
1230 restore_flags(flags);
1232 if ((IO_MASK(R_USB_EPT_DATA, valid) & data) &&
1233 (IO_EXTRACT(R_USB_EPT_DATA, dev, data) == devnum) &&
1234 (IO_EXTRACT(R_USB_EPT_DATA, ep, data) == endpoint) &&
1235 (IO_EXTRACT(R_USB_EPT_DATA, low_speed, data) == slow) &&
1236 (IO_EXTRACT(R_USB_EPT_DATA, max_len, data) == maxlen)) {
1237 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1238 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1250 static int etrax_usb_allocate_epid(void)
1256 for (i = 0; i < NBR_OF_EPIDS; i++) {
1257 if (!test_bit(i, (void *)&epid_usage_bitmask)) {
1258 dbg_epid("Found free epid %d", i);
1264 dbg_epid("Found no free epids");
1269 static int etrax_usb_submit_urb(struct urb *urb, int mem_flags)
1276 if (!urb->dev || !urb->dev->bus) {
1279 if (usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)) <= 0) {
1280 info("Submit urb to pipe with maxpacketlen 0, pipe 0x%X\n", urb->pipe);
1286 warn("urb->timeout specified, ignoring.");
1289 hc = (etrax_hc_t*)urb->dev->bus->hcpriv;
1291 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1292 /* This request is for the Virtual Root Hub. */
1293 ret = etrax_rh_submit_urb(urb);
1295 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1297 ret = etrax_usb_submit_bulk_urb(urb);
1299 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1301 ret = etrax_usb_submit_ctrl_urb(urb);
1303 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1306 if (urb->bandwidth == 0) {
1307 bustime = usb_check_bandwidth(urb->dev, urb);
1311 ret = etrax_usb_submit_intr_urb(urb);
1313 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1316 /* Bandwidth already set. */
1317 ret = etrax_usb_submit_intr_urb(urb);
1320 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1323 if (urb->bandwidth == 0) {
1324 bustime = usb_check_bandwidth(urb->dev, urb);
1328 ret = etrax_usb_submit_isoc_urb(urb);
1330 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1333 /* Bandwidth already set. */
1334 ret = etrax_usb_submit_isoc_urb(urb);
1341 printk("Submit URB error %d\n", ret);
1346 static int etrax_usb_unlink_urb(struct urb *urb, int status)
1349 etrax_urb_priv_t *urb_priv;
1359 /* Disable interrupts here since a descriptor interrupt for the isoc epid
1360 will modify the sb list. This could possibly be done more granular, but
1361 unlink_urb should not be used frequently anyway.
1367 if (!urb->dev || !urb->dev->bus) {
1368 restore_flags(flags);
1372 /* This happens if a device driver calls unlink on an urb that
1373 was never submitted (lazy driver) or if the urb was completed
1374 while unlink was being called. */
1375 restore_flags(flags);
1378 if (urb->transfer_flags & URB_ASYNC_UNLINK) {
1380 /* If URB_ASYNC_UNLINK is set:
1382 move to a separate urb list
1383 call complete at next sof with ECONNRESET
1388 call complete with ENOENT
1390 warn("URB_ASYNC_UNLINK set, ignoring.");
1393 /* One might think that urb->status = -EINPROGRESS would be a requirement for unlinking,
1394 but that doesn't work for interrupt and isochronous traffic since they are completed
1395 repeatedly, and urb->status is set then. That may in itself be a bug though. */
1397 hc = urb->dev->bus->hcpriv;
1398 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1399 epid = urb_priv->epid;
1401 /* Set the urb status (synchronous unlink). */
1402 urb->status = -ENOENT;
1403 urb_priv->urb_state = UNLINK;
1405 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1407 ret = etrax_rh_unlink_urb(urb);
1409 restore_flags(flags);
1412 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1414 dbg_bulk("Unlink of bulk urb (0x%lx)", (unsigned long)urb);
1416 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1417 /* The EP was enabled, disable it and wait. */
1418 TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1420 /* Ah, the luxury of busy-wait. */
1421 while (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[epid]));
1423 /* Kicking dummy list out of the party. */
1424 TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
1426 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1428 dbg_ctrl("Unlink of ctrl urb (0x%lx)", (unsigned long)urb);
1430 if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1431 /* The EP was enabled, disable it and wait. */
1432 TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1434 /* Ah, the luxury of busy-wait. */
1435 while (*R_DMA_CH8_SUB1_EP == virt_to_phys(&TxCtrlEPList[epid]));
1438 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1440 dbg_intr("Unlink of intr urb (0x%lx)", (unsigned long)urb);
1442 /* Separate function because it's a tad more complicated. */
1443 etrax_usb_unlink_intr_urb(urb);
1445 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1447 dbg_isoc("Unlink of isoc urb (0x%lx)", (unsigned long)urb);
1449 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1450 /* The EP was enabled, disable it and wait. */
1451 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1453 /* Ah, the luxury of busy-wait. */
1454 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
1458 /* Note that we need to remove the urb from the urb list *before* removing its SB
1459 descriptors. (This means that the isoc eof handler might get a null urb when we
1460 are unlinking the last urb.) */
1462 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1464 urb_list_del(urb, epid);
1465 TxBulkEPList[epid].sub = 0;
1466 etrax_remove_from_sb_list(urb);
1468 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1470 urb_list_del(urb, epid);
1471 TxCtrlEPList[epid].sub = 0;
1472 etrax_remove_from_sb_list(urb);
1474 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1476 urb_list_del(urb, epid);
1477 /* Sanity check (should never happen). */
1478 assert(urb_list_empty(epid));
1480 /* Release allocated bandwidth. */
1481 usb_release_bandwidth(urb->dev, urb, 0);
1483 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1485 if (usb_pipeout(urb->pipe)) {
1487 USB_SB_Desc_t *iter_sb, *prev_sb, *next_sb;
1489 if (__urb_list_entry(urb, epid)) {
1491 urb_list_del(urb, epid);
1492 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1494 while (iter_sb && (iter_sb != urb_priv->first_sb)) {
1496 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1500 /* Unlink of the URB currently being transmitted. */
1502 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1505 while (iter_sb && (iter_sb != urb_priv->last_sb)) {
1506 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1509 next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1511 /* This should only happen if the DMA has completed
1512 processing the SB list for this EP while interrupts
1514 dbg_isoc("Isoc urb not found, already sent?");
1518 prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
1520 TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
1523 etrax_remove_from_sb_list(urb);
1524 if (urb_list_empty(epid)) {
1525 TxIsocEPList[epid].sub = 0;
1526 dbg_isoc("Last isoc out urb epid %d", epid);
1527 } else if (next_sb || prev_sb) {
1528 dbg_isoc("Re-enable isoc out epid %d", epid);
1530 TxIsocEPList[epid].hw_len = 0;
1531 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1533 TxIsocEPList[epid].sub = 0;
1534 dbg_isoc("URB list non-empty and no SB list, EP disabled");
1537 dbg_isoc("Urb 0x%p not found, completed already?", urb);
1541 urb_list_del(urb, epid);
1543 /* For in traffic there is only one SB descriptor for each EP even
1544 though there may be several urbs (all urbs point at the same SB). */
1545 if (urb_list_empty(epid)) {
1546 /* No more urbs, remove the SB. */
1547 TxIsocEPList[epid].sub = 0;
1548 etrax_remove_from_sb_list(urb);
1550 TxIsocEPList[epid].hw_len = 0;
1551 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1554 /* Release allocated bandwidth. */
1555 usb_release_bandwidth(urb->dev, urb, 1);
1557 /* Free the epid if urb list is empty. */
1558 if (urb_list_empty(epid)) {
1559 etrax_usb_free_epid(epid);
1561 restore_flags(flags);
1563 /* Must be done before calling completion handler. */
1567 if (urb->complete) {
1568 urb->complete(urb, NULL);
1575 static int etrax_usb_get_frame_number(struct usb_device *usb_dev)
1579 return (*R_USB_FM_NUMBER & 0x7ff);
1582 static int etrax_usb_allocate_dev(struct usb_device *usb_dev)
1589 static int etrax_usb_deallocate_dev(struct usb_device *usb_dev)
1596 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1600 /* This interrupt handler could be used when unlinking EP descriptors. */
1602 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
1605 //dbg_bulk("dma8_sub0_descr (BULK) intr.");
1607 /* It should be safe clearing the interrupt here, since we don't expect to get a new
1608 one until we restart the bulk channel. */
1609 *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
1611 /* Wait while the DMA is running (though we don't expect it to be). */
1612 while (*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd));
1614 /* Advance the DMA to the next EP descriptor. */
1615 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
1617 //dbg_bulk("descr intr: DMA is at 0x%lx", (unsigned long)ep);
1619 /* ep->next is already a physical address; no need for a virt_to_phys. */
1620 *R_DMA_CH8_SUB0_EP = ep->next;
1622 /* Start the DMA bulk channel again. */
1623 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1625 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
1628 etrax_urb_priv_t *urb_priv;
1629 unsigned long int flags;
1631 dbg_ctrl("dma8_sub1_descr (CTRL) intr.");
1632 *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
1634 /* The complete callback gets called so we cli. */
1638 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1639 if ((TxCtrlEPList[epid].sub == 0) ||
1640 (epid == DUMMY_EPID) ||
1641 (epid == INVALID_EPID)) {
1642 /* Nothing here to see. */
1646 /* Get the first urb (if any). */
1647 urb = urb_list_first(epid);
1652 assert(usb_pipetype(urb->pipe) == PIPE_CONTROL);
1654 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1657 if (urb_priv->urb_state == WAITING_FOR_DESCR_INTR) {
1658 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
1660 etrax_usb_complete_urb(urb, 0);
1664 restore_flags(flags);
1666 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
1667 dbg_intr("dma8_sub2_descr (INTR) intr.");
1668 *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
1670 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
1674 etrax_urb_priv_t *urb_priv;
1675 USB_SB_Desc_t *sb_desc;
1677 usb_isoc_complete_data_t *comp_data = NULL;
1679 /* One or more isoc out transfers are done. */
1680 dbg_isoc("dma8_sub3_descr (ISOC) intr.");
1682 /* For each isoc out EP search for the first sb_desc with the intr flag
1683 set. This descriptor must be the last packet from an URB. Then
1684 traverse the URB list for the EP until the URB with urb_priv->last_sb
1685 matching the intr-marked sb_desc is found. All URBs before this have
1689 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1690 /* Skip past epids with no SB lists, epids used for in traffic,
1691 and special (dummy, invalid) epids. */
1692 if ((TxIsocEPList[epid].sub == 0) ||
1693 (test_bit(epid, (void *)&epid_out_traffic) == 0) ||
1694 (epid == DUMMY_EPID) ||
1695 (epid == INVALID_EPID)) {
1696 /* Nothing here to see. */
1699 sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
1701 /* Find the last descriptor of the currently active URB for this ep.
1702 This is the first descriptor in the sub list marked for a descriptor
1704 while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
1705 sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
1709 dbg_isoc("Check epid %d, sub 0x%p, SB 0x%p",
1711 phys_to_virt(TxIsocEPList[epid].sub),
1716 /* Get the first urb (if any). */
1717 urb = urb_list_first(epid);
1720 while (urb && !epid_done) {
1723 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
1725 if (!usb_pipeout(urb->pipe)) {
1726 /* descr interrupts are generated only for out pipes. */
1731 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1734 if (sb_desc != urb_priv->last_sb) {
1736 /* This urb has been sent. */
1737 dbg_isoc("out URB 0x%p sent", urb);
1739 urb_priv->urb_state = TRANSFER_DONE;
1741 } else if ((sb_desc == urb_priv->last_sb) &&
1742 !(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1744 assert((sb_desc->command & IO_MASK(USB_SB_command, eol)) == IO_STATE(USB_SB_command, eol, yes));
1745 assert(sb_desc->next == 0);
1747 dbg_isoc("out URB 0x%p last in list, epid disabled", urb);
1748 TxIsocEPList[epid].sub = 0;
1749 TxIsocEPList[epid].hw_len = 0;
1750 urb_priv->urb_state = TRANSFER_DONE;
1758 urb = urb_list_next(urb, epid);
1764 *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
1766 comp_data = (usb_isoc_complete_data_t*)kmem_cache_alloc(isoc_compl_cache, SLAB_ATOMIC);
1767 assert(comp_data != NULL);
1769 INIT_WORK(&comp_data->usb_bh, etrax_usb_isoc_descr_interrupt_bottom_half, comp_data);
1770 schedule_work(&comp_data->usb_bh);
1777 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data)
1779 usb_isoc_complete_data_t *comp_data = (usb_isoc_complete_data_t*)data;
1784 etrax_urb_priv_t *urb_priv;
1788 dbg_isoc("dma8_sub3_descr (ISOC) bottom half.");
1790 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1791 unsigned long flags;
1798 /* The descriptor interrupt handler has marked all transmitted isoch. out
1799 URBs with TRANSFER_DONE. Now we traverse all epids and for all that
1800 have isoch. out traffic traverse its URB list and complete the
1804 while (!epid_done) {
1806 /* Get the first urb (if any). */
1807 urb = urb_list_first(epid);
1813 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
1818 if (!usb_pipeout(urb->pipe)) {
1819 /* descr interrupts are generated only for out pipes. */
1824 dbg_isoc("Check epid %d, SB 0x%p", epid, (char*)TxIsocEPList[epid].sub);
1826 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1829 if (urb_priv->urb_state == TRANSFER_DONE) {
1831 struct usb_iso_packet_descriptor *packet;
1833 /* This urb has been sent. */
1834 dbg_isoc("Completing isoc out URB 0x%p", urb);
1836 for (i = 0; i < urb->number_of_packets; i++) {
1837 packet = &urb->iso_frame_desc[i];
1839 packet->actual_length = packet->length;
1842 etrax_usb_complete_isoc_urb(urb, 0);
1844 if (urb_list_empty(epid)) {
1845 etrax_usb_free_epid(epid);
1852 restore_flags(flags);
1855 kmem_cache_free(isoc_compl_cache, comp_data);
1862 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1865 etrax_urb_priv_t *urb_priv;
1867 unsigned long flags;
1869 /* Isoc diagnostics. */
1870 static int curr_fm = 0;
1871 static int prev_fm = 0;
1875 /* Clear this interrupt. */
1876 *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
1878 /* Note that this while loop assumes that all packets span only
1879 one rx descriptor. */
1881 /* The reason we cli here is that we call the driver's callback functions. */
1885 while (myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
1887 epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
1888 urb = urb_list_first(epid);
1890 //printk("eop for epid %d, first urb 0x%lx\n", epid, (unsigned long)urb);
1893 err("No urb for epid %d in rx interrupt", epid);
1894 __dump_ept_data(epid);
1898 /* Note that we cannot indescriminately assert(usb_pipein(urb->pipe)) since
1899 ctrl pipes are not. */
1901 if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
1902 __u32 r_usb_ept_data;
1905 assert(test_bit(epid, (void *)&epid_usage_bitmask));
1907 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1909 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1910 r_usb_ept_data = *R_USB_EPT_DATA_ISO;
1912 if ((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
1913 (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
1914 (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
1915 /* Not an error, just a failure to receive an expected iso
1916 in packet in this frame. This is not documented
1917 in the designers reference.
1921 warn("R_USB_EPT_DATA_ISO for epid %d = 0x%x", epid, r_usb_ept_data);
1924 r_usb_ept_data = *R_USB_EPT_DATA;
1925 warn("R_USB_EPT_DATA for epid %d = 0x%x", epid, r_usb_ept_data);
1929 warn("error in rx desc->status, epid %d, first urb = 0x%lx",
1930 epid, (unsigned long)urb);
1931 __dump_in_desc(myNextRxDesc);
1933 warn("R_USB_STATUS = 0x%x", *R_USB_STATUS);
1935 /* Check that ept was disabled when error occurred. */
1936 switch (usb_pipetype(urb->pipe)) {
1938 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1941 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1943 case PIPE_INTERRUPT:
1944 assert(!(TxIntrEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1946 case PIPE_ISOCHRONOUS:
1947 assert(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1950 warn("etrax_usb_rx_interrupt: bad pipetype %d in urb 0x%p",
1951 usb_pipetype(urb->pipe),
1954 etrax_usb_complete_urb(urb, -EPROTO);
1959 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1962 if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
1963 (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
1964 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1966 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
1967 /* We get nodata for empty data transactions, and the rx descriptor's
1968 hw_len field is not valid in that case. No data to copy in other
1971 /* Make sure the data fits in the buffer. */
1972 assert(urb_priv->rx_offset + myNextRxDesc->hw_len
1973 <= urb->transfer_buffer_length);
1975 memcpy(urb->transfer_buffer + urb_priv->rx_offset,
1976 phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
1977 urb_priv->rx_offset += myNextRxDesc->hw_len;
1980 if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
1981 if ((usb_pipetype(urb->pipe) == PIPE_CONTROL) &&
1982 ((TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)) ==
1983 IO_STATE(USB_EP_command, enable, yes))) {
1984 /* The EP is still enabled, so the OUT packet used to ack
1985 the in data is probably not processed yet. If the EP
1986 sub pointer has not moved beyond urb_priv->last_sb mark
1987 it for a descriptor interrupt and complete the urb in
1988 the descriptor interrupt handler.
1990 USB_SB_Desc_t *sub = TxCtrlEPList[urb_priv->epid].sub ? phys_to_virt(TxCtrlEPList[urb_priv->epid].sub) : 0;
1992 while ((sub != NULL) && (sub != urb_priv->last_sb)) {
1993 sub = sub->next ? phys_to_virt(sub->next) : 0;
1996 /* The urb has not been fully processed. */
1997 urb_priv->urb_state = WAITING_FOR_DESCR_INTR;
1999 warn("(CTRL) epid enabled and urb (0x%p) processed, ep->sub=0x%p", urb, (char*)TxCtrlEPList[urb_priv->epid].sub);
2000 etrax_usb_complete_urb(urb, 0);
2003 etrax_usb_complete_urb(urb, 0);
2007 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2009 struct usb_iso_packet_descriptor *packet;
2011 if (urb_priv->urb_state == UNLINK) {
2012 info("Ignoring rx data for urb being unlinked.");
2014 } else if (urb_priv->urb_state == NOT_STARTED) {
2015 info("What? Got rx data for urb that isn't started?");
2019 packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
2022 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
2023 /* We get nodata for empty data transactions, and the rx descriptor's
2024 hw_len field is not valid in that case. We copy 0 bytes however to
2026 packet->actual_length = 0;
2028 packet->actual_length = myNextRxDesc->hw_len;
2029 /* Make sure the data fits in the buffer. */
2030 assert(packet->actual_length <= packet->length);
2031 memcpy(urb->transfer_buffer + packet->offset,
2032 phys_to_virt(myNextRxDesc->buf), packet->actual_length);
2035 /* Increment the packet counter. */
2036 urb_priv->isoc_packet_counter++;
2038 /* Note that we don't care about the eot field in the rx descriptor's status.
2039 It will always be set for isoc traffic. */
2040 if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
2042 /* Out-of-synch diagnostics. */
2043 curr_fm = (*R_USB_FM_NUMBER & 0x7ff);
2044 if (((prev_fm + urb_priv->isoc_packet_counter) % (0x7ff + 1)) != curr_fm) {
2045 /* This test is wrong, if there is more than one isoc
2046 in endpoint active it will always calculate wrong
2047 since prev_fm is shared by all endpoints.
2049 FIXME Make this check per URB using urb->start_frame.
2051 dbg_isoc("Out of synch? Previous frame = %d, current frame = %d",
2057 /* Complete the urb with status OK. */
2058 etrax_usb_complete_isoc_urb(urb, 0);
2064 /* DMA IN cache bug. Flush the DMA IN buffer from the cache. (struct etrax_dma_descr
2065 has the same layout as USB_IN_Desc for the relevant fields.) */
2066 prepare_rx_descriptor((struct etrax_dma_descr*)myNextRxDesc);
2068 myPrevRxDesc = myNextRxDesc;
2069 myPrevRxDesc->command |= IO_MASK(USB_IN_command, eol);
2070 myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
2071 myLastRxDesc = myPrevRxDesc;
2073 myNextRxDesc->status = 0;
2074 myNextRxDesc = phys_to_virt(myNextRxDesc->next);
2077 restore_flags(flags);
2085 /* This function will unlink the SB descriptors associated with this urb. */
2086 static int etrax_remove_from_sb_list(struct urb *urb)
2088 USB_SB_Desc_t *next_sb, *first_sb, *last_sb;
2089 etrax_urb_priv_t *urb_priv;
2094 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2097 /* Just a sanity check. Since we don't fiddle with the DMA list the EP descriptor
2098 doesn't really need to be disabled, it's just that we expect it to be. */
2099 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2100 assert(!(TxBulkEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2101 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
2102 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2105 first_sb = urb_priv->first_sb;
2106 last_sb = urb_priv->last_sb;
2111 while (first_sb != last_sb) {
2112 next_sb = (USB_SB_Desc_t *)phys_to_virt(first_sb->next);
2113 kmem_cache_free(usb_desc_cache, first_sb);
2117 kmem_cache_free(usb_desc_cache, last_sb);
2119 dbg_sb("%d SB descriptors freed", i);
2120 /* Compare i with urb->number_of_packets for Isoc traffic.
2121 Should be same when calling unlink_urb */
2128 static int etrax_usb_submit_bulk_urb(struct urb *urb)
2132 unsigned long flags;
2133 etrax_urb_priv_t *urb_priv;
2137 /* Epid allocation, empty check and list add must be protected.
2138 Read about this in etrax_usb_submit_ctrl_urb. */
2140 spin_lock_irqsave(&urb_list_lock, flags);
2141 epid = etrax_usb_setup_epid(urb);
2144 spin_unlock_irqrestore(&urb_list_lock, flags);
2147 empty = urb_list_empty(epid);
2148 urb_list_add(urb, epid);
2149 spin_unlock_irqrestore(&urb_list_lock, flags);
2151 dbg_bulk("Adding bulk %s urb 0x%lx to %s list, epid %d",
2152 usb_pipein(urb->pipe) ? "IN" : "OUT", (unsigned long)urb, empty ? "empty" : "", epid);
2154 /* Mark the urb as being in progress. */
2155 urb->status = -EINPROGRESS;
2157 /* Setup the hcpriv data. */
2158 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2159 assert(urb_priv != NULL);
2160 /* This sets rx_offset to 0. */
2161 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2162 urb_priv->urb_state = NOT_STARTED;
2163 urb->hcpriv = urb_priv;
2166 etrax_usb_add_to_bulk_sb_list(urb, epid);
2174 static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid)
2176 USB_SB_Desc_t *sb_desc;
2177 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2178 unsigned long flags;
2183 dbg_bulk("etrax_usb_add_to_bulk_sb_list, urb 0x%lx", (unsigned long)urb);
2185 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2187 sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2188 assert(sb_desc != NULL);
2189 memset(sb_desc, 0, sizeof(USB_SB_Desc_t));
2192 if (usb_pipeout(urb->pipe)) {
2194 dbg_bulk("Grabbing bulk OUT, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2196 /* This is probably a sanity check of the bulk transaction length
2197 not being larger than 64 kB. */
2198 if (urb->transfer_buffer_length > 0xffff) {
2199 panic("urb->transfer_buffer_length > 0xffff");
2202 sb_desc->sw_len = urb->transfer_buffer_length;
2204 /* The rem field is don't care if it's not a full-length transfer, so setting
2205 it shouldn't hurt. Also, rem isn't used for OUT traffic. */
2206 sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
2207 IO_STATE(USB_SB_command, tt, out) |
2208 IO_STATE(USB_SB_command, eot, yes) |
2209 IO_STATE(USB_SB_command, eol, yes));
2211 /* The full field is set to yes, even if we don't actually check that this is
2212 a full-length transfer (i.e., that transfer_buffer_length % maxlen = 0).
2213 Setting full prevents the USB controller from sending an empty packet in
2214 that case. However, if URB_ZERO_PACKET was set we want that. */
2215 if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
2216 sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
2219 sb_desc->buf = virt_to_phys(urb->transfer_buffer);
2222 } else if (usb_pipein(urb->pipe)) {
2224 dbg_bulk("Grabbing bulk IN, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2226 sb_desc->sw_len = urb->transfer_buffer_length ?
2227 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2229 /* The rem field is don't care if it's not a full-length transfer, so setting
2230 it shouldn't hurt. */
2232 (IO_FIELD(USB_SB_command, rem,
2233 urb->transfer_buffer_length % maxlen) |
2234 IO_STATE(USB_SB_command, tt, in) |
2235 IO_STATE(USB_SB_command, eot, yes) |
2236 IO_STATE(USB_SB_command, eol, yes));
2242 urb_priv->first_sb = sb_desc;
2243 urb_priv->last_sb = sb_desc;
2244 urb_priv->epid = epid;
2246 urb->hcpriv = urb_priv;
2248 /* Reset toggle bits and reset error count. */
2252 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2255 /* FIXME: Is this a special case since the hold field is checked,
2256 or should we check hold in a lot of other cases as well? */
2257 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2258 panic("Hold was set in %s", __FUNCTION__);
2261 /* Reset error counters (regardless of which direction this traffic is). */
2263 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2264 IO_MASK(R_USB_EPT_DATA, error_count_out));
2266 /* Software must preset the toggle bits. */
2267 if (usb_pipeout(urb->pipe)) {
2269 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2270 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
2271 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
2274 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2275 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
2276 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
2279 /* Assert that the EP descriptor is disabled. */
2280 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2282 /* The reason we set the EP's sub pointer directly instead of
2283 walking the SB list and linking it last in the list is that we only
2284 have one active urb at a time (the rest are queued). */
2286 /* Note that we cannot have interrupts running when we have set the SB descriptor
2287 but the EP is not yet enabled. If a bulk eot happens for another EP, we will
2288 find this EP disabled and with a SB != 0, which will make us think that it's done. */
2289 TxBulkEPList[epid].sub = virt_to_phys(sb_desc);
2290 TxBulkEPList[epid].hw_len = 0;
2291 /* Note that we don't have to fill in the ep_id field since this
2292 was done when we allocated the EP descriptors in init_tx_bulk_ep. */
2294 /* Check if the dummy list is already with us (if several urbs were queued). */
2295 if (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0])) {
2297 dbg_bulk("Inviting dummy list to the party for urb 0x%lx, epid %d",
2298 (unsigned long)urb, epid);
2300 /* The last EP in the dummy list already has its next pointer set to
2301 TxBulkEPList[epid].next. */
2303 /* We don't need to check if the DMA is at this EP or not before changing the
2304 next pointer, since we will do it in one 32-bit write (EP descriptors are
2306 TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
2308 /* Enable the EP descr. */
2309 dbg_bulk("Enabling bulk EP for urb 0x%lx, epid %d", (unsigned long)urb, epid);
2310 TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2312 /* Everything is set up, safe to enable interrupts again. */
2313 restore_flags(flags);
2315 /* If the DMA bulk channel isn't running, we need to restart it if it
2316 has stopped at the last EP descriptor (DMA stopped because there was
2317 no more traffic) or if it has stopped at a dummy EP with the intr flag
2318 set (DMA stopped because we were too slow in inserting new traffic). */
2319 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
2322 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
2323 dbg_bulk("DMA channel not running in add");
2324 dbg_bulk("DMA is at 0x%lx", (unsigned long)ep);
2326 if (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[NBR_OF_EPIDS - 1]) ||
2327 (ep->command & 0x8) >> 3) {
2328 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
2329 /* Update/restart the bulk start timer since we just started the channel. */
2330 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
2331 /* Update/restart the bulk eot timer since we just inserted traffic. */
2332 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
2339 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status)
2341 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2342 int epid = urb_priv->epid;
2343 unsigned long flags;
2348 warn("Completing bulk urb with status %d.", status);
2350 dbg_bulk("Completing bulk urb 0x%lx for epid %d", (unsigned long)urb, epid);
2352 /* Update the urb list. */
2353 urb_list_del(urb, epid);
2355 /* For an IN pipe, we always set the actual length, regardless of whether there was
2356 an error or not (which means the device driver can use the data if it wants to). */
2357 if (usb_pipein(urb->pipe)) {
2358 urb->actual_length = urb_priv->rx_offset;
2360 /* Set actual_length for OUT urbs also; the USB mass storage driver seems
2361 to want that. We wouldn't know of any partial writes if there was an error. */
2363 urb->actual_length = urb->transfer_buffer_length;
2365 urb->actual_length = 0;
2369 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2370 Like, maybe we shouldn't toggle the toggle bits, or maybe we shouldn't insert more traffic. */
2375 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2378 /* We need to fiddle with the toggle bits because the hardware doesn't do it for us. */
2379 if (usb_pipeout(urb->pipe)) {
2381 IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
2382 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2383 usb_pipeout(urb->pipe), toggle);
2386 IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
2387 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2388 usb_pipeout(urb->pipe), toggle);
2390 restore_flags(flags);
2392 /* Remember to free the SBs. */
2393 etrax_remove_from_sb_list(urb);
2397 /* If there are any more urb's in the list we'd better start sending */
2398 if (!urb_list_empty(epid)) {
2400 struct urb *new_urb;
2402 /* Get the first urb. */
2403 new_urb = urb_list_first(epid);
2406 dbg_bulk("More bulk for epid %d", epid);
2408 etrax_usb_add_to_bulk_sb_list(new_urb, epid);
2411 urb->status = status;
2413 /* We let any non-zero status from the layer above have precedence. */
2415 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2416 is to be treated as an error. */
2417 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2418 if (usb_pipein(urb->pipe) &&
2419 (urb->actual_length !=
2420 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2421 urb->status = -EREMOTEIO;
2426 if (urb->complete) {
2427 urb->complete(urb, NULL);
2430 if (urb_list_empty(epid)) {
2431 /* This means that this EP is now free, deconfigure it. */
2432 etrax_usb_free_epid(epid);
2434 /* No more traffic; time to clean up.
2435 Must set sub pointer to 0, since we look at the sub pointer when handling
2436 the bulk eot interrupt. */
2438 dbg_bulk("No bulk for epid %d", epid);
2440 TxBulkEPList[epid].sub = 0;
2442 /* Unlink the dummy list. */
2444 dbg_bulk("Kicking dummy list out of party for urb 0x%lx, epid %d",
2445 (unsigned long)urb, epid);
2447 /* No need to wait for the DMA before changing the next pointer.
2448 The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
2449 the last one (INVALID_EPID) for actual traffic. */
2450 TxBulkEPList[epid].next =
2451 virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2457 static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2461 unsigned long flags;
2462 etrax_urb_priv_t *urb_priv;
2466 /* FIXME: Return -ENXIO if there is already a queued urb for this endpoint? */
2468 /* Epid allocation, empty check and list add must be protected.
2470 Epid allocation because if we find an existing epid for this endpoint an urb might be
2471 completed (emptying the list) before we add the new urb to the list, causing the epid
2472 to be de-allocated. We would then start the transfer with an invalid epid -> epid attn.
2474 Empty check and add because otherwise we might conclude that the list is not empty,
2475 after which it becomes empty before we add the new urb to the list, causing us not to
2476 insert the new traffic into the SB list. */
2478 spin_lock_irqsave(&urb_list_lock, flags);
2479 epid = etrax_usb_setup_epid(urb);
2481 spin_unlock_irqrestore(&urb_list_lock, flags);
2485 empty = urb_list_empty(epid);
2486 urb_list_add(urb, epid);
2487 spin_unlock_irqrestore(&urb_list_lock, flags);
2489 dbg_ctrl("Adding ctrl urb 0x%lx to %s list, epid %d",
2490 (unsigned long)urb, empty ? "empty" : "", epid);
2492 /* Mark the urb as being in progress. */
2493 urb->status = -EINPROGRESS;
2495 /* Setup the hcpriv data. */
2496 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2497 assert(urb_priv != NULL);
2498 /* This sets rx_offset to 0. */
2499 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2500 urb_priv->urb_state = NOT_STARTED;
2501 urb->hcpriv = urb_priv;
2504 etrax_usb_add_to_ctrl_sb_list(urb, epid);
2512 static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid)
2514 USB_SB_Desc_t *sb_desc_setup;
2515 USB_SB_Desc_t *sb_desc_data;
2516 USB_SB_Desc_t *sb_desc_status;
2518 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2520 unsigned long flags;
2525 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2527 sb_desc_setup = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2528 assert(sb_desc_setup != NULL);
2529 sb_desc_status = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2530 assert(sb_desc_status != NULL);
2532 /* Initialize the mandatory setup SB descriptor (used only in control transfers) */
2533 sb_desc_setup->sw_len = 8;
2534 sb_desc_setup->command = (IO_FIELD(USB_SB_command, rem, 0) |
2535 IO_STATE(USB_SB_command, tt, setup) |
2536 IO_STATE(USB_SB_command, full, yes) |
2537 IO_STATE(USB_SB_command, eot, yes));
2539 sb_desc_setup->buf = virt_to_phys(urb->setup_packet);
2541 if (usb_pipeout(urb->pipe)) {
2542 dbg_ctrl("Transfer for epid %d is OUT", epid);
2544 /* If this Control OUT transfer has an optional data stage we add an OUT token
2545 before the mandatory IN (status) token, hence the reordered SB list */
2547 sb_desc_setup->next = virt_to_phys(sb_desc_status);
2548 if (urb->transfer_buffer) {
2550 dbg_ctrl("This OUT transfer has an extra data stage");
2552 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2553 assert(sb_desc_data != NULL);
2555 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2557 sb_desc_data->sw_len = urb->transfer_buffer_length;
2558 sb_desc_data->command = (IO_STATE(USB_SB_command, tt, out) |
2559 IO_STATE(USB_SB_command, full, yes) |
2560 IO_STATE(USB_SB_command, eot, yes));
2561 sb_desc_data->buf = virt_to_phys(urb->transfer_buffer);
2562 sb_desc_data->next = virt_to_phys(sb_desc_status);
2565 sb_desc_status->sw_len = 1;
2566 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2567 IO_STATE(USB_SB_command, tt, in) |
2568 IO_STATE(USB_SB_command, eot, yes) |
2569 IO_STATE(USB_SB_command, intr, yes) |
2570 IO_STATE(USB_SB_command, eol, yes));
2572 sb_desc_status->buf = 0;
2573 sb_desc_status->next = 0;
2575 } else if (usb_pipein(urb->pipe)) {
2577 dbg_ctrl("Transfer for epid %d is IN", epid);
2578 dbg_ctrl("transfer_buffer_length = %d", urb->transfer_buffer_length);
2579 dbg_ctrl("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
2581 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2582 assert(sb_desc_data != NULL);
2584 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2586 sb_desc_data->sw_len = urb->transfer_buffer_length ?
2587 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2588 dbg_ctrl("sw_len got %d", sb_desc_data->sw_len);
2590 sb_desc_data->command =
2591 (IO_FIELD(USB_SB_command, rem,
2592 urb->transfer_buffer_length % maxlen) |
2593 IO_STATE(USB_SB_command, tt, in) |
2594 IO_STATE(USB_SB_command, eot, yes));
2596 sb_desc_data->buf = 0;
2597 sb_desc_data->next = virt_to_phys(sb_desc_status);
2599 /* Read comment at zout_buffer declaration for an explanation to this. */
2600 sb_desc_status->sw_len = 1;
2601 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2602 IO_STATE(USB_SB_command, tt, zout) |
2603 IO_STATE(USB_SB_command, full, yes) |
2604 IO_STATE(USB_SB_command, eot, yes) |
2605 IO_STATE(USB_SB_command, intr, yes) |
2606 IO_STATE(USB_SB_command, eol, yes));
2608 sb_desc_status->buf = virt_to_phys(&zout_buffer[0]);
2609 sb_desc_status->next = 0;
2612 urb_priv->first_sb = sb_desc_setup;
2613 urb_priv->last_sb = sb_desc_status;
2614 urb_priv->epid = epid;
2616 urb_priv->urb_state = STARTED;
2618 /* Reset toggle bits and reset error count, remember to di and ei */
2619 /* Warning: it is possible that this locking doesn't work with bottom-halves */
2624 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2626 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2627 panic("Hold was set in %s", __FUNCTION__);
2631 /* FIXME: Compare with etrax_usb_add_to_bulk_sb_list where the toggle bits
2632 are set to a specific value. Why the difference? Read "Transfer and Toggle Bits
2633 in Designer's Reference, p. 8 - 11. */
2635 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2636 IO_MASK(R_USB_EPT_DATA, error_count_out) |
2637 IO_MASK(R_USB_EPT_DATA, t_in) |
2638 IO_MASK(R_USB_EPT_DATA, t_out));
2640 /* Since we use the rx interrupt to complete ctrl urbs, we can enable interrupts now
2641 (i.e. we don't check the sub pointer on an eot interrupt like we do for bulk traffic). */
2642 restore_flags(flags);
2644 /* Assert that the EP descriptor is disabled. */
2645 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2647 /* Set up and enable the EP descriptor. */
2648 TxCtrlEPList[epid].sub = virt_to_phys(sb_desc_setup);
2649 TxCtrlEPList[epid].hw_len = 0;
2650 TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2652 /* We start the DMA sub channel without checking if it's running or not, because:
2653 1) If it's already running, issuing the start command is a nop.
2654 2) We avoid a test-and-set race condition. */
2655 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
2660 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status)
2662 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2663 int epid = urb_priv->epid;
2668 warn("Completing ctrl urb with status %d.", status);
2670 dbg_ctrl("Completing ctrl epid %d, urb 0x%lx", epid, (unsigned long)urb);
2672 /* Remove this urb from the list. */
2673 urb_list_del(urb, epid);
2675 /* For an IN pipe, we always set the actual length, regardless of whether there was
2676 an error or not (which means the device driver can use the data if it wants to). */
2677 if (usb_pipein(urb->pipe)) {
2678 urb->actual_length = urb_priv->rx_offset;
2681 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2682 Like, maybe we shouldn't insert more traffic. */
2684 /* Remember to free the SBs. */
2685 etrax_remove_from_sb_list(urb);
2689 /* If there are any more urbs in the list we'd better start sending. */
2690 if (!urb_list_empty(epid)) {
2691 struct urb *new_urb;
2693 /* Get the first urb. */
2694 new_urb = urb_list_first(epid);
2697 dbg_ctrl("More ctrl for epid %d, first urb = 0x%lx", epid, (unsigned long)new_urb);
2699 etrax_usb_add_to_ctrl_sb_list(new_urb, epid);
2702 urb->status = status;
2704 /* We let any non-zero status from the layer above have precedence. */
2706 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2707 is to be treated as an error. */
2708 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2709 if (usb_pipein(urb->pipe) &&
2710 (urb->actual_length !=
2711 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2712 urb->status = -EREMOTEIO;
2717 if (urb->complete) {
2718 urb->complete(urb, NULL);
2721 if (urb_list_empty(epid)) {
2722 /* No more traffic. Time to clean up. */
2723 etrax_usb_free_epid(epid);
2724 /* Must set sub pointer to 0. */
2725 dbg_ctrl("No ctrl for epid %d", epid);
2726 TxCtrlEPList[epid].sub = 0;
2732 static int etrax_usb_submit_intr_urb(struct urb *urb)
2739 if (usb_pipeout(urb->pipe)) {
2740 /* Unsupported transfer type.
2741 We don't support interrupt out traffic. (If we do, we can't support
2742 intervals for neither in or out traffic, but are forced to schedule all
2743 interrupt traffic in one frame.) */
2747 epid = etrax_usb_setup_epid(urb);
2753 if (!urb_list_empty(epid)) {
2754 /* There is already a queued urb for this endpoint. */
2755 etrax_usb_free_epid(epid);
2759 urb->status = -EINPROGRESS;
2761 dbg_intr("Add intr urb 0x%lx, to list, epid %d", (unsigned long)urb, epid);
2763 urb_list_add(urb, epid);
2764 etrax_usb_add_to_intr_sb_list(urb, epid);
2771 static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2774 volatile USB_EP_Desc_t *tmp_ep;
2775 volatile USB_EP_Desc_t *first_ep;
2781 etrax_urb_priv_t *urb_priv;
2785 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2786 interval = urb->interval;
2788 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2789 assert(urb_priv != NULL);
2790 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2791 urb->hcpriv = urb_priv;
2793 first_ep = &TxIntrEPList[0];
2795 /* Round of the interval to 2^n, it is obvious that this code favours
2796 smaller numbers, but that is actually a good thing */
2797 /* FIXME: The "rounding error" for larger intervals will be quite
2798 large. For in traffic this shouldn't be a problem since it will only
2799 mean that we "poll" more often. */
2800 for (i = 0; interval; i++) {
2801 interval = interval >> 1;
2803 interval = 1 << (i - 1);
2805 dbg_intr("Interval rounded to %d", interval);
2810 if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
2811 if ((i % interval) == 0) {
2812 /* Insert the traffic ep after tmp_ep */
2813 USB_EP_Desc_t *ep_desc;
2814 USB_SB_Desc_t *sb_desc;
2816 dbg_intr("Inserting EP for epid %d", epid);
2818 ep_desc = (USB_EP_Desc_t *)
2819 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2820 sb_desc = (USB_SB_Desc_t *)
2821 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2822 assert(ep_desc != NULL);
2823 CHECK_ALIGN(ep_desc);
2824 assert(sb_desc != NULL);
2826 ep_desc->sub = virt_to_phys(sb_desc);
2827 ep_desc->hw_len = 0;
2828 ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
2829 IO_STATE(USB_EP_command, enable, yes));
2832 /* Round upwards the number of packets of size maxlen
2833 that this SB descriptor should receive. */
2834 sb_desc->sw_len = urb->transfer_buffer_length ?
2835 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2839 (IO_FIELD(USB_SB_command, rem, urb->transfer_buffer_length % maxlen) |
2840 IO_STATE(USB_SB_command, tt, in) |
2841 IO_STATE(USB_SB_command, eot, yes) |
2842 IO_STATE(USB_SB_command, eol, yes));
2844 ep_desc->next = tmp_ep->next;
2845 tmp_ep->next = virt_to_phys(ep_desc);
2849 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
2850 } while (tmp_ep != first_ep);
2853 /* Note that first_sb/last_sb doesn't apply to interrupt traffic. */
2854 urb_priv->epid = epid;
2856 /* We start the DMA sub channel without checking if it's running or not, because:
2857 1) If it's already running, issuing the start command is a nop.
2858 2) We avoid a test-and-set race condition. */
2859 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
2866 static void etrax_usb_complete_intr_urb(struct urb *urb, int status)
2868 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2869 int epid = urb_priv->epid;
2874 warn("Completing intr urb with status %d.", status);
2876 dbg_intr("Completing intr epid %d, urb 0x%lx", epid, (unsigned long)urb);
2878 urb->status = status;
2879 urb->actual_length = urb_priv->rx_offset;
2881 dbg_intr("interrupt urb->actual_length = %d", urb->actual_length);
2883 /* We let any non-zero status from the layer above have precedence. */
2885 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2886 is to be treated as an error. */
2887 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2888 if (urb->actual_length !=
2889 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
2890 urb->status = -EREMOTEIO;
2895 /* The driver will resubmit the URB so we need to remove it first */
2896 etrax_usb_unlink_urb(urb, 0);
2897 if (urb->complete) {
2898 urb->complete(urb, NULL);
2905 static int etrax_usb_submit_isoc_urb(struct urb *urb)
2908 unsigned long flags;
2912 dbg_isoc("Submitting isoc urb = 0x%lx", (unsigned long)urb);
2914 /* Epid allocation, empty check and list add must be protected.
2915 Read about this in etrax_usb_submit_ctrl_urb. */
2917 spin_lock_irqsave(&urb_list_lock, flags);
2918 /* Is there an active epid for this urb ? */
2919 epid = etrax_usb_setup_epid(urb);
2922 spin_unlock_irqrestore(&urb_list_lock, flags);
2926 /* Ok, now we got valid endpoint, lets insert some traffic */
2928 urb->status = -EINPROGRESS;
2930 /* Find the last urb in the URB_List and add this urb after that one.
2931 Also add the traffic, that is do an etrax_usb_add_to_isoc_sb_list. This
2932 is important to make this in "real time" since isochronous traffic is
2935 dbg_isoc("Adding isoc urb to (possibly empty) list");
2936 urb_list_add(urb, epid);
2937 etrax_usb_add_to_isoc_sb_list(urb, epid);
2938 spin_unlock_irqrestore(&urb_list_lock, flags);
2945 static void etrax_usb_check_error_isoc_ep(const int epid)
2947 unsigned long int flags;
2949 __u32 r_usb_ept_data;
2951 /* We can't read R_USB_EPID_ATTN here since it would clear the iso_eof,
2952 bulk_eot and epid_attn interrupts. So we just check the status of
2953 the epid without testing if for it in R_USB_EPID_ATTN. */
2958 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2960 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
2961 registers, they are located at the same address and are of the same size.
2962 In other words, this read should be ok for isoc also. */
2963 r_usb_ept_data = *R_USB_EPT_DATA;
2964 restore_flags(flags);
2966 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
2968 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
2969 warn("Hold was set for epid %d.", epid);
2973 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, no_error)) {
2975 /* This indicates that the SB list of the ept was completed before
2976 new data was appended to it. This is not an error, but indicates
2977 large system or USB load and could possibly cause trouble for
2978 very timing sensitive USB device drivers so we log it.
2980 info("Isoc. epid %d disabled with no error", epid);
2983 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, stall)) {
2984 /* Not really a protocol error, just says that the endpoint gave
2985 a stall response. Note that error_code cannot be stall for isoc. */
2986 panic("Isoc traffic cannot stall");
2988 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, bus_error)) {
2989 /* Two devices responded to a transaction request. Must be resolved
2990 by software. FIXME: Reset ports? */
2991 panic("Bus error for epid %d."
2992 " Two devices responded to transaction request",
2995 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
2996 /* DMA overrun or underrun. */
2997 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
2999 /* It seems that error_code = buffer_error in
3000 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
3001 are the same error. */
3006 static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
3011 etrax_urb_priv_t *urb_priv;
3012 USB_SB_Desc_t *prev_sb_desc, *next_sb_desc, *temp_sb_desc;
3016 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
3018 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
3019 assert(urb_priv != NULL);
3020 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
3022 urb->hcpriv = urb_priv;
3023 urb_priv->epid = epid;
3025 if (usb_pipeout(urb->pipe)) {
3027 if (urb->number_of_packets == 0) panic("etrax_usb_add_to_isoc_sb_list 0 packets\n");
3029 dbg_isoc("Transfer for epid %d is OUT", epid);
3030 dbg_isoc("%d packets in URB", urb->number_of_packets);
3032 /* Create one SB descriptor for each packet and link them together. */
3033 for (i = 0; i < urb->number_of_packets; i++) {
3034 if (!urb->iso_frame_desc[i].length)
3037 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3038 assert(next_sb_desc != NULL);
3040 if (urb->iso_frame_desc[i].length > 0) {
3042 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, out) |
3043 IO_STATE(USB_SB_command, eot, yes));
3045 next_sb_desc->sw_len = urb->iso_frame_desc[i].length;
3046 next_sb_desc->buf = virt_to_phys((char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset);
3048 /* Check if full length transfer. */
3049 if (urb->iso_frame_desc[i].length ==
3050 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
3051 next_sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
3054 dbg_isoc("zero len packet");
3055 next_sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
3056 IO_STATE(USB_SB_command, tt, zout) |
3057 IO_STATE(USB_SB_command, eot, yes) |
3058 IO_STATE(USB_SB_command, full, yes));
3060 next_sb_desc->sw_len = 1;
3061 next_sb_desc->buf = virt_to_phys(&zout_buffer[0]);
3064 /* First SB descriptor that belongs to this urb */
3066 urb_priv->first_sb = next_sb_desc;
3068 prev_sb_desc->next = virt_to_phys(next_sb_desc);
3070 prev_sb_desc = next_sb_desc;
3073 next_sb_desc->command |= (IO_STATE(USB_SB_command, intr, yes) |
3074 IO_STATE(USB_SB_command, eol, yes));
3075 next_sb_desc->next = 0;
3076 urb_priv->last_sb = next_sb_desc;
3078 } else if (usb_pipein(urb->pipe)) {
3080 dbg_isoc("Transfer for epid %d is IN", epid);
3081 dbg_isoc("transfer_buffer_length = %d", urb->transfer_buffer_length);
3082 dbg_isoc("rem is calculated to %d", urb->iso_frame_desc[urb->number_of_packets - 1].length);
3084 /* Note that in descriptors for periodic traffic are not consumed. This means that
3085 the USB controller never propagates in the SB list. In other words, if there already
3086 is an SB descriptor in the list for this EP we don't have to do anything. */
3087 if (TxIsocEPList[epid].sub == 0) {
3088 dbg_isoc("Isoc traffic not already running, allocating SB");
3090 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3091 assert(next_sb_desc != NULL);
3093 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, in) |
3094 IO_STATE(USB_SB_command, eot, yes) |
3095 IO_STATE(USB_SB_command, eol, yes));
3097 next_sb_desc->next = 0;
3098 next_sb_desc->sw_len = 1; /* Actual number of packets is not relevant
3099 for periodic in traffic as long as it is more
3100 than zero. Set to 1 always. */
3101 next_sb_desc->buf = 0;
3103 /* The rem field is don't care for isoc traffic, so we don't set it. */
3105 /* Only one SB descriptor that belongs to this urb. */
3106 urb_priv->first_sb = next_sb_desc;
3107 urb_priv->last_sb = next_sb_desc;
3111 dbg_isoc("Isoc traffic already running, just setting first/last_sb");
3113 /* Each EP for isoc in will have only one SB descriptor, setup when submitting the
3114 already active urb. Note that even though we may have several first_sb/last_sb
3115 pointing at the same SB descriptor, they are freed only once (when the list has
3117 urb_priv->first_sb = phys_to_virt(TxIsocEPList[epid].sub);
3118 urb_priv->last_sb = phys_to_virt(TxIsocEPList[epid].sub);
3124 /* Find the spot to insert this urb and add it. */
3125 if (TxIsocEPList[epid].sub == 0) {
3126 /* First SB descriptor inserted in this list (in or out). */
3127 dbg_isoc("Inserting SB desc first in list");
3128 TxIsocEPList[epid].hw_len = 0;
3129 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3132 /* Isochronous traffic is already running, insert new traffic last (only out). */
3133 dbg_isoc("Inserting SB desc last in list");
3134 temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
3135 while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
3136 IO_STATE(USB_SB_command, eol, yes)) {
3137 assert(temp_sb_desc->next);
3138 temp_sb_desc = phys_to_virt(temp_sb_desc->next);
3140 dbg_isoc("Appending list on desc 0x%p", temp_sb_desc);
3142 /* Next pointer must be set before eol is removed. */
3143 temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
3144 /* Clear the previous end of list flag since there is a new in the
3145 added SB descriptor list. */
3146 temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
3148 if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
3149 /* 8.8.5 in Designer's Reference says we should check for and correct
3150 any errors in the EP here. That should not be necessary if epid_attn
3151 is handled correctly, so we assume all is ok. */
3152 dbg_isoc("EP disabled");
3153 etrax_usb_check_error_isoc_ep(epid);
3155 /* The SB list was exhausted. */
3156 if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
3157 /* The new sublist did not get processed before the EP was
3158 disabled. Setup the EP again. */
3159 dbg_isoc("Set EP sub to new list");
3160 TxIsocEPList[epid].hw_len = 0;
3161 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3166 if (urb->transfer_flags & URB_ISO_ASAP) {
3167 /* The isoc transfer should be started as soon as possible. The start_frame
3168 field is a return value if URB_ISO_ASAP was set. Comparing R_USB_FM_NUMBER
3169 with a USB Chief trace shows that the first isoc IN token is sent 2 frames
3170 later. I'm not sure how this affects usage of the start_frame field by the
3171 device driver, or how it affects things when USB_ISO_ASAP is not set, so
3172 therefore there's no compensation for the 2 frame "lag" here. */
3173 urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3174 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3175 urb_priv->urb_state = STARTED;
3176 dbg_isoc("URB_ISO_ASAP set, urb->start_frame set to %d", urb->start_frame);
3178 /* Not started yet. */
3179 urb_priv->urb_state = NOT_STARTED;
3180 dbg_isoc("urb_priv->urb_state set to NOT_STARTED");
3183 /* We start the DMA sub channel without checking if it's running or not, because:
3184 1) If it's already running, issuing the start command is a nop.
3185 2) We avoid a test-and-set race condition. */
3186 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3191 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status)
3193 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3194 int epid = urb_priv->epid;
3195 int auto_resubmit = 0;
3198 dbg_isoc("complete urb 0x%p, status %d", urb, status);
3201 warn("Completing isoc urb with status %d.", status);
3203 if (usb_pipein(urb->pipe)) {
3206 /* Make that all isoc packets have status and length set before
3207 completing the urb. */
3208 for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++) {
3209 urb->iso_frame_desc[i].actual_length = 0;
3210 urb->iso_frame_desc[i].status = -EPROTO;
3213 urb_list_del(urb, epid);
3215 if (!list_empty(&urb_list[epid])) {
3216 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3218 unsigned long int flags;
3219 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3220 /* The EP was enabled, disable it and wait. */
3221 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
3223 /* Ah, the luxury of busy-wait. */
3224 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
3227 etrax_remove_from_sb_list(urb);
3228 TxIsocEPList[epid].sub = 0;
3229 TxIsocEPList[epid].hw_len = 0;
3233 etrax_usb_free_epid(epid);
3234 restore_flags(flags);
3240 /* Release allocated bandwidth. */
3241 usb_release_bandwidth(urb->dev, urb, 0);
3242 } else if (usb_pipeout(urb->pipe)) {
3245 dbg_isoc("Isoc out urb complete 0x%p", urb);
3247 /* Update the urb list. */
3248 urb_list_del(urb, epid);
3250 freed_descr = etrax_remove_from_sb_list(urb);
3251 dbg_isoc("freed %d descriptors of %d packets", freed_descr, urb->number_of_packets);
3252 assert(freed_descr == urb->number_of_packets);
3256 /* Release allocated bandwidth. */
3257 usb_release_bandwidth(urb->dev, urb, 0);
3260 urb->status = status;
3261 if (urb->complete) {
3262 urb->complete(urb, NULL);
3265 if (auto_resubmit) {
3266 /* Check that urb was not unlinked by the complete callback. */
3267 if (__urb_list_entry(urb, epid)) {
3268 /* Move this one down the list. */
3269 urb_list_move_last(urb, epid);
3271 /* Mark the now first urb as started (may already be). */
3272 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3274 /* Must set this to 0 since this urb is still active after
3276 urb_priv->isoc_packet_counter = 0;
3278 warn("(ISOC) automatic resubmit urb 0x%p removed by complete.", urb);
3285 static void etrax_usb_complete_urb(struct urb *urb, int status)
3287 switch (usb_pipetype(urb->pipe)) {
3289 etrax_usb_complete_bulk_urb(urb, status);
3292 etrax_usb_complete_ctrl_urb(urb, status);
3294 case PIPE_INTERRUPT:
3295 etrax_usb_complete_intr_urb(urb, status);
3297 case PIPE_ISOCHRONOUS:
3298 etrax_usb_complete_isoc_urb(urb, status);
3301 err("Unknown pipetype");
3307 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs)
3309 usb_interrupt_registers_t *reg;
3310 unsigned long flags;
3314 __u16 port_status_1;
3315 __u16 port_status_2;
3320 /* Read critical registers into local variables, do kmalloc afterwards. */
3324 irq_mask = *R_USB_IRQ_MASK_READ;
3325 /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that R_USB_STATUS
3326 must be read before R_USB_EPID_ATTN since reading the latter clears the
3327 ourun and perror fields of R_USB_STATUS. */
3328 status = *R_USB_STATUS;
3330 /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn interrupts. */
3331 epid_attn = *R_USB_EPID_ATTN;
3333 /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
3334 port_status interrupt. */
3335 port_status_1 = *R_USB_RH_PORT_STATUS_1;
3336 port_status_2 = *R_USB_RH_PORT_STATUS_2;
3338 /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
3339 /* Note: the lower 11 bits contain the actual frame number, sent with each sof. */
3340 fm_number = *R_USB_FM_NUMBER;
3342 restore_flags(flags);
3344 reg = (usb_interrupt_registers_t *)kmem_cache_alloc(top_half_reg_cache, SLAB_ATOMIC);
3346 assert(reg != NULL);
3348 reg->hc = (etrax_hc_t *)vhc;
3350 /* Now put register values into kmalloc'd area. */
3351 reg->r_usb_irq_mask_read = irq_mask;
3352 reg->r_usb_status = status;
3353 reg->r_usb_epid_attn = epid_attn;
3354 reg->r_usb_rh_port_status_1 = port_status_1;
3355 reg->r_usb_rh_port_status_2 = port_status_2;
3356 reg->r_usb_fm_number = fm_number;
3358 INIT_WORK(®->usb_bh, etrax_usb_hc_interrupt_bottom_half, reg);
3359 schedule_work(®->usb_bh);
3366 static void etrax_usb_hc_interrupt_bottom_half(void *data)
3368 usb_interrupt_registers_t *reg = (usb_interrupt_registers_t *)data;
3369 __u32 irq_mask = reg->r_usb_irq_mask_read;
3373 /* Interrupts are handled in order of priority. */
3374 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
3375 etrax_usb_hc_epid_attn_interrupt(reg);
3377 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
3378 etrax_usb_hc_port_status_interrupt(reg);
3380 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
3381 etrax_usb_hc_ctl_status_interrupt(reg);
3383 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
3384 etrax_usb_hc_isoc_eof_interrupt();
3386 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
3387 /* Update/restart the bulk start timer since obviously the channel is running. */
3388 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
3389 /* Update/restart the bulk eot timer since we just received an bulk eot interrupt. */
3390 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3392 etrax_usb_hc_bulk_eot_interrupt(0);
3395 kmem_cache_free(top_half_reg_cache, reg);
3401 void etrax_usb_hc_isoc_eof_interrupt(void)
3404 etrax_urb_priv_t *urb_priv;
3406 unsigned long flags;
3410 /* Do not check the invalid epid (it has a valid sub pointer). */
3411 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
3413 /* Do not check the invalid epid (it has a valid sub pointer). */
3414 if ((epid == DUMMY_EPID) || (epid == INVALID_EPID))
3417 /* Disable interrupts to block the isoc out descriptor interrupt handler
3418 from being called while the isoc EPID list is being checked.
3423 if (TxIsocEPList[epid].sub == 0) {
3424 /* Nothing here to see. */
3425 restore_flags(flags);
3429 /* Get the first urb (if any). */
3430 urb = urb_list_first(epid);
3432 warn("Ignoring NULL urb");
3433 restore_flags(flags);
3436 if (usb_pipein(urb->pipe)) {
3439 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
3441 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3444 if (urb_priv->urb_state == NOT_STARTED) {
3446 /* If ASAP is not set and urb->start_frame is the current frame,
3447 start the transfer. */
3448 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3449 (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
3451 dbg_isoc("Enabling isoc IN EP descr for epid %d", epid);
3452 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3454 /* This urb is now active. */
3455 urb_priv->urb_state = STARTED;
3460 restore_flags(flags);
3467 void etrax_usb_hc_bulk_eot_interrupt(int timer_induced)
3471 /* The technique is to run one urb at a time, wait for the eot interrupt at which
3472 point the EP descriptor has been disabled. */
3475 dbg_bulk("bulk eot%s", timer_induced ? ", called by timer" : "");
3477 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3479 if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
3480 (TxBulkEPList[epid].sub != 0)) {
3483 etrax_urb_priv_t *urb_priv;
3484 unsigned long flags;
3485 __u32 r_usb_ept_data;
3487 /* Found a disabled EP descriptor which has a non-null sub pointer.
3488 Verify that this ctrl EP descriptor got disabled no errors.
3489 FIXME: Necessary to check error_code? */
3490 dbg_bulk("for epid %d?", epid);
3492 /* Get the first urb. */
3493 urb = urb_list_first(epid);
3495 /* FIXME: Could this happen for valid reasons? Why did it disappear? Because of
3498 warn("NULL urb for epid %d", epid);
3503 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3506 /* Sanity checks. */
3507 assert(usb_pipetype(urb->pipe) == PIPE_BULK);
3508 if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
3509 err("bulk endpoint got disabled before reaching last sb");
3512 /* For bulk IN traffic, there seems to be a race condition between
3513 between the bulk eot and eop interrupts, or rather an uncertainty regarding
3514 the order in which they happen. Normally we expect the eop interrupt from
3515 DMA channel 9 to happen before the eot interrupt.
3517 Therefore, we complete the bulk IN urb in the rx interrupt handler instead. */
3519 if (usb_pipein(urb->pipe)) {
3520 dbg_bulk("in urb, continuing");
3526 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3528 r_usb_ept_data = *R_USB_EPT_DATA;
3529 restore_flags(flags);
3531 if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
3532 IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3533 /* This means that the endpoint has no error, is disabled
3534 and had inserted traffic, i.e. transfer successfully completed. */
3535 etrax_usb_complete_bulk_urb(urb, 0);
3537 /* Shouldn't happen. We expect errors to be caught by epid attention. */
3538 err("Found disabled bulk EP desc, error_code != no_error");
3543 /* Normally, we should find (at least) one disabled EP descriptor with a valid sub pointer.
3544 However, because of the uncertainty in the deliverance of the eop/eot interrupts, we may
3545 not. Also, we might find two disabled EPs when handling an eot interrupt, and then find
3546 none the next time. */
3552 void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg)
3554 /* This function handles the epid attention interrupt. There are a variety of reasons
3555 for this interrupt to happen (Designer's Reference, p. 8 - 22 for the details):
3557 invalid ep_id - Invalid epid in an EP (EP disabled).
3558 stall - Not strictly an error condition (EP disabled).
3559 3rd error - Three successive transaction errors (EP disabled).
3560 buffer ourun - Buffer overrun or underrun (EP disabled).
3561 past eof1 - Intr or isoc transaction proceeds past EOF1.
3562 near eof - Intr or isoc transaction would not fit inside the frame.
3563 zout transfer - If zout transfer for a bulk endpoint (EP disabled).
3564 setup transfer - If setup transfer for a non-ctrl endpoint (EP disabled). */
3571 assert(reg != NULL);
3573 /* Note that we loop through all epids. We still want to catch errors for
3574 the invalid one, even though we might handle them differently. */
3575 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3577 if (test_bit(epid, (void *)®->r_usb_epid_attn)) {
3580 __u32 r_usb_ept_data;
3581 unsigned long flags;
3586 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3588 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
3589 registers, they are located at the same address and are of the same size.
3590 In other words, this read should be ok for isoc also. */
3591 r_usb_ept_data = *R_USB_EPT_DATA;
3592 restore_flags(flags);
3594 /* First some sanity checks. */
3595 if (epid == INVALID_EPID) {
3596 /* FIXME: What if it became disabled? Could seriously hurt interrupt
3597 traffic. (Use do_intr_recover.) */
3598 warn("Got epid_attn for INVALID_EPID (%d).", epid);
3599 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3600 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3602 } else if (epid == DUMMY_EPID) {
3603 /* We definitely don't care about these ones. Besides, they are
3604 always disabled, so any possible disabling caused by the
3605 epid attention interrupt is irrelevant. */
3606 warn("Got epid_attn for DUMMY_EPID (%d).", epid);
3610 /* Get the first urb in the urb list for this epid. We blatantly assume
3611 that only the first urb could have caused the epid attention.
3612 (For bulk and ctrl, only one urb is active at any one time. For intr
3613 and isoc we remove them once they are completed.) */
3614 urb = urb_list_first(epid);
3617 err("Got epid_attn for epid %i with no urb.", epid);
3618 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3619 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3623 switch (usb_pipetype(urb->pipe)) {
3625 warn("Got epid attn for bulk endpoint, epid %d", epid);
3628 warn("Got epid attn for control endpoint, epid %d", epid);
3630 case PIPE_INTERRUPT:
3631 warn("Got epid attn for interrupt endpoint, epid %d", epid);
3633 case PIPE_ISOCHRONOUS:
3634 warn("Got epid attn for isochronous endpoint, epid %d", epid);
3638 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
3639 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
3640 warn("Hold was set for epid %d.", epid);
3645 /* Even though error_code occupies bits 22 - 23 in both R_USB_EPT_DATA and
3646 R_USB_EPT_DATA_ISOC, we separate them here so we don't forget in other places. */
3647 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3648 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
3650 error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data);
3653 /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
3654 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3656 /* Isoc traffic doesn't have error_count_in/error_count_out. */
3657 if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
3658 (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, r_usb_ept_data) == 3 ||
3659 IO_EXTRACT(R_USB_EPT_DATA, error_count_out, r_usb_ept_data) == 3)) {
3661 warn("3rd error for epid %i", epid);
3662 etrax_usb_complete_urb(urb, -EPROTO);
3664 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3666 warn("Perror for epid %d", epid);
3668 if (!(r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
3670 panic("Perror because of invalid epid."
3671 " Deconfigured too early?");
3673 /* past eof1, near eof, zout transfer, setup transfer */
3675 /* Dump the urb and the relevant EP descriptor list. */
3678 __dump_ept_data(epid);
3679 __dump_ep_list(usb_pipetype(urb->pipe));
3681 panic("Something wrong with DMA descriptor contents."
3682 " Too much traffic inserted?");
3684 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3686 panic("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3689 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, stall)) {
3690 /* Not really a protocol error, just says that the endpoint gave
3691 a stall response. Note that error_code cannot be stall for isoc. */
3692 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3693 panic("Isoc traffic cannot stall");
3696 warn("Stall for epid %d", epid);
3697 etrax_usb_complete_urb(urb, -EPIPE);
3699 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, bus_error)) {
3700 /* Two devices responded to a transaction request. Must be resolved
3701 by software. FIXME: Reset ports? */
3702 panic("Bus error for epid %d."
3703 " Two devices responded to transaction request",
3706 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
3707 /* DMA overrun or underrun. */
3708 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3710 /* It seems that error_code = buffer_error in
3711 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
3712 are the same error. */
3713 etrax_usb_complete_urb(urb, -EPROTO);
3722 void etrax_usb_bulk_start_timer_func(unsigned long dummy)
3725 /* We might enable an EP descriptor behind the current DMA position when it's about
3726 to decide that there are no more bulk traffic and it should stop the bulk channel.
3727 Therefore we periodically check if the bulk channel is stopped and there is an
3728 enabled bulk EP descriptor, in which case we start the bulk channel. */
3729 dbg_bulk("bulk_start_timer timed out.");
3731 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
3734 dbg_bulk("Bulk DMA channel not running.");
3736 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3737 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3738 dbg_bulk("Found enabled EP for epid %d, starting bulk channel.\n",
3740 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
3742 /* Restart the bulk eot timer since we just started the bulk channel. */
3743 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3745 /* No need to search any further. */
3750 dbg_bulk("Bulk DMA channel running.");
3754 void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg)
3756 etrax_hc_t *hc = reg->hc;
3757 __u16 r_usb_rh_port_status_1 = reg->r_usb_rh_port_status_1;
3758 __u16 r_usb_rh_port_status_2 = reg->r_usb_rh_port_status_2;
3762 /* The Etrax RH does not include a wPortChange register, so this has to be handled in software
3763 (by saving the old port status value for comparison when the port status interrupt happens).
3764 See section 11.16.2.6.2 in the USB 1.1 spec for details. */
3766 dbg_rh("hc->rh.prev_wPortStatus_1 = 0x%x", hc->rh.prev_wPortStatus_1);
3767 dbg_rh("hc->rh.prev_wPortStatus_2 = 0x%x", hc->rh.prev_wPortStatus_2);
3768 dbg_rh("r_usb_rh_port_status_1 = 0x%x", r_usb_rh_port_status_1);
3769 dbg_rh("r_usb_rh_port_status_2 = 0x%x", r_usb_rh_port_status_2);
3771 /* C_PORT_CONNECTION is set on any transition. */
3772 hc->rh.wPortChange_1 |=
3773 ((r_usb_rh_port_status_1 & (1 << RH_PORT_CONNECTION)) !=
3774 (hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_CONNECTION))) ?
3775 (1 << RH_PORT_CONNECTION) : 0;
3777 hc->rh.wPortChange_2 |=
3778 ((r_usb_rh_port_status_2 & (1 << RH_PORT_CONNECTION)) !=
3779 (hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_CONNECTION))) ?
3780 (1 << RH_PORT_CONNECTION) : 0;
3782 /* C_PORT_ENABLE is _only_ set on a one to zero transition, i.e. when
3783 the port is disabled, not when it's enabled. */
3784 hc->rh.wPortChange_1 |=
3785 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_ENABLE))
3786 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
3787 (1 << RH_PORT_ENABLE) : 0;
3789 hc->rh.wPortChange_2 |=
3790 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_ENABLE))
3791 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
3792 (1 << RH_PORT_ENABLE) : 0;
3794 /* C_PORT_SUSPEND is set to one when the device has transitioned out
3795 of the suspended state, i.e. when suspend goes from one to zero. */
3796 hc->rh.wPortChange_1 |=
3797 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_SUSPEND))
3798 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_SUSPEND))) ?
3799 (1 << RH_PORT_SUSPEND) : 0;
3801 hc->rh.wPortChange_2 |=
3802 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_SUSPEND))
3803 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_SUSPEND))) ?
3804 (1 << RH_PORT_SUSPEND) : 0;
3807 /* C_PORT_RESET is set when reset processing on this port is complete. */
3808 hc->rh.wPortChange_1 |=
3809 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_RESET))
3810 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_RESET))) ?
3811 (1 << RH_PORT_RESET) : 0;
3813 hc->rh.wPortChange_2 |=
3814 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_RESET))
3815 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_RESET))) ?
3816 (1 << RH_PORT_RESET) : 0;
3818 /* Save the new values for next port status change. */
3819 hc->rh.prev_wPortStatus_1 = r_usb_rh_port_status_1;
3820 hc->rh.prev_wPortStatus_2 = r_usb_rh_port_status_2;
3822 dbg_rh("hc->rh.wPortChange_1 set to 0x%x", hc->rh.wPortChange_1);
3823 dbg_rh("hc->rh.wPortChange_2 set to 0x%x", hc->rh.wPortChange_2);
3829 void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg)
3833 /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
3834 list for the corresponding epid? */
3835 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3836 panic("USB controller got ourun.");
3838 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3840 /* Before, etrax_usb_do_intr_recover was called on this epid if it was
3841 an interrupt pipe. I don't see how re-enabling all EP descriptors
3842 will help if there was a programming error. */
3843 panic("USB controller got perror.");
3846 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
3847 /* We should never operate in device mode. */
3848 panic("USB controller in device mode.");
3851 /* These if-statements could probably be nested. */
3852 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, host_mode)) {
3853 info("USB controller in host mode.");
3855 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, started)) {
3856 info("USB controller started.");
3858 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, running)) {
3859 info("USB controller running.");
3867 static int etrax_rh_submit_urb(struct urb *urb)
3869 struct usb_device *usb_dev = urb->dev;
3870 etrax_hc_t *hc = usb_dev->bus->hcpriv;
3871 unsigned int pipe = urb->pipe;
3872 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
3873 void *data = urb->transfer_buffer;
3874 int leni = urb->transfer_buffer_length;
3885 /* FIXME: What is this interrupt urb that is sent to the root hub? */
3886 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
3887 dbg_rh("Root-Hub submit IRQ: every %d ms", urb->interval);
3890 /* FIXME: We could probably remove this line since it's done
3891 in etrax_rh_init_int_timer. (Don't remove it from
3892 etrax_rh_init_int_timer though.) */
3893 hc->rh.interval = urb->interval;
3894 etrax_rh_init_int_timer(urb);
3900 bmRType_bReq = cmd->bRequestType | (cmd->bRequest << 8);
3901 wValue = le16_to_cpu(cmd->wValue);
3902 wIndex = le16_to_cpu(cmd->wIndex);
3903 wLength = le16_to_cpu(cmd->wLength);
3905 dbg_rh("bmRType_bReq : 0x%04x (%d)", bmRType_bReq, bmRType_bReq);
3906 dbg_rh("wValue : 0x%04x (%d)", wValue, wValue);
3907 dbg_rh("wIndex : 0x%04x (%d)", wIndex, wIndex);
3908 dbg_rh("wLength : 0x%04x (%d)", wLength, wLength);
3910 switch (bmRType_bReq) {
3912 /* Request Destination:
3913 without flags: Device,
3914 RH_INTERFACE: interface,
3915 RH_ENDPOINT: endpoint,
3916 RH_CLASS means HUB here,
3917 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
3921 *(__u16 *) data = cpu_to_le16 (1);
3924 case RH_GET_STATUS | RH_INTERFACE:
3925 *(__u16 *) data = cpu_to_le16 (0);
3928 case RH_GET_STATUS | RH_ENDPOINT:
3929 *(__u16 *) data = cpu_to_le16 (0);
3932 case RH_GET_STATUS | RH_CLASS:
3933 *(__u32 *) data = cpu_to_le32 (0);
3934 OK (4); /* hub power ** */
3936 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
3938 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_1);
3939 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_1);
3940 } else if (wIndex == 2) {
3941 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_2);
3942 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_2);
3944 dbg_rh("RH_GET_STATUS whith invalid wIndex!");
3950 case RH_CLEAR_FEATURE | RH_ENDPOINT:
3952 case (RH_ENDPOINT_STALL):
3957 case RH_CLEAR_FEATURE | RH_CLASS:
3959 case (RH_C_HUB_OVER_CURRENT):
3960 OK (0); /* hub power over current ** */
3964 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
3966 case (RH_PORT_ENABLE):
3969 dbg_rh("trying to do disable port 1");
3971 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
3973 while (hc->rh.prev_wPortStatus_1 &
3974 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes));
3975 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
3976 dbg_rh("Port 1 is disabled");
3978 } else if (wIndex == 2) {
3980 dbg_rh("trying to do disable port 2");
3982 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
3984 while (hc->rh.prev_wPortStatus_2 &
3985 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes));
3986 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
3987 dbg_rh("Port 2 is disabled");
3990 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_ENABLE "
3991 "with invalid wIndex == %d!", wIndex);
3995 case (RH_PORT_SUSPEND):
3996 /* Opposite to suspend should be resume, so we'll do a resume. */
3997 /* FIXME: USB 1.1, 11.16.2.2 says:
3998 "Clearing the PORT_SUSPEND feature causes a host-initiated resume
3999 on the specified port. If the port is not in the Suspended state,
4000 the hub should treat this request as a functional no-operation."
4001 Shouldn't we check if the port is in a suspended state before
4004 /* Make sure the controller isn't busy. */
4005 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4009 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4010 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
4011 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4012 } else if (wIndex == 2) {
4014 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4015 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
4016 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4018 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_SUSPEND "
4019 "with invalid wIndex == %d!", wIndex);
4023 case (RH_PORT_POWER):
4024 OK (0); /* port power ** */
4025 case (RH_C_PORT_CONNECTION):
4027 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_CONNECTION);
4028 } else if (wIndex == 2) {
4029 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_CONNECTION);
4031 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_CONNECTION "
4032 "with invalid wIndex == %d!", wIndex);
4036 case (RH_C_PORT_ENABLE):
4038 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_ENABLE);
4039 } else if (wIndex == 2) {
4040 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_ENABLE);
4042 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_ENABLE "
4043 "with invalid wIndex == %d!", wIndex);
4046 case (RH_C_PORT_SUSPEND):
4047 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
4049 case (RH_C_PORT_OVER_CURRENT):
4050 OK (0); /* port power over current ** */
4051 case (RH_C_PORT_RESET):
4053 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_RESET);
4054 } else if (wIndex == 2) {
4055 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_RESET);
4057 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_RESET "
4058 "with invalid index == %d!", wIndex);
4066 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
4068 case (RH_PORT_SUSPEND):
4070 /* Make sure the controller isn't busy. */
4071 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4075 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4076 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4077 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4078 } else if (wIndex == 2) {
4080 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4081 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4082 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4084 dbg_rh("RH_SET_FEATURE->RH_PORT_SUSPEND "
4085 "with invalid wIndex == %d!", wIndex);
4089 case (RH_PORT_RESET):
4093 dbg_rh("Doing reset of port 1");
4095 /* Make sure the controller isn't busy. */
4096 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4099 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4100 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4101 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4103 /* We must wait at least 10 ms for the device to recover.
4104 15 ms should be enough. */
4107 /* Wait for reset bit to go low (should be done by now). */
4108 while (hc->rh.prev_wPortStatus_1 &
4109 IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes));
4111 /* If the port status is
4112 1) connected and enabled then there is a device and everything is fine
4113 2) neither connected nor enabled then there is no device, also fine
4114 3) connected and not enabled then we try again
4115 (Yes, there are other port status combinations besides these.) */
4117 if ((hc->rh.prev_wPortStatus_1 &
4118 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4119 (hc->rh.prev_wPortStatus_1 &
4120 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4121 dbg_rh("Connected device on port 1, but port not enabled?"
4122 " Trying reset again.");
4126 /* Diagnostic printouts. */
4127 if ((hc->rh.prev_wPortStatus_1 &
4128 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, no)) &&
4129 (hc->rh.prev_wPortStatus_1 &
4130 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4131 dbg_rh("No connected device on port 1");
4132 } else if ((hc->rh.prev_wPortStatus_1 &
4133 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4134 (hc->rh.prev_wPortStatus_1 &
4135 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes))) {
4136 dbg_rh("Connected device on port 1, port 1 enabled");
4139 } else if (wIndex == 2) {
4142 dbg_rh("Doing reset of port 2");
4144 /* Make sure the controller isn't busy. */
4145 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4147 /* Issue the reset command. */
4149 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4150 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4151 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4153 /* We must wait at least 10 ms for the device to recover.
4154 15 ms should be enough. */
4157 /* Wait for reset bit to go low (should be done by now). */
4158 while (hc->rh.prev_wPortStatus_2 &
4159 IO_STATE(R_USB_RH_PORT_STATUS_2, reset, yes));
4161 /* If the port status is
4162 1) connected and enabled then there is a device and everything is fine
4163 2) neither connected nor enabled then there is no device, also fine
4164 3) connected and not enabled then we try again
4165 (Yes, there are other port status combinations besides these.) */
4167 if ((hc->rh.prev_wPortStatus_2 &
4168 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4169 (hc->rh.prev_wPortStatus_2 &
4170 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4171 dbg_rh("Connected device on port 2, but port not enabled?"
4172 " Trying reset again.");
4176 /* Diagnostic printouts. */
4177 if ((hc->rh.prev_wPortStatus_2 &
4178 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, no)) &&
4179 (hc->rh.prev_wPortStatus_2 &
4180 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4181 dbg_rh("No connected device on port 2");
4182 } else if ((hc->rh.prev_wPortStatus_2 &
4183 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4184 (hc->rh.prev_wPortStatus_2 &
4185 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes))) {
4186 dbg_rh("Connected device on port 2, port 2 enabled");
4190 dbg_rh("RH_SET_FEATURE->RH_PORT_RESET with invalid wIndex = %d", wIndex);
4193 /* Make sure the controller isn't busy. */
4194 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4196 /* If all enabled ports were disabled the host controller goes down into
4197 started mode, so we need to bring it back into the running state.
4198 (This is safe even if it's already in the running state.) */
4200 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4201 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4202 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4207 case (RH_PORT_POWER):
4208 OK (0); /* port power ** */
4209 case (RH_PORT_ENABLE):
4210 /* There is no port enable command in the host controller, so if the
4211 port is already enabled, we do nothing. If not, we reset the port
4212 (with an ugly goto). */
4215 if (hc->rh.prev_wPortStatus_1 &
4216 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no)) {
4219 } else if (wIndex == 2) {
4220 if (hc->rh.prev_wPortStatus_2 &
4221 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no)) {
4225 dbg_rh("RH_SET_FEATURE->RH_GET_STATUS with invalid wIndex = %d", wIndex);
4231 case RH_SET_ADDRESS:
4232 hc->rh.devnum = wValue;
4233 dbg_rh("RH address set to: %d", hc->rh.devnum);
4236 case RH_GET_DESCRIPTOR:
4237 switch ((wValue & 0xff00) >> 8) {
4238 case (0x01): /* device descriptor */
4239 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_dev_des), wLength));
4240 memcpy (data, root_hub_dev_des, len);
4242 case (0x02): /* configuration descriptor */
4243 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_config_des), wLength));
4244 memcpy (data, root_hub_config_des, len);
4246 case (0x03): /* string descriptors */
4247 len = usb_root_hub_string (wValue & 0xff,
4248 0xff, "ETRAX 100LX",
4259 case RH_GET_DESCRIPTOR | RH_CLASS:
4260 root_hub_hub_des[2] = hc->rh.numports;
4261 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
4262 memcpy (data, root_hub_hub_des, len);
4265 case RH_GET_CONFIGURATION:
4266 *(__u8 *) data = 0x01;
4269 case RH_SET_CONFIGURATION:
4276 urb->actual_length = len;
4279 if (urb->complete) {
4280 urb->complete(urb, NULL);
4288 etrax_usb_bulk_eot_timer_func(unsigned long dummy)
4290 /* Because of a race condition in the top half, we might miss a bulk eot.
4291 This timer "simulates" a bulk eot if we don't get one for a while, hopefully
4292 correcting the situation. */
4293 dbg_bulk("bulk_eot_timer timed out.");
4294 etrax_usb_hc_bulk_eot_interrupt(1);
4298 etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size, int mem_flags, dma_addr_t *dma)
4300 return kmalloc(size, mem_flags);
4304 etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma)
4310 static struct device fake_device;
4312 static int __init etrax_usb_hc_init(void)
4314 static etrax_hc_t *hc;
4315 struct usb_bus *bus;
4316 struct usb_device *usb_rh;
4321 info("ETRAX 100LX USB-HCD %s (c) 2001-2003 Axis Communications AB\n", usb_hcd_version);
4323 hc = kmalloc(sizeof(etrax_hc_t), GFP_KERNEL);
4326 /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
4327 /* Note that we specify sizeof(USB_EP_Desc_t) as the size, but also allocate
4328 SB descriptors from this cache. This is ok since sizeof(USB_EP_Desc_t) ==
4329 sizeof(USB_SB_Desc_t). */
4331 usb_desc_cache = kmem_cache_create("usb_desc_cache", sizeof(USB_EP_Desc_t), 0,
4332 SLAB_HWCACHE_ALIGN, 0, 0);
4333 assert(usb_desc_cache != NULL);
4335 top_half_reg_cache = kmem_cache_create("top_half_reg_cache",
4336 sizeof(usb_interrupt_registers_t),
4337 0, SLAB_HWCACHE_ALIGN, 0, 0);
4338 assert(top_half_reg_cache != NULL);
4340 isoc_compl_cache = kmem_cache_create("isoc_compl_cache",
4341 sizeof(usb_isoc_complete_data_t),
4342 0, SLAB_HWCACHE_ALIGN, 0, 0);
4343 assert(isoc_compl_cache != NULL);
4345 etrax_usb_bus = bus = usb_alloc_bus(&etrax_usb_device_operations);
4347 bus->bus_name="ETRAX 100LX";
4350 /* Initalize RH to the default address.
4351 And make sure that we have no status change indication */
4352 hc->rh.numports = 2; /* The RH has two ports */
4354 hc->rh.wPortChange_1 = 0;
4355 hc->rh.wPortChange_2 = 0;
4357 /* Also initate the previous values to zero */
4358 hc->rh.prev_wPortStatus_1 = 0;
4359 hc->rh.prev_wPortStatus_2 = 0;
4361 /* Initialize the intr-traffic flags */
4362 /* FIXME: This isn't used. (Besides, the error field isn't initialized.) */
4363 hc->intr.sleeping = 0;
4366 epid_usage_bitmask = 0;
4367 epid_out_traffic = 0;
4369 /* Mark the invalid epid as being used. */
4370 set_bit(INVALID_EPID, (void *)&epid_usage_bitmask);
4371 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, INVALID_EPID);
4373 /* The valid bit should still be set ('invalid' is in our world; not the hardware's). */
4374 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, yes) |
4375 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4377 /* Mark the dummy epid as being used. */
4378 set_bit(DUMMY_EPID, (void *)&epid_usage_bitmask);
4379 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, DUMMY_EPID);
4381 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, no) |
4382 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4384 /* Initialize the urb list by initiating a head for each list. */
4385 for (i = 0; i < NBR_OF_EPIDS; i++) {
4386 INIT_LIST_HEAD(&urb_list[i]);
4388 spin_lock_init(&urb_list_lock);
4390 INIT_LIST_HEAD(&urb_unlink_list);
4393 /* Initiate the bulk start timer. */
4394 init_timer(&bulk_start_timer);
4395 bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
4396 bulk_start_timer.function = etrax_usb_bulk_start_timer_func;
4397 add_timer(&bulk_start_timer);
4400 /* Initiate the bulk eot timer. */
4401 init_timer(&bulk_eot_timer);
4402 bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
4403 bulk_eot_timer.function = etrax_usb_bulk_eot_timer_func;
4404 add_timer(&bulk_eot_timer);
4406 /* Set up the data structures for USB traffic. Note that this must be done before
4407 any interrupt that relies on sane DMA list occurrs. */
4414 device_initialize(&fake_device);
4415 kobject_set_name(&fake_device.kobj, "etrax_usb");
4416 kobject_add(&fake_device.kobj);
4417 hc->bus->controller = &fake_device;
4418 usb_register_bus(hc->bus);
4421 /* Note that these interrupts are not used. */
4422 IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
4423 /* Sub channel 1 (ctrl) descr. interrupts are used. */
4424 IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
4425 IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
4426 /* Sub channel 3 (isoc) descr. interrupts are used. */
4427 IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
4429 /* Note that the dma9_descr interrupt is not used. */
4431 IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
4432 IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
4434 /* FIXME: Enable iso_eof only when isoc traffic is running. */
4435 *R_USB_IRQ_MASK_SET =
4436 IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) |
4437 IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
4438 IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
4439 IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
4440 IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
4443 if (request_irq(ETRAX_USB_HC_IRQ, etrax_usb_hc_interrupt_top_half, 0,
4444 "ETRAX 100LX built-in USB (HC)", hc)) {
4445 err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
4446 etrax_usb_hc_cleanup();
4451 if (request_irq(ETRAX_USB_RX_IRQ, etrax_usb_rx_interrupt, 0,
4452 "ETRAX 100LX built-in USB (Rx)", hc)) {
4453 err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
4454 etrax_usb_hc_cleanup();
4459 if (request_irq(ETRAX_USB_TX_IRQ, etrax_usb_tx_interrupt, 0,
4460 "ETRAX 100LX built-in USB (Tx)", hc)) {
4461 err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
4462 etrax_usb_hc_cleanup();
4468 USB commands in host mode. The fields in this register should all be
4469 written to in one write. Do not read-modify-write one field at a time. A
4470 write to this register will trigger events in the USB controller and an
4471 incomplete command may lead to unpredictable results, and in worst case
4472 even to a deadlock in the controller.
4473 (Note however that the busy field is read-only, so no need to write to it.) */
4475 /* Check the busy bit before writing to R_USB_COMMAND. */
4477 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4479 /* Reset the USB interface. */
4481 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4482 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4483 IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
4485 /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to 0x2A30 (10800),
4486 to guarantee that control traffic gets 10% of the bandwidth, and periodic transfer may
4487 allocate the rest (90%). This doesn't work though. Read on for a lenghty explanation.
4489 While there is a difference between rev. 2 and rev. 3 of the ETRAX 100LX regarding the NAK
4490 behaviour, it doesn't solve this problem. What happens is that a control transfer will not
4491 be interrupted in its data stage when PSTART happens (the point at which periodic traffic
4492 is started). Thus, if PSTART is set to 10800 and its IN or OUT token is NAKed until just before
4493 PSTART happens, it will continue the IN/OUT transfer as long as it's ACKed. After it's done,
4494 there may be too little time left for an isochronous transfer, causing an epid attention
4495 interrupt due to perror. The work-around for this is to let the control transfers run at the
4496 end of the frame instead of at the beginning, and will be interrupted just fine if it doesn't
4497 fit into the frame. However, since there will *always* be a control transfer at the beginning
4498 of the frame, regardless of what we set PSTART to, that transfer might be a 64-byte transfer
4499 which consumes up to 15% of the frame, leaving only 85% for periodic traffic. The solution to
4500 this would be to 'dummy allocate' 5% of the frame with the usb_claim_bandwidth function to make
4501 sure that the periodic transfers that are inserted will always fit in the frame.
4503 The idea was suggested that a control transfer could be split up into several 8 byte transfers,
4504 so that it would be interrupted by PSTART, but since this can't be done for an IN transfer this
4505 hasn't been implemented.
4507 The value 11960 is chosen to be just after the SOF token, with a couple of bit times extra
4508 for possible bit stuffing. */
4510 *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
4512 #ifdef CONFIG_ETRAX_USB_HOST_PORT1
4513 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
4516 #ifdef CONFIG_ETRAX_USB_HOST_PORT2
4517 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
4520 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4522 /* Configure the USB interface as a host controller. */
4524 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4525 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4526 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
4528 /* Note: Do not reset any ports here. Await the port status interrupts, to have a controlled
4529 sequence of resetting the ports. If we reset both ports now, and there are devices
4530 on both ports, we will get a bus error because both devices will answer the set address
4533 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4535 /* Start processing of USB traffic. */
4537 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4538 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4539 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4541 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4543 usb_rh = usb_alloc_dev(NULL, hc->bus, 0);
4544 hc->bus->root_hub = usb_rh;
4545 usb_rh->state = USB_STATE_ADDRESS;
4546 usb_rh->speed = USB_SPEED_FULL;
4548 hc->bus->devnum_next = 2;
4549 usb_rh->epmaxpacketin[0] = usb_rh->epmaxpacketout[0] = 64;
4550 usb_get_device_descriptor(usb_rh, USB_DT_DEVICE_SIZE);
4551 usb_new_device(usb_rh);
4558 static void etrax_usb_hc_cleanup(void)
4562 free_irq(ETRAX_USB_HC_IRQ, NULL);
4563 free_irq(ETRAX_USB_RX_IRQ, NULL);
4564 free_irq(ETRAX_USB_TX_IRQ, NULL);
4566 usb_deregister_bus(etrax_usb_bus);
4568 /* FIXME: call kmem_cache_destroy here? */
4573 module_init(etrax_usb_hc_init);
4574 module_exit(etrax_usb_hc_cleanup);