File: /usr/src/linux/arch/cris/drivers/usb-host.c

1     /*
2      * usb-host.c: ETRAX 100LX USB Host Controller Driver (HCD)
3      *
4      * Copyright (c) 2001 Axis Communications AB.
5      *
6      * $Id: usb-host.c,v 1.9 2001/05/09 12:54:12 johana Exp $
7      *
8      */
9     
10     #include <linux/config.h>
11     #include <linux/kernel.h>
12     #include <linux/delay.h>
13     #include <linux/ioport.h>
14     #include <linux/sched.h>
15     #include <linux/slab.h>
16     #include <linux/errno.h>
17     #include <linux/unistd.h>
18     #include <linux/interrupt.h>
19     #include <linux/init.h>
20     #include <linux/version.h>
21     #include <linux/list.h>
22     
23     #include <asm/uaccess.h>
24     #include <asm/io.h>
25     #include <asm/irq.h>
26     #include <asm/dma.h>
27     #include <asm/system.h>
28     #include <asm/svinto.h>
29     
30     #include <linux/usb.h>
31     #include "usb-host.h"
32     
33     #define ETRAX_USB_HC_IRQ USB_HC_IRQ_NBR
34     #define ETRAX_USB_RX_IRQ USB_DMA_RX_IRQ_NBR
35     #define ETRAX_USB_TX_IRQ USB_DMA_TX_IRQ_NBR
36     
37     static const char *usb_hcd_version = "$Revision: 1.9 $";
38     
39     #undef KERN_DEBUG
40     #define KERN_DEBUG ""
41     
42     #undef USB_DEBUG_RH
43     #undef USB_DEBUG_EP
44     #undef USB_DEBUG_DESC
45     #undef USB_DEBUG_TRACE
46     #undef USB_DEBUG_CTRL
47     #undef USB_DEBUG_BULK
48     #undef USB_DEBUG_INTR
49     
50     #ifdef USB_DEBUG_RH
51     #define dbg_rh(format, arg...) printk(KERN_DEBUG __FILE__ ": (RH) " format "\n" , ## arg)
52     #else
53     #define dbg_rh(format, arg...) do {} while (0)
54     #endif
55     
56     #ifdef USB_DEBUG_EP
57     #define dbg_ep(format, arg...) printk(KERN_DEBUG __FILE__ ": (EP) " format "\n" , ## arg)
58     #else
59     #define dbg_ep(format, arg...) do {} while (0)
60     #endif
61     
62     #ifdef USB_DEBUG_CTRL
63     #define dbg_ctrl(format, arg...) printk(KERN_DEBUG __FILE__ ": (CTRL) " format "\n" , ## arg)
64     #else
65     #define dbg_ctrl(format, arg...) do {} while (0)
66     #endif
67     
68     #ifdef USB_DEBUG_BULK
69     #define dbg_bulk(format, arg...) printk(KERN_DEBUG __FILE__ ": (BULK) " format "\n" , ## arg)
70     #else
71     #define dbg_bulk(format, arg...) do {} while (0)
72     #endif
73     
74     #ifdef USB_DEBUG_INTR
75     #define dbg_intr(format, arg...) printk(KERN_DEBUG __FILE__ ": (INTR) " format "\n" , ## arg)
76     #else
77     #define dbg_intr(format, arg...) do {} while (0)
78     #endif
79     
80     #ifdef USB_DEBUG_TRACE
81     #define DBFENTER (printk(KERN_DEBUG __FILE__ ": Entering : " __FUNCTION__ "\n"))
82     #define DBFEXIT  (printk(KERN_DEBUG __FILE__ ": Exiting  : " __FUNCTION__ "\n"))
83     #else
84     #define DBFENTER (NULL)
85     #define DBFEXIT  (NULL)
86     #endif
87     
88     /*-------------------------------------------------------------------
89      Virtual Root Hub
90      -------------------------------------------------------------------*/
91     
92     static __u8 root_hub_dev_des[] =
93     {
94     	0x12,  /*  __u8  bLength; */
95     	0x01,  /*  __u8  bDescriptorType; Device */
96     	0x00,  /*  __u16 bcdUSB; v1.0 */
97     	0x01,
98     	0x09,  /*  __u8  bDeviceClass; HUB_CLASSCODE */
99     	0x00,  /*  __u8  bDeviceSubClass; */
100     	0x00,  /*  __u8  bDeviceProtocol; */
101     	0x08,  /*  __u8  bMaxPacketSize0; 8 Bytes */
102     	0x00,  /*  __u16 idVendor; */
103     	0x00,
104     	0x00,  /*  __u16 idProduct; */
105     	0x00,
106     	0x00,  /*  __u16 bcdDevice; */
107     	0x00,
108     	0x00,  /*  __u8  iManufacturer; */
109     	0x02,  /*  __u8  iProduct; */
110     	0x01,  /*  __u8  iSerialNumber; */
111     	0x01   /*  __u8  bNumConfigurations; */
112     };
113     
114     /* Configuration descriptor */
115     static __u8 root_hub_config_des[] =
116     {
117     	0x09,  /*  __u8  bLength; */
118     	0x02,  /*  __u8  bDescriptorType; Configuration */
119     	0x19,  /*  __u16 wTotalLength; */
120     	0x00,
121     	0x01,  /*  __u8  bNumInterfaces; */
122     	0x01,  /*  __u8  bConfigurationValue; */
123     	0x00,  /*  __u8  iConfiguration; */
124     	0x40,  /*  __u8  bmAttributes; Bit 7: Bus-powered */
125     	0x00,  /*  __u8  MaxPower; */
126     
127          /* interface */
128     	0x09,  /*  __u8  if_bLength; */
129     	0x04,  /*  __u8  if_bDescriptorType; Interface */
130     	0x00,  /*  __u8  if_bInterfaceNumber; */
131     	0x00,  /*  __u8  if_bAlternateSetting; */
132     	0x01,  /*  __u8  if_bNumEndpoints; */
133     	0x09,  /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
134     	0x00,  /*  __u8  if_bInterfaceSubClass; */
135     	0x00,  /*  __u8  if_bInterfaceProtocol; */
136     	0x00,  /*  __u8  if_iInterface; */
137     
138          /* endpoint */
139     	0x07,  /*  __u8  ep_bLength; */
140     	0x05,  /*  __u8  ep_bDescriptorType; Endpoint */
141     	0x81,  /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
142     	0x03,  /*  __u8  ep_bmAttributes; Interrupt */
143     	0x08,  /*  __u16 ep_wMaxPacketSize; 8 Bytes */
144     	0x00,
145     	0xff   /*  __u8  ep_bInterval; 255 ms */
146     };
147     
148     static __u8 root_hub_hub_des[] =
149     {
150     	0x09,  /*  __u8  bLength; */
151     	0x29,  /*  __u8  bDescriptorType; Hub-descriptor */
152     	0x02,  /*  __u8  bNbrPorts; */
153     	0x00,  /* __u16  wHubCharacteristics; */
154     	0x00,
155     	0x01,  /*  __u8  bPwrOn2pwrGood; 2ms */
156     	0x00,  /*  __u8  bHubContrCurrent; 0 mA */
157     	0x00,  /*  __u8  DeviceRemovable; *** 7 Ports max *** */
158     	0xff   /*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
159     };
160     
161     
162     #define OK(x) len = (x); dbg_rh("OK(%d): line: %d", x, __LINE__); break
163     #define CHECK_ALIGN(x) if (((__u32)(x)) & 0x00000003) \
164     {panic("Alignment check (DWORD) failed at %s:%s:%d\n", __FILE__, __FUNCTION__, __LINE__);}
165     
166     static submit_urb_count = 0;
167     
168     //#define ETRAX_USB_INTR_IRQ
169     //#define ETRAX_USB_INTR_ERROR_FATAL
170     
171     #define RX_BUF_SIZE        32768
172     #define RX_DESC_BUF_SIZE   64
173     #define NBR_OF_RX_DESC     (RX_BUF_SIZE / RX_DESC_BUF_SIZE)
174     
175     #define NBR_OF_EP_DESC     32
176     
177     #define MAX_INTR_INTERVAL 128
178     
179     static __u32 ep_usage_bitmask;
180     static __u32 ep_really_active;
181     
182     static unsigned char RxBuf[RX_BUF_SIZE];
183     static USB_IN_Desc_t RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned (4)));
184     
185     static volatile USB_IN_Desc_t *myNextRxDesc;
186     static volatile USB_IN_Desc_t *myLastRxDesc;
187     static volatile USB_IN_Desc_t *myPrevRxDesc;
188     
189     static USB_EP_Desc_t TxCtrlEPList[NBR_OF_EP_DESC] __attribute__ ((aligned (4)));
190     static USB_EP_Desc_t TxBulkEPList[NBR_OF_EP_DESC] __attribute__ ((aligned (4)));
191     
192     static USB_EP_Desc_t TxIntrEPList[MAX_INTR_INTERVAL] __attribute__ ((aligned (4)));
193     static USB_SB_Desc_t TxIntrSB_zout __attribute__ ((aligned (4)));
194     
195     static urb_t *URB_List[NBR_OF_EP_DESC];
196     static kmem_cache_t *usb_desc_cache;
197     static struct usb_bus *etrax_usb_bus;
198     
199     static void dump_urb (purb_t purb);
200     static void init_rx_buffers(void);
201     static int etrax_rh_unlink_urb (urb_t *urb);
202     static void etrax_rh_send_irq(urb_t *urb);
203     static void etrax_rh_init_int_timer(urb_t *urb);
204     static void etrax_rh_int_timer_do(unsigned long ptr);
205     
206     static void etrax_usb_setup_epid(char epid, char devnum, char endpoint,
207     				 char packsize, char slow);
208     static int etrax_usb_lookup_epid(unsigned char devnum, char endpoint, char slow, int maxp);
209     static int etrax_usb_allocate_epid(void);
210     static void etrax_usb_free_epid(char epid);
211     static void cleanup_sb(USB_SB_Desc_t *sb);
212     
213     static int etrax_usb_do_ctrl_hw_add(urb_t *urb, char epid, char maxlen);
214     static int etrax_usb_do_bulk_hw_add(urb_t *urb, char epid, char maxlen);
215     
216     static int etrax_usb_submit_ctrl_urb(urb_t *urb);
217     
218     static int etrax_usb_submit_urb(urb_t *urb);
219     static int etrax_usb_unlink_urb(urb_t *urb);
220     static int etrax_usb_get_frame_number(struct usb_device *usb_dev);
221     static int etrax_usb_allocate_dev(struct usb_device *usb_dev);
222     static int etrax_usb_deallocate_dev(struct usb_device *usb_dev);
223     
224     static void etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs);
225     static void etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs);
226     static void etrax_usb_hc_intr_top_half(int irq, void *vhc, struct pt_regs *regs);
227     
228     static int etrax_rh_submit_urb (urb_t *urb);
229     
230     static int etrax_usb_hc_init(void);
231     static void etrax_usb_hc_cleanup(void);
232     
233     static struct usb_operations etrax_usb_device_operations =
234     {
235     	etrax_usb_allocate_dev,
236     	etrax_usb_deallocate_dev,
237     	etrax_usb_get_frame_number,
238     	etrax_usb_submit_urb,
239     	etrax_usb_unlink_urb
240     };
241     
242     #ifdef USB_DEBUG_DESC
243     static void dump_urb(purb_t purb)
244     {
245     	printk("\nurb                   :0x%08X\n", purb);
246     	printk("next                  :0x%08X\n", purb->next);
247     	printk("dev                   :0x%08X\n", purb->dev);
248     	printk("pipe                  :0x%08X\n", purb->pipe);
249     	printk("status                :%d\n", purb->status);
250     	printk("transfer_flags        :0x%08X\n", purb->transfer_flags);
251     	printk("transfer_buffer       :0x%08X\n", purb->transfer_buffer);
252     	printk("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
253     	printk("actual_length         :%d\n", purb->actual_length);
254     	printk("setup_packet          :0x%08X\n", purb->setup_packet);
255     	printk("start_frame           :%d\n", purb->start_frame);
256     	printk("number_of_packets     :%d\n", purb->number_of_packets);
257     	printk("interval              :%d\n", purb->interval);
258     	printk("error_count           :%d\n", purb->error_count);
259     	printk("context               :0x%08X\n", purb->context);
260     	printk("complete              :0x%08X\n\n", purb->complete);
261     }
262     
263     static void dump_in_desc(USB_IN_Desc_t *in)
264     {
265     	printk("\nUSB_IN_Desc at 0x%08X\n", in);
266     	printk("  sw_len  : 0x%04X (%d)\n", in->sw_len, in->sw_len);
267     	printk("  command : 0x%04X\n", in->command);
268     	printk("  next    : 0x%08X\n", in->next);
269     	printk("  buf     : 0x%08X\n", in->buf);
270     	printk("  hw_len  : 0x%04X (%d)\n", in->hw_len, in->hw_len);
271     	printk("  status  : 0x%04X\n\n", in->status);
272     }
273     
274     static void dump_sb_desc(USB_SB_Desc_t *sb)
275     {
276     	printk("\nUSB_SB_Desc at 0x%08X\n", sb);
277     	printk("  sw_len  : 0x%04X (%d)\n", sb->sw_len, sb->sw_len);
278     	printk("  command : 0x%04X\n", sb->command);
279     	printk("  next    : 0x%08X\n", sb->next);
280     	printk("  buf     : 0x%08X\n\n", sb->buf);
281     }
282     
283     
284     static void dump_ep_desc(USB_EP_Desc_t *ep)
285     {
286     	printk("\nUSB_EP_Desc at 0x%08X\n", ep);
287     	printk("  hw_len  : 0x%04X (%d)\n", ep->hw_len, ep->hw_len);
288     	printk("  command : 0x%08X\n", ep->command);
289     	printk("  sub     : 0x%08X\n", ep->sub);
290     	printk("  nep     : 0x%08X\n\n", ep->nep);
291     }
292     
293     
294     #else
295     #define dump_urb(...)     (NULL)
296     #define dump_ep_desc(...) (NULL)
297     #define dump_sb_desc(...) (NULL)
298     #define dump_in_desc(...) (NULL)
299     #endif
300     
301     static void init_rx_buffers(void)
302     {
303     	int i;
304     	
305     	DBFENTER;
306     	
307     	for (i = 0; i < (NBR_OF_RX_DESC - 1); i++) {
308     		RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
309     		RxDescList[i].command = 0;
310     		RxDescList[i].next = virt_to_phys(&RxDescList[i + 1]);
311     		RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
312     		RxDescList[i].hw_len = 0;
313     		RxDescList[i].status = 0;
314     	}
315     	
316     	RxDescList[i].sw_len = RX_DESC_BUF_SIZE;
317     	RxDescList[i].command = IO_STATE(USB_IN_command, eol, yes);
318     	RxDescList[i].next = virt_to_phys(&RxDescList[0]);
319     	RxDescList[i].buf = virt_to_phys(RxBuf + (i * RX_DESC_BUF_SIZE));
320     	RxDescList[i].hw_len = 0;
321     	RxDescList[i].status = 0;
322     
323     	myNextRxDesc = &RxDescList[0];
324     	myLastRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
325     	myPrevRxDesc = &RxDescList[NBR_OF_RX_DESC - 1];
326     
327     	*R_DMA_CH9_FIRST = virt_to_phys(myNextRxDesc);
328     	*R_DMA_CH9_CMD = IO_STATE(R_DMA_CH9_CMD, cmd, start);
329     	
330     	DBFEXIT;
331     }
332     
333     static void init_tx_ctrl_ep(void)
334     {
335     	int i;
336     	
337     	DBFENTER;
338     	
339     	for (i = 0; i < (NBR_OF_EP_DESC - 1); i++) {
340     		TxCtrlEPList[i].hw_len = 0;
341     		TxCtrlEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
342     		TxCtrlEPList[i].sub = 0;
343     		TxCtrlEPList[i].nep = virt_to_phys(&TxCtrlEPList[i + 1]);
344     	}
345     	
346     	TxCtrlEPList[i].hw_len = 0;
347     	TxCtrlEPList[i].command = IO_STATE(USB_EP_command, eol, yes) |
348     		IO_FIELD(USB_EP_command, epid, i);
349     
350     	TxCtrlEPList[i].sub = 0;
351     	TxCtrlEPList[i].nep = virt_to_phys(&TxCtrlEPList[0]);
352     	
353     	*R_DMA_CH8_SUB1_EP = virt_to_phys(&TxCtrlEPList[0]);
354     	*R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
355     	
356     	DBFEXIT;
357     }
358     
359     static void init_tx_bulk_ep(void)
360     {
361     	int i;
362     	
363     	DBFENTER;
364     	
365     	for (i = 0; i < (NBR_OF_EP_DESC - 1); i++) {
366     		TxBulkEPList[i].hw_len = 0;
367     		TxBulkEPList[i].command = IO_FIELD(USB_EP_command, epid, i);
368     		TxBulkEPList[i].sub = 0;
369     		TxBulkEPList[i].nep = virt_to_phys(&TxBulkEPList[i + 1]);
370     	}
371     	
372     	TxBulkEPList[i].hw_len = 0;
373     	TxBulkEPList[i].command = IO_STATE(USB_EP_command, eol, yes) |
374     		IO_FIELD(USB_EP_command, epid, i);
375     
376     	TxBulkEPList[i].sub = 0;
377     	TxBulkEPList[i].nep = virt_to_phys(&TxBulkEPList[0]);
378     	
379     	*R_DMA_CH8_SUB0_EP = virt_to_phys(&TxBulkEPList[0]);
380     	*R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
381     	
382     	DBFEXIT;
383     }
384     
385     static void init_tx_intr_ep(void)
386     {
387     	int i;
388     
389     	DBFENTER;
390     
391     	TxIntrSB_zout.sw_len = 0;
392     	TxIntrSB_zout.next = 0;
393     	TxIntrSB_zout.buf = 0;
394     	TxIntrSB_zout.command = IO_FIELD(USB_SB_command, rem, 0) |
395     		IO_STATE(USB_SB_command, tt, zout) |
396     		IO_STATE(USB_SB_command, full, yes) |
397     		IO_STATE(USB_SB_command, eot, yes) |
398     		IO_STATE(USB_SB_command, eol, yes);
399     
400     	for (i = 0; i < (MAX_INTR_INTERVAL - 1); i++) {
401     		TxIntrEPList[i].hw_len = 0;
402     		TxIntrEPList[i].command = IO_STATE(USB_EP_command, eof, yes) |
403     			IO_STATE(USB_EP_command, enable, yes) |
404     			IO_FIELD(USB_EP_command, epid, 0);
405     		TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
406     		TxIntrEPList[i].nep = virt_to_phys(&TxIntrEPList[i + 1]);
407     	}
408     	
409     	TxIntrEPList[i].hw_len = 0;
410     	TxIntrEPList[i].command =
411     		IO_STATE(USB_EP_command, eof, yes) |
412     		IO_STATE(USB_EP_command, enable, yes) |
413     		IO_FIELD(USB_EP_command, epid, 0);
414     	TxIntrEPList[i].sub = virt_to_phys(&TxIntrSB_zout);
415     	TxIntrEPList[i].nep = virt_to_phys(&TxIntrEPList[0]);
416     
417     	*R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
418     	*R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
419     	
420     	DBFEXIT;
421     }
422     
423     
424     static int etrax_usb_unlink_intr_urb(urb_t *urb)
425     {
426     	struct usb_device *usb_dev = urb->dev;
427     	etrax_hc_t *hc = usb_dev->bus->hcpriv;
428     
429     	USB_EP_Desc_t *tmp_ep;
430     	USB_EP_Desc_t *first_ep;
431     	
432     	USB_EP_Desc_t *ep_desc;
433     	USB_SB_Desc_t *sb_desc;
434     	
435     	char epid;
436     	char devnum;
437     	char endpoint;
438     	char slow;
439     	int maxlen;
440     	int i;
441     	
442     	etrax_urb_priv_t *urb_priv;
443     	unsigned long flags;
444     	
445     	DBFENTER;
446     
447     	devnum = usb_pipedevice(urb->pipe);
448     	endpoint = usb_pipeendpoint(urb->pipe);
449     	slow = usb_pipeslow(urb->pipe);
450     	maxlen = usb_maxpacket(urb->dev, urb->pipe,
451     			       usb_pipeout(urb->pipe));
452     
453     	epid = etrax_usb_lookup_epid(devnum, endpoint, slow, maxlen);
454     	if (epid == -1) {
455     		err("Trying to unlink urb that is not in traffic queue!!");
456     		return -1;  /* fix this */
457     	}
458     
459     	*R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, stop);
460     	/* Somehow wait for the DMA to finish current activities */
461     	i = jiffies + 100;
462     	while (jiffies < i);	
463     	
464     	first_ep = &TxIntrEPList[0];
465     	tmp_ep = first_ep;
466     	
467     	do {
468     		if (IO_EXTRACT(USB_EP_command, epid, ((USB_EP_Desc_t *)phys_to_virt(tmp_ep->nep))->command)
469     		    == epid) {
470     			/* Unlink it !!! */
471     			dbg_intr("Found urb to unlink for epid %d", epid);
472     			
473     			ep_desc = phys_to_virt(tmp_ep->nep);
474     			tmp_ep->nep = ep_desc->nep;
475     			kmem_cache_free(usb_desc_cache, phys_to_virt(ep_desc->sub));
476     			kmem_cache_free(usb_desc_cache, ep_desc);
477     		}
478     
479     		tmp_ep = phys_to_virt(tmp_ep->nep);
480     		
481     	} while (tmp_ep != first_ep);
482     
483     	/* We should really try to move the EP register to an EP that is not removed
484     	   instead of restarting, but this will work too */
485     	*R_DMA_CH8_SUB2_EP = virt_to_phys(&TxIntrEPList[0]);
486     	*R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
487     
488     	clear_bit(epid, (void *)&ep_really_active);
489     	URB_List[epid] = NULL;
490     	etrax_usb_free_epid(epid);
491     	
492     	DBFEXIT;
493     
494     	return 0;
495     }
496     
497     void etrax_usb_do_intr_recover(int epid)
498     {
499     	USB_EP_Desc_t *first_ep, *tmp_ep;
500     	
501     	first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
502     	tmp_ep = first_ep;
503     
504     	do {
505     		if (IO_EXTRACT(USB_EP_command, epid, tmp_ep->command) == epid &&
506     		    !(tmp_ep->command & IO_MASK(USB_EP_command, enable))) {
507     			tmp_ep->command |= IO_STATE(USB_EP_command, enable, yes);
508     		}
509     		
510     		tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->nep);
511     		
512     	} while (tmp_ep != first_ep);
513     }
514     
515     static int etrax_usb_submit_intr_urb(urb_t *urb)
516     {
517     	USB_EP_Desc_t *tmp_ep;
518     	USB_EP_Desc_t *first_ep;
519     	
520     	USB_SB_Desc_t *sb_desc;
521     	
522     	char epid;
523     	char devnum;
524     	char endpoint;
525     	char maxlen;
526     	char slow;
527     	int interval;
528     	int i;
529     	
530     	etrax_urb_priv_t *urb_priv;
531     	unsigned long flags;
532     	
533     	DBFENTER;
534     
535     	devnum = usb_pipedevice(urb->pipe);
536     	endpoint = usb_pipeendpoint(urb->pipe);
537     	maxlen = usb_maxpacket(urb->dev, urb->pipe,
538     			       usb_pipeout(urb->pipe));
539     
540     	slow = usb_pipeslow(urb->pipe);
541     	interval = urb->interval;
542     
543     	dbg_intr("Intr traffic for dev %d, endpoint %d, maxlen %d, slow %d",
544     		 devnum, endpoint, maxlen, slow);
545     	
546     	epid = etrax_usb_lookup_epid(devnum, endpoint, slow, maxlen);
547     	if (epid == -1) {
548     		epid = etrax_usb_allocate_epid();
549     		if (epid == -1) {
550     			/* We're out of endpoints, return some error */
551     			err("We're out of endpoints");
552     			return -ENOMEM;
553     		}
554     		/* Now we have to fill in this ep */
555     		etrax_usb_setup_epid(epid, devnum, endpoint, maxlen, slow);
556     	}
557     	/* Ok, now we got valid endpoint, lets insert some traffic */
558     
559     	urb_priv = (etrax_urb_priv_t *)kmalloc(sizeof(etrax_urb_priv_t), GFP_KERNEL);
560     	urb_priv->first_sb = 0;
561     	urb_priv->rx_offset = 0;
562     	urb_priv->eot = 0;
563     	INIT_LIST_HEAD(&urb_priv->ep_in_list);
564     	urb->hcpriv = urb_priv;
565     
566     	/* This is safe since there cannot be any other URB's for this epid */
567     	URB_List[epid] = urb;
568     #if 0
569     	first_ep = (USB_EP_Desc_t *)phys_to_virt(*R_DMA_CH8_SUB2_EP);
570     #else
571     	first_ep = &TxIntrEPList[0];
572     #endif
573     
574     	/* Round of the interval to 2^n, it is obvious that this code favours
575     	   smaller numbers, but that is actually a good thing */
576     	for (i = 0; interval; i++) {
577     		interval = interval >> 1;
578     	}
579     
580     	urb->interval = interval = 1 << (i - 1);
581     
582     	dbg_intr("Interval rounded to %d", interval);
583     
584     	tmp_ep = first_ep;
585     	i = 0;
586     	do {
587     		if (tmp_ep->command & IO_MASK(USB_EP_command, eof)) {
588     			if ((i % interval) == 0) {
589     				/* Insert the traffic ep after tmp_ep */
590     				USB_EP_Desc_t *traffic_ep;
591     				USB_SB_Desc_t *traffic_sb;
592     
593     				traffic_ep = (USB_EP_Desc_t *)
594     					kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
595     				traffic_sb = (USB_SB_Desc_t *)
596     					kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
597     
598     				traffic_ep->hw_len = 0;
599     				traffic_ep->command = IO_FIELD(USB_EP_command, epid, epid) |
600     					IO_STATE(USB_EP_command, enable, yes);
601     				traffic_ep->sub = virt_to_phys(traffic_sb);
602     
603     				if (usb_pipein(urb->pipe)) {
604     					traffic_sb->sw_len = urb->transfer_buffer_length ?
605     						(urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
606     					traffic_sb->next = 0;
607     					traffic_sb->buf = 0;
608     					traffic_sb->command = IO_FIELD(USB_SB_command, rem,
609     								       urb->transfer_buffer_length % maxlen) |
610     						IO_STATE(USB_SB_command, tt, in) |
611     						IO_STATE(USB_SB_command, eot, yes) |
612     						IO_STATE(USB_SB_command, eol, yes);
613     					
614     				} else if (usb_pipeout(urb->pipe)) {
615     					traffic_sb->sw_len = urb->transfer_buffer_length;
616     					traffic_sb->next = 0;
617     					traffic_sb->buf = virt_to_phys(urb->transfer_buffer);
618     					traffic_sb->command = IO_FIELD(USB_SB_command, rem, 0) |
619     						IO_STATE(USB_SB_command, tt, out) |
620     						IO_STATE(USB_SB_command, eot, yes) |
621     						IO_STATE(USB_SB_command, eol, yes) |
622     						IO_STATE(USB_SB_command, full, yes);
623     				}
624     
625     				traffic_ep->nep = tmp_ep->nep;
626     				tmp_ep->nep = virt_to_phys(traffic_ep);
627     				dbg_intr("One ep sucessfully inserted");
628     			}
629     			i++;
630     		}
631     		tmp_ep = (USB_EP_Desc_t *)phys_to_virt(tmp_ep->nep);
632     	} while (tmp_ep != first_ep);
633     
634     	set_bit(epid, (void *)&ep_really_active);
635     	
636     	*R_DMA_CH8_SUB2_CMD = IO_STATE(R_DMA_CH8_SUB2_CMD, cmd, start);
637     
638     	DBFEXIT;
639     	
640     	return 0;
641     }
642     
643     
644     static int handle_intr_transfer_attn(char epid, int status)
645     {
646     	urb_t *old_urb;
647     
648     	DBFENTER;
649     
650     	old_urb = URB_List[epid];
651     	
652     	/* if (status == 0 && IN) find data and copy to urb */
653     	if (status == 0 && usb_pipein(old_urb->pipe)) {
654     		unsigned long flags;
655     		etrax_urb_priv_t *urb_priv;
656     		struct list_head *entry;
657     		struct in_chunk *in;
658     
659     		urb_priv = (etrax_urb_priv_t *)old_urb->hcpriv;
660     		
661     		save_flags(flags);
662     		cli();
663     
664     		list_for_each(entry, &urb_priv->ep_in_list) {
665     			in = list_entry(entry, struct in_chunk, list);
666     			memcpy(old_urb->transfer_buffer, in->data, in->length);
667     			old_urb->actual_length = in->length;
668     			old_urb->status = status;
669     			
670     			if (old_urb->complete) {
671     				old_urb->complete(old_urb);
672     			}
673     			
674     			list_del(entry);
675     			kfree(in->data);
676     			kfree(in);
677     		}		
678     		
679     		restore_flags(flags);
680     
681     	} else if (status != 0) {
682     		warn("Some sort of error for INTR EP !!!!");
683     #ifdef ETRAX_USB_INTR_ERROR_FATAL
684     		/* This means that an INTR error is fatal for that endpoint */
685     		etrax_usb_unlink_intr_urb(old_urb);
686     		old_urb->status = status;
687     		if (old_urb->complete) {
688     			old_urb->complete(old_urb);
689     		}
690     #else
691     		/* In this case we reenable the disabled endpoint(s) */
692     		etrax_usb_do_intr_recover(epid);
693     #endif	
694     	}
695     	
696     	DBFEXIT;
697     }
698     
699     static int etrax_rh_unlink_urb (urb_t *urb)
700     {
701     	etrax_hc_t *hc;
702     	
703     	DBFENTER;
704     	
705     	hc = urb->dev->bus->hcpriv;
706     	
707     	if (hc->rh.urb == urb) {
708     		hc->rh.send = 0;
709     		del_timer(&hc->rh.rh_int_timer);
710     	}
711     	
712     	DBFEXIT;
713     	return 0;
714     }
715     
716     static void etrax_rh_send_irq(urb_t *urb)
717     {
718     	__u16 data = 0;
719     	etrax_hc_t *hc = urb->dev->bus->hcpriv;
720     //	static prev_wPortStatus_1 = 0;
721     //	static prev_wPortStatus_2 = 0;
722     	
723     /*	DBFENTER; */
724     	
725     	
726     /*
727       dbg_rh("R_USB_FM_NUMBER   : 0x%08X", *R_USB_FM_NUMBER);
728       dbg_rh("R_USB_FM_REMAINING: 0x%08X", *R_USB_FM_REMAINING);
729     */
730     	
731     	data |= (hc->rh.wPortChange_1) ? (1 << 1) : 0;
732     	data |= (hc->rh.wPortChange_2) ? (1 << 2) : 0;
733     
734     	*((__u16 *)urb->transfer_buffer) = cpu_to_le16(data);
735     	urb->actual_length = 1;
736     	urb->status = 0;
737     
738     	
739     	if (data && hc->rh.send && urb->complete) {
740     		dbg_rh("wPortChange_1: 0x%04X", hc->rh.wPortChange_1); 
741     		dbg_rh("wPortChange_2: 0x%04X", hc->rh.wPortChange_2);
742     
743     		urb->complete(urb);
744     	}
745       
746     /*	DBFEXIT; */
747     }
748     
749     static void etrax_rh_init_int_timer(urb_t *urb)
750     {
751     	etrax_hc_t *hc;
752     	
753     /*	DBFENTER; */
754     	
755     	hc = urb->dev->bus->hcpriv;
756     	hc->rh.interval = urb->interval;
757     	init_timer(&hc->rh.rh_int_timer);
758     	hc->rh.rh_int_timer.function = etrax_rh_int_timer_do;
759     	hc->rh.rh_int_timer.data = (unsigned long)urb;
760     	hc->rh.rh_int_timer.expires = jiffies + ((HZ * hc->rh.interval) / 1000);
761     	add_timer(&hc->rh.rh_int_timer);
762     	
763     /*	DBFEXIT; */
764     }
765     
766     static void etrax_rh_int_timer_do(unsigned long ptr)
767     {
768     	urb_t *urb;
769     	etrax_hc_t *hc;
770     	
771     /*	DBFENTER; */
772     	
773     	urb = (urb_t*)ptr;
774     	hc = urb->dev->bus->hcpriv;
775     	
776     	if (hc->rh.send) {
777     		etrax_rh_send_irq(urb);
778     	}
779     	
780     	etrax_rh_init_int_timer(urb);
781     	
782     /*	DBFEXIT; */
783     }
784     
785     static void etrax_usb_setup_epid(char epid, char devnum, char endpoint, char packsize, char slow)
786     {
787     	unsigned long flags;
788     	
789     	DBFENTER;
790     
791     	save_flags(flags);
792     	cli();
793     	
794     	if (test_bit(epid, (void *)&ep_usage_bitmask)) {
795     		warn("Trying to setup used epid %d", epid);
796     		DBFEXIT;
797     		return;
798     	}
799     	
800     	set_bit(epid, (void *)&ep_usage_bitmask);
801     	dbg_ep("Setting up ep_id %d with devnum %d, endpoint %d and max_len %d",
802     	       epid, devnum, endpoint, packsize);
803     	
804     	*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
805     	nop();
806     	*R_USB_EPT_DATA = IO_STATE(R_USB_EPT_DATA, valid, yes) |
807     		IO_FIELD(R_USB_EPT_DATA, ep, endpoint) |
808     		IO_FIELD(R_USB_EPT_DATA, dev, devnum) |
809     		IO_FIELD(R_USB_EPT_DATA, max_len, packsize) |
810     		IO_FIELD(R_USB_EPT_DATA, low_speed, slow);
811     
812     	restore_flags(flags);
813     
814     	DBFEXIT;
815     }
816     
817     static void etrax_usb_free_epid(char epid)
818     {
819     	unsigned long flags;
820     	
821     	DBFENTER;
822     
823     	if (!test_bit(epid, (void *)&ep_usage_bitmask)) {
824     		warn("Trying to free unused epid %d", epid);
825     		DBFEXIT;
826     		return;
827     	}
828     
829     	save_flags(flags);
830     	cli();
831     	*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid);
832     	nop();
833     	while (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold))printk("+");
834     	*R_USB_EPT_DATA = 0;
835     	clear_bit(epid, (void *)&ep_usage_bitmask);
836     	restore_flags(flags);
837     	dbg_ep("epid: %d freed", epid);
838     	
839     	DBFEXIT;
840     }
841     
842     
843     static int etrax_usb_lookup_epid(unsigned char devnum, char endpoint, char slow, int maxp)
844     {
845     	int i;
846     	unsigned long flags;
847     	__u32 data;
848     	
849     	DBFENTER;
850     
851     	save_flags(flags);
852     	
853     	/* Skip first ep_id since it is reserved when intr. or iso traffic is used */
854     	for (i = 0; i < NBR_OF_EP_DESC; i++) {
855     		if (test_bit(i, (void *)&ep_usage_bitmask)) {
856     			*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, i);
857     			nop();
858     			data = *R_USB_EPT_DATA;
859     			if ((IO_MASK(R_USB_EPT_DATA, valid) & data) &&
860     			    (IO_EXTRACT(R_USB_EPT_DATA, dev, data) == devnum) &&
861     			    (IO_EXTRACT(R_USB_EPT_DATA, ep, data) == endpoint) &&
862     			    (IO_EXTRACT(R_USB_EPT_DATA, low_speed, data) == slow) &&
863     			    (IO_EXTRACT(R_USB_EPT_DATA, max_len, data) == maxp)) {
864     				dbg_ep("Found ep_id %d for devnum %d, endpoint %d",
865     				       i, devnum, endpoint);
866     				DBFEXIT;
867     				return i;
868     			}
869     		}
870     	}
871     
872     	restore_flags(flags);
873     	
874     	dbg_ep("Found no ep_id for devnum %d, endpoint %d",
875     	       devnum, endpoint);
876     	DBFEXIT;
877     	return -1;
878     }
879     
880     static int etrax_usb_allocate_epid(void)
881     {
882     	int i;
883     	
884     	DBFENTER;
885     
886     	for (i = 0; i < NBR_OF_EP_DESC; i++) {
887     		if (!test_bit(i, (void *)&ep_usage_bitmask)) {
888     			dbg_ep("Found free ep_id at %d", i);
889     			DBFEXIT;
890     			return i;
891     		}
892     	}
893     
894     	dbg_ep("Found no free ep_id's");
895     	DBFEXIT;
896     	return -1;
897     }
898     
899     static int etrax_usb_submit_bulk_urb(urb_t *urb)
900     {
901     	char epid;
902     	char devnum;
903     	char endpoint;
904     	char maxlen;
905     	char slow;
906     
907     	urb_t *tmp_urb;
908     	
909     	etrax_urb_priv_t *urb_priv;
910     	unsigned long flags;
911     	
912     	DBFENTER;
913     
914     	devnum = usb_pipedevice(urb->pipe);
915     	endpoint = usb_pipeendpoint(urb->pipe);
916     	maxlen = usb_maxpacket(urb->dev, urb->pipe,
917     			       usb_pipeout(urb->pipe));
918     	slow = usb_pipeslow(urb->pipe);
919     	
920     	epid = etrax_usb_lookup_epid(devnum, endpoint, slow, maxlen);
921     	if (epid == -1) {
922     		epid = etrax_usb_allocate_epid();
923     		if (epid == -1) {
924     			/* We're out of endpoints, return some error */
925     			err("We're out of endpoints");
926     			return -ENOMEM;
927     		}
928     		/* Now we have to fill in this ep */
929     		etrax_usb_setup_epid(epid, devnum, endpoint, maxlen, slow);
930     	}
931     	/* Ok, now we got valid endpoint, lets insert some traffic */
932     
933     	urb->status = -EINPROGRESS;
934     
935     	save_flags(flags);
936     	cli();
937     	
938     	if (URB_List[epid]) {
939     		/* Find end of list and add */
940     		for (tmp_urb = URB_List[epid]; tmp_urb->next; tmp_urb = tmp_urb->next)
941     			dump_urb(tmp_urb);
942     
943     		tmp_urb->next = urb;
944     		restore_flags(flags);
945     	} else {
946     		/* If this is the first URB, add the URB and do HW add */
947     		URB_List[epid] = urb;
948     		restore_flags(flags);
949     		etrax_usb_do_bulk_hw_add(urb, epid, maxlen);
950     	}
951     
952     	DBFEXIT;
953     
954     	return 0;
955     }
956     
957     static int etrax_usb_do_bulk_hw_add(urb_t *urb, char epid, char maxlen)
958     {
959     	USB_SB_Desc_t *sb_desc_1;
960     
961     	etrax_urb_priv_t *urb_priv;
962     
963     	unsigned long flags;
964     	__u32 r_usb_ept_data;
965     
966     	DBFENTER;
967     
968     	urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_KERNEL);
969     	sb_desc_1 = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
970     
971     	if (usb_pipeout(urb->pipe)) {
972     
973     		dbg_bulk("Bulk transfer for epid %d is OUT", epid);
974     		dbg_bulk("transfer_buffer_length == %d", urb->transfer_buffer_length);
975     		dbg_bulk("actual_length == %d", urb->actual_length);
976     	
977     		if (urb->transfer_buffer_length > 0xffff) {
978     			panic(__FILE__ __FUNCTION__ ":urb->transfer_buffer_length > 0xffff\n");
979     		}
980     
981     		sb_desc_1->sw_len = urb->transfer_buffer_length;  /* was actual_length */
982     		sb_desc_1->command = IO_FIELD(USB_SB_command, rem, 0) |
983     			IO_STATE(USB_SB_command, tt, out) |
984     
985     #if 0
986     			IO_STATE(USB_SB_command, full, no) |
987     #else
988     			IO_STATE(USB_SB_command, full, yes) |
989     #endif
990     
991     			IO_STATE(USB_SB_command, eot, yes) |
992     			IO_STATE(USB_SB_command, eol, yes);
993     		
994     		dbg_bulk("transfer_buffer is at 0x%08X", urb->transfer_buffer);
995     		
996     		sb_desc_1->buf = virt_to_phys(urb->transfer_buffer);
997     		sb_desc_1->next = 0;
998     		
999     	} else if (usb_pipein(urb->pipe)) {
1000     
1001     		dbg_bulk("Transfer for epid %d is IN", epid);
1002     		dbg_bulk("transfer_buffer_length = %d", urb->transfer_buffer_length);
1003     		dbg_bulk("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
1004     		
1005     		sb_desc_1->sw_len = urb->transfer_buffer_length ?
1006     			(urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
1007     		dbg_bulk("sw_len got %d", sb_desc_1->sw_len);
1008     		dbg_bulk("transfer_buffer is at 0x%08X", urb->transfer_buffer);
1009     		
1010     		sb_desc_1->command =
1011     			IO_FIELD(USB_SB_command, rem,
1012     				 urb->transfer_buffer_length % maxlen) |
1013     			IO_STATE(USB_SB_command, tt, in) |
1014     			IO_STATE(USB_SB_command, eot, yes) |
1015     			IO_STATE(USB_SB_command, eol, yes);
1016     		
1017     		sb_desc_1->buf = 0;
1018     		sb_desc_1->next = 0;
1019     
1020     		urb_priv->rx_offset = 0;
1021     		urb_priv->eot = 0;
1022     	}
1023     	
1024     	urb_priv->first_sb = sb_desc_1;
1025     	
1026     	urb->hcpriv = (void *)urb_priv;
1027     	
1028     	/* Reset toggle bits and reset error count, remeber to di and ei */
1029     	/* Warning: it is possible that this locking doesn't work with bottom-halves */
1030     
1031     	save_flags(flags);
1032     	cli();
1033     
1034     	*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid); nop();
1035     	if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
1036     		panic("Hold was set in %s\n", __FUNCTION__);
1037     	}
1038     
1039     	*R_USB_EPT_DATA &=
1040     		~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
1041     		  IO_MASK(R_USB_EPT_DATA, error_count_out));
1042     	
1043     	if (usb_pipeout(urb->pipe)) {
1044     		char toggle =
1045     		usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1046     		*R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_out);
1047     		*R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_out, toggle);
1048     	} else {
1049     		char toggle =
1050     		usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe));
1051     		*R_USB_EPT_DATA &= ~IO_MASK(R_USB_EPT_DATA, t_in);
1052     		*R_USB_EPT_DATA |= IO_FIELD(R_USB_EPT_DATA, t_in, toggle);
1053     	}
1054     		
1055     	/* Enable the EP descr. */
1056     
1057     	set_bit(epid, (void *)&ep_really_active);
1058     	
1059     	TxBulkEPList[epid].sub = virt_to_phys(sb_desc_1);
1060     	TxBulkEPList[epid].hw_len = 0;
1061     	TxBulkEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1062     
1063     	restore_flags(flags);
1064     
1065     	if (!(*R_DMA_CH8_SUB0_CMD & IO_MASK(R_DMA_CH8_SUB0_CMD, cmd))) {
1066     		*R_DMA_CH8_SUB0_CMD = IO_STATE(R_DMA_CH8_SUB0_CMD, cmd, start);
1067     		
1068     	}
1069     
1070     	DBFEXIT;
1071     }
1072     
1073     static int handle_bulk_transfer_attn(char epid, int status)
1074     {
1075     	urb_t *old_urb;
1076     	etrax_urb_priv_t *hc_priv;
1077     	unsigned long flags;
1078     
1079     	DBFENTER;
1080     
1081     	clear_bit(epid, (void *)&ep_really_active);
1082     	
1083     	old_urb = URB_List[epid];
1084     	URB_List[epid] = old_urb->next;
1085     
1086     	/* if (status == 0 && IN) find data and copy to urb */
1087     	if (status == 0 && usb_pipein(old_urb->pipe)) {
1088     		etrax_urb_priv_t *urb_priv;
1089     		
1090     		urb_priv = (etrax_urb_priv_t *)old_urb->hcpriv;
1091     		save_flags(flags);
1092     		cli();
1093     		if (urb_priv->eot == 1) {
1094     			old_urb->actual_length = urb_priv->rx_offset;
1095     		} else {
1096     			if (urb_priv->rx_offset == 0) {
1097     				status = 0;
1098     			} else {
1099     				status = -EPROTO;
1100     			}
1101     			
1102     			old_urb->actual_length = 0;
1103     			err("(BULK) No eot set in IN data!!! rx_offset is: %d", urb_priv->rx_offset);
1104     		}
1105     		
1106     		restore_flags(flags);
1107     	}
1108     
1109     	save_flags(flags);
1110     	cli();
1111     		
1112     	*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid); nop();
1113     	if (usb_pipeout(old_urb->pipe)) {
1114     		char toggle =
1115     		IO_EXTRACT(R_USB_EPT_DATA, t_out, *R_USB_EPT_DATA);
1116     		usb_settoggle(old_urb->dev, usb_pipeendpoint(old_urb->pipe),
1117     			      usb_pipeout(old_urb->pipe), toggle);
1118     	} else {
1119     		char toggle =
1120     		IO_EXTRACT(R_USB_EPT_DATA, t_in, *R_USB_EPT_DATA);
1121     		usb_settoggle(old_urb->dev, usb_pipeendpoint(old_urb->pipe),
1122     			      usb_pipeout(old_urb->pipe), toggle);
1123     	}
1124     	restore_flags(flags);
1125     	
1126     	/* If there are any more URB's in the list we'd better start sending */
1127     	if (URB_List[epid]) {
1128     		etrax_usb_do_bulk_hw_add(URB_List[epid], epid,
1129     					 usb_maxpacket(URB_List[epid]->dev, URB_List[epid]->pipe,
1130     						       usb_pipeout(URB_List[epid]->pipe)));
1131     	}
1132     #if 1
1133     	else {
1134     		/* This means that this EP is now free, deconfigure it */
1135     		etrax_usb_free_epid(epid);
1136     	}
1137     #endif
1138     	
1139     	/* Remember to free the SB's */
1140     	hc_priv = (etrax_urb_priv_t *)old_urb->hcpriv;
1141     	cleanup_sb(hc_priv->first_sb);
1142     	kfree(hc_priv);
1143     
1144     	old_urb->status = status;
1145     	if (old_urb->complete) {
1146     		old_urb->complete(old_urb);
1147     	}
1148     
1149     	DBFEXIT;
1150     }
1151     
1152     /* ---------------------------------------------------------------------------- */
1153     
1154     static int etrax_usb_submit_ctrl_urb(urb_t *urb)
1155     {
1156     	char epid;
1157     	char devnum;
1158     	char endpoint;
1159     	char maxlen;
1160     	char slow;
1161     
1162     	urb_t *tmp_urb;
1163     	
1164     	etrax_urb_priv_t *urb_priv;
1165     	unsigned long flags;
1166     	
1167     	DBFENTER;
1168     
1169     	devnum = usb_pipedevice(urb->pipe);
1170     	endpoint = usb_pipeendpoint(urb->pipe);
1171     	maxlen = usb_maxpacket(urb->dev, urb->pipe,
1172     			       usb_pipeout(urb->pipe));
1173     	slow = usb_pipeslow(urb->pipe);
1174     	
1175     	epid = etrax_usb_lookup_epid(devnum, endpoint, slow, maxlen);
1176     	if (epid == -1) {
1177     		epid = etrax_usb_allocate_epid();
1178     		if (epid == -1) {
1179     			/* We're out of endpoints, return some error */
1180     			err("We're out of endpoints");
1181     			return -ENOMEM;
1182     		}
1183     		/* Now we have to fill in this ep */
1184     		etrax_usb_setup_epid(epid, devnum, endpoint, maxlen, slow);
1185     	}
1186     	/* Ok, now we got valid endpoint, lets insert some traffic */
1187     
1188     	urb->status = -EINPROGRESS;
1189     
1190     	save_flags(flags);
1191     	cli();
1192     	
1193     	if (URB_List[epid]) {
1194     		/* Find end of list and add */
1195     		for (tmp_urb = URB_List[epid]; tmp_urb->next; tmp_urb = tmp_urb->next)
1196     			dump_urb(tmp_urb);
1197     
1198     		tmp_urb->next = urb;
1199     		restore_flags(flags);
1200     	} else {
1201     		/* If this is the first URB, add the URB and do HW add */
1202     		URB_List[epid] = urb;
1203     		restore_flags(flags);
1204     		etrax_usb_do_ctrl_hw_add(urb, epid, maxlen);
1205     	}
1206     
1207     	DBFEXIT;
1208     
1209     	return 0;
1210     }
1211     
1212     static int etrax_usb_do_ctrl_hw_add(urb_t *urb, char epid, char maxlen)
1213     {
1214     	USB_SB_Desc_t *sb_desc_1;
1215     	USB_SB_Desc_t *sb_desc_2;
1216     	USB_SB_Desc_t *sb_desc_3;
1217     
1218     	etrax_urb_priv_t *urb_priv;
1219     
1220     	unsigned long flags;
1221     	__u32 r_usb_ept_data;
1222     	
1223     
1224     	DBFENTER;
1225     
1226     	urb_priv = kmalloc(sizeof(etrax_urb_priv_t), GFP_KERNEL);
1227     	sb_desc_1 = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
1228     	sb_desc_2 = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
1229     
1230     	if (!(sb_desc_1 && sb_desc_2)) {
1231     		panic("kmem_cache_alloc in ctrl_hw_add gave NULL pointers !!!\n");
1232     	}
1233     	
1234     	sb_desc_1->sw_len = 8;
1235     	sb_desc_1->command = IO_FIELD(USB_SB_command, rem, 0) |
1236     		IO_STATE(USB_SB_command, tt, setup) |
1237     		IO_STATE(USB_SB_command, full, yes) |
1238     		IO_STATE(USB_SB_command, eot, yes);
1239     	
1240     	sb_desc_1->buf = virt_to_phys(urb->setup_packet);
1241     	sb_desc_1->next = virt_to_phys(sb_desc_2);
1242     	dump_sb_desc(sb_desc_1);
1243     
1244     	if (usb_pipeout(urb->pipe)) {
1245     		dbg_ctrl("Transfer for epid %d is OUT", epid);
1246     
1247     		/* If this Control OUT transfer has an optional data stage we add an OUT token
1248     		   before the mandatory IN (status) token, hence the reordered SB list */
1249     		
1250     		if (urb->transfer_buffer) {
1251     			dbg_ctrl("This OUT transfer has an extra data stage");
1252     			sb_desc_3 = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
1253     
1254     			sb_desc_1->next = virt_to_phys(sb_desc_3);
1255     			
1256     			sb_desc_3->sw_len = urb->transfer_buffer_length;
1257     			sb_desc_3->command = IO_STATE(USB_SB_command, tt, out) |
1258     				IO_STATE(USB_SB_command, full, yes) |
1259     				IO_STATE(USB_SB_command, eot, yes);
1260     			sb_desc_3->buf = virt_to_phys(urb->transfer_buffer);
1261     			sb_desc_3->next = virt_to_phys(sb_desc_2);
1262     		}
1263     		
1264     		sb_desc_2->sw_len = 1;
1265     		sb_desc_2->command = IO_FIELD(USB_SB_command, rem, 0) |
1266     			IO_STATE(USB_SB_command, tt, in) |
1267     			IO_STATE(USB_SB_command, eot, yes) |
1268     			IO_STATE(USB_SB_command, eol, yes);
1269     
1270     		sb_desc_2->buf = 0;
1271     		sb_desc_2->next = 0;
1272     		dump_sb_desc(sb_desc_2);
1273     		
1274     	} else if (usb_pipein(urb->pipe)) {
1275     
1276     		dbg_ctrl("Transfer for epid %d is IN", epid);
1277     		dbg_ctrl("transfer_buffer_length = %d", urb->transfer_buffer_length);
1278     		dbg_ctrl("rem is calculated to %d", urb->transfer_buffer_length % maxlen);
1279     
1280     		sb_desc_3 = (USB_SB_Desc_t*)kmem_cache_alloc(usb_desc_cache, GFP_KERNEL);
1281     		
1282     		sb_desc_2->sw_len = urb->transfer_buffer_length ?
1283     			(urb->transfer_buffer_length - 1) / maxlen + 1 : 0;
1284     		dbg_ctrl("sw_len got %d", sb_desc_2->sw_len);
1285     		
1286     		sb_desc_2->command =
1287     			IO_FIELD(USB_SB_command, rem,
1288     				 urb->transfer_buffer_length % maxlen) |
1289     			IO_STATE(USB_SB_command, tt, in) |
1290     			IO_STATE(USB_SB_command, eot, yes);
1291     		
1292     		sb_desc_2->buf = 0;
1293     		sb_desc_2->next = virt_to_phys(sb_desc_3);
1294     		dump_sb_desc(sb_desc_2);
1295     
1296     		sb_desc_3->sw_len = 1;
1297     		sb_desc_3->command = IO_FIELD(USB_SB_command, rem, 0) |
1298     			IO_STATE(USB_SB_command, tt, zout) |
1299     			IO_STATE(USB_SB_command, full, yes) |
1300     			IO_STATE(USB_SB_command, eot, yes) |
1301     			IO_STATE(USB_SB_command, eol, yes);
1302     				
1303     		sb_desc_3->buf = 0;
1304     		sb_desc_3->next = 0;
1305     		dump_sb_desc(sb_desc_3);
1306     
1307     		urb_priv->rx_offset = 0;
1308     		urb_priv->eot = 0;
1309     	}
1310     	
1311     	urb_priv->first_sb = sb_desc_1;
1312     	
1313     	urb->hcpriv = (void *)urb_priv;
1314     	
1315     	/* Reset toggle bits and reset error count, remeber to di and ei */
1316     	/* Warning: it is possible that this locking doesn't work with bottom-halves */
1317     
1318     	save_flags(flags);
1319     	cli();
1320     
1321     	*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid); nop();
1322     	if (*R_USB_EPT_DATA & IO_MASK(R_USB_EPT_DATA, hold)) {
1323     		panic("Hold was set in %s\n", __FUNCTION__);
1324     	}
1325     	
1326     
1327     	*R_USB_EPT_DATA &=
1328     		~(IO_MASK(R_USB_EPT_DATA, error_count_in) |
1329     		  IO_MASK(R_USB_EPT_DATA, error_count_out) |
1330     		  IO_MASK(R_USB_EPT_DATA, t_in) |
1331     		  IO_MASK(R_USB_EPT_DATA, t_out));
1332     
1333     	/* Enable the EP descr. */
1334     
1335     	set_bit(epid, (void *)&ep_really_active);
1336     	
1337     	TxCtrlEPList[epid].sub = virt_to_phys(sb_desc_1);
1338     	TxCtrlEPList[epid].hw_len = 0;
1339     	TxCtrlEPList[epid].command |= IO_STATE(USB_EP_command, enable, yes);
1340     	restore_flags(flags);
1341     
1342     	dump_ep_desc(&TxCtrlEPList[epid]);
1343     
1344     	if (!(*R_DMA_CH8_SUB1_CMD & IO_MASK(R_DMA_CH8_SUB1_CMD, cmd))) {
1345     		*R_DMA_CH8_SUB1_CMD = IO_STATE(R_DMA_CH8_SUB1_CMD, cmd, start);
1346     		
1347     	}
1348     	
1349     	DBFEXIT;
1350     }
1351     
1352     static int etrax_usb_submit_urb(urb_t *urb)
1353     {
1354     	etrax_hc_t *hc;
1355     	int rval = -EINVAL;
1356     	
1357     	DBFENTER;
1358     
1359     	dump_urb(urb);
1360     	submit_urb_count++;
1361     	
1362     	hc = (etrax_hc_t*) urb->dev->bus->hcpriv;
1363     	
1364     	if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1365     		/* This request if for the Virtual Root Hub */
1366     		rval = etrax_rh_submit_urb(urb);
1367     		
1368     	} else if (usb_pipetype(urb->pipe) == PIPE_CONTROL) {
1369     		rval = etrax_usb_submit_ctrl_urb(urb);
1370     
1371     	} else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
1372     		rval = etrax_usb_submit_bulk_urb(urb);
1373     
1374     	} else if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1375     		int bustime;
1376     
1377     		if (urb->bandwidth == 0) {
1378     			bustime = usb_check_bandwidth(urb->dev, urb);
1379     			if (bustime < 0) {
1380     				rval = bustime;
1381     			} else {
1382     				usb_claim_bandwidth(urb->dev, urb, bustime, 0);
1383     				rval = etrax_usb_submit_intr_urb(urb);
1384     			}
1385     			
1386     		}
1387     	} else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1388     		warn("Isochronous traffic is not supported !!!");
1389     		rval = -EINVAL;
1390     	}
1391     
1392     	DBFEXIT;
1393     
1394     	return rval;
1395     }
1396     
1397     static int etrax_usb_unlink_urb(urb_t *urb)
1398     {
1399     	etrax_hc_t *hc = urb->dev->bus->hcpriv;
1400     	int epid;
1401     	int pos;
1402     	int devnum, endpoint, slow, maxlen;
1403     	etrax_urb_priv_t *hc_priv;
1404     	unsigned long flags;
1405     	
1406     	DBFENTER;
1407     	dump_urb(urb);
1408     	devnum = usb_pipedevice(urb->pipe);
1409     	endpoint = usb_pipeendpoint(urb->pipe);
1410     	slow = usb_pipeslow(urb->pipe);
1411     	maxlen = usb_maxpacket(urb->dev, urb->pipe,
1412     			       usb_pipeout(urb->pipe));
1413     
1414     	epid = etrax_usb_lookup_epid(devnum, endpoint, slow, maxlen);
1415     
1416     	if (epid == -1)
1417     		return 0;
1418     	
1419     	
1420     	if (usb_pipedevice(urb->pipe) == hc->rh.devnum) {
1421     		int ret;
1422     		ret =  etrax_rh_unlink_urb(urb);
1423     		DBFEXIT;
1424     		return ret;
1425     	} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1426     		int ret;
1427     		ret = etrax_usb_unlink_intr_urb(urb);
1428     		urb->status = -ENOENT;
1429     		if (urb->complete) {
1430     			urb->complete(urb);
1431     		}
1432     		DBFEXIT;
1433     		return ret;
1434     	}
1435     
1436     	info("Unlink of BULK or CTRL");
1437     
1438     	save_flags(flags);
1439     	cli();
1440     	
1441     	for (epid = 0; epid < 32; epid++) {
1442     		urb_t *u = URB_List[epid];
1443     		pos = 0;
1444     
1445     		for (; u; u = u->next) {
1446     			pos++;
1447     			if (u == urb) {
1448     				info("Found urb at epid %d, pos %d", epid, pos);
1449     
1450     				if (pos == 1) {
1451     					if (usb_pipetype(u->pipe) == PIPE_CONTROL) {
1452     						if (TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1453     							/* The EP was enabled, disable it and wait */
1454     							TxCtrlEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1455     							while (*R_DMA_CH8_SUB1_EP == virt_to_phys(&TxCtrlEPList[epid]));
1456     						}
1457     						
1458     					} else if (usb_pipetype(u->pipe) == PIPE_BULK) {
1459     						if (TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable)) {
1460     							TxBulkEPList[epid].command &= ~IO_MASK(USB_EP_command, enable);
1461     							while (*R_DMA_CH8_SUB0_EP == virt_to_phys(&TxBulkEPList[epid]));
1462     						}
1463     					}
1464     
1465     					URB_List[epid] = u->next;
1466     				
1467     				} else {
1468     					urb_t *up;
1469     					for (up = URB_List[epid]; up->next != u; up = up->next);
1470     					up->next = u->next;
1471     				}
1472     				u->status = -ENOENT;
1473     				if (u->complete) {
1474     					u->complete(u);
1475     				}
1476     				
1477     				hc_priv = (etrax_urb_priv_t *)u->hcpriv;
1478     				cleanup_sb(hc_priv->first_sb);
1479     				kfree(hc_priv);
1480     			}
1481     		}
1482     	}
1483     
1484     	restore_flags(flags);
1485     		
1486     	DBFEXIT;
1487     	return 0;
1488     }
1489     
1490     static int etrax_usb_get_frame_number(struct usb_device *usb_dev)
1491     {
1492     	DBFENTER;
1493     	DBFEXIT;
1494     	return (*R_USB_FM_NUMBER);
1495     }
1496     
1497     static int etrax_usb_allocate_dev(struct usb_device *usb_dev)
1498     {  
1499     	DBFENTER;
1500     	DBFEXIT;
1501     	return 0;
1502     }
1503     
1504     static int etrax_usb_deallocate_dev(struct usb_device *usb_dev)
1505     {
1506     	DBFENTER;
1507     	DBFEXIT;
1508     	return 0;
1509     }
1510     
1511     static void etrax_usb_tx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1512     {
1513     	etrax_hc_t *hc = (etrax_hc_t *)vhc;
1514     	int epid;
1515     	char eol;
1516     	urb_t *urb;
1517     	USB_EP_Desc_t *tmp_ep;
1518     	USB_SB_Desc_t *tmp_sb;
1519     	
1520     	DBFENTER;
1521     
1522     	if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub0_descr)) {
1523     		info("dma8_sub0_descr (BULK) intr.");
1524     		*R_DMA_CH8_SUB0_CLR_INTR = IO_STATE(R_DMA_CH8_SUB0_CLR_INTR, clr_descr, do);
1525     	}
1526     	if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub1_descr)) {
1527     		info("dma8_sub1_descr (CTRL) intr.");
1528     		*R_DMA_CH8_SUB1_CLR_INTR = IO_STATE(R_DMA_CH8_SUB1_CLR_INTR, clr_descr, do);
1529     	}
1530     	if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub2_descr)) {
1531     		info("dma8_sub2_descr (INT) intr.");
1532     		*R_DMA_CH8_SUB2_CLR_INTR = IO_STATE(R_DMA_CH8_SUB2_CLR_INTR, clr_descr, do);
1533     	}
1534     	if (*R_IRQ_READ2 & IO_MASK(R_IRQ_READ2, dma8_sub3_descr)) {
1535     		info("dma8_sub3_descr (ISO) intr.");
1536     		*R_DMA_CH8_SUB3_CLR_INTR = IO_STATE(R_DMA_CH8_SUB3_CLR_INTR, clr_descr, do);
1537     	}
1538     	
1539     	DBFEXIT;
1540     }
1541     
1542     static void etrax_usb_rx_interrupt(int irq, void *vhc, struct pt_regs *regs)
1543     {
1544     	int epid = 0;
1545     	urb_t *urb;
1546     	etrax_urb_priv_t *urb_priv;
1547     		
1548     	*R_DMA_CH9_CLR_INTR = IO_STATE(R_DMA_CH9_CLR_INTR, clr_eop, do);
1549     
1550     	while (myNextRxDesc->status & IO_MASK(USB_IN_status, eop)) {
1551     		if (myNextRxDesc->status & IO_MASK(USB_IN_status, nodata)) {
1552     
1553     			goto skip_out;
1554     		}
1555     
1556     		if (myNextRxDesc->status & IO_MASK(USB_IN_status, error)) {
1557     			
1558     			goto skip_out;
1559     		}
1560     		
1561     		epid = IO_EXTRACT(USB_IN_status, epid, myNextRxDesc->status);
1562     
1563     		urb = URB_List[epid];
1564     
1565     		if (urb && usb_pipein(urb->pipe)) {
1566     			urb_priv = (etrax_urb_priv_t *)urb->hcpriv;
1567     
1568     			if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
1569     				struct in_chunk *in;
1570     				dbg_intr("Packet for epid %d in rx buffers", epid);
1571     				in = kmalloc(sizeof(struct in_chunk), GFP_ATOMIC);
1572     				in->length = myNextRxDesc->hw_len;
1573     				in->data = kmalloc(in->length, GFP_ATOMIC);
1574     				memcpy(in->data, phys_to_virt(myNextRxDesc->buf), in->length);
1575     				list_add_tail(&in->list, &urb_priv->ep_in_list);
1576     #ifndef ETRAX_USB_INTR_IRQ
1577     				etrax_usb_hc_intr_top_half(irq, vhc, regs);
1578     #endif
1579     				
1580     			} else {
1581     				if ((urb_priv->rx_offset + myNextRxDesc->hw_len) >
1582     				    urb->transfer_buffer_length) {
1583     					err("Packet (epid: %d) in RX buffer was bigger "
1584     					    "than the URB has room for !!!", epid);
1585     					goto skip_out;
1586     				}
1587     				
1588     				memcpy(urb->transfer_buffer + urb_priv->rx_offset,
1589     				       phys_to_virt(myNextRxDesc->buf), myNextRxDesc->hw_len);
1590     				
1591     				urb_priv->rx_offset += myNextRxDesc->hw_len;
1592     			}
1593     			
1594     			if (myNextRxDesc->status & IO_MASK(USB_IN_status, eot)) {
1595     				urb_priv->eot = 1;
1596     			}
1597     			
1598     		} else {
1599     			err("This is almost fatal, inpacket for epid %d which does not exist "
1600     			    " or is out !!!\nURB was at 0x%08X", epid, urb);
1601     			
1602     			goto skip_out;
1603     		}
1604     
1605     	skip_out:
1606     		myPrevRxDesc = myNextRxDesc;
1607     		myPrevRxDesc->command |= IO_MASK(USB_IN_command, eol);
1608     		myLastRxDesc->command &= ~IO_MASK(USB_IN_command, eol);
1609     		myLastRxDesc = myPrevRxDesc;
1610     
1611     		myNextRxDesc->status = 0;
1612     		myNextRxDesc = phys_to_virt(myNextRxDesc->next);
1613     	}
1614     }
1615     
1616     
1617     
1618     static void cleanup_sb(USB_SB_Desc_t *sb)
1619     {
1620     	USB_SB_Desc_t *next_sb;
1621     	
1622     	DBFENTER;
1623     
1624     	if (sb == NULL) {
1625     		err("cleanup_sb was given a NULL pointer");
1626     		return;
1627     	}
1628     
1629     	while (!(sb->command & IO_MASK(USB_SB_command, eol))) {
1630     		next_sb = (USB_SB_Desc_t *)phys_to_virt(sb->next);
1631     		kmem_cache_free(usb_desc_cache, sb);
1632     		sb = next_sb;
1633     	}
1634     
1635     	kmem_cache_free(usb_desc_cache, sb);
1636     
1637     	DBFEXIT;
1638     
1639     }
1640     
1641     static int handle_control_transfer_attn(char epid, int status)
1642     {
1643     	urb_t *old_urb;
1644     	etrax_urb_priv_t *hc_priv;	
1645     
1646     	DBFENTER;
1647     
1648     	clear_bit(epid, (void *)&ep_really_active);
1649     	
1650     	old_urb = URB_List[epid];
1651     	URB_List[epid] = old_urb->next;
1652     	
1653     	/* if (status == 0 && IN) find data and copy to urb */
1654     	if (status == 0 && usb_pipein(old_urb->pipe)) {
1655     		unsigned long flags;
1656     		etrax_urb_priv_t *urb_priv;
1657     
1658     		urb_priv = (etrax_urb_priv_t *)old_urb->hcpriv;
1659     		save_flags(flags);
1660     		cli();
1661     		if (urb_priv->eot == 1) {
1662     			old_urb->actual_length = urb_priv->rx_offset;
1663     			dbg_ctrl("urb_priv->rx_offset: %d in handle_control_attn", urb_priv->rx_offset);
1664     		} else {
1665     			status = -EPROTO;
1666     			old_urb->actual_length = 0;
1667     			err("(CTRL) No eot set in IN data!!! rx_offset: %d", urb_priv->rx_offset);
1668     		}
1669     
1670     		restore_flags(flags);
1671     	}
1672     	
1673     	/* If there are any more URB's in the list we'd better start sending */
1674     	if (URB_List[epid]) {
1675     		etrax_usb_do_ctrl_hw_add(URB_List[epid], epid,
1676     					 usb_maxpacket(URB_List[epid]->dev, URB_List[epid]->pipe,
1677     						       usb_pipeout(URB_List[epid]->pipe)));
1678     	}
1679     #if 1
1680     	else {
1681     		/* This means that this EP is now free, deconfigure it */
1682     		etrax_usb_free_epid(epid);
1683     	}
1684     #endif
1685     	
1686     	/* Remember to free the SB's */
1687     	hc_priv = (etrax_urb_priv_t *)old_urb->hcpriv;
1688     	cleanup_sb(hc_priv->first_sb);
1689     	kfree(hc_priv);
1690     
1691     	old_urb->status = status;
1692     	if (old_urb->complete) {
1693     		old_urb->complete(old_urb);
1694     	}
1695     
1696     	DBFEXIT;
1697     }
1698     
1699     
1700     
1701     static void etrax_usb_hc_intr_bottom_half(void *data)
1702     {
1703     	struct usb_reg_context *reg = (struct usb_reg_context *)data;
1704     	urb_t *old_urb;
1705     	
1706     	int error_code;
1707     	int epid;
1708     
1709     	__u32 r_usb_ept_data;
1710     
1711     	etrax_hc_t *hc = reg->hc; 
1712     	__u16 r_usb_rh_port_status_1;
1713     	__u16 r_usb_rh_port_status_2;
1714     	
1715     	DBFENTER;
1716     
1717     	if (reg->r_usb_irq_mask_read & IO_MASK(R_USB_IRQ_MASK_READ, port_status)) {
1718     
1719     		/*
1720     		  The Etrax RH does not include a wPortChange register, so this has
1721     		  to be handled in software. See section 11.16.2.6.2 in USB 1.1 spec
1722     		  for details.
1723     		*/
1724     		
1725     		r_usb_rh_port_status_1 = reg->r_usb_rh_port_status_1;
1726     		r_usb_rh_port_status_2 = reg->r_usb_rh_port_status_2;
1727     		
1728     		dbg_rh("port_status pending");
1729     		dbg_rh("r_usb_rh_port_status_1: 0x%04X", r_usb_rh_port_status_1);
1730     		dbg_rh("r_usb_rh_port_status_2: 0x%04X", r_usb_rh_port_status_2);
1731     
1732     		/* C_PORT_CONNECTION is set on any transition */
1733     		hc->rh.wPortChange_1 |=
1734     			((r_usb_rh_port_status_1 & (1 << RH_PORT_CONNECTION)) !=
1735     			 (hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_CONNECTION))) ?
1736     			(1 << RH_PORT_CONNECTION) : 0;
1737     		
1738     		hc->rh.wPortChange_2 |=
1739     			((r_usb_rh_port_status_2 & (1 << RH_PORT_CONNECTION)) !=
1740     			 (hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_CONNECTION))) ?
1741     			(1 << RH_PORT_CONNECTION) : 0;
1742     
1743     		/* C_PORT_ENABLE is _only_ set on a one to zero transition */
1744     		hc->rh.wPortChange_1 |=
1745     			((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_ENABLE))
1746     			 && !(r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
1747     			(1 << RH_PORT_ENABLE) : 0;
1748     		
1749     		hc->rh.wPortChange_2 |=
1750     			((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_ENABLE))
1751     			 && !(r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
1752     			(1 << RH_PORT_ENABLE) : 0;
1753     		
1754     		/* C_PORT_SUSPEND seems difficult, lets ignore it.. (for now) */
1755     		
1756     		/* C_PORT_RESET is _only_ set on a transition from the resetting state
1757     		   to the enabled state */
1758     		hc->rh.wPortChange_1 |=
1759     			((hc->rh.prev_wPortStatus_1 & (1 << RH_PORT_RESET))
1760     			 && (r_usb_rh_port_status_1 & (1 << RH_PORT_ENABLE))) ?
1761     			(1 << RH_PORT_RESET) : 0;
1762     		
1763     		hc->rh.wPortChange_2 |=
1764     			((hc->rh.prev_wPortStatus_2 & (1 << RH_PORT_RESET))
1765     			 && (r_usb_rh_port_status_2 & (1 << RH_PORT_ENABLE))) ?
1766     			(1 << RH_PORT_RESET) : 0;
1767     		
1768     		hc->rh.prev_wPortStatus_1 = r_usb_rh_port_status_1;
1769     		hc->rh.prev_wPortStatus_2 = r_usb_rh_port_status_2;	
1770     	}
1771     
1772     	for (epid = 0; epid < 32; epid++) {
1773     
1774     		unsigned long flags;
1775     
1776     		save_flags(flags);
1777     		cli();
1778     		*R_USB_EPT_INDEX = IO_FIELD(R_USB_EPT_INDEX, value, epid); nop();
1779     		r_usb_ept_data = *R_USB_EPT_DATA;
1780     		restore_flags(flags);
1781     
1782     		if (r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, hold)) {
1783     			warn("Was hold for epid %d", epid);
1784     			continue;
1785     		}
1786     
1787     		if (!(r_usb_ept_data & IO_MASK(R_USB_EPT_DATA, valid))) {
1788     			continue;
1789     		}
1790     		
1791     		
1792     		if (test_bit(epid, (void *)&reg->r_usb_epid_attn)) {
1793     
1794     			if (URB_List[epid] == NULL) {
1795     				err("R_USB_EPT_DATA is 0x%08X", r_usb_ept_data);
1796     				err("submit urb has been called %d times..", submit_urb_count);
1797     				err("EPID_ATTN for epid %d, with NULL entry in list", epid);
1798     				return;
1799     			}
1800     			
1801     			dbg_ep("r_usb_ept_data [%d] == 0x%08X", epid,
1802     			       r_usb_ept_data);
1803     			
1804     			error_code = IO_EXTRACT(R_USB_EPT_DATA, error_code,
1805     						r_usb_ept_data);
1806     			
1807     			if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
1808     				/* no_error means that this urb was sucessfully sent or that we have
1809     				   some undefinde error*/
1810     				
1811     				if (IO_EXTRACT(R_USB_EPT_DATA, error_count_out, r_usb_ept_data) == 3 ||
1812     				    IO_EXTRACT(R_USB_EPT_DATA, error_count_in, r_usb_ept_data) == 3) {
1813     				/* Actually there were transmission errors */
1814     					warn("Undefined error for endpoint %d", epid);
1815     					if (usb_pipetype(URB_List[epid]->pipe) == PIPE_CONTROL) {
1816     						handle_control_transfer_attn(epid, -EPROTO);
1817     					} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_BULK) {
1818     						handle_bulk_transfer_attn(epid, -EPROTO);
1819     					} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1820     						handle_intr_transfer_attn(epid, -EPROTO);
1821     					}
1822     							   
1823     				} else {
1824     
1825     					if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
1826     						if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1827     							etrax_usb_do_intr_recover(epid);
1828     						} else {
1829     							panic("Epid attention for epid %d (none INTR), with no errors and no "
1830     							      "exessive retry r_usb_status is 0x%02X\n",
1831     							      epid, reg->r_usb_status);
1832     						}
1833     						
1834     					} else if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
1835     						panic("Epid attention for epid %d, with no errors and no "
1836     						      "exessive retry r_usb_status is 0x%02X\n",
1837     						      epid, reg->r_usb_status);
1838     						
1839     					}
1840     					
1841     					warn("Epid attention for epid %d, with no errors and no "
1842     					     "exessive retry r_usb_status is 0x%02X",
1843     					     epid, reg->r_usb_status);
1844     					warn("OUT error count: %d", IO_EXTRACT(R_USB_EPT_DATA, error_count_out,
1845     									       r_usb_ept_data));
1846     					warn("IN  error count: %d", IO_EXTRACT(R_USB_EPT_DATA, error_count_in,
1847     									       r_usb_ept_data));
1848     					
1849     
1850     				}
1851     				
1852     			} else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, stall)) {
1853     				warn("Stall for endpoint %d", epid);
1854     				if (usb_pipetype(URB_List[epid]->pipe) == PIPE_CONTROL) {
1855     					handle_control_transfer_attn(epid, -EPIPE);
1856     				} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_BULK) {
1857     					handle_bulk_transfer_attn(epid, -EPIPE);
1858     				} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1859     					handle_intr_transfer_attn(epid, -EPIPE);
1860     				}
1861     				
1862     				
1863     			} else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, bus_error)) {
1864     				panic("USB bus error for endpoint %d\n", epid);
1865     				
1866     			} else if (error_code == IO_STATE_VALUE(R_USB_EPT_DATA, error_code, buffer_error)) {
1867     				warn("Buffer error for endpoint %d", epid);
1868     
1869     				if (usb_pipetype(URB_List[epid]->pipe) == PIPE_CONTROL) {
1870     					handle_control_transfer_attn(epid, -EPROTO);
1871     				} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_BULK) {
1872     					handle_bulk_transfer_attn(epid, -EPROTO);
1873     				} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1874     					handle_intr_transfer_attn(epid, -EPROTO);
1875     				}
1876     
1877     			}
1878     		} else if (test_bit(epid, (void *)&ep_really_active)) {
1879     			/* Should really be else if (testbit(really active)) */
1880     
1881     			if (usb_pipetype(URB_List[epid]->pipe) == PIPE_CONTROL) {
1882     
1883     				if (!(TxCtrlEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1884     					/* Now we have to verify that this CTRL endpoint got disabled
1885     					   cause it reached end of list with no error */
1886     					
1887     					if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
1888     					    IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
1889     						/*
1890     						  This means that the endpoint has no error, is disabled
1891     						  and had inserted traffic,
1892     						  i.e. transfer sucessfully completed
1893     						*/
1894     						dbg_ctrl("Last SB for CTRL %d sent sucessfully", epid);
1895     						handle_control_transfer_attn(epid, 0);
1896     					}
1897     				}
1898     				
1899     			} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_BULK) {
1900     				if (!(TxBulkEPList[epid].command & IO_MASK(USB_EP_command, enable))) {
1901     					/* Now we have to verify that this BULK endpoint go disabled
1902     					   cause it reached end of list with no error */
1903     
1904     					if (IO_EXTRACT(R_USB_EPT_DATA, error_code, r_usb_ept_data) ==
1905     					    IO_STATE_VALUE(R_USB_EPT_DATA, error_code, no_error)) {
1906     						/*
1907     						  This means that the endpoint has no error, is disabled
1908     						  and had inserted traffic,
1909     						  i.e. transfer sucessfully completed
1910     						*/
1911     						dbg_bulk("Last SB for BULK %d sent sucessfully", epid);
1912     						handle_bulk_transfer_attn(epid, 0);
1913     					}
1914     				}
1915     			} else if (usb_pipetype(URB_List[epid]->pipe) == PIPE_INTERRUPT) {
1916     				handle_intr_transfer_attn(epid, 0);
1917     			}
1918     		}
1919     		
1920     	}	
1921     
1922     	kfree(reg);
1923     
1924     	DBFEXIT;
1925     }
1926     
1927     
1928     static void etrax_usb_hc_intr_top_half(int irq, void *vhc, struct pt_regs *regs)
1929     {
1930     	struct usb_reg_context *reg;
1931     
1932     	DBFENTER;
1933     
1934     	reg = (struct usb_reg_context *)kmalloc(sizeof(struct usb_reg_context), GFP_ATOMIC);
1935     
1936     	if (!(reg)) {
1937     		panic("kmalloc failed in top_half\n");
1938     	}
1939     
1940     	reg->hc = (etrax_hc_t *)vhc;
1941     	reg->r_usb_irq_mask_read    = *R_USB_IRQ_MASK_READ;
1942     	reg->r_usb_status           = *R_USB_STATUS;
1943     
1944     #if 0
1945     	if (reg->r_usb_status & IO_MASK(R_USB_STATUS, perror)) {
1946     		panic("r_usb_status said perror\n");
1947     	}
1948     	if (reg->r_usb_status & IO_MASK(R_USB_STATUS, ourun)) {
1949     		panic("r_usb_status said ourun !!!\n");
1950     	}
1951     #endif
1952     	
1953     	reg->r_usb_epid_attn        = *R_USB_EPID_ATTN;
1954     
1955     	reg->r_usb_rh_port_status_1 = *R_USB_RH_PORT_STATUS_1;
1956     	reg->r_usb_rh_port_status_2 = *R_USB_RH_PORT_STATUS_2;
1957     
1958     	reg->usb_bh.sync = 0;
1959     	reg->usb_bh.routine = etrax_usb_hc_intr_bottom_half;
1960     	reg->usb_bh.data = reg;
1961     
1962     	queue_task(&reg->usb_bh, &tq_immediate);
1963     	mark_bh(IMMEDIATE_BH);
1964     
1965     	DBFEXIT;
1966     }
1967     
1968     static int etrax_rh_submit_urb(urb_t *urb)
1969     {
1970     	struct usb_device *usb_dev = urb->dev;
1971     	etrax_hc_t *hc = usb_dev->bus->hcpriv;
1972     	unsigned int pipe = urb->pipe;
1973     	devrequest *cmd = (devrequest *) urb->setup_packet;
1974     	void *data = urb->transfer_buffer;
1975     	int leni = urb->transfer_buffer_length;
1976     	int len = 0;
1977     	int status = 0;
1978     	int stat = 0;
1979     	int i;
1980     
1981     	__u16 cstatus;
1982     
1983     	__u16 bmRType_bReq;
1984     	__u16 wValue;
1985     	__u16 wIndex;
1986     	__u16 wLength;
1987     
1988     	DBFENTER;
1989     
1990     	if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
1991     		dbg_rh("Root-Hub submit IRQ: every %d ms", urb->interval);
1992     		hc->rh.urb = urb;
1993     		hc->rh.send = 1;
1994     		hc->rh.interval = urb->interval;
1995     		etrax_rh_init_int_timer(urb);
1996     		DBFEXIT;
1997     		
1998     		return 0;
1999     	}
2000     
2001     	bmRType_bReq = cmd->requesttype | cmd->request << 8;
2002     	wValue = le16_to_cpu(cmd->value);
2003     	wIndex = le16_to_cpu(cmd->index);
2004     	wLength = le16_to_cpu(cmd->length);
2005     
2006     	dbg_rh("bmRType_bReq : 0x%04X (%d)", bmRType_bReq, bmRType_bReq);
2007     	dbg_rh("wValue       : 0x%04X (%d)", wValue, wValue);
2008     	dbg_rh("wIndex       : 0x%04X (%d)", wIndex, wIndex);
2009     	dbg_rh("wLength      : 0x%04X (%d)", wLength, wLength);
2010     	
2011     	switch (bmRType_bReq) {
2012     		
2013     		/* Request Destination:
2014     		   without flags: Device, 
2015     		   RH_INTERFACE: interface, 
2016     		   RH_ENDPOINT: endpoint,
2017     		   RH_CLASS means HUB here, 
2018     		   RH_OTHER | RH_CLASS  almost ever means HUB_PORT here 
2019     		 */
2020     
2021     	case RH_GET_STATUS:
2022     		*(__u16 *) data = cpu_to_le16 (1);
2023     		OK (2);
2024     		
2025     	case RH_GET_STATUS | RH_INTERFACE:
2026     		*(__u16 *) data = cpu_to_le16 (0);
2027     		OK (2);
2028     		
2029     	case RH_GET_STATUS | RH_ENDPOINT:
2030     		*(__u16 *) data = cpu_to_le16 (0);
2031     		OK (2);
2032     		
2033     	case RH_GET_STATUS | RH_CLASS:
2034     		*(__u32 *) data = cpu_to_le32 (0);
2035     		OK (4);		/* hub power ** */
2036     		
2037     	case RH_GET_STATUS | RH_OTHER | RH_CLASS:
2038     		if (wIndex == 1) {
2039     			*((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_1);
2040     			*((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_1);
2041     		}
2042     		else if (wIndex == 2) {
2043     			*((__u16*)data) = cpu_to_le16(hc->rh.prev_wPortStatus_2);
2044     			*((__u16*)data + 1) = cpu_to_le16(hc->rh.wPortChange_2);
2045     		}
2046     		else {
2047     			dbg_rh("RH_GET_STATUS whith invalid wIndex !!");
2048     			OK(0);
2049     		}
2050     		
2051     		OK(4);
2052     
2053     	case RH_CLEAR_FEATURE | RH_ENDPOINT:
2054     		switch (wValue) {
2055     		case (RH_ENDPOINT_STALL):
2056     			OK (0);
2057     		}
2058     		break;
2059     
2060     	case RH_CLEAR_FEATURE | RH_CLASS:
2061     		switch (wValue) {
2062     		case (RH_C_HUB_OVER_CURRENT):
2063     			OK (0);	/* hub power over current ** */
2064     		}
2065     		break;
2066     
2067     	case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
2068     		switch (wValue) {
2069     		case (RH_PORT_ENABLE):
2070     			if (wIndex == 1) {
2071     
2072     				dbg_rh("trying to do disable of port 1");
2073     
2074     				*R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, yes);
2075     				while (hc->rh.prev_wPortStatus_1 &
2076     				       IO_STATE(R_USB_RH_PORT_STATUS_1, enabled, yes));
2077     				*R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
2078     				dbg_rh("Port 1 is disabled");
2079     
2080     			} else if (wIndex == 2) {
2081     
2082     				dbg_rh("trying to do disable of port 2");
2083     				
2084     				*R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, yes);
2085     				while (hc->rh.prev_wPortStatus_2 &
2086     				       IO_STATE(R_USB_RH_PORT_STATUS_2, enabled, yes));
2087     				*R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
2088     				dbg_rh("Port 2 is disabled");
2089     
2090     			} else {
2091     				dbg_rh("RH_CLEAR_FEATURE->RH_PORT_ENABLE "
2092     				       "with invalid wIndex == %d!!", wIndex);
2093     			}			
2094     				
2095     			OK (0);
2096     		case (RH_PORT_SUSPEND):
2097     			/* Opposite to suspend should be resume, so well do a resume */
2098     			if (wIndex == 1) {
2099     				*R_USB_COMMAND =
2100     					IO_STATE(R_USB_COMMAND, port_sel, port1) |
2101     					IO_STATE(R_USB_COMMAND, port_cmd, resume)|
2102     					IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
2103     			} else if (wIndex == 2) {
2104     				*R_USB_COMMAND =
2105     					IO_STATE(R_USB_COMMAND, port_sel, port2) |
2106     					IO_STATE(R_USB_COMMAND, port_cmd, resume)|
2107     					IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
2108     			} else {
2109     				dbg_rh("RH_CLEAR_FEATURE->RH_PORT_SUSPEND "
2110     				       "with invalid wIndex == %d!!", wIndex);
2111     			}
2112     			
2113     			OK (0);
2114     		case (RH_PORT_POWER):
2115     			OK (0);	/* port power ** */
2116     		case (RH_C_PORT_CONNECTION):
2117     			
2118     			if (wIndex == 1) {
2119     				hc->rh.wPortChange_1 &= ~(1 << RH_PORT_CONNECTION);
2120     			}
2121     			else if (wIndex == 2) {
2122     				hc->rh.wPortChange_2 &= ~(1 << RH_PORT_CONNECTION);
2123     			}
2124     			else {
2125     				dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_CONNECTION "
2126     				       "with invalid wIndex == %d!!", wIndex);
2127     			}
2128     
2129     			OK (0);
2130     		case (RH_C_PORT_ENABLE):
2131     			if (wIndex == 1) {
2132     				hc->rh.wPortChange_1 &= ~(1 << RH_PORT_ENABLE);
2133     			}
2134     			else if (wIndex == 2) {
2135     				hc->rh.wPortChange_2 &= ~(1 << RH_PORT_ENABLE);
2136     			}
2137     			else {
2138     				dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_ENABLE "
2139     				       "with invalid wIndex == %d!!", wIndex);
2140     			}
2141     			OK (0);
2142     		case (RH_C_PORT_SUSPEND):
2143     /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
2144     			OK (0);
2145     		case (RH_C_PORT_OVER_CURRENT):
2146     			OK (0);	/* port power over current ** */
2147     		case (RH_C_PORT_RESET):
2148     			if (wIndex == 1) {
2149     				hc->rh.wPortChange_1 &= ~(1 << RH_PORT_RESET);
2150     			}
2151     			else if (wIndex == 2) {
2152     				dbg_rh("This is wPortChange before clear: 0x%04X", hc->rh.wPortChange_2);
2153     				
2154     				hc->rh.wPortChange_2 &= ~(1 << RH_PORT_RESET);
2155     				dbg_rh("This is wPortChange after clear: 0x%04X", hc->rh.wPortChange_2);
2156     			} else {
2157     				dbg_rh("RH_CLEAR_FEATURE->RH_C_PORT_RESET "
2158     				       "with invalid index == %d!!", wIndex);
2159     			}
2160     
2161     			OK (0);
2162     		
2163     		}
2164     		break;
2165     		
2166     	case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
2167     		switch (wValue) {
2168     		case (RH_PORT_SUSPEND):
2169     			if (wIndex == 1) {
2170     				*R_USB_COMMAND =
2171     					IO_STATE(R_USB_COMMAND, port_sel, port1) |
2172     					IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
2173     					IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
2174     			} else if (wIndex == 2) {
2175     				*R_USB_COMMAND =
2176     					IO_STATE(R_USB_COMMAND, port_sel, port2) |
2177     					IO_STATE(R_USB_COMMAND, port_cmd, suspend) |
2178     					IO_STATE(R_USB_COMMAND, ctrl_cmd, nop);
2179     			} else {
2180     				dbg_rh("RH_SET_FEATURE->RH_C_PORT_SUSPEND "
2181     				       "with invalid wIndex == %d!!", wIndex);
2182     			}			
2183     
2184     			OK (0);
2185     		case (RH_PORT_RESET):
2186     			if (wIndex == 1) {
2187     				int port1_retry;
2188     				
2189     			port1_redo:
2190     				dbg_rh("Doing reset of port 1");
2191     
2192     				*R_USB_COMMAND =
2193     					IO_STATE(R_USB_COMMAND, port_cmd, reset) |
2194     					IO_STATE(R_USB_COMMAND, port_sel, port1);
2195     
2196     				/* We must once again wait at least 10ms for the device to recover */
2197     
2198     				port1_retry = 0;
2199     				while (!((*((volatile __u16 *)&hc->rh.prev_wPortStatus_1)) &
2200     					 IO_STATE(R_USB_RH_PORT_STATUS_1,
2201     						  enabled, yes))) {
2202     					printk(""); if (port1_retry++ >= 10000) {goto port1_redo;}
2203     				}
2204     				
2205     				/* This only seems to work if we use printk,
2206     				   not even schedule() works !!! WHY ?? */
2207     
2208     				udelay(15000);
2209     			}
2210     			else if (wIndex == 2) {
2211     				int port2_retry;
2212     				
2213     			port2_redo:
2214     				dbg_rh("Doing reset of port 2");
2215     				
2216     				*R_USB_COMMAND =
2217     					IO_STATE(R_USB_COMMAND, port_cmd, reset) |
2218     					IO_STATE(R_USB_COMMAND, port_sel, port2);
2219     
2220     				/* We must once again wait at least 10ms for the device to recover */
2221     
2222     				port2_retry = 0;
2223     				while (!((*((volatile __u16 *)&hc->rh.prev_wPortStatus_2)) &
2224     					 IO_STATE(R_USB_RH_PORT_STATUS_2,
2225     						  enabled, yes))) {
2226     					printk(""); if (port2_retry++ >= 10000) {goto port2_redo;}
2227     				}
2228     				
2229     				/* This only seems to work if we use printk,
2230     				   not even schedule() works !!! WHY ?? */
2231     
2232     				udelay(15000);
2233     			}
2234     
2235     			/* Try to bring the HC into running state */
2236     			*R_USB_COMMAND =
2237     				IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
2238     			
2239     			nop(); while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
2240     			
2241     			dbg_rh("...Done");
2242     			OK(0);
2243     
2244     		case (RH_PORT_POWER):
2245     			OK (0);	/* port power ** */
2246     		case (RH_PORT_ENABLE):
2247     			/* There is no rh port enable command in the Etrax USB interface!!!! */
2248     			OK (0);
2249     
2250     		}
2251     		break;
2252     		
2253     	case RH_SET_ADDRESS:
2254     		hc->rh.devnum = wValue;
2255     		dbg_rh("RH address set to: %d", hc->rh.devnum);
2256     		OK (0);
2257     		
2258     	case RH_GET_DESCRIPTOR:
2259     		switch ((wValue & 0xff00) >> 8) {
2260     		case (0x01):	/* device descriptor */
2261     			len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_dev_des), wLength));
2262     			memcpy (data, root_hub_dev_des, len);
2263     			OK (len);
2264     		case (0x02):	/* configuration descriptor */
2265     			len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_config_des), wLength));
2266     			memcpy (data, root_hub_config_des, len);
2267     			OK (len);
2268     		case (0x03):	/* string descriptors */
2269     			len = usb_root_hub_string (wValue & 0xff,
2270     						   0xff, "ETRAX 100LX",
2271     						   data, wLength);
2272     			if (len > 0) {
2273     				OK(min_t(int, leni, len));
2274     			} else 
2275     				stat = -EPIPE;
2276     		}
2277     		break;
2278     		
2279     	case RH_GET_DESCRIPTOR | RH_CLASS:
2280     		root_hub_hub_des[2] = hc->rh.numports;
2281     		len = min_t(unsigned int, leni, min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
2282     		memcpy (data, root_hub_hub_des, len);
2283     		OK (len);
2284     		
2285     	case RH_GET_CONFIGURATION:
2286     		*(__u8 *) data = 0x01;
2287     		OK (1);
2288     		
2289     	case RH_SET_CONFIGURATION:
2290     		OK (0);
2291     		
2292     	default:
2293     		stat = -EPIPE;
2294     	}
2295     
2296     	urb->actual_length = len;
2297     	urb->status = stat;
2298     	urb->dev=NULL;
2299     	if (urb->complete) {
2300     		urb->complete (urb);
2301     	}
2302     	DBFEXIT;
2303     	
2304     	return 0;
2305     }
2306     
2307     static int __init etrax_usb_hc_init(void)
2308     {
2309     	static etrax_hc_t *hc;
2310     	struct usb_bus *bus;
2311     	struct usb_device *usb_rh;
2312     	
2313     	DBFENTER;
2314     
2315     	info("ETRAX 100LX USB-HCD %s (c) 2001 Axis Communications AB\n", usb_hcd_version);
2316     	
2317     	hc = kmalloc(sizeof(etrax_hc_t), GFP_KERNEL);
2318     
2319     	/* We use kmem_cache_* to make sure that all DMA desc. are dword aligned */
2320     	usb_desc_cache = kmem_cache_create("usb_desc_cache", sizeof(USB_EP_Desc_t), 0, 0, 0, 0);
2321     	if (!usb_desc_cache) {
2322     		panic("USB Desc Cache allocation failed !!!\n");
2323     	}
2324     	
2325     	etrax_usb_bus = bus = usb_alloc_bus(&etrax_usb_device_operations);
2326     	hc->bus = bus;
2327     	bus->hcpriv = hc;
2328     
2329     	/* Initalize RH to the default address.
2330     	   And make sure that we have no status change indication */
2331     	hc->rh.numports = 2;  /* The RH has two ports */
2332     	hc->rh.devnum = 0;
2333     	hc->rh.wPortChange_1 = 0;
2334     	hc->rh.wPortChange_2 = 0;
2335     
2336     	/* Also initate the previous values to zero */
2337     	hc->rh.prev_wPortStatus_1 = 0;
2338     	hc->rh.prev_wPortStatus_2 = 0;
2339     
2340     	/* Initialize the intr-traffic flags */
2341     	hc->intr.sleeping = 0;
2342     	hc->intr.wq = NULL;
2343     
2344     	/* Initially all ep's are free except ep 0 */
2345     	ep_usage_bitmask = 0;
2346     	set_bit(0, (void *)&ep_usage_bitmask);
2347     	ep_really_active = 0;
2348     
2349     	memset(URB_List, 0, sizeof(URB_List));
2350     
2351     	/* This code should really be moved */
2352     
2353     	if (request_dma(USB_TX_DMA_NBR, "ETRAX 100LX built-in USB (Tx)")) {
2354     		err("Could not allocate DMA ch 8 for USB");
2355     		etrax_usb_hc_cleanup();
2356     		DBFEXIT;
2357     		return -1;
2358     	}
2359     	
2360     	if (request_dma(USB_RX_DMA_NBR, "ETRAX 100LX built-in USB (Rx)")) {
2361     		err("Could not allocate DMA ch 9 for USB");
2362     		etrax_usb_hc_cleanup();
2363     		DBFEXIT;
2364     		return -1;
2365     	}	
2366     #if 0  /* Moved to head.S */
2367     	*R_GEN_CONFIG = genconfig_shadow =
2368     		(genconfig_shadow & ~(IO_MASK(R_GEN_CONFIG, usb1) |
2369     				      IO_MASK(R_GEN_CONFIG, usb2) |
2370     				      IO_MASK(R_GEN_CONFIG, dma8) |
2371     				      IO_MASK(R_GEN_CONFIG, dma9))) |
2372     		IO_STATE(R_GEN_CONFIG, dma8, usb) |
2373     		IO_STATE(R_GEN_CONFIG, dma9, usb)
2374     #ifdef CONFIG_ETRAX_USB_HOST_PORT1
2375     		| IO_STATE(R_GEN_CONFIG, usb1, select)
2376     #endif
2377     #ifdef CONFIG_ETRAX_USB_HOST_PORT2
2378     		| IO_STATE(R_GEN_CONFIG, usb2, select)
2379     #endif
2380     	;
2381     #endif
2382     	
2383     	usb_register_bus(hc->bus);
2384     
2385     	/* We may have to set more bits, but these are the obvious ones */
2386     	*R_IRQ_MASK2_SET =
2387     		IO_STATE(R_IRQ_MASK2_SET, dma8_sub0_descr, set) |
2388     		IO_STATE(R_IRQ_MASK2_SET, dma8_sub1_descr, set) |
2389     		IO_STATE(R_IRQ_MASK2_SET, dma8_sub2_descr, set) |
2390     		IO_STATE(R_IRQ_MASK2_SET, dma8_sub3_descr, set);
2391     
2392     	*R_IRQ_MASK2_SET =
2393     		IO_STATE(R_IRQ_MASK2_SET, dma9_eop, set) |
2394     		IO_STATE(R_IRQ_MASK2_SET, dma9_descr, set);
2395     
2396     	*R_USB_IRQ_MASK_SET = 
2397     		IO_STATE(R_USB_IRQ_MASK_SET, ctl_status, set) |
2398     		IO_STATE(R_USB_IRQ_MASK_SET, ctl_eot, set) |
2399     		IO_STATE(R_USB_IRQ_MASK_SET, bulk_eot, set) |
2400     #ifdef ETRAX_USB_INTR_IRQ
2401     		IO_STATE(R_USB_IRQ_MASK_SET, intr_eot, set) |
2402     #endif
2403     		IO_STATE(R_USB_IRQ_MASK_SET, epid_attn, set) |
2404     		IO_STATE(R_USB_IRQ_MASK_SET, port_status, set);
2405     	
2406     	if (request_irq(ETRAX_USB_HC_IRQ, etrax_usb_hc_intr_top_half, 0,
2407     			"ETRAX 100LX built-in USB (HC)", hc)) {
2408     		err("Could not allocate IRQ %d for USB", ETRAX_USB_HC_IRQ);
2409     		etrax_usb_hc_cleanup();
2410     		DBFEXIT;
2411     		return -1;
2412     	}
2413     	
2414     	if (request_irq(ETRAX_USB_RX_IRQ, etrax_usb_rx_interrupt, 0,
2415     			"ETRAX 100LX built-in USB (Rx)", hc)) {
2416     		err("Could not allocate IRQ %d for USB", ETRAX_USB_RX_IRQ);
2417     		etrax_usb_hc_cleanup();
2418     		DBFEXIT;
2419     		return -1;
2420     	}
2421     	
2422     	if (request_irq(ETRAX_USB_TX_IRQ, etrax_usb_tx_interrupt, 0,
2423     			"ETRAX 100LX built-in USB (Tx)", hc)) {
2424     		err("Could not allocate IRQ %d for USB", ETRAX_USB_TX_IRQ);
2425     		etrax_usb_hc_cleanup();
2426     		DBFEXIT;
2427     		return -1;
2428     	}
2429     
2430     	/* Reset the USB interface (configures as HC) */
2431     	*R_USB_COMMAND =
2432     		IO_STATE(R_USB_COMMAND, ctrl_cmd, reset) |
2433     		IO_STATE(R_USB_COMMAND, port_cmd, reset);
2434     	
2435     	nop(); while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
2436     #if 1
2437     	/* Initate PSTART to all unallocatable bit times */
2438     	*R_USB_FM_PSTART = IO_FIELD(R_USB_FM_PSTART, value, 10000);
2439     #endif
2440     
2441     #ifdef CONFIG_ETRAX_USB_HOST_PORT1
2442     	*R_USB_PORT1_DISABLE = IO_STATE(R_USB_PORT1_DISABLE, disable, no);
2443     #endif
2444     	
2445     #ifdef CONFIG_ETRAX_USB_HOST_PORT2
2446     	*R_USB_PORT2_DISABLE = IO_STATE(R_USB_PORT2_DISABLE, disable, no);
2447     #endif
2448     	
2449     	*R_USB_COMMAND =
2450     		IO_STATE(R_USB_COMMAND, ctrl_cmd, host_config) |
2451     		IO_STATE(R_USB_COMMAND, port_cmd, reset);
2452     	
2453     	nop(); while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
2454     
2455     	*R_USB_COMMAND =
2456     		IO_STATE(R_USB_COMMAND, port_sel, port1) |
2457     		IO_STATE(R_USB_COMMAND, port_cmd, reset);
2458     
2459     	nop(); while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
2460     
2461     	/* Here we must wait at least 10ms so the device has time to recover */
2462     	udelay(15000);
2463     
2464     	init_rx_buffers();
2465     	init_tx_bulk_ep();
2466     	init_tx_ctrl_ep();
2467     	init_tx_intr_ep();
2468     
2469     	/* This works. It seems like the host_run command only has effect when a device is connected,
2470     	 i.e. it has to be done when a interrup */
2471     	*R_USB_COMMAND =
2472     		IO_STATE(R_USB_COMMAND, ctrl_cmd, host_run);
2473     	
2474     	nop(); while (*R_USB_COMMAND & IO_MASK(R_USB_COMMAND, busy));
2475     
2476     	usb_rh = usb_alloc_dev(NULL, hc->bus);
2477     	hc->bus->root_hub = usb_rh;
2478     	usb_connect(usb_rh);
2479     	usb_new_device(usb_rh);
2480     
2481     	DBFEXIT;
2482     	
2483     	return 0;
2484     }
2485     
2486     static void etrax_usb_hc_cleanup(void)
2487     {
2488     	DBFENTER;
2489     	
2490     	free_irq(ETRAX_USB_HC_IRQ, NULL);
2491     	free_irq(ETRAX_USB_RX_IRQ, NULL);
2492     	free_irq(ETRAX_USB_TX_IRQ, NULL);
2493     
2494     	free_dma(USB_TX_DMA_NBR);
2495     	free_dma(USB_RX_DMA_NBR);
2496     	usb_deregister_bus(etrax_usb_bus);
2497     
2498     	DBFEXIT;
2499     }
2500     
2501     module_init(etrax_usb_hc_init);
2502     module_exit(etrax_usb_hc_cleanup);
2503