1 /*-------------------------------------------------------------------------*/
2 /* list of all controllers using this driver
3  * */
4 
5 static LIST_HEAD (hci_hcd_list);
6 
7 /* URB states (urb_state) */
8 /* isoc, interrupt single state */
9 
10 /* bulk transfer main state and 0-length packet */
11 #define US_BULK		0
12 #define US_BULK0	1
13 /* three setup states */
14 #define US_CTRL_SETUP	2
15 #define US_CTRL_DATA	1
16 #define US_CTRL_ACK	0
17 
18 /*-------------------------------------------------------------------------*/
19 /* HC private part of a device descriptor
20  * */
21 
22 #define NUM_EDS 32
23 
24 typedef struct epd {
25 	struct urb *pipe_head;
26 	struct list_head urb_queue;
27 //	int urb_state;
28 	struct timer_list timeout;
29 	int last_iso;		/* timestamp of last queued ISOC transfer */
30 
31 } epd_t;
32 
33 struct hci_device {
34 	epd_t ed[NUM_EDS];
35 };
36 
37 /*-------------------------------------------------------------------------*/
38 /* Virtual Root HUB
39  */
40 
41 #define usb_to_hci(usb)	((struct hci_device *)(usb)->hcpriv)
42 
43 struct virt_root_hub {
44 	int devnum;		/* Address of Root Hub endpoint */
45 	void *urb;		/* interrupt URB of root hub */
46 	int send;		/* active flag */
47 	int interval;		/* intervall of roothub interrupt transfers */
48 	struct timer_list rh_int_timer;	/* intervall timer for rh interrupt EP */
49 };
50 
51 #if 1
52 /* USB HUB CONSTANTS (not OHCI-specific; see hub.h and USB spec) */
53 
54 /* destination of request */
55 #define RH_INTERFACE		0x01
56 #define RH_ENDPOINT		0x02
57 #define RH_OTHER		0x03
58 
59 #define RH_CLASS		0x20
60 #define RH_VENDOR		0x40
61 
62 /* Requests: bRequest << 8 | bmRequestType */
63 #define RH_GET_STATUS		0x0080
64 #define RH_CLEAR_FEATURE	0x0100
65 #define RH_SET_FEATURE		0x0300
66 #define RH_SET_ADDRESS		0x0500
67 #define RH_GET_DESCRIPTOR	0x0680
68 #define RH_SET_DESCRIPTOR	0x0700
69 #define RH_GET_CONFIGURATION	0x0880
70 #define RH_SET_CONFIGURATION	0x0900
71 #define RH_GET_STATE		0x0280
72 #define RH_GET_INTERFACE	0x0A80
73 #define RH_SET_INTERFACE	0x0B00
74 #define RH_SYNC_FRAME		0x0C80
75 /* Our Vendor Specific Request */
76 #define RH_SET_EP		0x2000
77 
78 /* Hub port features */
79 #define RH_PORT_CONNECTION	0x00
80 #define RH_PORT_ENABLE		0x01
81 #define RH_PORT_SUSPEND		0x02
82 #define RH_PORT_OVER_CURRENT	0x03
83 #define RH_PORT_RESET		0x04
84 #define RH_PORT_POWER		0x08
85 #define RH_PORT_LOW_SPEED	0x09
86 
87 #define RH_C_PORT_CONNECTION	0x10
88 #define RH_C_PORT_ENABLE	0x11
89 #define RH_C_PORT_SUSPEND	0x12
90 #define RH_C_PORT_OVER_CURRENT	0x13
91 #define RH_C_PORT_RESET		0x14
92 
93 /* Hub features */
94 #define RH_C_HUB_LOCAL_POWER	0x00
95 #define RH_C_HUB_OVER_CURRENT	0x01
96 
97 #define RH_DEVICE_REMOTE_WAKEUP	0x00
98 #define RH_ENDPOINT_STALL	0x01
99 
100 #endif
101 
102 /*-------------------------------------------------------------------------*/
103 /* struct for each HC
104  */
105 
106 #define MAX_TRANS	32
107 
108 typedef struct td {
109 	struct urb *urb;
110 	__u16 len;
111 	__u16 iso_index;
112 } td_t;
113 
114 typedef struct td_array {
115 	int len;
116 	td_t td[MAX_TRANS];
117 } td_array_t;
118 
119 typedef struct hci {
120 	struct virt_root_hub rh;	/* roothub */
121 	wait_queue_head_t waitq;	/* deletion of URBs and devices needs a waitqueue */
122 	int active;			/* HC is operating */
123 
124 	struct list_head ctrl_list;	/* set of ctrl endpoints */
125 	struct list_head bulk_list;	/* set of bulk endpoints */
126 	struct list_head iso_list;	/* set of isoc endpoints */
127 	struct list_head intr_list;	/* ordered (tree) set of int endpoints */
128 	struct list_head del_list;	/* set of entpoints to be deleted */
129 
130 	td_array_t *td_array;
131 	td_array_t a_td_array;
132 	td_array_t i_td_array[2];
133 
134 	struct list_head hci_hcd_list;	/* list of all hci_hcd */
135 	struct usb_bus *bus;		/* our bus */
136 
137 //	int trans;			/* number of transactions pending */
138 	int active_urbs;
139 	int active_trans;
140 	int frame_number;		/* frame number */
141 	hcipriv_t hp;			/* individual part of hc type */
142 	int nakCnt;
143 	int last_packet_nak;
144 
145 } hci_t;
146 
147 /*-------------------------------------------------------------------------*/
148 /* condition (error) CC codes and mapping OHCI like
149  */
150 
151 #define TD_CC_NOERROR		0x00
152 #define TD_CC_CRC		0x01
153 #define TD_CC_BITSTUFFING	0x02
154 #define TD_CC_DATATOGGLEM	0x03
155 #define TD_CC_STALL		0x04
156 #define TD_DEVNOTRESP		0x05
157 #define TD_PIDCHECKFAIL		0x06
158 #define TD_UNEXPECTEDPID	0x07
159 #define TD_DATAOVERRUN		0x08
160 #define TD_DATAUNDERRUN		0x09
161 #define TD_BUFFEROVERRUN	0x0C
162 #define TD_BUFFERUNDERRUN	0x0D
163 #define TD_NOTACCESSED		0x0F
164 
165 
166 /* urb interface functions */
167 static int hci_get_current_frame_number (struct usb_device *usb_dev);
168 static int hci_unlink_urb (struct urb * urb);
169 
170 static int qu_queue_urb (hci_t * hci, struct urb * urb);
171 
172 /* root hub */
173 static int rh_init_int_timer (struct urb * urb);
174 static int rh_submit_urb (struct urb * urb);
175 static int rh_unlink_urb (struct urb * urb);
176 
177 /* schedule functions */
178 static int sh_add_packet (hci_t * hci, struct urb * urb);
179 
180 /* hc specific functions */
181 static inline void hc_flush_data_cache (hci_t * hci, void *data, int len);
182 static inline int hc_parse_trans (hci_t * hci, int *actbytes, __u8 * data,
183 				  int *cc, int *toggle, int length, int pid,
184 				  int urb_state);
185 static inline int hc_add_trans (hci_t * hci, int len, void *data, int toggle,
186 				int maxps, int slow, int endpoint, int address,
187 				int pid, int format, int urb_state);
188 
189 static void hc_start_int (hci_t * hci);
190 static void hc_stop_int (hci_t * hci);
191 
192 /* debug| print the main components of an URB
193  * small: 0) header + data packets 1) just header */
194 
urb_print(struct urb * urb,char * str,int small)195 static void urb_print (struct urb * urb, char *str, int small)
196 {
197 	unsigned int pipe = urb->pipe;
198 	int i, len;
199 
200 	if (!urb->dev || !urb->dev->bus) {
201 		dbg ("%s URB: no dev", str);
202 		return;
203 	}
204 
205 	printk ("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,flags:%4x,len:%d/%d,stat:%d(%x)\n",
206 		str, hci_get_current_frame_number (urb->dev),
207 		usb_pipedevice (pipe), usb_pipeendpoint (pipe),
208 		usb_pipeout (pipe) ? 'O' : 'I',
209 		usb_pipetype (pipe) < 2 ? (usb_pipeint (pipe) ? "INTR" : "ISOC")
210 		: (usb_pipecontrol (pipe) ? "CTRL" : "BULK"), urb->transfer_flags,
211 		urb->actual_length, urb->transfer_buffer_length, urb->status,
212 		urb->status);
213 	if (!small) {
214 		if (usb_pipecontrol (pipe)) {
215 			printk (__FILE__ ": cmd(8):");
216 			for (i = 0; i < 8; i++)
217 				printk (" %02x", ((__u8 *) urb->setup_packet)[i]);
218 			printk ("\n");
219 		}
220 		if (urb->transfer_buffer_length > 0 && urb->transfer_buffer) {
221 			printk (__FILE__ ": data(%d/%d):", urb->actual_length,
222 				urb->transfer_buffer_length);
223 			len = usb_pipeout (pipe) ? urb-> transfer_buffer_length : urb->actual_length;
224 			for (i = 0; i < 2096 && i < len; i++)
225 				printk (" %02x", ((__u8 *) urb->transfer_buffer)[i]);
226 			printk ("%s stat:%d\n", i < len ? "..." : "",
227 				urb->status);
228 		}
229 	}
230 }
231