OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [arch/] [cris/] [drivers/] [usb-host.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.