[PATCH] timer initialization cleanup: DEFINE_TIMER
[linux-2.6/mini2440.git] / drivers / usb / host / hc_crisv10.c
bloba8267cf17db4685b34da64dd1c4adec99fb5642c
1 /*
2 * usb-host.c: ETRAX 100LX USB Host Controller Driver (HCD)
4 * Copyright (c) 2002, 2003 Axis Communications AB.
5 */
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>
22 #include <asm/io.h>
23 #include <asm/irq.h>
24 #include <asm/dma.h>
25 #include <asm/system.h>
26 #include <asm/arch/svinto.h>
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 $";
41 #undef KERN_DEBUG
42 #define KERN_DEBUG ""
45 #undef USB_DEBUG_RH
46 #undef USB_DEBUG_EPID
47 #undef USB_DEBUG_SB
48 #undef USB_DEBUG_DESC
49 #undef USB_DEBUG_URB
50 #undef USB_DEBUG_TRACE
51 #undef USB_DEBUG_BULK
52 #undef USB_DEBUG_CTRL
53 #undef USB_DEBUG_INTR
54 #undef USB_DEBUG_ISOC
56 #ifdef USB_DEBUG_RH
57 #define dbg_rh(format, arg...) printk(KERN_DEBUG __FILE__ ": (RH) " format "\n" , ## arg)
58 #else
59 #define dbg_rh(format, arg...) do {} while (0)
60 #endif
62 #ifdef USB_DEBUG_EPID
63 #define dbg_epid(format, arg...) printk(KERN_DEBUG __FILE__ ": (EPID) " format "\n" , ## arg)
64 #else
65 #define dbg_epid(format, arg...) do {} while (0)
66 #endif
68 #ifdef USB_DEBUG_SB
69 #define dbg_sb(format, arg...) printk(KERN_DEBUG __FILE__ ": (SB) " format "\n" , ## arg)
70 #else
71 #define dbg_sb(format, arg...) do {} while (0)
72 #endif
74 #ifdef USB_DEBUG_CTRL
75 #define dbg_ctrl(format, arg...) printk(KERN_DEBUG __FILE__ ": (CTRL) " format "\n" , ## arg)
76 #else
77 #define dbg_ctrl(format, arg...) do {} while (0)
78 #endif
80 #ifdef USB_DEBUG_BULK
81 #define dbg_bulk(format, arg...) printk(KERN_DEBUG __FILE__ ": (BULK) " format "\n" , ## arg)
82 #else
83 #define dbg_bulk(format, arg...) do {} while (0)
84 #endif
86 #ifdef USB_DEBUG_INTR
87 #define dbg_intr(format, arg...) printk(KERN_DEBUG __FILE__ ": (INTR) " format "\n" , ## arg)
88 #else
89 #define dbg_intr(format, arg...) do {} while (0)
90 #endif
92 #ifdef USB_DEBUG_ISOC
93 #define dbg_isoc(format, arg...) printk(KERN_DEBUG __FILE__ ": (ISOC) " format "\n" , ## arg)
94 #else
95 #define dbg_isoc(format, arg...) do {} while (0)
96 #endif
98 #ifdef USB_DEBUG_TRACE
99 #define DBFENTER (printk(": Entering: %s\n", __FUNCTION__))
100 #define DBFEXIT (printk(": Exiting: %s\n", __FUNCTION__))
101 #else
102 #define DBFENTER do {} while (0)
103 #define DBFEXIT do {} while (0)
104 #endif
106 #define usb_pipeslow(pipe) (((pipe) >> 26) & 1)
108 /*-------------------------------------------------------------------
109 Virtual Root Hub
110 -------------------------------------------------------------------*/
112 static __u8 root_hub_dev_des[] =
114 0x12, /* __u8 bLength; */
115 0x01, /* __u8 bDescriptorType; Device */
116 0x00, /* __le16 bcdUSB; v1.0 */
117 0x01,
118 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
119 0x00, /* __u8 bDeviceSubClass; */
120 0x00, /* __u8 bDeviceProtocol; */
121 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
122 0x00, /* __le16 idVendor; */
123 0x00,
124 0x00, /* __le16 idProduct; */
125 0x00,
126 0x00, /* __le16 bcdDevice; */
127 0x00,
128 0x00, /* __u8 iManufacturer; */
129 0x02, /* __u8 iProduct; */
130 0x01, /* __u8 iSerialNumber; */
131 0x01 /* __u8 bNumConfigurations; */
134 /* Configuration descriptor */
135 static __u8 root_hub_config_des[] =
137 0x09, /* __u8 bLength; */
138 0x02, /* __u8 bDescriptorType; Configuration */
139 0x19, /* __le16 wTotalLength; */
140 0x00,
141 0x01, /* __u8 bNumInterfaces; */
142 0x01, /* __u8 bConfigurationValue; */
143 0x00, /* __u8 iConfiguration; */
144 0x40, /* __u8 bmAttributes; Bit 7: Bus-powered */
145 0x00, /* __u8 MaxPower; */
147 /* interface */
148 0x09, /* __u8 if_bLength; */
149 0x04, /* __u8 if_bDescriptorType; Interface */
150 0x00, /* __u8 if_bInterfaceNumber; */
151 0x00, /* __u8 if_bAlternateSetting; */
152 0x01, /* __u8 if_bNumEndpoints; */
153 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
154 0x00, /* __u8 if_bInterfaceSubClass; */
155 0x00, /* __u8 if_bInterfaceProtocol; */
156 0x00, /* __u8 if_iInterface; */
158 /* endpoint */
159 0x07, /* __u8 ep_bLength; */
160 0x05, /* __u8 ep_bDescriptorType; Endpoint */
161 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
162 0x03, /* __u8 ep_bmAttributes; Interrupt */
163 0x08, /* __le16 ep_wMaxPacketSize; 8 Bytes */
164 0x00,
165 0xff /* __u8 ep_bInterval; 255 ms */
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; */
174 0x00,
175 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
176 0x00, /* __u8 bHubContrCurrent; 0 mA */
177 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
178 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
181 static DEFINE_TIMER(bulk_start_timer, NULL, 0, 0);
182 static DEFINE_TIMER(bulk_eot_timer, 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) \
203 if (!(expr)) { \
204 err("assert failed at line %d",__LINE__); \
205 while (1); \
211 /* FIXME: Should RX_BUF_SIZE be a config option, or maybe we should adjust it dynamically?
212 To adjust it dynamically we would have to get an interrupt when we reach the end
213 of the rx descriptor list, or when we get close to the end, and then allocate more
214 descriptors. */
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
264 in each frame. */
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
294 protection. */
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)
306 int retval;
308 for (retval = 0; *ascii && utfmax > 1; utfmax -= 2, retval += 2) {
309 *utf++ = *ascii++ & 0x7f;
310 *utf++ = 0;
312 return retval;
315 static int usb_root_hub_string (int id, int serial, char *type, __u8 *data, int len)
317 char buf [30];
319 // assert (len > (2 * (sizeof (buf) + 1)));
320 // assert (strlen (type) <= 8);
322 // language ids
323 if (id == 0) {
324 *data++ = 4; *data++ = 3; /* 4 bytes data */
325 *data++ = 0; *data++ = 0; /* some language id */
326 return 4;
328 // serial number
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"
339 } else
340 return 0;
342 data [0] = 2 + ascii2utf (buf, data + 2, len - 2);
343 data [1] = 3;
344 return data [0];
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;
364 return first_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);
371 assert(urb_entry);
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);
388 assert(urb_entry);
389 assert(urb_entry->urb);
391 if (urb_entry->urb == urb) {
392 return urb_entry;
395 return 0;
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);
402 assert(urb_entry);
404 /* Delete entry and free. */
405 list_del(&urb_entry->list);
406 kfree(urb_entry);
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);
413 assert(urb_entry);
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);
424 assert(urb_entry);
426 if (urb_entry->list.next != &urb_list[epid]) {
427 struct list_head *elem = urb_entry->list.next;
428 urb_entry = list_entry(elem, urb_entry_t, list);
429 return urb_entry->urb;
430 } else {
431 return NULL;
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;
443 int i = 0;
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,
467 unsigned mem_flags, dma_addr_t *dma);
468 static void etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma);
470 static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid);
471 static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid);
472 static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid);
473 static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid);
475 static int etrax_usb_submit_bulk_urb(struct urb *urb);
476 static int etrax_usb_submit_ctrl_urb(struct urb *urb);
477 static int etrax_usb_submit_intr_urb(struct urb *urb);
478 static int etrax_usb_submit_isoc_urb(struct urb *urb);
480 static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags);
481 static int etrax_usb_unlink_urb(struct urb *urb, int status);
482 static int etrax_usb_get_frame_number(struct usb_device *usb_dev);
484 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs);
485 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs);
486 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs);
487 static void etrax_usb_hc_interrupt_bottom_half(void *data);
489 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data);
492 /* The following is a list of interrupt handlers for the host controller interrupts we use.
493 They are called from etrax_usb_hc_interrupt_bottom_half. */
494 static void etrax_usb_hc_isoc_eof_interrupt(void);
495 static void etrax_usb_hc_bulk_eot_interrupt(int timer_induced);
496 static void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg);
497 static void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg);
498 static void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg);
500 static int etrax_rh_submit_urb (struct urb *urb);
502 /* Forward declaration needed because they are used in the rx interrupt routine. */
503 static void etrax_usb_complete_urb(struct urb *urb, int status);
504 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status);
505 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status);
506 static void etrax_usb_complete_intr_urb(struct urb *urb, int status);
507 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status);
509 static int etrax_usb_hc_init(void);
510 static void etrax_usb_hc_cleanup(void);
512 static struct usb_operations etrax_usb_device_operations =
514 .get_frame_number = etrax_usb_get_frame_number,
515 .submit_urb = etrax_usb_submit_urb,
516 .unlink_urb = etrax_usb_unlink_urb,
517 .buffer_alloc = etrax_usb_buffer_alloc,
518 .buffer_free = etrax_usb_buffer_free
521 /* Note that these functions are always available in their "__" variants, for use in
522 error situations. The "__" missing variants are controlled by the USB_DEBUG_DESC/
523 USB_DEBUG_URB macros. */
524 static void __dump_urb(struct urb* purb)
526 printk("\nurb :0x%08lx\n", (unsigned long)purb);
527 printk("dev :0x%08lx\n", (unsigned long)purb->dev);
528 printk("pipe :0x%08x\n", purb->pipe);
529 printk("status :%d\n", purb->status);
530 printk("transfer_flags :0x%08x\n", purb->transfer_flags);
531 printk("transfer_buffer :0x%08lx\n", (unsigned long)purb->transfer_buffer);
532 printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
533 printk("actual_length :%d\n", purb->actual_length);
534 printk("setup_packet :0x%08lx\n", (unsigned long)purb->setup_packet);
535 printk("start_frame :%d\n", purb->start_frame);
536 printk("number_of_packets :%d\n", purb->number_of_packets);
537 printk("interval :%d\n", purb->interval);
538 printk("error_count :%d\n", purb->error_count);
539 printk("context :0x%08lx\n", (unsigned long)purb->context);
540 printk("complete :0x%08lx\n\n", (unsigned long)purb->complete);
543 static void __dump_in_desc(volatile USB_IN_Desc_t *in)
545 printk("\nUSB_IN_Desc at 0x%08lx\n", (unsigned long)in);
546 printk(" sw_len : 0x%04x (%d)\n", in->sw_len, in->sw_len);
547 printk(" command : 0x%04x\n", in->command);
548 printk(" next : 0x%08lx\n", in->next);
549 printk(" buf : 0x%08lx\n", in->buf);
550 printk(" hw_len : 0x%04x (%d)\n", in->hw_len, in->hw_len);
551 printk(" status : 0x%04x\n\n", in->status);
554 static void __dump_sb_desc(volatile USB_SB_Desc_t *sb)
556 char tt = (sb->command & 0x30) >> 4;
557 char *tt_string;
559 switch (tt) {
560 case 0:
561 tt_string = "zout";
562 break;
563 case 1:
564 tt_string = "in";
565 break;
566 case 2:
567 tt_string = "out";
568 break;
569 case 3:
570 tt_string = "setup";
571 break;
572 default:
573 tt_string = "unknown (weird)";
576 printk("\n USB_SB_Desc at 0x%08lx\n", (unsigned long)sb);
577 printk(" command : 0x%04x\n", sb->command);
578 printk(" rem : %d\n", (sb->command & 0x3f00) >> 8);
579 printk(" full : %d\n", (sb->command & 0x40) >> 6);
580 printk(" tt : %d (%s)\n", tt, tt_string);
581 printk(" intr : %d\n", (sb->command & 0x8) >> 3);
582 printk(" eot : %d\n", (sb->command & 0x2) >> 1);
583 printk(" eol : %d\n", sb->command & 0x1);
584 printk(" sw_len : 0x%04x (%d)\n", sb->sw_len, sb->sw_len);
585 printk(" next : 0x%08lx\n", sb->next);
586 printk(" buf : 0x%08lx\n\n", sb->buf);
590 static void __dump_ep_desc(volatile USB_EP_Desc_t *ep)
592 printk("\nUSB_EP_Desc at 0x%08lx\n", (unsigned long)ep);
593 printk(" command : 0x%04x\n", ep->command);
594 printk(" ep_id : %d\n", (ep->command & 0x1f00) >> 8);
595 printk(" enable : %d\n", (ep->command & 0x10) >> 4);
596 printk(" intr : %d\n", (ep->command & 0x8) >> 3);
597 printk(" eof : %d\n", (ep->command & 0x2) >> 1);
598 printk(" eol : %d\n", ep->command & 0x1);
599 printk(" hw_len : 0x%04x (%d)\n", ep->hw_len, ep->hw_len);
600 printk(" next : 0x%08lx\n", ep->next);
601 printk(" sub : 0x%08lx\n\n", ep->sub);
604 static inline void __dump_ep_list(int pipe_type)
606 volatile USB_EP_Desc_t *ep;
607 volatile USB_EP_Desc_t *first_ep;
608 volatile USB_SB_Desc_t *sb;
610 switch (pipe_type)
612 case PIPE_BULK:
613 first_ep = &TxBulkEPList[0];
614 break;
615 case PIPE_CONTROL:
616 first_ep = &TxCtrlEPList[0];
617 break;
618 case PIPE_INTERRUPT:
619 first_ep = &TxIntrEPList[0];
620 break;
621 case PIPE_ISOCHRONOUS:
622 first_ep = &TxIsocEPList[0];
623 break;
624 default:
625 warn("Cannot dump unknown traffic type");
626 return;
628 ep = first_ep;
630 printk("\n\nDumping EP list...\n\n");
632 do {
633 __dump_ep_desc(ep);
634 /* Cannot phys_to_virt on 0 as it turns into 80000000, which is != 0. */
635 sb = ep->sub ? phys_to_virt(ep->sub) : 0;
636 while (sb) {
637 __dump_sb_desc(sb);
638 sb = sb->next ? phys_to_virt(sb->next) : 0;
640 ep = (volatile USB_EP_Desc_t *)(phys_to_virt(ep->next));
642 } while (ep != first_ep);
645 static inline void __dump_ept_data(int epid)
647 unsigned long flags;
648 __u32 r_usb_ept_data;
650 if (epid < 0 || epid > 31) {
651 printk("Cannot dump ept data for invalid epid %d\n", epid);
652 return;
655 save_flags(flags);
656 cli();
657 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
658 nop();
659 r_usb_ept_data = *R_USB_EPT_DATA;
660 restore_flags(flags);
662 printk("\nR_USB_EPT_DATA = 0x%x for epid %d :\n", r_usb_ept_data, epid);
663 if (r_usb_ept_data == 0) {
664 /* No need for more detailed printing. */
665 return;
667 printk(" valid : %d\n", (r_usb_ept_data & 0x80000000) >> 31);
668 printk(" hold : %d\n", (r_usb_ept_data & 0x40000000) >> 30);
669 printk(" error_count_in : %d\n", (r_usb_ept_data & 0x30000000) >> 28);
670 printk(" t_in : %d\n", (r_usb_ept_data & 0x08000000) >> 27);
671 printk(" low_speed : %d\n", (r_usb_ept_data & 0x04000000) >> 26);
672 printk(" port : %d\n", (r_usb_ept_data & 0x03000000) >> 24);
673 printk(" error_code : %d\n", (r_usb_ept_data & 0x00c00000) >> 22);
674 printk(" t_out : %d\n", (r_usb_ept_data & 0x00200000) >> 21);
675 printk(" error_count_out : %d\n", (r_usb_ept_data & 0x00180000) >> 19);
676 printk(" max_len : %d\n", (r_usb_ept_data & 0x0003f800) >> 11);
677 printk(" ep : %d\n", (r_usb_ept_data & 0x00000780) >> 7);
678 printk(" dev : %d\n", (r_usb_ept_data & 0x0000003f));
681 static inline void __dump_ept_data_list(void)
683 int i;
685 printk("Dumping the whole R_USB_EPT_DATA list\n");
687 for (i = 0; i < 32; i++) {
688 __dump_ept_data(i);
691 #ifdef USB_DEBUG_DESC
692 #define dump_in_desc(...) __dump_in_desc(...)
693 #define dump_sb_desc(...) __dump_sb_desc(...)
694 #define dump_ep_desc(...) __dump_ep_desc(...)
695 #else
696 #define dump_in_desc(...) do {} while (0)
697 #define dump_sb_desc(...) do {} while (0)
698 #define dump_ep_desc(...) do {} while (0)
699 #endif
701 #ifdef USB_DEBUG_URB
702 #define dump_urb(x) __dump_urb(x)
703 #else
704 #define dump_urb(x) do {} while (0)
705 #endif
707 static void init_rx_buffers(void)
709 int i;
711 DBFENTER;
713 for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
714 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
715 RxDescList[i].command = 0;
716 RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
717 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
718 RxDescList[i].hw_len = 0;
719 RxDescList[i].status = 0;
721 /* DMA IN cache bug. (struct etrax_dma_descr has the same layout as USB_IN_Desc
722 for the relevant fields.) */
723 prepare_rx_descriptor((struct etrax_dma_descr*)&RxDescList[i]);
727 RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
728 RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
729 RxDescList[i].next = virt_to_phys(&RxDescList[0]);
730 RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
731 RxDescList[i].hw_len = 0;
732 RxDescList[i].status = 0;
734 myNextRxDesc = &RxDescList[0];
735 myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
736 myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
738 *R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
739 *R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
741 DBFEXIT;
744 static void init_tx_bulk_ep(void)
746 int i;
748 DBFENTER;
750 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
751 CHECK_ALIGN(&TxBulkEPList[i]);
752 TxBulkEPList[i].hw_len = 0;
753 TxBulkEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
754 TxBulkEPList[i].sub = 0;
755 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[i + 1]);
757 /* Initiate two EPs, disabled and with the eol flag set. No need for any
758 preserved epid. */
760 /* The first one has the intr flag set so we get an interrupt when the DMA
761 channel is about to become disabled. */
762 CHECK_ALIGN(&TxBulkDummyEPList[i][0]);
763 TxBulkDummyEPList[i][0].hw_len = 0;
764 TxBulkDummyEPList[i][0].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
765 IO_STATE(USB_EP_command, eol, yes) |
766 IO_STATE(USB_EP_command, intr, yes));
767 TxBulkDummyEPList[i][0].sub = 0;
768 TxBulkDummyEPList[i][0].next = virt_to_phys(&TxBulkDummyEPList[i][1]);
770 /* The second one. */
771 CHECK_ALIGN(&TxBulkDummyEPList[i][1]);
772 TxBulkDummyEPList[i][1].hw_len = 0;
773 TxBulkDummyEPList[i][1].command = (IO_FIELD(USB_EP_command, epid, DUMMY_EPID) |
774 IO_STATE(USB_EP_command, eol, yes));
775 TxBulkDummyEPList[i][1].sub = 0;
776 /* The last dummy's next pointer is the same as the current EP's next pointer. */
777 TxBulkDummyEPList[i][1].next = virt_to_phys(&TxBulkEPList[i + 1]);
780 /* Configure the last one. */
781 CHECK_ALIGN(&TxBulkEPList[i]);
782 TxBulkEPList[i].hw_len = 0;
783 TxBulkEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
784 IO_FIELD(USB_EP_command, epid, i));
785 TxBulkEPList[i].sub = 0;
786 TxBulkEPList[i].next = virt_to_phys(&TxBulkEPList[0]);
788 /* No need configuring dummy EPs for the last one as it will never be used for
789 bulk traffic (i == INVALD_EPID at this point). */
791 /* Set up to start on the last EP so we will enable it when inserting traffic
792 for the first time (imitating the situation where the DMA has stopped
793 because there was no more traffic). */
794 *R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[i]);
795 /* No point in starting the bulk channel yet.
796 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start); */
797 DBFEXIT;
800 static void init_tx_ctrl_ep(void)
802 int i;
804 DBFENTER;
806 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
807 CHECK_ALIGN(&TxCtrlEPList[i]);
808 TxCtrlEPList[i].hw_len = 0;
809 TxCtrlEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
810 TxCtrlEPList[i].sub = 0;
811 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[i + 1]);
814 CHECK_ALIGN(&TxCtrlEPList[i]);
815 TxCtrlEPList[i].hw_len = 0;
816 TxCtrlEPList[i].command = (IO_STATE(USB_EP_command, eol, yes) |
817 IO_FIELD(USB_EP_command, epid, i));
819 TxCtrlEPList[i].sub = 0;
820 TxCtrlEPList[i].next = virt_to_phys(&TxCtrlEPList[0]);
822 *R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[0]);
823 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
825 DBFEXIT;
829 static void init_tx_intr_ep(void)
831 int i;
833 DBFENTER;
835 /* Read comment at zout_buffer declaration for an explanation to this. */
836 TxIntrSB_zout.sw_len = 1;
837 TxIntrSB_zout.next = 0;
838 TxIntrSB_zout.buf = virt_to_phys(&zout_buffer[0]);
839 TxIntrSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
840 IO_STATE(USB_SB_command, tt, zout) |
841 IO_STATE(USB_SB_command, full, yes) |
842 IO_STATE(USB_SB_command, eot, yes) |
843 IO_STATE(USB_SB_command, eol, yes));
845 for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
846 CHECK_ALIGN(&TxIntrEPList[i]);
847 TxIntrEPList[i].hw_len = 0;
848 TxIntrEPList[i].command =
849 (IO_STATE(USB_EP_command, eof, yes) |
850 IO_STATE(USB_EP_command, enable, yes) |
851 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
852 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
853 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[i + 1]);
856 CHECK_ALIGN(&TxIntrEPList[i]);
857 TxIntrEPList[i].hw_len = 0;
858 TxIntrEPList[i].command =
859 (IO_STATE(USB_EP_command, eof, yes) |
860 IO_STATE(USB_EP_command, eol, yes) |
861 IO_STATE(USB_EP_command, enable, yes) |
862 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
863 TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
864 TxIntrEPList[i].next = virt_to_phys(&TxIntrEPList[0]);
866 *R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
867 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
868 DBFEXIT;
871 static void init_tx_isoc_ep(void)
873 int i;
875 DBFENTER;
877 /* Read comment at zout_buffer declaration for an explanation to this. */
878 TxIsocSB_zout.sw_len = 1;
879 TxIsocSB_zout.next = 0;
880 TxIsocSB_zout.buf = virt_to_phys(&zout_buffer[0]);
881 TxIsocSB_zout.command = (IO_FIELD(USB_SB_command, rem, 0) |
882 IO_STATE(USB_SB_command, tt, zout) |
883 IO_STATE(USB_SB_command, full, yes) |
884 IO_STATE(USB_SB_command, eot, yes) |
885 IO_STATE(USB_SB_command, eol, yes));
887 /* The last isochronous EP descriptor is a dummy. */
889 for (i = 0; i < (NBR_OF_EPIDS - 1); i++) {
890 CHECK_ALIGN(&TxIsocEPList[i]);
891 TxIsocEPList[i].hw_len = 0;
892 TxIsocEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
893 TxIsocEPList[i].sub = 0;
894 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[i + 1]);
897 CHECK_ALIGN(&TxIsocEPList[i]);
898 TxIsocEPList[i].hw_len = 0;
900 /* Must enable the last EP descr to get eof interrupt. */
901 TxIsocEPList[i].command = (IO_STATE(USB_EP_command, enable, yes) |
902 IO_STATE(USB_EP_command, eof, yes) |
903 IO_STATE(USB_EP_command, eol, yes) |
904 IO_FIELD(USB_EP_command, epid, INVALID_EPID));
905 TxIsocEPList[i].sub = virt_to_phys(&TxIsocSB_zout);
906 TxIsocEPList[i].next = virt_to_phys(&TxIsocEPList[0]);
908 *R_DMA_CH8_SUB3_EP = virt_to_phys(&TxIsocEPList[0]);
909 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
911 DBFEXIT;
914 static void etrax_usb_unlink_intr_urb(struct urb *urb)
916 volatile USB_EP_Desc_t *first_ep; /* First EP in the list. */
917 volatile USB_EP_Desc_t *curr_ep; /* Current EP, the iterator. */
918 volatile USB_EP_Desc_t *next_ep; /* The EP after current. */
919 volatile USB_EP_Desc_t *unlink_ep; /* The one we should remove from the list. */
921 int epid;
923 /* Read 8.8.4 in Designer's Reference, "Removing an EP Descriptor from the List". */
925 DBFENTER;
927 epid = ((etrax_urb_priv_t *)urb->hcpriv)->epid;
929 first_ep = &TxIntrEPList[0];
930 curr_ep = first_ep;
933 /* Note that this loop removes all EP descriptors with this epid. This assumes
934 that all EP descriptors belong to the one and only urb for this epid. */
936 do {
937 next_ep = (USB_EP_Desc_t *)phys_to_virt(curr_ep->next);
939 if (IO_EXTRACT(USB_EP_command, epid, next_ep->command) == epid) {
941 dbg_intr("Found EP to unlink for epid %d", epid);
943 /* This is the one we should unlink. */
944 unlink_ep = next_ep;
946 /* Actually unlink the EP from the DMA list. */
947 curr_ep->next = unlink_ep->next;
949 /* Wait until the DMA is no longer at this descriptor. */
950 while (*R_DMA_CH8_SUB2_EP == virt_to_phys(unlink_ep));
952 /* Now we are free to remove it and its SB descriptor.
953 Note that it is assumed here that there is only one sb in the
954 sb list for this ep. */
955 kmem_cache_free(usb_desc_cache, phys_to_virt(unlink_ep->sub));
956 kmem_cache_free(usb_desc_cache, (USB_EP_Desc_t *)unlink_ep);
959 curr_ep = phys_to_virt(curr_ep->next);
961 } while (curr_ep != first_ep);
962 urb->hcpriv = NULL;
965 void etrax_usb_do_intr_recover(int epid)
967 USB_EP_Desc_t *first_ep, *tmp_ep;
969 DBFENTER;
971 first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
972 tmp_ep = first_ep;
974 /* What this does is simply to walk the list of interrupt
975 ep descriptors and enable those that are disabled. */
977 do {
978 if (IO_EXTRACT(USB_EP_command, epid, tmp_ep->command) == epid &&
979 !(tmp_ep->command & IO_MASK(USB_EP_command, enable))) {
980 tmp_ep->command |= IO_STATE(USB_EP_command, enable, yes);
983 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
985 } while (tmp_ep != first_ep);
988 DBFEXIT;
991 static int etrax_rh_unlink_urb (struct urb *urb)
993 etrax_hc_t *hc;
995 DBFENTER;
997 hc = urb->dev->bus->hcpriv;
999 if (hc->rh.urb == urb) {
1000 hc->rh.send = 0;
1001 del_timer(&hc->rh.rh_int_timer);
1004 DBFEXIT;
1005 return 0;
1008 static void etrax_rh_send_irq(struct urb *urb)
1010 __u16 data = 0;
1011 etrax_hc_t *hc = urb->dev->bus->hcpriv;
1012 DBFENTER;
1015 dbg_rh("R_USB_FM_NUMBER : 0x%08X", *R_USB_FM_NUMBER);
1016 dbg_rh("R_USB_FM_REMAINING: 0x%08X", *R_USB_FM_REMAINING);
1019 data |= (hc->rh.wPortChange_1) ? (1 << 1) : 0;
1020 data |= (hc->rh.wPortChange_2) ? (1 << 2) : 0;
1022 *((__u16 *)urb->transfer_buffer) = cpu_to_le16(data);
1023 /* FIXME: Why is actual_length set to 1 when data is 2 bytes?
1024 Since only 1 byte is used, why not declare data as __u8? */
1025 urb->actual_length = 1;
1026 urb->status = 0;
1028 if (hc->rh.send && urb->complete) {
1029 dbg_rh("wPortChange_1: 0x%04X", hc->rh.wPortChange_1);
1030 dbg_rh("wPortChange_2: 0x%04X", hc->rh.wPortChange_2);
1032 urb->complete(urb, NULL);
1035 DBFEXIT;
1038 static void etrax_rh_init_int_timer(struct urb *urb)
1040 etrax_hc_t *hc;
1042 DBFENTER;
1044 hc = urb->dev->bus->hcpriv;
1045 hc->rh.interval = urb->interval;
1046 init_timer(&hc->rh.rh_int_timer);
1047 hc->rh.rh_int_timer.function = etrax_rh_int_timer_do;
1048 hc->rh.rh_int_timer.data = (unsigned long)urb;
1049 /* FIXME: Is the jiffies resolution enough? All intervals < 10 ms will be mapped
1050 to 0, and the rest to the nearest lower 10 ms. */
1051 hc->rh.rh_int_timer.expires = jiffies + ((HZ * hc->rh.interval) / 1000);
1052 add_timer(&hc->rh.rh_int_timer);
1054 DBFEXIT;
1057 static void etrax_rh_int_timer_do(unsigned long ptr)
1059 struct urb *urb;
1060 etrax_hc_t *hc;
1062 DBFENTER;
1064 urb = (struct urb*)ptr;
1065 hc = urb->dev->bus->hcpriv;
1067 if (hc->rh.send) {
1068 etrax_rh_send_irq(urb);
1071 DBFEXIT;
1074 static int etrax_usb_setup_epid(struct urb *urb)
1076 int epid;
1077 char devnum, endpoint, out_traffic, slow;
1078 int maxlen;
1079 unsigned long flags;
1081 DBFENTER;
1083 epid = etrax_usb_lookup_epid(urb);
1084 if ((epid != -1)){
1085 /* An epid that fits this urb has been found. */
1086 DBFEXIT;
1087 return epid;
1090 /* We must find and initiate a new epid for this urb. */
1091 epid = etrax_usb_allocate_epid();
1093 if (epid == -1) {
1094 /* Failed to allocate a new epid. */
1095 DBFEXIT;
1096 return epid;
1099 /* We now have a new epid to use. Initiate it. */
1100 set_bit(epid, (void *)&epid_usage_bitmask);
1102 devnum = usb_pipedevice(urb->pipe);
1103 endpoint = usb_pipeendpoint(urb->pipe);
1104 slow = usb_pipeslow(urb->pipe);
1105 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1106 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1107 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1108 out_traffic = 1;
1109 } else {
1110 out_traffic = usb_pipeout(urb->pipe);
1113 save_flags(flags);
1114 cli();
1116 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1117 nop();
1119 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1120 *R_USB_EPT_DATA_ISO = IO_STATE(R_USB_EPT_DATA_ISO, valid, yes) |
1121 /* FIXME: Change any to the actual port? */
1122 IO_STATE(R_USB_EPT_DATA_ISO, port, any) |
1123 IO_FIELD(R_USB_EPT_DATA_ISO, max_len, maxlen) |
1124 IO_FIELD(R_USB_EPT_DATA_ISO, ep, endpoint) |
1125 IO_FIELD(R_USB_EPT_DATA_ISO, dev, devnum);
1126 } else {
1127 *R_USB_EPT_DATA = IO_STATE(R_USB_EPT_DATA, valid, yes) |
1128 IO_FIELD(R_USB_EPT_DATA, low_speed, slow) |
1129 /* FIXME: Change any to the actual port? */
1130 IO_STATE(R_USB_EPT_DATA, port, any) |
1131 IO_FIELD(R_USB_EPT_DATA, max_len, maxlen) |
1132 IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
1133 IO_FIELD(R_USB_EPT_DATA, dev, devnum);
1136 restore_flags(flags);
1138 if (out_traffic) {
1139 set_bit(epid, (void *)&epid_out_traffic);
1140 } else {
1141 clear_bit(epid, (void *)&epid_out_traffic);
1144 dbg_epid("Setting up epid %d with devnum %d, endpoint %d and max_len %d (%s)",
1145 epid, devnum, endpoint, maxlen, out_traffic ? "OUT" : "IN");
1147 DBFEXIT;
1148 return epid;
1151 static void etrax_usb_free_epid(int epid)
1153 unsigned long flags;
1155 DBFENTER;
1157 if (!test_bit(epid, (void *)&epid_usage_bitmask)) {
1158 warn("Trying to free unused epid %d", epid);
1159 DBFEXIT;
1160 return;
1163 save_flags(flags);
1164 cli();
1166 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1167 nop();
1168 while (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold));
1169 /* This will, among other things, set the valid field to 0. */
1170 *R_USB_EPT_DATA = 0;
1171 restore_flags(flags);
1173 clear_bit(epid, (void *)&epid_usage_bitmask);
1176 dbg_epid("Freed epid %d", epid);
1178 DBFEXIT;
1181 static int etrax_usb_lookup_epid(struct urb *urb)
1183 int i;
1184 __u32 data;
1185 char devnum, endpoint, slow, out_traffic;
1186 int maxlen;
1187 unsigned long flags;
1189 DBFENTER;
1191 devnum = usb_pipedevice(urb->pipe);
1192 endpoint = usb_pipeendpoint(urb->pipe);
1193 slow = usb_pipeslow(urb->pipe);
1194 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
1195 if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1196 /* We want both IN and OUT control traffic to be put on the same EP/SB list. */
1197 out_traffic = 1;
1198 } else {
1199 out_traffic = usb_pipeout(urb->pipe);
1202 /* Step through att epids. */
1203 for (i = 0; i < NBR_OF_EPIDS; i++) {
1204 if (test_bit(i, (void *)&epid_usage_bitmask) &&
1205 test_bit(i, (void *)&epid_out_traffic) == out_traffic) {
1207 save_flags(flags);
1208 cli();
1209 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, i);
1210 nop();
1212 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1213 data = *R_USB_EPT_DATA_ISO;
1214 restore_flags(flags);
1216 if ((IO_MASK(R_USB_EPT_DATA_ISO, valid) & data) &&
1217 (IO_EXTRACT(R_USB_EPT_DATA_ISO, dev, data) == devnum) &&
1218 (IO_EXTRACT(R_USB_EPT_DATA_ISO, ep, data) == endpoint) &&
1219 (IO_EXTRACT(R_USB_EPT_DATA_ISO, max_len, data) == maxlen)) {
1220 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1221 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1222 DBFEXIT;
1223 return i;
1225 } else {
1226 data = *R_USB_EPT_DATA;
1227 restore_flags(flags);
1229 if ((IO_MASK(R_USB_EPT_DATA, valid) & data) &&
1230 (IO_EXTRACT(R_USB_EPT_DATA, dev, data) == devnum) &&
1231 (IO_EXTRACT(R_USB_EPT_DATA, ep, data) == endpoint) &&
1232 (IO_EXTRACT(R_USB_EPT_DATA, low_speed, data) == slow) &&
1233 (IO_EXTRACT(R_USB_EPT_DATA, max_len, data) == maxlen)) {
1234 dbg_epid("Found epid %d for devnum %d, endpoint %d (%s)",
1235 i, devnum, endpoint, out_traffic ? "OUT" : "IN");
1236 DBFEXIT;
1237 return i;
1243 DBFEXIT;
1244 return -1;
1247 static int etrax_usb_allocate_epid(void)
1249 int i;
1251 DBFENTER;
1253 for (i = 0; i < NBR_OF_EPIDS; i++) {
1254 if (!test_bit(i, (void *)&epid_usage_bitmask)) {
1255 dbg_epid("Found free epid %d", i);
1256 DBFEXIT;
1257 return i;
1261 dbg_epid("Found no free epids");
1262 DBFEXIT;
1263 return -1;
1266 static int etrax_usb_submit_urb(struct urb *urb, unsigned mem_flags)
1268 etrax_hc_t *hc;
1269 int ret = -EINVAL;
1271 DBFENTER;
1273 if (!urb->dev || !urb->dev->bus) {
1274 return -ENODEV;
1276 if (usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)) <= 0) {
1277 info("Submit urb to pipe with maxpacketlen 0, pipe 0x%X\n", urb->pipe);
1278 return -EMSGSIZE;
1281 if (urb->timeout) {
1282 /* FIXME. */
1283 warn("urb->timeout specified, ignoring.");
1286 hc = (etrax_hc_t*)urb->dev->bus->hcpriv;
1288 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1289 /* This request is for the Virtual Root Hub. */
1290 ret = etrax_rh_submit_urb(urb);
1292 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1294 ret = etrax_usb_submit_bulk_urb(urb);
1296 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1298 ret = etrax_usb_submit_ctrl_urb(urb);
1300 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1301 int bustime;
1303 if (urb->bandwidth == 0) {
1304 bustime = usb_check_bandwidth(urb->dev, urb);
1305 if (bustime < 0) {
1306 ret = bustime;
1307 } else {
1308 ret = etrax_usb_submit_intr_urb(urb);
1309 if (ret == 0)
1310 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1312 } else {
1313 /* Bandwidth already set. */
1314 ret = etrax_usb_submit_intr_urb(urb);
1317 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1318 int bustime;
1320 if (urb->bandwidth == 0) {
1321 bustime = usb_check_bandwidth(urb->dev, urb);
1322 if (bustime < 0) {
1323 ret = bustime;
1324 } else {
1325 ret = etrax_usb_submit_isoc_urb(urb);
1326 if (ret == 0)
1327 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1329 } else {
1330 /* Bandwidth already set. */
1331 ret = etrax_usb_submit_isoc_urb(urb);
1335 DBFEXIT;
1337 if (ret != 0)
1338 printk("Submit URB error %d\n", ret);
1340 return ret;
1343 static int etrax_usb_unlink_urb(struct urb *urb, int status)
1345 etrax_hc_t *hc;
1346 etrax_urb_priv_t *urb_priv;
1347 int epid;
1348 unsigned int flags;
1350 DBFENTER;
1352 if (!urb) {
1353 return -EINVAL;
1356 /* Disable interrupts here since a descriptor interrupt for the isoc epid
1357 will modify the sb list. This could possibly be done more granular, but
1358 unlink_urb should not be used frequently anyway.
1361 save_flags(flags);
1362 cli();
1364 if (!urb->dev || !urb->dev->bus) {
1365 restore_flags(flags);
1366 return -ENODEV;
1368 if (!urb->hcpriv) {
1369 /* This happens if a device driver calls unlink on an urb that
1370 was never submitted (lazy driver) or if the urb was completed
1371 while unlink was being called. */
1372 restore_flags(flags);
1373 return 0;
1375 if (urb->transfer_flags & URB_ASYNC_UNLINK) {
1376 /* FIXME. */
1377 /* If URB_ASYNC_UNLINK is set:
1378 unlink
1379 move to a separate urb list
1380 call complete at next sof with ECONNRESET
1382 If not:
1383 wait 1 ms
1384 unlink
1385 call complete with ENOENT
1387 warn("URB_ASYNC_UNLINK set, ignoring.");
1390 /* One might think that urb->status = -EINPROGRESS would be a requirement for unlinking,
1391 but that doesn't work for interrupt and isochronous traffic since they are completed
1392 repeatedly, and urb->status is set then. That may in itself be a bug though. */
1394 hc = urb->dev->bus->hcpriv;
1395 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1396 epid = urb_priv->epid;
1398 /* Set the urb status (synchronous unlink). */
1399 urb->status = -ENOENT;
1400 urb_priv->urb_state = UNLINK;
1402 if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1403 int ret;
1404 ret = etrax_rh_unlink_urb(urb);
1405 DBFEXIT;
1406 restore_flags(flags);
1407 return ret;
1409 } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1411 dbg_bulk("Unlink of bulk urb (0x%lx)", (unsigned long)urb);
1413 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1414 /* The EP was enabled, disable it and wait. */
1415 TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1417 /* Ah, the luxury of busy-wait. */
1418 while (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[epid]));
1420 /* Kicking dummy list out of the party. */
1421 TxBulkEPList[epid].next = virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
1423 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1425 dbg_ctrl("Unlink of ctrl urb (0x%lx)", (unsigned long)urb);
1427 if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1428 /* The EP was enabled, disable it and wait. */
1429 TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1431 /* Ah, the luxury of busy-wait. */
1432 while (*R_DMA_CH8_SUB1_EP == virt_to_phys(&TxCtrlEPList[epid]));
1435 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1437 dbg_intr("Unlink of intr urb (0x%lx)", (unsigned long)urb);
1439 /* Separate function because it's a tad more complicated. */
1440 etrax_usb_unlink_intr_urb(urb);
1442 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1444 dbg_isoc("Unlink of isoc urb (0x%lx)", (unsigned long)urb);
1446 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1447 /* The EP was enabled, disable it and wait. */
1448 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1450 /* Ah, the luxury of busy-wait. */
1451 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
1455 /* Note that we need to remove the urb from the urb list *before* removing its SB
1456 descriptors. (This means that the isoc eof handler might get a null urb when we
1457 are unlinking the last urb.) */
1459 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1461 urb_list_del(urb, epid);
1462 TxBulkEPList[epid].sub = 0;
1463 etrax_remove_from_sb_list(urb);
1465 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1467 urb_list_del(urb, epid);
1468 TxCtrlEPList[epid].sub = 0;
1469 etrax_remove_from_sb_list(urb);
1471 } else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1473 urb_list_del(urb, epid);
1474 /* Sanity check (should never happen). */
1475 assert(urb_list_empty(epid));
1477 /* Release allocated bandwidth. */
1478 usb_release_bandwidth(urb->dev, urb, 0);
1480 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1482 if (usb_pipeout(urb->pipe)) {
1484 USB_SB_Desc_t *iter_sb, *prev_sb, *next_sb;
1486 if (__urb_list_entry(urb, epid)) {
1488 urb_list_del(urb, epid);
1489 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1490 prev_sb = 0;
1491 while (iter_sb && (iter_sb != urb_priv->first_sb)) {
1492 prev_sb = iter_sb;
1493 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1496 if (iter_sb == 0) {
1497 /* Unlink of the URB currently being transmitted. */
1498 prev_sb = 0;
1499 iter_sb = TxIsocEPList[epid].sub ? phys_to_virt(TxIsocEPList[epid].sub) : 0;
1502 while (iter_sb && (iter_sb != urb_priv->last_sb)) {
1503 iter_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1505 if (iter_sb) {
1506 next_sb = iter_sb->next ? phys_to_virt(iter_sb->next) : 0;
1507 } else {
1508 /* This should only happen if the DMA has completed
1509 processing the SB list for this EP while interrupts
1510 are disabled. */
1511 dbg_isoc("Isoc urb not found, already sent?");
1512 next_sb = 0;
1514 if (prev_sb) {
1515 prev_sb->next = next_sb ? virt_to_phys(next_sb) : 0;
1516 } else {
1517 TxIsocEPList[epid].sub = next_sb ? virt_to_phys(next_sb) : 0;
1520 etrax_remove_from_sb_list(urb);
1521 if (urb_list_empty(epid)) {
1522 TxIsocEPList[epid].sub = 0;
1523 dbg_isoc("Last isoc out urb epid %d", epid);
1524 } else if (next_sb || prev_sb) {
1525 dbg_isoc("Re-enable isoc out epid %d", epid);
1527 TxIsocEPList[epid].hw_len = 0;
1528 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1529 } else {
1530 TxIsocEPList[epid].sub = 0;
1531 dbg_isoc("URB list non-empty and no SB list, EP disabled");
1533 } else {
1534 dbg_isoc("Urb 0x%p not found, completed already?", urb);
1536 } else {
1538 urb_list_del(urb, epid);
1540 /* For in traffic there is only one SB descriptor for each EP even
1541 though there may be several urbs (all urbs point at the same SB). */
1542 if (urb_list_empty(epid)) {
1543 /* No more urbs, remove the SB. */
1544 TxIsocEPList[epid].sub = 0;
1545 etrax_remove_from_sb_list(urb);
1546 } else {
1547 TxIsocEPList[epid].hw_len = 0;
1548 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1551 /* Release allocated bandwidth. */
1552 usb_release_bandwidth(urb->dev, urb, 1);
1554 /* Free the epid if urb list is empty. */
1555 if (urb_list_empty(epid)) {
1556 etrax_usb_free_epid(epid);
1558 restore_flags(flags);
1560 /* Must be done before calling completion handler. */
1561 kfree(urb_priv);
1562 urb->hcpriv = 0;
1564 if (urb->complete) {
1565 urb->complete(urb, NULL);
1568 DBFEXIT;
1569 return 0;
1572 static int etrax_usb_get_frame_number(struct usb_device *usb_dev)
1574 DBFENTER;
1575 DBFEXIT;
1576 return (*R_USB_FM_NUMBER & 0x7ff);
1579 static irqreturn_t etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1581 DBFENTER;
1583 /* This interrupt handler could be used when unlinking EP descriptors. */
1585 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
1586 USB_EP_Desc_t *ep;
1588 //dbg_bulk("dma8_sub0_descr (BULK) intr.");
1590 /* It should be safe clearing the interrupt here, since we don't expect to get a new
1591 one until we restart the bulk channel. */
1592 *R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
1594 /* Wait while the DMA is running (though we don't expect it to be). */
1595 while (*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd));
1597 /* Advance the DMA to the next EP descriptor. */
1598 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
1600 //dbg_bulk("descr intr: DMA is at 0x%lx", (unsigned long)ep);
1602 /* ep->next is already a physical address; no need for a virt_to_phys. */
1603 *R_DMA_CH8_SUB0_EP = ep->next;
1605 /* Start the DMA bulk channel again. */
1606 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1608 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
1609 struct urb *urb;
1610 int epid;
1611 etrax_urb_priv_t *urb_priv;
1612 unsigned long int flags;
1614 dbg_ctrl("dma8_sub1_descr (CTRL) intr.");
1615 *R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
1617 /* The complete callback gets called so we cli. */
1618 save_flags(flags);
1619 cli();
1621 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1622 if ((TxCtrlEPList[epid].sub == 0) ||
1623 (epid == DUMMY_EPID) ||
1624 (epid == INVALID_EPID)) {
1625 /* Nothing here to see. */
1626 continue;
1629 /* Get the first urb (if any). */
1630 urb = urb_list_first(epid);
1632 if (urb) {
1634 /* Sanity check. */
1635 assert(usb_pipetype(urb->pipe) == PIPE_CONTROL);
1637 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1638 assert(urb_priv);
1640 if (urb_priv->urb_state == WAITING_FOR_DESCR_INTR) {
1641 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
1643 etrax_usb_complete_urb(urb, 0);
1647 restore_flags(flags);
1649 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
1650 dbg_intr("dma8_sub2_descr (INTR) intr.");
1651 *R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
1653 if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
1654 struct urb *urb;
1655 int epid;
1656 int epid_done;
1657 etrax_urb_priv_t *urb_priv;
1658 USB_SB_Desc_t *sb_desc;
1660 usb_isoc_complete_data_t *comp_data = NULL;
1662 /* One or more isoc out transfers are done. */
1663 dbg_isoc("dma8_sub3_descr (ISOC) intr.");
1665 /* For each isoc out EP search for the first sb_desc with the intr flag
1666 set. This descriptor must be the last packet from an URB. Then
1667 traverse the URB list for the EP until the URB with urb_priv->last_sb
1668 matching the intr-marked sb_desc is found. All URBs before this have
1669 been sent.
1672 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1673 /* Skip past epids with no SB lists, epids used for in traffic,
1674 and special (dummy, invalid) epids. */
1675 if ((TxIsocEPList[epid].sub == 0) ||
1676 (test_bit(epid, (void *)&epid_out_traffic) == 0) ||
1677 (epid == DUMMY_EPID) ||
1678 (epid == INVALID_EPID)) {
1679 /* Nothing here to see. */
1680 continue;
1682 sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
1684 /* Find the last descriptor of the currently active URB for this ep.
1685 This is the first descriptor in the sub list marked for a descriptor
1686 interrupt. */
1687 while (sb_desc && !IO_EXTRACT(USB_SB_command, intr, sb_desc->command)) {
1688 sb_desc = sb_desc->next ? phys_to_virt(sb_desc->next) : 0;
1690 assert(sb_desc);
1692 dbg_isoc("Check epid %d, sub 0x%p, SB 0x%p",
1693 epid,
1694 phys_to_virt(TxIsocEPList[epid].sub),
1695 sb_desc);
1697 epid_done = 0;
1699 /* Get the first urb (if any). */
1700 urb = urb_list_first(epid);
1701 assert(urb);
1703 while (urb && !epid_done) {
1705 /* Sanity check. */
1706 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
1708 if (!usb_pipeout(urb->pipe)) {
1709 /* descr interrupts are generated only for out pipes. */
1710 epid_done = 1;
1711 continue;
1714 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1715 assert(urb_priv);
1717 if (sb_desc != urb_priv->last_sb) {
1719 /* This urb has been sent. */
1720 dbg_isoc("out URB 0x%p sent", urb);
1722 urb_priv->urb_state = TRANSFER_DONE;
1724 } else if ((sb_desc == urb_priv->last_sb) &&
1725 !(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1727 assert((sb_desc->command & IO_MASK(USB_SB_command, eol)) == IO_STATE(USB_SB_command, eol, yes));
1728 assert(sb_desc->next == 0);
1730 dbg_isoc("out URB 0x%p last in list, epid disabled", urb);
1731 TxIsocEPList[epid].sub = 0;
1732 TxIsocEPList[epid].hw_len = 0;
1733 urb_priv->urb_state = TRANSFER_DONE;
1735 epid_done = 1;
1737 } else {
1738 epid_done = 1;
1740 if (!epid_done) {
1741 urb = urb_list_next(urb, epid);
1747 *R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
1749 comp_data = (usb_isoc_complete_data_t*)kmem_cache_alloc(isoc_compl_cache, SLAB_ATOMIC);
1750 assert(comp_data != NULL);
1752 INIT_WORK(&comp_data->usb_bh, etrax_usb_isoc_descr_interrupt_bottom_half, comp_data);
1753 schedule_work(&comp_data->usb_bh);
1756 DBFEXIT;
1757 return IRQ_HANDLED;
1760 static void etrax_usb_isoc_descr_interrupt_bottom_half(void *data)
1762 usb_isoc_complete_data_t *comp_data = (usb_isoc_complete_data_t*)data;
1764 struct urb *urb;
1765 int epid;
1766 int epid_done;
1767 etrax_urb_priv_t *urb_priv;
1769 DBFENTER;
1771 dbg_isoc("dma8_sub3_descr (ISOC) bottom half.");
1773 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
1774 unsigned long flags;
1776 save_flags(flags);
1777 cli();
1779 epid_done = 0;
1781 /* The descriptor interrupt handler has marked all transmitted isoch. out
1782 URBs with TRANSFER_DONE. Now we traverse all epids and for all that
1783 have isoch. out traffic traverse its URB list and complete the
1784 transmitted URB.
1787 while (!epid_done) {
1789 /* Get the first urb (if any). */
1790 urb = urb_list_first(epid);
1791 if (urb == 0) {
1792 epid_done = 1;
1793 continue;
1796 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
1797 epid_done = 1;
1798 continue;
1801 if (!usb_pipeout(urb->pipe)) {
1802 /* descr interrupts are generated only for out pipes. */
1803 epid_done = 1;
1804 continue;
1807 dbg_isoc("Check epid %d, SB 0x%p", epid, (char*)TxIsocEPList[epid].sub);
1809 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1810 assert(urb_priv);
1812 if (urb_priv->urb_state == TRANSFER_DONE) {
1813 int i;
1814 struct usb_iso_packet_descriptor *packet;
1816 /* This urb has been sent. */
1817 dbg_isoc("Completing isoc out URB 0x%p", urb);
1819 for (i = 0; i < urb->number_of_packets; i++) {
1820 packet = &urb->iso_frame_desc[i];
1821 packet->status = 0;
1822 packet->actual_length = packet->length;
1825 etrax_usb_complete_isoc_urb(urb, 0);
1827 if (urb_list_empty(epid)) {
1828 etrax_usb_free_epid(epid);
1829 epid_done = 1;
1831 } else {
1832 epid_done = 1;
1835 restore_flags(flags);
1838 kmem_cache_free(isoc_compl_cache, comp_data);
1840 DBFEXIT;
1845 static irqreturn_t etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1847 struct urb *urb;
1848 etrax_urb_priv_t *urb_priv;
1849 int epid = 0;
1850 unsigned long flags;
1852 /* Isoc diagnostics. */
1853 static int curr_fm = 0;
1854 static int prev_fm = 0;
1856 DBFENTER;
1858 /* Clear this interrupt. */
1859 *R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
1861 /* Note that this while loop assumes that all packets span only
1862 one rx descriptor. */
1864 /* The reason we cli here is that we call the driver's callback functions. */
1865 save_flags(flags);
1866 cli();
1868 while (myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
1870 epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
1871 urb = urb_list_first(epid);
1873 //printk("eop for epid %d, first urb 0x%lx\n", epid, (unsigned long)urb);
1875 if (!urb) {
1876 err("No urb for epid %d in rx interrupt", epid);
1877 __dump_ept_data(epid);
1878 goto skip_out;
1881 /* Note that we cannot indescriminately assert(usb_pipein(urb->pipe)) since
1882 ctrl pipes are not. */
1884 if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
1885 __u32 r_usb_ept_data;
1886 int no_error = 0;
1888 assert(test_bit(epid, (void *)&epid_usage_bitmask));
1890 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
1891 nop();
1892 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1893 r_usb_ept_data = *R_USB_EPT_DATA_ISO;
1895 if ((r_usb_ept_data & IO_MASK(R_USB_EPT_DATA_ISO, valid)) &&
1896 (IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data) == 0) &&
1897 (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata))) {
1898 /* Not an error, just a failure to receive an expected iso
1899 in packet in this frame. This is not documented
1900 in the designers reference.
1902 no_error++;
1903 } else {
1904 warn("R_USB_EPT_DATA_ISO for epid %d = 0x%x", epid, r_usb_ept_data);
1906 } else {
1907 r_usb_ept_data = *R_USB_EPT_DATA;
1908 warn("R_USB_EPT_DATA for epid %d = 0x%x", epid, r_usb_ept_data);
1911 if (!no_error){
1912 warn("error in rx desc->status, epid %d, first urb = 0x%lx",
1913 epid, (unsigned long)urb);
1914 __dump_in_desc(myNextRxDesc);
1916 warn("R_USB_STATUS = 0x%x", *R_USB_STATUS);
1918 /* Check that ept was disabled when error occurred. */
1919 switch (usb_pipetype(urb->pipe)) {
1920 case PIPE_BULK:
1921 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1922 break;
1923 case PIPE_CONTROL:
1924 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1925 break;
1926 case PIPE_INTERRUPT:
1927 assert(!(TxIntrEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1928 break;
1929 case PIPE_ISOCHRONOUS:
1930 assert(!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)));
1931 break;
1932 default:
1933 warn("etrax_usb_rx_interrupt: bad pipetype %d in urb 0x%p",
1934 usb_pipetype(urb->pipe),
1935 urb);
1937 etrax_usb_complete_urb(urb, -EPROTO);
1938 goto skip_out;
1942 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1943 assert(urb_priv);
1945 if ((usb_pipetype(urb->pipe) == PIPE_BULK) ||
1946 (usb_pipetype(urb->pipe) == PIPE_CONTROL) ||
1947 (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
1949 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
1950 /* We get nodata for empty data transactions, and the rx descriptor's
1951 hw_len field is not valid in that case. No data to copy in other
1952 words. */
1953 } else {
1954 /* Make sure the data fits in the buffer. */
1955 assert(urb_priv->rx_offset + myNextRxDesc->hw_len
1956 <= urb->transfer_buffer_length);
1958 memcpy(urb->transfer_buffer + urb_priv->rx_offset,
1959 phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
1960 urb_priv->rx_offset += myNextRxDesc->hw_len;
1963 if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
1964 if ((usb_pipetype(urb->pipe) == PIPE_CONTROL) &&
1965 ((TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)) ==
1966 IO_STATE(USB_EP_command, enable, yes))) {
1967 /* The EP is still enabled, so the OUT packet used to ack
1968 the in data is probably not processed yet. If the EP
1969 sub pointer has not moved beyond urb_priv->last_sb mark
1970 it for a descriptor interrupt and complete the urb in
1971 the descriptor interrupt handler.
1973 USB_SB_Desc_t *sub = TxCtrlEPList[urb_priv->epid].sub ? phys_to_virt(TxCtrlEPList[urb_priv->epid].sub) : 0;
1975 while ((sub != NULL) && (sub != urb_priv->last_sb)) {
1976 sub = sub->next ? phys_to_virt(sub->next) : 0;
1978 if (sub != NULL) {
1979 /* The urb has not been fully processed. */
1980 urb_priv->urb_state = WAITING_FOR_DESCR_INTR;
1981 } else {
1982 warn("(CTRL) epid enabled and urb (0x%p) processed, ep->sub=0x%p", urb, (char*)TxCtrlEPList[urb_priv->epid].sub);
1983 etrax_usb_complete_urb(urb, 0);
1985 } else {
1986 etrax_usb_complete_urb(urb, 0);
1990 } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1992 struct usb_iso_packet_descriptor *packet;
1994 if (urb_priv->urb_state == UNLINK) {
1995 info("Ignoring rx data for urb being unlinked.");
1996 goto skip_out;
1997 } else if (urb_priv->urb_state == NOT_STARTED) {
1998 info("What? Got rx data for urb that isn't started?");
1999 goto skip_out;
2002 packet = &urb->iso_frame_desc[urb_priv->isoc_packet_counter];
2003 packet->status = 0;
2005 if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
2006 /* We get nodata for empty data transactions, and the rx descriptor's
2007 hw_len field is not valid in that case. We copy 0 bytes however to
2008 stay in synch. */
2009 packet->actual_length = 0;
2010 } else {
2011 packet->actual_length = myNextRxDesc->hw_len;
2012 /* Make sure the data fits in the buffer. */
2013 assert(packet->actual_length <= packet->length);
2014 memcpy(urb->transfer_buffer + packet->offset,
2015 phys_to_virt(myNextRxDesc->buf), packet->actual_length);
2018 /* Increment the packet counter. */
2019 urb_priv->isoc_packet_counter++;
2021 /* Note that we don't care about the eot field in the rx descriptor's status.
2022 It will always be set for isoc traffic. */
2023 if (urb->number_of_packets == urb_priv->isoc_packet_counter) {
2025 /* Out-of-synch diagnostics. */
2026 curr_fm = (*R_USB_FM_NUMBER & 0x7ff);
2027 if (((prev_fm + urb_priv->isoc_packet_counter) % (0x7ff + 1)) != curr_fm) {
2028 /* This test is wrong, if there is more than one isoc
2029 in endpoint active it will always calculate wrong
2030 since prev_fm is shared by all endpoints.
2032 FIXME Make this check per URB using urb->start_frame.
2034 dbg_isoc("Out of synch? Previous frame = %d, current frame = %d",
2035 prev_fm, curr_fm);
2038 prev_fm = curr_fm;
2040 /* Complete the urb with status OK. */
2041 etrax_usb_complete_isoc_urb(urb, 0);
2045 skip_out:
2047 /* DMA IN cache bug. Flush the DMA IN buffer from the cache. (struct etrax_dma_descr
2048 has the same layout as USB_IN_Desc for the relevant fields.) */
2049 prepare_rx_descriptor((struct etrax_dma_descr*)myNextRxDesc);
2051 myPrevRxDesc = myNextRxDesc;
2052 myPrevRxDesc->command |= IO_MASK(USB_IN_command, eol);
2053 myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
2054 myLastRxDesc = myPrevRxDesc;
2056 myNextRxDesc->status = 0;
2057 myNextRxDesc = phys_to_virt(myNextRxDesc->next);
2060 restore_flags(flags);
2062 DBFEXIT;
2064 return IRQ_HANDLED;
2068 /* This function will unlink the SB descriptors associated with this urb. */
2069 static int etrax_remove_from_sb_list(struct urb *urb)
2071 USB_SB_Desc_t *next_sb, *first_sb, *last_sb;
2072 etrax_urb_priv_t *urb_priv;
2073 int i = 0;
2075 DBFENTER;
2077 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2078 assert(urb_priv);
2080 /* Just a sanity check. Since we don't fiddle with the DMA list the EP descriptor
2081 doesn't really need to be disabled, it's just that we expect it to be. */
2082 if (usb_pipetype(urb->pipe) == PIPE_BULK) {
2083 assert(!(TxBulkEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2084 } else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
2085 assert(!(TxCtrlEPList[urb_priv->epid].command & IO_MASK(USB_EP_command, enable)));
2088 first_sb = urb_priv->first_sb;
2089 last_sb = urb_priv->last_sb;
2091 assert(first_sb);
2092 assert(last_sb);
2094 while (first_sb != last_sb) {
2095 next_sb = (USB_SB_Desc_t *)phys_to_virt(first_sb->next);
2096 kmem_cache_free(usb_desc_cache, first_sb);
2097 first_sb = next_sb;
2098 i++;
2100 kmem_cache_free(usb_desc_cache, last_sb);
2101 i++;
2102 dbg_sb("%d SB descriptors freed", i);
2103 /* Compare i with urb->number_of_packets for Isoc traffic.
2104 Should be same when calling unlink_urb */
2106 DBFEXIT;
2108 return i;
2111 static int etrax_usb_submit_bulk_urb(struct urb *urb)
2113 int epid;
2114 int empty;
2115 unsigned long flags;
2116 etrax_urb_priv_t *urb_priv;
2118 DBFENTER;
2120 /* Epid allocation, empty check and list add must be protected.
2121 Read about this in etrax_usb_submit_ctrl_urb. */
2123 spin_lock_irqsave(&urb_list_lock, flags);
2124 epid = etrax_usb_setup_epid(urb);
2125 if (epid == -1) {
2126 DBFEXIT;
2127 spin_unlock_irqrestore(&urb_list_lock, flags);
2128 return -ENOMEM;
2130 empty = urb_list_empty(epid);
2131 urb_list_add(urb, epid);
2132 spin_unlock_irqrestore(&urb_list_lock, flags);
2134 dbg_bulk("Adding bulk %s urb 0x%lx to %s list, epid %d",
2135 usb_pipein(urb->pipe) ? "IN" : "OUT", (unsigned long)urb, empty ? "empty" : "", epid);
2137 /* Mark the urb as being in progress. */
2138 urb->status = -EINPROGRESS;
2140 /* Setup the hcpriv data. */
2141 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2142 assert(urb_priv != NULL);
2143 /* This sets rx_offset to 0. */
2144 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2145 urb_priv->urb_state = NOT_STARTED;
2146 urb->hcpriv = urb_priv;
2148 if (empty) {
2149 etrax_usb_add_to_bulk_sb_list(urb, epid);
2152 DBFEXIT;
2154 return 0;
2157 static void etrax_usb_add_to_bulk_sb_list(struct urb *urb, int epid)
2159 USB_SB_Desc_t *sb_desc;
2160 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2161 unsigned long flags;
2162 char maxlen;
2164 DBFENTER;
2166 dbg_bulk("etrax_usb_add_to_bulk_sb_list, urb 0x%lx", (unsigned long)urb);
2168 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2170 sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2171 assert(sb_desc != NULL);
2172 memset(sb_desc, 0, sizeof(USB_SB_Desc_t));
2175 if (usb_pipeout(urb->pipe)) {
2177 dbg_bulk("Grabbing bulk OUT, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2179 /* This is probably a sanity check of the bulk transaction length
2180 not being larger than 64 kB. */
2181 if (urb->transfer_buffer_length > 0xffff) {
2182 panic("urb->transfer_buffer_length > 0xffff");
2185 sb_desc->sw_len = urb->transfer_buffer_length;
2187 /* The rem field is don't care if it's not a full-length transfer, so setting
2188 it shouldn't hurt. Also, rem isn't used for OUT traffic. */
2189 sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
2190 IO_STATE(USB_SB_command, tt, out) |
2191 IO_STATE(USB_SB_command, eot, yes) |
2192 IO_STATE(USB_SB_command, eol, yes));
2194 /* The full field is set to yes, even if we don't actually check that this is
2195 a full-length transfer (i.e., that transfer_buffer_length % maxlen = 0).
2196 Setting full prevents the USB controller from sending an empty packet in
2197 that case. However, if URB_ZERO_PACKET was set we want that. */
2198 if (!(urb->transfer_flags & URB_ZERO_PACKET)) {
2199 sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
2202 sb_desc->buf = virt_to_phys(urb->transfer_buffer);
2203 sb_desc->next = 0;
2205 } else if (usb_pipein(urb->pipe)) {
2207 dbg_bulk("Grabbing bulk IN, urb 0x%lx, epid %d", (unsigned long)urb, epid);
2209 sb_desc->sw_len = urb->transfer_buffer_length ?
2210 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2212 /* The rem field is don't care if it's not a full-length transfer, so setting
2213 it shouldn't hurt. */
2214 sb_desc->command =
2215 (IO_FIELD(USB_SB_command, rem,
2216 urb->transfer_buffer_length % maxlen) |
2217 IO_STATE(USB_SB_command, tt, in) |
2218 IO_STATE(USB_SB_command, eot, yes) |
2219 IO_STATE(USB_SB_command, eol, yes));
2221 sb_desc->buf = 0;
2222 sb_desc->next = 0;
2225 urb_priv->first_sb = sb_desc;
2226 urb_priv->last_sb = sb_desc;
2227 urb_priv->epid = epid;
2229 urb->hcpriv = urb_priv;
2231 /* Reset toggle bits and reset error count. */
2232 save_flags(flags);
2233 cli();
2235 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2236 nop();
2238 /* FIXME: Is this a special case since the hold field is checked,
2239 or should we check hold in a lot of other cases as well? */
2240 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2241 panic("Hold was set in %s", __FUNCTION__);
2244 /* Reset error counters (regardless of which direction this traffic is). */
2245 *R_USB_EPT_DATA &=
2246 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2247 IO_MASK(R_USB_EPT_DATA, error_count_out));
2249 /* Software must preset the toggle bits. */
2250 if (usb_pipeout(urb->pipe)) {
2251 char toggle =
2252 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2253 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
2254 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
2255 } else {
2256 char toggle =
2257 usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
2258 *R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
2259 *R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
2262 /* Assert that the EP descriptor is disabled. */
2263 assert(!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2265 /* The reason we set the EP's sub pointer directly instead of
2266 walking the SB list and linking it last in the list is that we only
2267 have one active urb at a time (the rest are queued). */
2269 /* Note that we cannot have interrupts running when we have set the SB descriptor
2270 but the EP is not yet enabled. If a bulk eot happens for another EP, we will
2271 find this EP disabled and with a SB != 0, which will make us think that it's done. */
2272 TxBulkEPList[epid].sub = virt_to_phys(sb_desc);
2273 TxBulkEPList[epid].hw_len = 0;
2274 /* Note that we don't have to fill in the ep_id field since this
2275 was done when we allocated the EP descriptors in init_tx_bulk_ep. */
2277 /* Check if the dummy list is already with us (if several urbs were queued). */
2278 if (TxBulkEPList[epid].next != virt_to_phys(&TxBulkDummyEPList[epid][0])) {
2280 dbg_bulk("Inviting dummy list to the party for urb 0x%lx, epid %d",
2281 (unsigned long)urb, epid);
2283 /* The last EP in the dummy list already has its next pointer set to
2284 TxBulkEPList[epid].next. */
2286 /* We don't need to check if the DMA is at this EP or not before changing the
2287 next pointer, since we will do it in one 32-bit write (EP descriptors are
2288 32-bit aligned). */
2289 TxBulkEPList[epid].next = virt_to_phys(&TxBulkDummyEPList[epid][0]);
2291 /* Enable the EP descr. */
2292 dbg_bulk("Enabling bulk EP for urb 0x%lx, epid %d", (unsigned long)urb, epid);
2293 TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2295 /* Everything is set up, safe to enable interrupts again. */
2296 restore_flags(flags);
2298 /* If the DMA bulk channel isn't running, we need to restart it if it
2299 has stopped at the last EP descriptor (DMA stopped because there was
2300 no more traffic) or if it has stopped at a dummy EP with the intr flag
2301 set (DMA stopped because we were too slow in inserting new traffic). */
2302 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
2304 USB_EP_Desc_t *ep;
2305 ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB0_EP);
2306 dbg_bulk("DMA channel not running in add");
2307 dbg_bulk("DMA is at 0x%lx", (unsigned long)ep);
2309 if (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[NBR_OF_EPIDS - 1]) ||
2310 (ep->command & 0x8) >> 3) {
2311 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
2312 /* Update/restart the bulk start timer since we just started the channel. */
2313 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
2314 /* Update/restart the bulk eot timer since we just inserted traffic. */
2315 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
2319 DBFEXIT;
2322 static void etrax_usb_complete_bulk_urb(struct urb *urb, int status)
2324 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2325 int epid = urb_priv->epid;
2326 unsigned long flags;
2328 DBFENTER;
2330 if (status)
2331 warn("Completing bulk urb with status %d.", status);
2333 dbg_bulk("Completing bulk urb 0x%lx for epid %d", (unsigned long)urb, epid);
2335 /* Update the urb list. */
2336 urb_list_del(urb, epid);
2338 /* For an IN pipe, we always set the actual length, regardless of whether there was
2339 an error or not (which means the device driver can use the data if it wants to). */
2340 if (usb_pipein(urb->pipe)) {
2341 urb->actual_length = urb_priv->rx_offset;
2342 } else {
2343 /* Set actual_length for OUT urbs also; the USB mass storage driver seems
2344 to want that. We wouldn't know of any partial writes if there was an error. */
2345 if (status == 0) {
2346 urb->actual_length = urb->transfer_buffer_length;
2347 } else {
2348 urb->actual_length = 0;
2352 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2353 Like, maybe we shouldn't toggle the toggle bits, or maybe we shouldn't insert more traffic. */
2355 save_flags(flags);
2356 cli();
2358 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2359 nop();
2361 /* We need to fiddle with the toggle bits because the hardware doesn't do it for us. */
2362 if (usb_pipeout(urb->pipe)) {
2363 char toggle =
2364 IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
2365 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2366 usb_pipeout(urb->pipe), toggle);
2367 } else {
2368 char toggle =
2369 IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
2370 usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
2371 usb_pipeout(urb->pipe), toggle);
2373 restore_flags(flags);
2375 /* Remember to free the SBs. */
2376 etrax_remove_from_sb_list(urb);
2377 kfree(urb_priv);
2378 urb->hcpriv = 0;
2380 /* If there are any more urb's in the list we'd better start sending */
2381 if (!urb_list_empty(epid)) {
2383 struct urb *new_urb;
2385 /* Get the first urb. */
2386 new_urb = urb_list_first(epid);
2387 assert(new_urb);
2389 dbg_bulk("More bulk for epid %d", epid);
2391 etrax_usb_add_to_bulk_sb_list(new_urb, epid);
2394 urb->status = status;
2396 /* We let any non-zero status from the layer above have precedence. */
2397 if (status == 0) {
2398 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2399 is to be treated as an error. */
2400 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2401 if (usb_pipein(urb->pipe) &&
2402 (urb->actual_length !=
2403 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2404 urb->status = -EREMOTEIO;
2409 if (urb->complete) {
2410 urb->complete(urb, NULL);
2413 if (urb_list_empty(epid)) {
2414 /* This means that this EP is now free, deconfigure it. */
2415 etrax_usb_free_epid(epid);
2417 /* No more traffic; time to clean up.
2418 Must set sub pointer to 0, since we look at the sub pointer when handling
2419 the bulk eot interrupt. */
2421 dbg_bulk("No bulk for epid %d", epid);
2423 TxBulkEPList[epid].sub = 0;
2425 /* Unlink the dummy list. */
2427 dbg_bulk("Kicking dummy list out of party for urb 0x%lx, epid %d",
2428 (unsigned long)urb, epid);
2430 /* No need to wait for the DMA before changing the next pointer.
2431 The modulo NBR_OF_EPIDS isn't actually necessary, since we will never use
2432 the last one (INVALID_EPID) for actual traffic. */
2433 TxBulkEPList[epid].next =
2434 virt_to_phys(&TxBulkEPList[(epid + 1) % NBR_OF_EPIDS]);
2437 DBFEXIT;
2440 static int etrax_usb_submit_ctrl_urb(struct urb *urb)
2442 int epid;
2443 int empty;
2444 unsigned long flags;
2445 etrax_urb_priv_t *urb_priv;
2447 DBFENTER;
2449 /* FIXME: Return -ENXIO if there is already a queued urb for this endpoint? */
2451 /* Epid allocation, empty check and list add must be protected.
2453 Epid allocation because if we find an existing epid for this endpoint an urb might be
2454 completed (emptying the list) before we add the new urb to the list, causing the epid
2455 to be de-allocated. We would then start the transfer with an invalid epid -> epid attn.
2457 Empty check and add because otherwise we might conclude that the list is not empty,
2458 after which it becomes empty before we add the new urb to the list, causing us not to
2459 insert the new traffic into the SB list. */
2461 spin_lock_irqsave(&urb_list_lock, flags);
2462 epid = etrax_usb_setup_epid(urb);
2463 if (epid == -1) {
2464 spin_unlock_irqrestore(&urb_list_lock, flags);
2465 DBFEXIT;
2466 return -ENOMEM;
2468 empty = urb_list_empty(epid);
2469 urb_list_add(urb, epid);
2470 spin_unlock_irqrestore(&urb_list_lock, flags);
2472 dbg_ctrl("Adding ctrl urb 0x%lx to %s list, epid %d",
2473 (unsigned long)urb, empty ? "empty" : "", epid);
2475 /* Mark the urb as being in progress. */
2476 urb->status = -EINPROGRESS;
2478 /* Setup the hcpriv data. */
2479 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2480 assert(urb_priv != NULL);
2481 /* This sets rx_offset to 0. */
2482 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2483 urb_priv->urb_state = NOT_STARTED;
2484 urb->hcpriv = urb_priv;
2486 if (empty) {
2487 etrax_usb_add_to_ctrl_sb_list(urb, epid);
2490 DBFEXIT;
2492 return 0;
2495 static void etrax_usb_add_to_ctrl_sb_list(struct urb *urb, int epid)
2497 USB_SB_Desc_t *sb_desc_setup;
2498 USB_SB_Desc_t *sb_desc_data;
2499 USB_SB_Desc_t *sb_desc_status;
2501 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2503 unsigned long flags;
2504 char maxlen;
2506 DBFENTER;
2508 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2510 sb_desc_setup = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2511 assert(sb_desc_setup != NULL);
2512 sb_desc_status = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2513 assert(sb_desc_status != NULL);
2515 /* Initialize the mandatory setup SB descriptor (used only in control transfers) */
2516 sb_desc_setup->sw_len = 8;
2517 sb_desc_setup->command = (IO_FIELD(USB_SB_command, rem, 0) |
2518 IO_STATE(USB_SB_command, tt, setup) |
2519 IO_STATE(USB_SB_command, full, yes) |
2520 IO_STATE(USB_SB_command, eot, yes));
2522 sb_desc_setup->buf = virt_to_phys(urb->setup_packet);
2524 if (usb_pipeout(urb->pipe)) {
2525 dbg_ctrl("Transfer for epid %d is OUT", epid);
2527 /* If this Control OUT transfer has an optional data stage we add an OUT token
2528 before the mandatory IN (status) token, hence the reordered SB list */
2530 sb_desc_setup->next = virt_to_phys(sb_desc_status);
2531 if (urb->transfer_buffer) {
2533 dbg_ctrl("This OUT transfer has an extra data stage");
2535 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2536 assert(sb_desc_data != NULL);
2538 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2540 sb_desc_data->sw_len = urb->transfer_buffer_length;
2541 sb_desc_data->command = (IO_STATE(USB_SB_command, tt, out) |
2542 IO_STATE(USB_SB_command, full, yes) |
2543 IO_STATE(USB_SB_command, eot, yes));
2544 sb_desc_data->buf = virt_to_phys(urb->transfer_buffer);
2545 sb_desc_data->next = virt_to_phys(sb_desc_status);
2548 sb_desc_status->sw_len = 1;
2549 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2550 IO_STATE(USB_SB_command, tt, in) |
2551 IO_STATE(USB_SB_command, eot, yes) |
2552 IO_STATE(USB_SB_command, intr, yes) |
2553 IO_STATE(USB_SB_command, eol, yes));
2555 sb_desc_status->buf = 0;
2556 sb_desc_status->next = 0;
2558 } else if (usb_pipein(urb->pipe)) {
2560 dbg_ctrl("Transfer for epid %d is IN", epid);
2561 dbg_ctrl("transfer_buffer_length = %d", urb->transfer_buffer_length);
2562 dbg_ctrl("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
2564 sb_desc_data = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2565 assert(sb_desc_data != NULL);
2567 sb_desc_setup->next = virt_to_phys(sb_desc_data);
2569 sb_desc_data->sw_len = urb->transfer_buffer_length ?
2570 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2571 dbg_ctrl("sw_len got %d", sb_desc_data->sw_len);
2573 sb_desc_data->command =
2574 (IO_FIELD(USB_SB_command, rem,
2575 urb->transfer_buffer_length % maxlen) |
2576 IO_STATE(USB_SB_command, tt, in) |
2577 IO_STATE(USB_SB_command, eot, yes));
2579 sb_desc_data->buf = 0;
2580 sb_desc_data->next = virt_to_phys(sb_desc_status);
2582 /* Read comment at zout_buffer declaration for an explanation to this. */
2583 sb_desc_status->sw_len = 1;
2584 sb_desc_status->command = (IO_FIELD(USB_SB_command, rem, 0) |
2585 IO_STATE(USB_SB_command, tt, zout) |
2586 IO_STATE(USB_SB_command, full, yes) |
2587 IO_STATE(USB_SB_command, eot, yes) |
2588 IO_STATE(USB_SB_command, intr, yes) |
2589 IO_STATE(USB_SB_command, eol, yes));
2591 sb_desc_status->buf = virt_to_phys(&zout_buffer[0]);
2592 sb_desc_status->next = 0;
2595 urb_priv->first_sb = sb_desc_setup;
2596 urb_priv->last_sb = sb_desc_status;
2597 urb_priv->epid = epid;
2599 urb_priv->urb_state = STARTED;
2601 /* Reset toggle bits and reset error count, remember to di and ei */
2602 /* Warning: it is possible that this locking doesn't work with bottom-halves */
2604 save_flags(flags);
2605 cli();
2607 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2608 nop();
2609 if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
2610 panic("Hold was set in %s", __FUNCTION__);
2614 /* FIXME: Compare with etrax_usb_add_to_bulk_sb_list where the toggle bits
2615 are set to a specific value. Why the difference? Read "Transfer and Toggle Bits
2616 in Designer's Reference, p. 8 - 11. */
2617 *R_USB_EPT_DATA &=
2618 ~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
2619 IO_MASK(R_USB_EPT_DATA, error_count_out) |
2620 IO_MASK(R_USB_EPT_DATA, t_in) |
2621 IO_MASK(R_USB_EPT_DATA, t_out));
2623 /* Since we use the rx interrupt to complete ctrl urbs, we can enable interrupts now
2624 (i.e. we don't check the sub pointer on an eot interrupt like we do for bulk traffic). */
2625 restore_flags(flags);
2627 /* Assert that the EP descriptor is disabled. */
2628 assert(!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)));
2630 /* Set up and enable the EP descriptor. */
2631 TxCtrlEPList[epid].sub = virt_to_phys(sb_desc_setup);
2632 TxCtrlEPList[epid].hw_len = 0;
2633 TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
2635 /* We start the DMA sub channel without checking if it's running or not, because:
2636 1) If it's already running, issuing the start command is a nop.
2637 2) We avoid a test-and-set race condition. */
2638 *R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
2640 DBFEXIT;
2643 static void etrax_usb_complete_ctrl_urb(struct urb *urb, int status)
2645 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2646 int epid = urb_priv->epid;
2648 DBFENTER;
2650 if (status)
2651 warn("Completing ctrl urb with status %d.", status);
2653 dbg_ctrl("Completing ctrl epid %d, urb 0x%lx", epid, (unsigned long)urb);
2655 /* Remove this urb from the list. */
2656 urb_list_del(urb, epid);
2658 /* For an IN pipe, we always set the actual length, regardless of whether there was
2659 an error or not (which means the device driver can use the data if it wants to). */
2660 if (usb_pipein(urb->pipe)) {
2661 urb->actual_length = urb_priv->rx_offset;
2664 /* FIXME: Is there something of the things below we shouldn't do if there was an error?
2665 Like, maybe we shouldn't insert more traffic. */
2667 /* Remember to free the SBs. */
2668 etrax_remove_from_sb_list(urb);
2669 kfree(urb_priv);
2670 urb->hcpriv = 0;
2672 /* If there are any more urbs in the list we'd better start sending. */
2673 if (!urb_list_empty(epid)) {
2674 struct urb *new_urb;
2676 /* Get the first urb. */
2677 new_urb = urb_list_first(epid);
2678 assert(new_urb);
2680 dbg_ctrl("More ctrl for epid %d, first urb = 0x%lx", epid, (unsigned long)new_urb);
2682 etrax_usb_add_to_ctrl_sb_list(new_urb, epid);
2685 urb->status = status;
2687 /* We let any non-zero status from the layer above have precedence. */
2688 if (status == 0) {
2689 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2690 is to be treated as an error. */
2691 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2692 if (usb_pipein(urb->pipe) &&
2693 (urb->actual_length !=
2694 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)))) {
2695 urb->status = -EREMOTEIO;
2700 if (urb->complete) {
2701 urb->complete(urb, NULL);
2704 if (urb_list_empty(epid)) {
2705 /* No more traffic. Time to clean up. */
2706 etrax_usb_free_epid(epid);
2707 /* Must set sub pointer to 0. */
2708 dbg_ctrl("No ctrl for epid %d", epid);
2709 TxCtrlEPList[epid].sub = 0;
2712 DBFEXIT;
2715 static int etrax_usb_submit_intr_urb(struct urb *urb)
2718 int epid;
2720 DBFENTER;
2722 if (usb_pipeout(urb->pipe)) {
2723 /* Unsupported transfer type.
2724 We don't support interrupt out traffic. (If we do, we can't support
2725 intervals for neither in or out traffic, but are forced to schedule all
2726 interrupt traffic in one frame.) */
2727 return -EINVAL;
2730 epid = etrax_usb_setup_epid(urb);
2731 if (epid == -1) {
2732 DBFEXIT;
2733 return -ENOMEM;
2736 if (!urb_list_empty(epid)) {
2737 /* There is already a queued urb for this endpoint. */
2738 etrax_usb_free_epid(epid);
2739 return -ENXIO;
2742 urb->status = -EINPROGRESS;
2744 dbg_intr("Add intr urb 0x%lx, to list, epid %d", (unsigned long)urb, epid);
2746 urb_list_add(urb, epid);
2747 etrax_usb_add_to_intr_sb_list(urb, epid);
2749 return 0;
2751 DBFEXIT;
2754 static void etrax_usb_add_to_intr_sb_list(struct urb *urb, int epid)
2757 volatile USB_EP_Desc_t *tmp_ep;
2758 volatile USB_EP_Desc_t *first_ep;
2760 char maxlen;
2761 int interval;
2762 int i;
2764 etrax_urb_priv_t *urb_priv;
2766 DBFENTER;
2768 maxlen = usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe));
2769 interval = urb->interval;
2771 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), KMALLOC_FLAG);
2772 assert(urb_priv != NULL);
2773 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
2774 urb->hcpriv = urb_priv;
2776 first_ep = &TxIntrEPList[0];
2778 /* Round of the interval to 2^n, it is obvious that this code favours
2779 smaller numbers, but that is actually a good thing */
2780 /* FIXME: The "rounding error" for larger intervals will be quite
2781 large. For in traffic this shouldn't be a problem since it will only
2782 mean that we "poll" more often. */
2783 for (i = 0; interval; i++) {
2784 interval = interval >> 1;
2786 interval = 1 << (i - 1);
2788 dbg_intr("Interval rounded to %d", interval);
2790 tmp_ep = first_ep;
2791 i = 0;
2792 do {
2793 if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
2794 if ((i % interval) == 0) {
2795 /* Insert the traffic ep after tmp_ep */
2796 USB_EP_Desc_t *ep_desc;
2797 USB_SB_Desc_t *sb_desc;
2799 dbg_intr("Inserting EP for epid %d", epid);
2801 ep_desc = (USB_EP_Desc_t *)
2802 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2803 sb_desc = (USB_SB_Desc_t *)
2804 kmem_cache_alloc(usb_desc_cache, SLAB_FLAG);
2805 assert(ep_desc != NULL);
2806 CHECK_ALIGN(ep_desc);
2807 assert(sb_desc != NULL);
2809 ep_desc->sub = virt_to_phys(sb_desc);
2810 ep_desc->hw_len = 0;
2811 ep_desc->command = (IO_FIELD(USB_EP_command, epid, epid) |
2812 IO_STATE(USB_EP_command, enable, yes));
2815 /* Round upwards the number of packets of size maxlen
2816 that this SB descriptor should receive. */
2817 sb_desc->sw_len = urb->transfer_buffer_length ?
2818 (urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
2819 sb_desc->next = 0;
2820 sb_desc->buf = 0;
2821 sb_desc->command =
2822 (IO_FIELD(USB_SB_command, rem, urb->transfer_buffer_length % maxlen) |
2823 IO_STATE(USB_SB_command, tt, in) |
2824 IO_STATE(USB_SB_command, eot, yes) |
2825 IO_STATE(USB_SB_command, eol, yes));
2827 ep_desc->next = tmp_ep->next;
2828 tmp_ep->next = virt_to_phys(ep_desc);
2830 i++;
2832 tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->next);
2833 } while (tmp_ep != first_ep);
2836 /* Note that first_sb/last_sb doesn't apply to interrupt traffic. */
2837 urb_priv->epid = epid;
2839 /* We start the DMA sub channel without checking if it's running or not, because:
2840 1) If it's already running, issuing the start command is a nop.
2841 2) We avoid a test-and-set race condition. */
2842 *R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
2844 DBFEXIT;
2849 static void etrax_usb_complete_intr_urb(struct urb *urb, int status)
2851 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
2852 int epid = urb_priv->epid;
2854 DBFENTER;
2856 if (status)
2857 warn("Completing intr urb with status %d.", status);
2859 dbg_intr("Completing intr epid %d, urb 0x%lx", epid, (unsigned long)urb);
2861 urb->status = status;
2862 urb->actual_length = urb_priv->rx_offset;
2864 dbg_intr("interrupt urb->actual_length = %d", urb->actual_length);
2866 /* We let any non-zero status from the layer above have precedence. */
2867 if (status == 0) {
2868 /* URB_SHORT_NOT_OK means that short reads (shorter than the endpoint's max length)
2869 is to be treated as an error. */
2870 if (urb->transfer_flags & URB_SHORT_NOT_OK) {
2871 if (urb->actual_length !=
2872 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
2873 urb->status = -EREMOTEIO;
2878 /* The driver will resubmit the URB so we need to remove it first */
2879 etrax_usb_unlink_urb(urb, 0);
2880 if (urb->complete) {
2881 urb->complete(urb, NULL);
2884 DBFEXIT;
2888 static int etrax_usb_submit_isoc_urb(struct urb *urb)
2890 int epid;
2891 unsigned long flags;
2893 DBFENTER;
2895 dbg_isoc("Submitting isoc urb = 0x%lx", (unsigned long)urb);
2897 /* Epid allocation, empty check and list add must be protected.
2898 Read about this in etrax_usb_submit_ctrl_urb. */
2900 spin_lock_irqsave(&urb_list_lock, flags);
2901 /* Is there an active epid for this urb ? */
2902 epid = etrax_usb_setup_epid(urb);
2903 if (epid == -1) {
2904 DBFEXIT;
2905 spin_unlock_irqrestore(&urb_list_lock, flags);
2906 return -ENOMEM;
2909 /* Ok, now we got valid endpoint, lets insert some traffic */
2911 urb->status = -EINPROGRESS;
2913 /* Find the last urb in the URB_List and add this urb after that one.
2914 Also add the traffic, that is do an etrax_usb_add_to_isoc_sb_list. This
2915 is important to make this in "real time" since isochronous traffic is
2916 time sensitive. */
2918 dbg_isoc("Adding isoc urb to (possibly empty) list");
2919 urb_list_add(urb, epid);
2920 etrax_usb_add_to_isoc_sb_list(urb, epid);
2921 spin_unlock_irqrestore(&urb_list_lock, flags);
2923 DBFEXIT;
2925 return 0;
2928 static void etrax_usb_check_error_isoc_ep(const int epid)
2930 unsigned long int flags;
2931 int error_code;
2932 __u32 r_usb_ept_data;
2934 /* We can't read R_USB_EPID_ATTN here since it would clear the iso_eof,
2935 bulk_eot and epid_attn interrupts. So we just check the status of
2936 the epid without testing if for it in R_USB_EPID_ATTN. */
2939 save_flags(flags);
2940 cli();
2941 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
2942 nop();
2943 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
2944 registers, they are located at the same address and are of the same size.
2945 In other words, this read should be ok for isoc also. */
2946 r_usb_ept_data = *R_USB_EPT_DATA;
2947 restore_flags(flags);
2949 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
2951 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
2952 warn("Hold was set for epid %d.", epid);
2953 return;
2956 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, no_error)) {
2958 /* This indicates that the SB list of the ept was completed before
2959 new data was appended to it. This is not an error, but indicates
2960 large system or USB load and could possibly cause trouble for
2961 very timing sensitive USB device drivers so we log it.
2963 info("Isoc. epid %d disabled with no error", epid);
2964 return;
2966 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, stall)) {
2967 /* Not really a protocol error, just says that the endpoint gave
2968 a stall response. Note that error_code cannot be stall for isoc. */
2969 panic("Isoc traffic cannot stall");
2971 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA_ISO, error_code, bus_error)) {
2972 /* Two devices responded to a transaction request. Must be resolved
2973 by software. FIXME: Reset ports? */
2974 panic("Bus error for epid %d."
2975 " Two devices responded to transaction request",
2976 epid);
2978 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
2979 /* DMA overrun or underrun. */
2980 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
2982 /* It seems that error_code = buffer_error in
2983 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
2984 are the same error. */
2989 static void etrax_usb_add_to_isoc_sb_list(struct urb *urb, int epid)
2992 int i = 0;
2994 etrax_urb_priv_t *urb_priv;
2995 USB_SB_Desc_t *prev_sb_desc, *next_sb_desc, *temp_sb_desc;
2997 DBFENTER;
2999 prev_sb_desc = next_sb_desc = temp_sb_desc = NULL;
3001 urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_ATOMIC);
3002 assert(urb_priv != NULL);
3003 memset(urb_priv, 0, sizeof(etrax_urb_priv_t));
3005 urb->hcpriv = urb_priv;
3006 urb_priv->epid = epid;
3008 if (usb_pipeout(urb->pipe)) {
3010 if (urb->number_of_packets == 0) panic("etrax_usb_add_to_isoc_sb_list 0 packets\n");
3012 dbg_isoc("Transfer for epid %d is OUT", epid);
3013 dbg_isoc("%d packets in URB", urb->number_of_packets);
3015 /* Create one SB descriptor for each packet and link them together. */
3016 for (i = 0; i < urb->number_of_packets; i++) {
3017 if (!urb->iso_frame_desc[i].length)
3018 continue;
3020 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3021 assert(next_sb_desc != NULL);
3023 if (urb->iso_frame_desc[i].length > 0) {
3025 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, out) |
3026 IO_STATE(USB_SB_command, eot, yes));
3028 next_sb_desc->sw_len = urb->iso_frame_desc[i].length;
3029 next_sb_desc->buf = virt_to_phys((char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset);
3031 /* Check if full length transfer. */
3032 if (urb->iso_frame_desc[i].length ==
3033 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe))) {
3034 next_sb_desc->command |= IO_STATE(USB_SB_command, full, yes);
3036 } else {
3037 dbg_isoc("zero len packet");
3038 next_sb_desc->command = (IO_FIELD(USB_SB_command, rem, 0) |
3039 IO_STATE(USB_SB_command, tt, zout) |
3040 IO_STATE(USB_SB_command, eot, yes) |
3041 IO_STATE(USB_SB_command, full, yes));
3043 next_sb_desc->sw_len = 1;
3044 next_sb_desc->buf = virt_to_phys(&zout_buffer[0]);
3047 /* First SB descriptor that belongs to this urb */
3048 if (i == 0)
3049 urb_priv->first_sb = next_sb_desc;
3050 else
3051 prev_sb_desc->next = virt_to_phys(next_sb_desc);
3053 prev_sb_desc = next_sb_desc;
3056 next_sb_desc->command |= (IO_STATE(USB_SB_command, intr, yes) |
3057 IO_STATE(USB_SB_command, eol, yes));
3058 next_sb_desc->next = 0;
3059 urb_priv->last_sb = next_sb_desc;
3061 } else if (usb_pipein(urb->pipe)) {
3063 dbg_isoc("Transfer for epid %d is IN", epid);
3064 dbg_isoc("transfer_buffer_length = %d", urb->transfer_buffer_length);
3065 dbg_isoc("rem is calculated to %d", urb->iso_frame_desc[urb->number_of_packets - 1].length);
3067 /* Note that in descriptors for periodic traffic are not consumed. This means that
3068 the USB controller never propagates in the SB list. In other words, if there already
3069 is an SB descriptor in the list for this EP we don't have to do anything. */
3070 if (TxIsocEPList[epid].sub == 0) {
3071 dbg_isoc("Isoc traffic not already running, allocating SB");
3073 next_sb_desc = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, SLAB_ATOMIC);
3074 assert(next_sb_desc != NULL);
3076 next_sb_desc->command = (IO_STATE(USB_SB_command, tt, in) |
3077 IO_STATE(USB_SB_command, eot, yes) |
3078 IO_STATE(USB_SB_command, eol, yes));
3080 next_sb_desc->next = 0;
3081 next_sb_desc->sw_len = 1; /* Actual number of packets is not relevant
3082 for periodic in traffic as long as it is more
3083 than zero. Set to 1 always. */
3084 next_sb_desc->buf = 0;
3086 /* The rem field is don't care for isoc traffic, so we don't set it. */
3088 /* Only one SB descriptor that belongs to this urb. */
3089 urb_priv->first_sb = next_sb_desc;
3090 urb_priv->last_sb = next_sb_desc;
3092 } else {
3094 dbg_isoc("Isoc traffic already running, just setting first/last_sb");
3096 /* Each EP for isoc in will have only one SB descriptor, setup when submitting the
3097 already active urb. Note that even though we may have several first_sb/last_sb
3098 pointing at the same SB descriptor, they are freed only once (when the list has
3099 become empty). */
3100 urb_priv->first_sb = phys_to_virt(TxIsocEPList[epid].sub);
3101 urb_priv->last_sb = phys_to_virt(TxIsocEPList[epid].sub);
3102 return;
3107 /* Find the spot to insert this urb and add it. */
3108 if (TxIsocEPList[epid].sub == 0) {
3109 /* First SB descriptor inserted in this list (in or out). */
3110 dbg_isoc("Inserting SB desc first in list");
3111 TxIsocEPList[epid].hw_len = 0;
3112 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3114 } else {
3115 /* Isochronous traffic is already running, insert new traffic last (only out). */
3116 dbg_isoc("Inserting SB desc last in list");
3117 temp_sb_desc = phys_to_virt(TxIsocEPList[epid].sub);
3118 while ((temp_sb_desc->command & IO_MASK(USB_SB_command, eol)) !=
3119 IO_STATE(USB_SB_command, eol, yes)) {
3120 assert(temp_sb_desc->next);
3121 temp_sb_desc = phys_to_virt(temp_sb_desc->next);
3123 dbg_isoc("Appending list on desc 0x%p", temp_sb_desc);
3125 /* Next pointer must be set before eol is removed. */
3126 temp_sb_desc->next = virt_to_phys(urb_priv->first_sb);
3127 /* Clear the previous end of list flag since there is a new in the
3128 added SB descriptor list. */
3129 temp_sb_desc->command &= ~IO_MASK(USB_SB_command, eol);
3131 if (!(TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
3132 /* 8.8.5 in Designer's Reference says we should check for and correct
3133 any errors in the EP here. That should not be necessary if epid_attn
3134 is handled correctly, so we assume all is ok. */
3135 dbg_isoc("EP disabled");
3136 etrax_usb_check_error_isoc_ep(epid);
3138 /* The SB list was exhausted. */
3139 if (virt_to_phys(urb_priv->last_sb) != TxIsocEPList[epid].sub) {
3140 /* The new sublist did not get processed before the EP was
3141 disabled. Setup the EP again. */
3142 dbg_isoc("Set EP sub to new list");
3143 TxIsocEPList[epid].hw_len = 0;
3144 TxIsocEPList[epid].sub = virt_to_phys(urb_priv->first_sb);
3149 if (urb->transfer_flags & URB_ISO_ASAP) {
3150 /* The isoc transfer should be started as soon as possible. The start_frame
3151 field is a return value if URB_ISO_ASAP was set. Comparing R_USB_FM_NUMBER
3152 with a USB Chief trace shows that the first isoc IN token is sent 2 frames
3153 later. I'm not sure how this affects usage of the start_frame field by the
3154 device driver, or how it affects things when USB_ISO_ASAP is not set, so
3155 therefore there's no compensation for the 2 frame "lag" here. */
3156 urb->start_frame = (*R_USB_FM_NUMBER & 0x7ff);
3157 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3158 urb_priv->urb_state = STARTED;
3159 dbg_isoc("URB_ISO_ASAP set, urb->start_frame set to %d", urb->start_frame);
3160 } else {
3161 /* Not started yet. */
3162 urb_priv->urb_state = NOT_STARTED;
3163 dbg_isoc("urb_priv->urb_state set to NOT_STARTED");
3166 /* We start the DMA sub channel without checking if it's running or not, because:
3167 1) If it's already running, issuing the start command is a nop.
3168 2) We avoid a test-and-set race condition. */
3169 *R_DMA_CH8_SUB3_CMD = IO_STATE(R_DMA_CH8_SUB3_CMD, cmd, start);
3171 DBFEXIT;
3174 static void etrax_usb_complete_isoc_urb(struct urb *urb, int status)
3176 etrax_urb_priv_t *urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3177 int epid = urb_priv->epid;
3178 int auto_resubmit = 0;
3180 DBFENTER;
3181 dbg_isoc("complete urb 0x%p, status %d", urb, status);
3183 if (status)
3184 warn("Completing isoc urb with status %d.", status);
3186 if (usb_pipein(urb->pipe)) {
3187 int i;
3189 /* Make that all isoc packets have status and length set before
3190 completing the urb. */
3191 for (i = urb_priv->isoc_packet_counter; i < urb->number_of_packets; i++) {
3192 urb->iso_frame_desc[i].actual_length = 0;
3193 urb->iso_frame_desc[i].status = -EPROTO;
3196 urb_list_del(urb, epid);
3198 if (!list_empty(&urb_list[epid])) {
3199 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3200 } else {
3201 unsigned long int flags;
3202 if (TxIsocEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3203 /* The EP was enabled, disable it and wait. */
3204 TxIsocEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
3206 /* Ah, the luxury of busy-wait. */
3207 while (*R_DMA_CH8_SUB3_EP == virt_to_phys(&TxIsocEPList[epid]));
3210 etrax_remove_from_sb_list(urb);
3211 TxIsocEPList[epid].sub = 0;
3212 TxIsocEPList[epid].hw_len = 0;
3214 save_flags(flags);
3215 cli();
3216 etrax_usb_free_epid(epid);
3217 restore_flags(flags);
3220 urb->hcpriv = 0;
3221 kfree(urb_priv);
3223 /* Release allocated bandwidth. */
3224 usb_release_bandwidth(urb->dev, urb, 0);
3225 } else if (usb_pipeout(urb->pipe)) {
3226 int freed_descr;
3228 dbg_isoc("Isoc out urb complete 0x%p", urb);
3230 /* Update the urb list. */
3231 urb_list_del(urb, epid);
3233 freed_descr = etrax_remove_from_sb_list(urb);
3234 dbg_isoc("freed %d descriptors of %d packets", freed_descr, urb->number_of_packets);
3235 assert(freed_descr == urb->number_of_packets);
3236 urb->hcpriv = 0;
3237 kfree(urb_priv);
3239 /* Release allocated bandwidth. */
3240 usb_release_bandwidth(urb->dev, urb, 0);
3243 urb->status = status;
3244 if (urb->complete) {
3245 urb->complete(urb, NULL);
3248 if (auto_resubmit) {
3249 /* Check that urb was not unlinked by the complete callback. */
3250 if (__urb_list_entry(urb, epid)) {
3251 /* Move this one down the list. */
3252 urb_list_move_last(urb, epid);
3254 /* Mark the now first urb as started (may already be). */
3255 ((etrax_urb_priv_t *)(urb_list_first(epid)->hcpriv))->urb_state = STARTED;
3257 /* Must set this to 0 since this urb is still active after
3258 completion. */
3259 urb_priv->isoc_packet_counter = 0;
3260 } else {
3261 warn("(ISOC) automatic resubmit urb 0x%p removed by complete.", urb);
3265 DBFEXIT;
3268 static void etrax_usb_complete_urb(struct urb *urb, int status)
3270 switch (usb_pipetype(urb->pipe)) {
3271 case PIPE_BULK:
3272 etrax_usb_complete_bulk_urb(urb, status);
3273 break;
3274 case PIPE_CONTROL:
3275 etrax_usb_complete_ctrl_urb(urb, status);
3276 break;
3277 case PIPE_INTERRUPT:
3278 etrax_usb_complete_intr_urb(urb, status);
3279 break;
3280 case PIPE_ISOCHRONOUS:
3281 etrax_usb_complete_isoc_urb(urb, status);
3282 break;
3283 default:
3284 err("Unknown pipetype");
3290 static irqreturn_t etrax_usb_hc_interrupt_top_half(int irq, void *vhc, struct pt_regs *regs)
3292 usb_interrupt_registers_t *reg;
3293 unsigned long flags;
3294 __u32 irq_mask;
3295 __u8 status;
3296 __u32 epid_attn;
3297 __u16 port_status_1;
3298 __u16 port_status_2;
3299 __u32 fm_number;
3301 DBFENTER;
3303 /* Read critical registers into local variables, do kmalloc afterwards. */
3304 save_flags(flags);
3305 cli();
3307 irq_mask = *R_USB_IRQ_MASK_READ;
3308 /* Reading R_USB_STATUS clears the ctl_status interrupt. Note that R_USB_STATUS
3309 must be read before R_USB_EPID_ATTN since reading the latter clears the
3310 ourun and perror fields of R_USB_STATUS. */
3311 status = *R_USB_STATUS;
3313 /* Reading R_USB_EPID_ATTN clears the iso_eof, bulk_eot and epid_attn interrupts. */
3314 epid_attn = *R_USB_EPID_ATTN;
3316 /* Reading R_USB_RH_PORT_STATUS_1 and R_USB_RH_PORT_STATUS_2 clears the
3317 port_status interrupt. */
3318 port_status_1 = *R_USB_RH_PORT_STATUS_1;
3319 port_status_2 = *R_USB_RH_PORT_STATUS_2;
3321 /* Reading R_USB_FM_NUMBER clears the sof interrupt. */
3322 /* Note: the lower 11 bits contain the actual frame number, sent with each sof. */
3323 fm_number = *R_USB_FM_NUMBER;
3325 restore_flags(flags);
3327 reg = (usb_interrupt_registers_t *)kmem_cache_alloc(top_half_reg_cache, SLAB_ATOMIC);
3329 assert(reg != NULL);
3331 reg->hc = (etrax_hc_t *)vhc;
3333 /* Now put register values into kmalloc'd area. */
3334 reg->r_usb_irq_mask_read = irq_mask;
3335 reg->r_usb_status = status;
3336 reg->r_usb_epid_attn = epid_attn;
3337 reg->r_usb_rh_port_status_1 = port_status_1;
3338 reg->r_usb_rh_port_status_2 = port_status_2;
3339 reg->r_usb_fm_number = fm_number;
3341 INIT_WORK(&reg->usb_bh, etrax_usb_hc_interrupt_bottom_half, reg);
3342 schedule_work(&reg->usb_bh);
3344 DBFEXIT;
3346 return IRQ_HANDLED;
3349 static void etrax_usb_hc_interrupt_bottom_half(void *data)
3351 usb_interrupt_registers_t *reg = (usb_interrupt_registers_t *)data;
3352 __u32 irq_mask = reg->r_usb_irq_mask_read;
3354 DBFENTER;
3356 /* Interrupts are handled in order of priority. */
3357 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, epid_attn)) {
3358 etrax_usb_hc_epid_attn_interrupt(reg);
3360 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
3361 etrax_usb_hc_port_status_interrupt(reg);
3363 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, ctl_status)) {
3364 etrax_usb_hc_ctl_status_interrupt(reg);
3366 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, iso_eof)) {
3367 etrax_usb_hc_isoc_eof_interrupt();
3369 if (irq_mask & IO_MASK(R_USB_IRQ_MASK_READ, bulk_eot)) {
3370 /* Update/restart the bulk start timer since obviously the channel is running. */
3371 mod_timer(&bulk_start_timer, jiffies + BULK_START_TIMER_INTERVAL);
3372 /* Update/restart the bulk eot timer since we just received an bulk eot interrupt. */
3373 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3375 etrax_usb_hc_bulk_eot_interrupt(0);
3378 kmem_cache_free(top_half_reg_cache, reg);
3380 DBFEXIT;
3384 void etrax_usb_hc_isoc_eof_interrupt(void)
3386 struct urb *urb;
3387 etrax_urb_priv_t *urb_priv;
3388 int epid;
3389 unsigned long flags;
3391 DBFENTER;
3393 /* Do not check the invalid epid (it has a valid sub pointer). */
3394 for (epid = 0; epid < NBR_OF_EPIDS - 1; epid++) {
3396 /* Do not check the invalid epid (it has a valid sub pointer). */
3397 if ((epid == DUMMY_EPID) || (epid == INVALID_EPID))
3398 continue;
3400 /* Disable interrupts to block the isoc out descriptor interrupt handler
3401 from being called while the isoc EPID list is being checked.
3403 save_flags(flags);
3404 cli();
3406 if (TxIsocEPList[epid].sub == 0) {
3407 /* Nothing here to see. */
3408 restore_flags(flags);
3409 continue;
3412 /* Get the first urb (if any). */
3413 urb = urb_list_first(epid);
3414 if (urb == 0) {
3415 warn("Ignoring NULL urb");
3416 restore_flags(flags);
3417 continue;
3419 if (usb_pipein(urb->pipe)) {
3421 /* Sanity check. */
3422 assert(usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS);
3424 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3425 assert(urb_priv);
3427 if (urb_priv->urb_state == NOT_STARTED) {
3429 /* If ASAP is not set and urb->start_frame is the current frame,
3430 start the transfer. */
3431 if (!(urb->transfer_flags & URB_ISO_ASAP) &&
3432 (urb->start_frame == (*R_USB_FM_NUMBER & 0x7ff))) {
3434 dbg_isoc("Enabling isoc IN EP descr for epid %d", epid);
3435 TxIsocEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
3437 /* This urb is now active. */
3438 urb_priv->urb_state = STARTED;
3439 continue;
3443 restore_flags(flags);
3446 DBFEXIT;
3450 void etrax_usb_hc_bulk_eot_interrupt(int timer_induced)
3452 int epid;
3454 /* The technique is to run one urb at a time, wait for the eot interrupt at which
3455 point the EP descriptor has been disabled. */
3457 DBFENTER;
3458 dbg_bulk("bulk eot%s", timer_induced ? ", called by timer" : "");
3460 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3462 if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) &&
3463 (TxBulkEPList[epid].sub != 0)) {
3465 struct urb *urb;
3466 etrax_urb_priv_t *urb_priv;
3467 unsigned long flags;
3468 __u32 r_usb_ept_data;
3470 /* Found a disabled EP descriptor which has a non-null sub pointer.
3471 Verify that this ctrl EP descriptor got disabled no errors.
3472 FIXME: Necessary to check error_code? */
3473 dbg_bulk("for epid %d?", epid);
3475 /* Get the first urb. */
3476 urb = urb_list_first(epid);
3478 /* FIXME: Could this happen for valid reasons? Why did it disappear? Because of
3479 wrong unlinking? */
3480 if (!urb) {
3481 warn("NULL urb for epid %d", epid);
3482 continue;
3485 assert(urb);
3486 urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
3487 assert(urb_priv);
3489 /* Sanity checks. */
3490 assert(usb_pipetype(urb->pipe) == PIPE_BULK);
3491 if (phys_to_virt(TxBulkEPList[epid].sub) != urb_priv->last_sb) {
3492 err("bulk endpoint got disabled before reaching last sb");
3495 /* For bulk IN traffic, there seems to be a race condition between
3496 between the bulk eot and eop interrupts, or rather an uncertainty regarding
3497 the order in which they happen. Normally we expect the eop interrupt from
3498 DMA channel 9 to happen before the eot interrupt.
3500 Therefore, we complete the bulk IN urb in the rx interrupt handler instead. */
3502 if (usb_pipein(urb->pipe)) {
3503 dbg_bulk("in urb, continuing");
3504 continue;
3507 save_flags(flags);
3508 cli();
3509 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3510 nop();
3511 r_usb_ept_data = *R_USB_EPT_DATA;
3512 restore_flags(flags);
3514 if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
3515 IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3516 /* This means that the endpoint has no error, is disabled
3517 and had inserted traffic, i.e. transfer successfully completed. */
3518 etrax_usb_complete_bulk_urb(urb, 0);
3519 } else {
3520 /* Shouldn't happen. We expect errors to be caught by epid attention. */
3521 err("Found disabled bulk EP desc, error_code != no_error");
3526 /* Normally, we should find (at least) one disabled EP descriptor with a valid sub pointer.
3527 However, because of the uncertainty in the deliverance of the eop/eot interrupts, we may
3528 not. Also, we might find two disabled EPs when handling an eot interrupt, and then find
3529 none the next time. */
3531 DBFEXIT;
3535 void etrax_usb_hc_epid_attn_interrupt(usb_interrupt_registers_t *reg)
3537 /* This function handles the epid attention interrupt. There are a variety of reasons
3538 for this interrupt to happen (Designer's Reference, p. 8 - 22 for the details):
3540 invalid ep_id - Invalid epid in an EP (EP disabled).
3541 stall - Not strictly an error condition (EP disabled).
3542 3rd error - Three successive transaction errors (EP disabled).
3543 buffer ourun - Buffer overrun or underrun (EP disabled).
3544 past eof1 - Intr or isoc transaction proceeds past EOF1.
3545 near eof - Intr or isoc transaction would not fit inside the frame.
3546 zout transfer - If zout transfer for a bulk endpoint (EP disabled).
3547 setup transfer - If setup transfer for a non-ctrl endpoint (EP disabled). */
3549 int epid;
3552 DBFENTER;
3554 assert(reg != NULL);
3556 /* Note that we loop through all epids. We still want to catch errors for
3557 the invalid one, even though we might handle them differently. */
3558 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3560 if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
3562 struct urb *urb;
3563 __u32 r_usb_ept_data;
3564 unsigned long flags;
3565 int error_code;
3567 save_flags(flags);
3568 cli();
3569 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
3570 nop();
3571 /* Note that although there are separate R_USB_EPT_DATA and R_USB_EPT_DATA_ISO
3572 registers, they are located at the same address and are of the same size.
3573 In other words, this read should be ok for isoc also. */
3574 r_usb_ept_data = *R_USB_EPT_DATA;
3575 restore_flags(flags);
3577 /* First some sanity checks. */
3578 if (epid == INVALID_EPID) {
3579 /* FIXME: What if it became disabled? Could seriously hurt interrupt
3580 traffic. (Use do_intr_recover.) */
3581 warn("Got epid_attn for INVALID_EPID (%d).", epid);
3582 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3583 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3584 continue;
3585 } else if (epid == DUMMY_EPID) {
3586 /* We definitely don't care about these ones. Besides, they are
3587 always disabled, so any possible disabling caused by the
3588 epid attention interrupt is irrelevant. */
3589 warn("Got epid_attn for DUMMY_EPID (%d).", epid);
3590 continue;
3593 /* Get the first urb in the urb list for this epid. We blatantly assume
3594 that only the first urb could have caused the epid attention.
3595 (For bulk and ctrl, only one urb is active at any one time. For intr
3596 and isoc we remove them once they are completed.) */
3597 urb = urb_list_first(epid);
3599 if (urb == NULL) {
3600 err("Got epid_attn for epid %i with no urb.", epid);
3601 err("R_USB_EPT_DATA = 0x%x", r_usb_ept_data);
3602 err("R_USB_STATUS = 0x%x", reg->r_usb_status);
3603 continue;
3606 switch (usb_pipetype(urb->pipe)) {
3607 case PIPE_BULK:
3608 warn("Got epid attn for bulk endpoint, epid %d", epid);
3609 break;
3610 case PIPE_CONTROL:
3611 warn("Got epid attn for control endpoint, epid %d", epid);
3612 break;
3613 case PIPE_INTERRUPT:
3614 warn("Got epid attn for interrupt endpoint, epid %d", epid);
3615 break;
3616 case PIPE_ISOCHRONOUS:
3617 warn("Got epid attn for isochronous endpoint, epid %d", epid);
3618 break;
3621 if (usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) {
3622 if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
3623 warn("Hold was set for epid %d.", epid);
3624 continue;
3628 /* Even though error_code occupies bits 22 - 23 in both R_USB_EPT_DATA and
3629 R_USB_EPT_DATA_ISOC, we separate them here so we don't forget in other places. */
3630 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3631 error_code = IO_EXTRACT(R_USB_EPT_DATA_ISO, error_code, r_usb_ept_data);
3632 } else {
3633 error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data);
3636 /* Using IO_STATE_VALUE on R_USB_EPT_DATA should be ok for isoc also. */
3637 if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
3639 /* Isoc traffic doesn't have error_count_in/error_count_out. */
3640 if ((usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS) &&
3641 (IO_EXTRACT(R_USB_EPT_DATA, error_count_in, r_usb_ept_data) == 3 ||
3642 IO_EXTRACT(R_USB_EPT_DATA, error_count_out, r_usb_ept_data) == 3)) {
3643 /* 3rd error. */
3644 warn("3rd error for epid %i", epid);
3645 etrax_usb_complete_urb(urb, -EPROTO);
3647 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3649 warn("Perror for epid %d", epid);
3651 if (!(r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
3652 /* invalid ep_id */
3653 panic("Perror because of invalid epid."
3654 " Deconfigured too early?");
3655 } else {
3656 /* past eof1, near eof, zout transfer, setup transfer */
3658 /* Dump the urb and the relevant EP descriptor list. */
3660 __dump_urb(urb);
3661 __dump_ept_data(epid);
3662 __dump_ep_list(usb_pipetype(urb->pipe));
3664 panic("Something wrong with DMA descriptor contents."
3665 " Too much traffic inserted?");
3667 } else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3668 /* buffer ourun */
3669 panic("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3672 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, stall)) {
3673 /* Not really a protocol error, just says that the endpoint gave
3674 a stall response. Note that error_code cannot be stall for isoc. */
3675 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
3676 panic("Isoc traffic cannot stall");
3679 warn("Stall for epid %d", epid);
3680 etrax_usb_complete_urb(urb, -EPIPE);
3682 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, bus_error)) {
3683 /* Two devices responded to a transaction request. Must be resolved
3684 by software. FIXME: Reset ports? */
3685 panic("Bus error for epid %d."
3686 " Two devices responded to transaction request",
3687 epid);
3689 } else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
3690 /* DMA overrun or underrun. */
3691 warn("Buffer overrun/underrun for epid %d. DMA too busy?", epid);
3693 /* It seems that error_code = buffer_error in
3694 R_USB_EPT_DATA/R_USB_EPT_DATA_ISO and ourun = yes in R_USB_STATUS
3695 are the same error. */
3696 etrax_usb_complete_urb(urb, -EPROTO);
3701 DBFEXIT;
3705 void etrax_usb_bulk_start_timer_func(unsigned long dummy)
3708 /* We might enable an EP descriptor behind the current DMA position when it's about
3709 to decide that there are no more bulk traffic and it should stop the bulk channel.
3710 Therefore we periodically check if the bulk channel is stopped and there is an
3711 enabled bulk EP descriptor, in which case we start the bulk channel. */
3712 dbg_bulk("bulk_start_timer timed out.");
3714 if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
3715 int epid;
3717 dbg_bulk("Bulk DMA channel not running.");
3719 for (epid = 0; epid < NBR_OF_EPIDS; epid++) {
3720 if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
3721 dbg_bulk("Found enabled EP for epid %d, starting bulk channel.\n",
3722 epid);
3723 *R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
3725 /* Restart the bulk eot timer since we just started the bulk channel. */
3726 mod_timer(&bulk_eot_timer, jiffies + BULK_EOT_TIMER_INTERVAL);
3728 /* No need to search any further. */
3729 break;
3732 } else {
3733 dbg_bulk("Bulk DMA channel running.");
3737 void etrax_usb_hc_port_status_interrupt(usb_interrupt_registers_t *reg)
3739 etrax_hc_t *hc = reg->hc;
3740 __u16 r_usb_rh_port_status_1 = reg->r_usb_rh_port_status_1;
3741 __u16 r_usb_rh_port_status_2 = reg->r_usb_rh_port_status_2;
3743 DBFENTER;
3745 /* The Etrax RH does not include a wPortChange register, so this has to be handled in software
3746 (by saving the old port status value for comparison when the port status interrupt happens).
3747 See section 11.16.2.6.2 in the USB 1.1 spec for details. */
3749 dbg_rh("hc->rh.prev_wPortStatus_1 = 0x%x", hc->rh.prev_wPortStatus_1);
3750 dbg_rh("hc->rh.prev_wPortStatus_2 = 0x%x", hc->rh.prev_wPortStatus_2);
3751 dbg_rh("r_usb_rh_port_status_1 = 0x%x", r_usb_rh_port_status_1);
3752 dbg_rh("r_usb_rh_port_status_2 = 0x%x", r_usb_rh_port_status_2);
3754 /* C_PORT_CONNECTION is set on any transition. */
3755 hc->rh.wPortChange_1 |=
3756 ((r_usb_rh_port_status_1 & (1 << RH_PORT_CONNECTION)) !=
3757 (hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_CONNECTION))) ?
3758 (1 << RH_PORT_CONNECTION) : 0;
3760 hc->rh.wPortChange_2 |=
3761 ((r_usb_rh_port_status_2 & (1 << RH_PORT_CONNECTION)) !=
3762 (hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_CONNECTION))) ?
3763 (1 << RH_PORT_CONNECTION) : 0;
3765 /* C_PORT_ENABLE is _only_ set on a one to zero transition, i.e. when
3766 the port is disabled, not when it's enabled. */
3767 hc->rh.wPortChange_1 |=
3768 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_ENABLE))
3769 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
3770 (1 << RH_PORT_ENABLE) : 0;
3772 hc->rh.wPortChange_2 |=
3773 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_ENABLE))
3774 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
3775 (1 << RH_PORT_ENABLE) : 0;
3777 /* C_PORT_SUSPEND is set to one when the device has transitioned out
3778 of the suspended state, i.e. when suspend goes from one to zero. */
3779 hc->rh.wPortChange_1 |=
3780 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_SUSPEND))
3781 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_SUSPEND))) ?
3782 (1 << RH_PORT_SUSPEND) : 0;
3784 hc->rh.wPortChange_2 |=
3785 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_SUSPEND))
3786 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_SUSPEND))) ?
3787 (1 << RH_PORT_SUSPEND) : 0;
3790 /* C_PORT_RESET is set when reset processing on this port is complete. */
3791 hc->rh.wPortChange_1 |=
3792 ((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_RESET))
3793 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_RESET))) ?
3794 (1 << RH_PORT_RESET) : 0;
3796 hc->rh.wPortChange_2 |=
3797 ((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_RESET))
3798 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_RESET))) ?
3799 (1 << RH_PORT_RESET) : 0;
3801 /* Save the new values for next port status change. */
3802 hc->rh.prev_wPortStatus_1 = r_usb_rh_port_status_1;
3803 hc->rh.prev_wPortStatus_2 = r_usb_rh_port_status_2;
3805 dbg_rh("hc->rh.wPortChange_1 set to 0x%x", hc->rh.wPortChange_1);
3806 dbg_rh("hc->rh.wPortChange_2 set to 0x%x", hc->rh.wPortChange_2);
3808 DBFEXIT;
3812 void etrax_usb_hc_ctl_status_interrupt(usb_interrupt_registers_t *reg)
3814 DBFENTER;
3816 /* FIXME: What should we do if we get ourun or perror? Dump the EP and SB
3817 list for the corresponding epid? */
3818 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
3819 panic("USB controller got ourun.");
3821 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
3823 /* Before, etrax_usb_do_intr_recover was called on this epid if it was
3824 an interrupt pipe. I don't see how re-enabling all EP descriptors
3825 will help if there was a programming error. */
3826 panic("USB controller got perror.");
3829 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, device_mode)) {
3830 /* We should never operate in device mode. */
3831 panic("USB controller in device mode.");
3834 /* These if-statements could probably be nested. */
3835 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, host_mode)) {
3836 info("USB controller in host mode.");
3838 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, started)) {
3839 info("USB controller started.");
3841 if (reg->r_usb_status & IO_MASK(R_USB_STATUS, running)) {
3842 info("USB controller running.");
3845 DBFEXIT;
3850 static int etrax_rh_submit_urb(struct urb *urb)
3852 struct usb_device *usb_dev = urb->dev;
3853 etrax_hc_t *hc = usb_dev->bus->hcpriv;
3854 unsigned int pipe = urb->pipe;
3855 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
3856 void *data = urb->transfer_buffer;
3857 int leni = urb->transfer_buffer_length;
3858 int len = 0;
3859 int stat = 0;
3861 __u16 bmRType_bReq;
3862 __u16 wValue;
3863 __u16 wIndex;
3864 __u16 wLength;
3866 DBFENTER;
3868 /* FIXME: What is this interrupt urb that is sent to the root hub? */
3869 if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
3870 dbg_rh("Root-Hub submit IRQ: every %d ms", urb->interval);
3871 hc->rh.urb = urb;
3872 hc->rh.send = 1;
3873 /* FIXME: We could probably remove this line since it's done
3874 in etrax_rh_init_int_timer. (Don't remove it from
3875 etrax_rh_init_int_timer though.) */
3876 hc->rh.interval = urb->interval;
3877 etrax_rh_init_int_timer(urb);
3878 DBFEXIT;
3880 return 0;
3883 bmRType_bReq = cmd->bRequestType | (cmd->bRequest << 8);
3884 wValue = le16_to_cpu(cmd->wValue);
3885 wIndex = le16_to_cpu(cmd->wIndex);
3886 wLength = le16_to_cpu(cmd->wLength);
3888 dbg_rh("bmRType_bReq : 0x%04x (%d)", bmRType_bReq, bmRType_bReq);
3889 dbg_rh("wValue : 0x%04x (%d)", wValue, wValue);
3890 dbg_rh("wIndex : 0x%04x (%d)", wIndex, wIndex);
3891 dbg_rh("wLength : 0x%04x (%d)", wLength, wLength);
3893 switch (bmRType_bReq) {
3895 /* Request Destination:
3896 without flags: Device,
3897 RH_INTERFACE: interface,
3898 RH_ENDPOINT: endpoint,
3899 RH_CLASS means HUB here,
3900 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
3903 case RH_GET_STATUS:
3904 *(__u16 *) data = cpu_to_le16 (1);
3905 OK (2);
3907 case RH_GET_STATUS | RH_INTERFACE:
3908 *(__u16 *) data = cpu_to_le16 (0);
3909 OK (2);
3911 case RH_GET_STATUS | RH_ENDPOINT:
3912 *(__u16 *) data = cpu_to_le16 (0);
3913 OK (2);
3915 case RH_GET_STATUS | RH_CLASS:
3916 *(__u32 *) data = cpu_to_le32 (0);
3917 OK (4); /* hub power ** */
3919 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
3920 if (wIndex == 1) {
3921 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_1);
3922 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_1);
3923 } else if (wIndex == 2) {
3924 *((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_2);
3925 *((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_2);
3926 } else {
3927 dbg_rh("RH_GET_STATUS whith invalid wIndex!");
3928 OK(0);
3931 OK(4);
3933 case RH_CLEAR_FEATURE | RH_ENDPOINT:
3934 switch (wValue) {
3935 case (RH_ENDPOINT_STALL):
3936 OK (0);
3938 break;
3940 case RH_CLEAR_FEATURE | RH_CLASS:
3941 switch (wValue) {
3942 case (RH_C_HUB_OVER_CURRENT):
3943 OK (0); /* hub power over current ** */
3945 break;
3947 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
3948 switch (wValue) {
3949 case (RH_PORT_ENABLE):
3950 if (wIndex == 1) {
3952 dbg_rh("trying to do disable port 1");
3954 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
3956 while (hc->rh.prev_wPortStatus_1 &
3957 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes));
3958 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
3959 dbg_rh("Port 1 is disabled");
3961 } else if (wIndex == 2) {
3963 dbg_rh("trying to do disable port 2");
3965 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
3967 while (hc->rh.prev_wPortStatus_2 &
3968 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes));
3969 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
3970 dbg_rh("Port 2 is disabled");
3972 } else {
3973 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_ENABLE "
3974 "with invalid wIndex == %d!", wIndex);
3977 OK (0);
3978 case (RH_PORT_SUSPEND):
3979 /* Opposite to suspend should be resume, so we'll do a resume. */
3980 /* FIXME: USB 1.1, 11.16.2.2 says:
3981 "Clearing the PORT_SUSPEND feature causes a host-initiated resume
3982 on the specified port. If the port is not in the Suspended state,
3983 the hub should treat this request as a functional no-operation."
3984 Shouldn't we check if the port is in a suspended state before
3985 resuming? */
3987 /* Make sure the controller isn't busy. */
3988 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
3990 if (wIndex == 1) {
3991 *R_USB_COMMAND =
3992 IO_STATE(R_USB_COMMAND, port_sel, port1) |
3993 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3994 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
3995 } else if (wIndex == 2) {
3996 *R_USB_COMMAND =
3997 IO_STATE(R_USB_COMMAND, port_sel, port2) |
3998 IO_STATE(R_USB_COMMAND, port_cmd, resume) |
3999 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4000 } else {
4001 dbg_rh("RH_CLEAR_FEATURE->RH_PORT_SUSPEND "
4002 "with invalid wIndex == %d!", wIndex);
4005 OK (0);
4006 case (RH_PORT_POWER):
4007 OK (0); /* port power ** */
4008 case (RH_C_PORT_CONNECTION):
4009 if (wIndex == 1) {
4010 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_CONNECTION);
4011 } else if (wIndex == 2) {
4012 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_CONNECTION);
4013 } else {
4014 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_CONNECTION "
4015 "with invalid wIndex == %d!", wIndex);
4018 OK (0);
4019 case (RH_C_PORT_ENABLE):
4020 if (wIndex == 1) {
4021 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_ENABLE);
4022 } else if (wIndex == 2) {
4023 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_ENABLE);
4024 } else {
4025 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_ENABLE "
4026 "with invalid wIndex == %d!", wIndex);
4028 OK (0);
4029 case (RH_C_PORT_SUSPEND):
4030 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
4031 OK (0);
4032 case (RH_C_PORT_OVER_CURRENT):
4033 OK (0); /* port power over current ** */
4034 case (RH_C_PORT_RESET):
4035 if (wIndex == 1) {
4036 hc->rh.wPortChange_1 &= ~(1 << RH_PORT_RESET);
4037 } else if (wIndex == 2) {
4038 hc->rh.wPortChange_2 &= ~(1 << RH_PORT_RESET);
4039 } else {
4040 dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_RESET "
4041 "with invalid index == %d!", wIndex);
4044 OK (0);
4047 break;
4049 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
4050 switch (wValue) {
4051 case (RH_PORT_SUSPEND):
4053 /* Make sure the controller isn't busy. */
4054 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4056 if (wIndex == 1) {
4057 *R_USB_COMMAND =
4058 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4059 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4060 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4061 } else if (wIndex == 2) {
4062 *R_USB_COMMAND =
4063 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4064 IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
4065 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4066 } else {
4067 dbg_rh("RH_SET_FEATURE->RH_PORT_SUSPEND "
4068 "with invalid wIndex == %d!", wIndex);
4071 OK (0);
4072 case (RH_PORT_RESET):
4073 if (wIndex == 1) {
4075 port_1_reset:
4076 dbg_rh("Doing reset of port 1");
4078 /* Make sure the controller isn't busy. */
4079 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4081 *R_USB_COMMAND =
4082 IO_STATE(R_USB_COMMAND, port_sel, port1) |
4083 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4084 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4086 /* We must wait at least 10 ms for the device to recover.
4087 15 ms should be enough. */
4088 udelay(15000);
4090 /* Wait for reset bit to go low (should be done by now). */
4091 while (hc->rh.prev_wPortStatus_1 &
4092 IO_STATE(R_USB_RH_PORT_STATUS_1, reset, yes));
4094 /* If the port status is
4095 1) connected and enabled then there is a device and everything is fine
4096 2) neither connected nor enabled then there is no device, also fine
4097 3) connected and not enabled then we try again
4098 (Yes, there are other port status combinations besides these.) */
4100 if ((hc->rh.prev_wPortStatus_1 &
4101 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4102 (hc->rh.prev_wPortStatus_1 &
4103 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4104 dbg_rh("Connected device on port 1, but port not enabled?"
4105 " Trying reset again.");
4106 goto port_2_reset;
4109 /* Diagnostic printouts. */
4110 if ((hc->rh.prev_wPortStatus_1 &
4111 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, no)) &&
4112 (hc->rh.prev_wPortStatus_1 &
4113 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no))) {
4114 dbg_rh("No connected device on port 1");
4115 } else if ((hc->rh.prev_wPortStatus_1 &
4116 IO_STATE(R_USB_RH_PORT_STATUS_1, connected, yes)) &&
4117 (hc->rh.prev_wPortStatus_1 &
4118 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes))) {
4119 dbg_rh("Connected device on port 1, port 1 enabled");
4122 } else if (wIndex == 2) {
4124 port_2_reset:
4125 dbg_rh("Doing reset of port 2");
4127 /* Make sure the controller isn't busy. */
4128 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4130 /* Issue the reset command. */
4131 *R_USB_COMMAND =
4132 IO_STATE(R_USB_COMMAND, port_sel, port2) |
4133 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4134 IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
4136 /* We must wait at least 10 ms for the device to recover.
4137 15 ms should be enough. */
4138 udelay(15000);
4140 /* Wait for reset bit to go low (should be done by now). */
4141 while (hc->rh.prev_wPortStatus_2 &
4142 IO_STATE(R_USB_RH_PORT_STATUS_2, reset, yes));
4144 /* If the port status is
4145 1) connected and enabled then there is a device and everything is fine
4146 2) neither connected nor enabled then there is no device, also fine
4147 3) connected and not enabled then we try again
4148 (Yes, there are other port status combinations besides these.) */
4150 if ((hc->rh.prev_wPortStatus_2 &
4151 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4152 (hc->rh.prev_wPortStatus_2 &
4153 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4154 dbg_rh("Connected device on port 2, but port not enabled?"
4155 " Trying reset again.");
4156 goto port_2_reset;
4159 /* Diagnostic printouts. */
4160 if ((hc->rh.prev_wPortStatus_2 &
4161 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, no)) &&
4162 (hc->rh.prev_wPortStatus_2 &
4163 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no))) {
4164 dbg_rh("No connected device on port 2");
4165 } else if ((hc->rh.prev_wPortStatus_2 &
4166 IO_STATE(R_USB_RH_PORT_STATUS_2, connected, yes)) &&
4167 (hc->rh.prev_wPortStatus_2 &
4168 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes))) {
4169 dbg_rh("Connected device on port 2, port 2 enabled");
4172 } else {
4173 dbg_rh("RH_SET_FEATURE->RH_PORT_RESET with invalid wIndex = %d", wIndex);
4176 /* Make sure the controller isn't busy. */
4177 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4179 /* If all enabled ports were disabled the host controller goes down into
4180 started mode, so we need to bring it back into the running state.
4181 (This is safe even if it's already in the running state.) */
4182 *R_USB_COMMAND =
4183 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4184 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4185 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4187 dbg_rh("...Done");
4188 OK(0);
4190 case (RH_PORT_POWER):
4191 OK (0); /* port power ** */
4192 case (RH_PORT_ENABLE):
4193 /* There is no port enable command in the host controller, so if the
4194 port is already enabled, we do nothing. If not, we reset the port
4195 (with an ugly goto). */
4197 if (wIndex == 1) {
4198 if (hc->rh.prev_wPortStatus_1 &
4199 IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, no)) {
4200 goto port_1_reset;
4202 } else if (wIndex == 2) {
4203 if (hc->rh.prev_wPortStatus_2 &
4204 IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, no)) {
4205 goto port_2_reset;
4207 } else {
4208 dbg_rh("RH_SET_FEATURE->RH_GET_STATUS with invalid wIndex = %d", wIndex);
4210 OK (0);
4212 break;
4214 case RH_SET_ADDRESS:
4215 hc->rh.devnum = wValue;
4216 dbg_rh("RH address set to: %d", hc->rh.devnum);
4217 OK (0);
4219 case RH_GET_DESCRIPTOR:
4220 switch ((wValue & 0xff00) >> 8) {
4221 case (0x01): /* device descriptor */
4222 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_dev_des), wLength));
4223 memcpy (data, root_hub_dev_des, len);
4224 OK (len);
4225 case (0x02): /* configuration descriptor */
4226 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_config_des), wLength));
4227 memcpy (data, root_hub_config_des, len);
4228 OK (len);
4229 case (0x03): /* string descriptors */
4230 len = usb_root_hub_string (wValue & 0xff,
4231 0xff, "ETRAX 100LX",
4232 data, wLength);
4233 if (len > 0) {
4234 OK(min(leni, len));
4235 } else {
4236 stat = -EPIPE;
4240 break;
4242 case RH_GET_DESCRIPTOR | RH_CLASS:
4243 root_hub_hub_des[2] = hc->rh.numports;
4244 len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
4245 memcpy (data, root_hub_hub_des, len);
4246 OK (len);
4248 case RH_GET_CONFIGURATION:
4249 *(__u8 *) data = 0x01;
4250 OK (1);
4252 case RH_SET_CONFIGURATION:
4253 OK (0);
4255 default:
4256 stat = -EPIPE;
4259 urb->actual_length = len;
4260 urb->status = stat;
4261 urb->dev = NULL;
4262 if (urb->complete) {
4263 urb->complete(urb, NULL);
4265 DBFEXIT;
4267 return 0;
4270 static void
4271 etrax_usb_bulk_eot_timer_func(unsigned long dummy)
4273 /* Because of a race condition in the top half, we might miss a bulk eot.
4274 This timer "simulates" a bulk eot if we don't get one for a while, hopefully
4275 correcting the situation. */
4276 dbg_bulk("bulk_eot_timer timed out.");
4277 etrax_usb_hc_bulk_eot_interrupt(1);
4280 static void*
4281 etrax_usb_buffer_alloc(struct usb_bus* bus, size_t size,
4282 unsigned mem_flags, dma_addr_t *dma)
4284 return kmalloc(size, mem_flags);
4287 static void
4288 etrax_usb_buffer_free(struct usb_bus *bus, size_t size, void *addr, dma_addr_t dma)
4290 kfree(addr);
4294 static struct device fake_device;
4296 static int __init etrax_usb_hc_init(void)
4298 static etrax_hc_t *hc;
4299 struct usb_bus *bus;
4300 struct usb_device *usb_rh;
4301 int i;
4303 DBFENTER;
4305 info("ETRAX 100LX USB-HCD %s (c) 2001-2003 Axis Communications AB\n", usb_hcd_version);
4307 hc = kmalloc(sizeof(etrax_hc_t), GFP_KERNEL);
4308 assert(hc != NULL);
4310 /* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
4311 /* Note that we specify sizeof(USB_EP_Desc_t) as the size, but also allocate
4312 SB descriptors from this cache. This is ok since sizeof(USB_EP_Desc_t) ==
4313 sizeof(USB_SB_Desc_t). */
4315 usb_desc_cache = kmem_cache_create("usb_desc_cache", sizeof(USB_EP_Desc_t), 0,
4316 SLAB_HWCACHE_ALIGN, 0, 0);
4317 assert(usb_desc_cache != NULL);
4319 top_half_reg_cache = kmem_cache_create("top_half_reg_cache",
4320 sizeof(usb_interrupt_registers_t),
4321 0, SLAB_HWCACHE_ALIGN, 0, 0);
4322 assert(top_half_reg_cache != NULL);
4324 isoc_compl_cache = kmem_cache_create("isoc_compl_cache",
4325 sizeof(usb_isoc_complete_data_t),
4326 0, SLAB_HWCACHE_ALIGN, 0, 0);
4327 assert(isoc_compl_cache != NULL);
4329 etrax_usb_bus = bus = usb_alloc_bus(&etrax_usb_device_operations);
4330 hc->bus = bus;
4331 bus->bus_name="ETRAX 100LX";
4332 bus->hcpriv = hc;
4334 /* Initialize RH to the default address.
4335 And make sure that we have no status change indication */
4336 hc->rh.numports = 2; /* The RH has two ports */
4337 hc->rh.devnum = 1;
4338 hc->rh.wPortChange_1 = 0;
4339 hc->rh.wPortChange_2 = 0;
4341 /* Also initate the previous values to zero */
4342 hc->rh.prev_wPortStatus_1 = 0;
4343 hc->rh.prev_wPortStatus_2 = 0;
4345 /* Initialize the intr-traffic flags */
4346 /* FIXME: This isn't used. (Besides, the error field isn't initialized.) */
4347 hc->intr.sleeping = 0;
4348 hc->intr.wq = NULL;
4350 epid_usage_bitmask = 0;
4351 epid_out_traffic = 0;
4353 /* Mark the invalid epid as being used. */
4354 set_bit(INVALID_EPID, (void *)&epid_usage_bitmask);
4355 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, INVALID_EPID);
4356 nop();
4357 /* The valid bit should still be set ('invalid' is in our world; not the hardware's). */
4358 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, yes) |
4359 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4361 /* Mark the dummy epid as being used. */
4362 set_bit(DUMMY_EPID, (void *)&epid_usage_bitmask);
4363 *R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, DUMMY_EPID);
4364 nop();
4365 *R_USB_EPT_DATA = (IO_STATE(R_USB_EPT_DATA, valid, no) |
4366 IO_FIELD(R_USB_EPT_DATA, max_len, 1));
4368 /* Initialize the urb list by initiating a head for each list. */
4369 for (i = 0; i < NBR_OF_EPIDS; i++) {
4370 INIT_LIST_HEAD(&urb_list[i]);
4372 spin_lock_init(&urb_list_lock);
4374 INIT_LIST_HEAD(&urb_unlink_list);
4377 /* Initiate the bulk start timer. */
4378 init_timer(&bulk_start_timer);
4379 bulk_start_timer.expires = jiffies + BULK_START_TIMER_INTERVAL;
4380 bulk_start_timer.function = etrax_usb_bulk_start_timer_func;
4381 add_timer(&bulk_start_timer);
4384 /* Initiate the bulk eot timer. */
4385 init_timer(&bulk_eot_timer);
4386 bulk_eot_timer.expires = jiffies + BULK_EOT_TIMER_INTERVAL;
4387 bulk_eot_timer.function = etrax_usb_bulk_eot_timer_func;
4388 add_timer(&bulk_eot_timer);
4390 /* Set up the data structures for USB traffic. Note that this must be done before
4391 any interrupt that relies on sane DMA list occurrs. */
4392 init_rx_buffers();
4393 init_tx_bulk_ep();
4394 init_tx_ctrl_ep();
4395 init_tx_intr_ep();
4396 init_tx_isoc_ep();
4398 device_initialize(&fake_device);
4399 kobject_set_name(&fake_device.kobj, "etrax_usb");
4400 kobject_add(&fake_device.kobj);
4401 kobject_hotplug(&fake_device.kobj, KOBJ_ADD);
4402 hc->bus->controller = &fake_device;
4403 usb_register_bus(hc->bus);
4405 *R_IRQ_MASK2_SET =
4406 /* Note that these interrupts are not used. */
4407 IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
4408 /* Sub channel 1 (ctrl) descr. interrupts are used. */
4409 IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
4410 IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
4411 /* Sub channel 3 (isoc) descr. interrupts are used. */
4412 IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
4414 /* Note that the dma9_descr interrupt is not used. */
4415 *R_IRQ_MASK2_SET =
4416 IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
4417 IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
4419 /* FIXME: Enable iso_eof only when isoc traffic is running. */
4420 *R_USB_IRQ_MASK_SET =
4421 IO_STATE(R_USB_IRQ_MASK_SET, iso_eof, set) |
4422 IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
4423 IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
4424 IO_STATE(R_USB_IRQ_MASK_SET, port_status, set) |
4425 IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set);
4428 if (request_irq(ETRAX_USB_HC_IRQ, etrax_usb_hc_interrupt_top_half, 0,
4429 "ETRAX 100LX built-in USB (HC)", hc)) {
4430 err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
4431 etrax_usb_hc_cleanup();
4432 DBFEXIT;
4433 return -1;
4436 if (request_irq(ETRAX_USB_RX_IRQ, etrax_usb_rx_interrupt, 0,
4437 "ETRAX 100LX built-in USB (Rx)", hc)) {
4438 err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
4439 etrax_usb_hc_cleanup();
4440 DBFEXIT;
4441 return -1;
4444 if (request_irq(ETRAX_USB_TX_IRQ, etrax_usb_tx_interrupt, 0,
4445 "ETRAX 100LX built-in USB (Tx)", hc)) {
4446 err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
4447 etrax_usb_hc_cleanup();
4448 DBFEXIT;
4449 return -1;
4452 /* R_USB_COMMAND:
4453 USB commands in host mode. The fields in this register should all be
4454 written to in one write. Do not read-modify-write one field at a time. A
4455 write to this register will trigger events in the USB controller and an
4456 incomplete command may lead to unpredictable results, and in worst case
4457 even to a deadlock in the controller.
4458 (Note however that the busy field is read-only, so no need to write to it.) */
4460 /* Check the busy bit before writing to R_USB_COMMAND. */
4462 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4464 /* Reset the USB interface. */
4465 *R_USB_COMMAND =
4466 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4467 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4468 IO_STATE(R_USB_COMMAND, ctrl_cmd, reset);
4470 /* Designer's Reference, p. 8 - 10 says we should Initate R_USB_FM_PSTART to 0x2A30 (10800),
4471 to guarantee that control traffic gets 10% of the bandwidth, and periodic transfer may
4472 allocate the rest (90%). This doesn't work though. Read on for a lenghty explanation.
4474 While there is a difference between rev. 2 and rev. 3 of the ETRAX 100LX regarding the NAK
4475 behaviour, it doesn't solve this problem. What happens is that a control transfer will not
4476 be interrupted in its data stage when PSTART happens (the point at which periodic traffic
4477 is started). Thus, if PSTART is set to 10800 and its IN or OUT token is NAKed until just before
4478 PSTART happens, it will continue the IN/OUT transfer as long as it's ACKed. After it's done,
4479 there may be too little time left for an isochronous transfer, causing an epid attention
4480 interrupt due to perror. The work-around for this is to let the control transfers run at the
4481 end of the frame instead of at the beginning, and will be interrupted just fine if it doesn't
4482 fit into the frame. However, since there will *always* be a control transfer at the beginning
4483 of the frame, regardless of what we set PSTART to, that transfer might be a 64-byte transfer
4484 which consumes up to 15% of the frame, leaving only 85% for periodic traffic. The solution to
4485 this would be to 'dummy allocate' 5% of the frame with the usb_claim_bandwidth function to make
4486 sure that the periodic transfers that are inserted will always fit in the frame.
4488 The idea was suggested that a control transfer could be split up into several 8 byte transfers,
4489 so that it would be interrupted by PSTART, but since this can't be done for an IN transfer this
4490 hasn't been implemented.
4492 The value 11960 is chosen to be just after the SOF token, with a couple of bit times extra
4493 for possible bit stuffing. */
4495 *R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 11960);
4497 #ifdef CONFIG_ETRAX_USB_HOST_PORT1
4498 *R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
4499 #endif
4501 #ifdef CONFIG_ETRAX_USB_HOST_PORT2
4502 *R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
4503 #endif
4505 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4507 /* Configure the USB interface as a host controller. */
4508 *R_USB_COMMAND =
4509 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4510 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4511 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config);
4513 /* Note: Do not reset any ports here. Await the port status interrupts, to have a controlled
4514 sequence of resetting the ports. If we reset both ports now, and there are devices
4515 on both ports, we will get a bus error because both devices will answer the set address
4516 request. */
4518 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4520 /* Start processing of USB traffic. */
4521 *R_USB_COMMAND =
4522 IO_STATE(R_USB_COMMAND, port_sel, nop) |
4523 IO_STATE(R_USB_COMMAND, port_cmd, reset) |
4524 IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
4526 while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
4528 usb_rh = usb_alloc_dev(NULL, hc->bus, 0);
4529 hc->bus->root_hub = usb_rh;
4530 usb_rh->state = USB_STATE_ADDRESS;
4531 usb_rh->speed = USB_SPEED_FULL;
4532 usb_rh->devnum = 1;
4533 hc->bus->devnum_next = 2;
4534 usb_rh->ep0.desc.wMaxPacketSize = __const_cpu_to_le16(64);
4535 usb_get_device_descriptor(usb_rh, USB_DT_DEVICE_SIZE);
4536 usb_new_device(usb_rh);
4538 DBFEXIT;
4540 return 0;
4543 static void etrax_usb_hc_cleanup(void)
4545 DBFENTER;
4547 free_irq(ETRAX_USB_HC_IRQ, NULL);
4548 free_irq(ETRAX_USB_RX_IRQ, NULL);
4549 free_irq(ETRAX_USB_TX_IRQ, NULL);
4551 usb_deregister_bus(etrax_usb_bus);
4553 /* FIXME: call kmem_cache_destroy here? */
4555 DBFEXIT;
4558 module_init(etrax_usb_hc_init);
4559 module_exit(etrax_usb_hc_cleanup);