tinyusb  0.4
Click here to lend your support to tinyusb donation and make a donation at pledgie.com
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ohci.c
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
39 #include "common/common.h"
40 
41 #if MODE_HOST_SUPPORTED && (TUSB_CFG_MCU == MCU_LPC175X_6X)
42 //--------------------------------------------------------------------+
43 // INCLUDE
44 //--------------------------------------------------------------------+
45 #include "hal/hal.h"
46 #include "osal/osal.h"
47 #include "common/timeout_timer.h"
48 
49 #include "../hcd.h"
50 #include "../usbh_hcd.h"
51 #include "ohci.h"
52 
53 //--------------------------------------------------------------------+
54 // MACRO CONSTANT TYPEDEF
55 //--------------------------------------------------------------------+
56 #define OHCI_REG ((ohci_registers_t *) LPC_USB_BASE)
57 
58 enum {
59  OHCI_CONTROL_FUNCSTATE_RESET = 0,
60  OHCI_CONTROL_FUNCSTATE_RESUME,
61  OHCI_CONTROL_FUNCSTATE_OPERATIONAL,
62  OHCI_CONTROL_FUNCSTATE_SUSPEND
63 };
64 
65 enum {
66  OHCI_CONTROL_CONTROL_BULK_RATIO = 3,
67  OHCI_CONTROL_LIST_PERIODIC_ENABLE_MASK = BIT_(2),
68  OHCI_CONTROL_LIST_ISOCHRONOUS_ENABLE_MASK = BIT_(3),
69  OHCI_CONTROL_LIST_CONTROL_ENABLE_MASK = BIT_(4),
70  OHCI_CONTROL_LIST_BULK_ENABLE_MASK = BIT_(5),
71 };
72 
73 enum {
74  OHCI_FMINTERVAL_FI = 0x2EDF, // 7.3.1 nominal (reset) value
75  OHCI_FMINTERVAL_FSMPS = (6*(OHCI_FMINTERVAL_FI-210)) / 7, // 5.4 calculated based on maximum overhead + bit stuffing
76 };
77 
78 enum {
79  OHCI_PERIODIC_START = 0x3E67
80 };
81 
82 #ifdef __CC_ARM
83 #pragma diag_suppress 66 // Suppress Keil warnings #66-D: enumeration value is out of "int" range
84 #endif
85 
86 enum {
87  OHCI_INT_SCHEDULING_OVERUN_MASK = BIT_(0),
88  OHCI_INT_WRITEBACK_DONEHEAD_MASK = BIT_(1),
89  OHCI_INT_SOF_MASK = BIT_(2),
90  OHCI_INT_RESUME_DETECTED_MASK = BIT_(3),
91  OHCI_INT_UNRECOVERABLE_ERROR_MASK = BIT_(4),
92  OHCI_INT_FRAME_OVERFLOW_MASK = BIT_(5),
93  OHCI_INT_RHPORT_STATUS_CHANGE_MASK = BIT_(6),
94 
95  OHCI_INT_OWNERSHIP_CHANGE_MASK = BIT_(30),
96  OHCI_INT_MASTER_ENABLE_MASK = BIT_(31),
97 };
98 
99 #ifdef __CC_ARM
100 #pragma diag_default 66 // return Keil 66 to normal severity
101 #endif
102 
103 enum {
104  OHCI_RHPORT_CURRENT_CONNECT_STATUS_MASK = BIT_(0),
105  OHCI_RHPORT_PORT_ENABLE_STATUS_MASK = BIT_(1),
106  OHCI_RHPORT_PORT_SUSPEND_STATUS_MASK = BIT_(2),
107  OHCI_RHPORT_PORT_OVER_CURRENT_INDICATOR_MASK = BIT_(3),
108  OHCI_RHPORT_PORT_RESET_STATUS_MASK = BIT_(4),
109 
110  OHCI_RHPORT_PORT_POWER_STATUS_MASK = BIT_(8),
111  OHCI_RHPORT_LOW_SPEED_DEVICE_ATTACHED_MASK = BIT_(9),
112 
113  OHCI_RHPORT_CONNECT_STATUS_CHANGE_MASK = BIT_(16),
114  OHCI_RHPORT_PORT_ENABLE_CHANGE_MASK = BIT_(17),
115  OHCI_RHPORT_PORT_SUSPEND_CHANGE_MASK = BIT_(18),
116  OHCI_RHPORT_OVER_CURRENT_CHANGE_MASK = BIT_(19),
117  OHCI_RHPORT_PORT_RESET_CHANGE_MASK = BIT_(20),
118 
119  OHCI_RHPORT_ALL_CHANGE_MASK = OHCI_RHPORT_CONNECT_STATUS_CHANGE_MASK | OHCI_RHPORT_PORT_ENABLE_CHANGE_MASK |
120  OHCI_RHPORT_PORT_SUSPEND_CHANGE_MASK | OHCI_RHPORT_OVER_CURRENT_CHANGE_MASK | OHCI_RHPORT_PORT_RESET_CHANGE_MASK
121 };
122 
123 enum {
124  OHCI_CCODE_NO_ERROR = 0,
125  OHCI_CCODE_CRC = 1,
126  OHCI_CCODE_BIT_STUFFING = 2,
127  OHCI_CCODE_DATA_TOGGLE_MISMATCH = 3,
128  OHCI_CCODE_STALL = 4,
129  OHCI_CCODE_DEVICE_NOT_RESPONDING = 5,
130  OHCI_CCODE_PID_CHECK_FAILURE = 6,
131  OHCI_CCODE_UNEXPECTED_PID = 7,
132  OHCI_CCODE_DATA_OVERRUN = 8,
133  OHCI_CCODE_DATA_UNDERRUN = 9,
134  OHCI_CCODE_BUFFER_OVERRUN = 12,
135  OHCI_CCODE_BUFFER_UNDERRUN = 13,
136  OHCI_CCODE_NOT_ACCESSED = 14,
137 };
138 
139 enum {
140  OHCI_INT_ON_COMPLETE_YES = 0,
141  OHCI_INT_ON_COMPLETE_NO = BIN8(111)
142 };
143 //--------------------------------------------------------------------+
144 // INTERNAL OBJECT & FUNCTION DECLARATION
145 //--------------------------------------------------------------------+
146 TUSB_CFG_ATTR_USBRAM ATTR_ALIGNED(256) STATIC_VAR ohci_data_t ohci_data;
147 
148 static ohci_ed_t * const p_ed_head[] =
149 {
150  [TUSB_XFER_CONTROL] = &ohci_data.control[0].ed,
151  [TUSB_XFER_BULK ] = &ohci_data.bulk_head_ed,
152  [TUSB_XFER_INTERRUPT] = &ohci_data.period_head_ed,
153  [TUSB_XFER_ISOCHRONOUS] = NULL // TODO Isochronous
154 };
155 
156 static void ed_list_insert(ohci_ed_t * p_pre, ohci_ed_t * p_ed);
157 static void ed_list_remove(ohci_ed_t * p_head, ohci_ed_t * p_ed);
158 
159 static ohci_ed_t * ed_list_find_previous(ohci_ed_t const * p_head, ohci_ed_t const * p_ed);
160 
161 //--------------------------------------------------------------------+
162 // USBH-HCD API
163 //--------------------------------------------------------------------+
164 // Initialization according to 5.1.1.4
165 tusb_error_t hcd_init(void)
166 {
167  //------------- Data Structure init -------------//
168  memclr_(&ohci_data, sizeof(ohci_data_t));
169  for(uint8_t i=0; i<32; i++)
170  { // assign all interrupt pointes to period head ed
171  ohci_data.hcca.interrupt_table[i] = (uint32_t) &ohci_data.period_head_ed;
172  }
173 
174  ohci_data.control[0].ed.skip = 1;
175  ohci_data.bulk_head_ed.skip = 1;
176  ohci_data.period_head_ed.skip = 1;
177 
178  // reset controller
179  OHCI_REG->command_status_bit.controller_reset = 1;
180  while( OHCI_REG->command_status_bit.controller_reset ) {} // should not take longer than 10 us
181 
182  //------------- init ohci registers -------------//
183  OHCI_REG->control_head_ed = (uint32_t) &ohci_data.control[0].ed;
184  OHCI_REG->bulk_head_ed = (uint32_t) &ohci_data.bulk_head_ed;
185  OHCI_REG->hcca = (uint32_t) &ohci_data.hcca;
186 
187  OHCI_REG->interrupt_disable = OHCI_REG->interrupt_enable; // disable all interrupts
188  OHCI_REG->interrupt_status = OHCI_REG->interrupt_status; // clear current set bits
189  OHCI_REG->interrupt_enable = OHCI_INT_WRITEBACK_DONEHEAD_MASK | OHCI_INT_RESUME_DETECTED_MASK |
190  OHCI_INT_UNRECOVERABLE_ERROR_MASK | /*OHCI_INT_FRAME_OVERFLOW_MASK |*/ OHCI_INT_RHPORT_STATUS_CHANGE_MASK |
191  OHCI_INT_MASTER_ENABLE_MASK;
192 
193  OHCI_REG->control |= OHCI_CONTROL_CONTROL_BULK_RATIO | OHCI_CONTROL_LIST_CONTROL_ENABLE_MASK |
194  OHCI_CONTROL_LIST_BULK_ENABLE_MASK | OHCI_CONTROL_LIST_PERIODIC_ENABLE_MASK; // TODO Isochronous
195 
196  OHCI_REG->frame_interval = (OHCI_FMINTERVAL_FSMPS << 16) | OHCI_FMINTERVAL_FI;
197  OHCI_REG->periodic_start = (OHCI_FMINTERVAL_FI * 9) / 10; // Periodic start is 90% of frame interval
198 
199  OHCI_REG->control_bit.hc_functional_state = OHCI_CONTROL_FUNCSTATE_OPERATIONAL; // make HC's state to operational state TODO use this to suspend (save power)
200  OHCI_REG->rh_status_bit.local_power_status_change = 1; // set global power for ports
201 
202  return TUSB_ERROR_NONE;
203 }
204 
205 //--------------------------------------------------------------------+
206 // PORT API
207 //--------------------------------------------------------------------+
208 void hcd_port_reset(uint8_t hostid)
209 {
210  OHCI_REG->rhport_status[0] = OHCI_RHPORT_PORT_RESET_STATUS_MASK;
211 }
212 
213 bool hcd_port_connect_status(uint8_t hostid)
214 {
215  return OHCI_REG->rhport_status_bit[0].current_connect_status;
216 }
217 
218 tusb_speed_t hcd_port_speed_get(uint8_t hostid)
219 {
220  return OHCI_REG->rhport_status_bit[0].low_speed_device_attached ? TUSB_SPEED_LOW : TUSB_SPEED_FULL;
221 }
222 
223 // TODO refractor abtract later
224 void hcd_port_unplug(uint8_t hostid)
225 {
226  // TODO OHCI
227 }
228 
229 //--------------------------------------------------------------------+
230 // Controller API
231 //--------------------------------------------------------------------+
232 
233 //--------------------------------------------------------------------+
234 // CONTROL PIPE API
235 //--------------------------------------------------------------------+
236 static inline tusb_xfer_type_t ed_get_xfer_type(ohci_ed_t const * const p_ed) ATTR_PURE ATTR_ALWAYS_INLINE;
237 static inline tusb_xfer_type_t ed_get_xfer_type(ohci_ed_t const * const p_ed)
238 {
239  return (p_ed->endpoint_number == 0 ) ? TUSB_XFER_CONTROL :
240  (p_ed->is_iso ) ? TUSB_XFER_ISOCHRONOUS :
241  (p_ed->is_interrupt_xfer ) ? TUSB_XFER_INTERRUPT : TUSB_XFER_BULK;
242 }
243 
244 static void ed_init(ohci_ed_t *p_ed, uint8_t dev_addr, uint16_t max_packet_size, uint8_t endpoint_addr, uint8_t xfer_type, uint8_t interval)
245 {
246  // address 0 is used as async head, which always on the list --> cannot be cleared
247  if (dev_addr != 0)
248  {
249  memclr_(p_ed, sizeof(ohci_ed_t));
250  }
251 
252  p_ed->device_address = dev_addr;
253  p_ed->endpoint_number = endpoint_addr & 0x0F;
254  p_ed->direction = (xfer_type == TUSB_XFER_CONTROL) ? OHCI_PID_SETUP : ( (endpoint_addr & TUSB_DIR_DEV_TO_HOST_MASK) ? OHCI_PID_IN : OHCI_PID_OUT );
255  p_ed->speed = usbh_devices[dev_addr].speed;
256  p_ed->is_iso = (xfer_type == TUSB_XFER_ISOCHRONOUS) ? 1 : 0;
257  p_ed->max_package_size = max_packet_size;
258 
259  p_ed->used = 1;
260  p_ed->is_interrupt_xfer = (xfer_type == TUSB_XFER_INTERRUPT ? 1 : 0);
261 }
262 
263 static void gtd_init(ohci_gtd_t* p_td, void* data_ptr, uint16_t total_bytes)
264 {
265  memclr_(p_td, sizeof(ohci_gtd_t));
266 
267  p_td->used = 1;
268  p_td->expected_bytes = total_bytes;
269 
270  p_td->buffer_rounding = 1; // less than queued length is not a error
271  p_td->delay_interrupt = OHCI_INT_ON_COMPLETE_NO;
272  p_td->condition_code = OHCI_CCODE_NOT_ACCESSED;
273 
274  p_td->current_buffer_pointer = data_ptr;
275  p_td->buffer_end = total_bytes ? (((uint8_t*) data_ptr) + total_bytes-1) : NULL;
276 }
277 
278 tusb_error_t hcd_pipe_control_open(uint8_t dev_addr, uint8_t max_packet_size)
279 {
280  ohci_ed_t* const p_ed = &ohci_data.control[dev_addr].ed;
281 
282  ed_init(p_ed, dev_addr, max_packet_size, 0, TUSB_XFER_CONTROL, 0); // TODO binterval of control is ignored
283 
284  if ( dev_addr != 0 )
285  { // insert to control head
286  ed_list_insert( p_ed_head[TUSB_XFER_CONTROL], p_ed);
287  }else
288  {
289  p_ed->skip = 0; // addr0 is used as static control head --> only need to clear skip bit
290  }
291 
292  return TUSB_ERROR_NONE;
293 }
294 
295 tusb_error_t hcd_pipe_control_xfer(uint8_t dev_addr, tusb_control_request_t const * p_request, uint8_t data[])
296 {
297  ohci_ed_t* const p_ed = &ohci_data.control[dev_addr].ed;
298 
299  ohci_gtd_t *p_setup = &ohci_data.control[dev_addr].gtd[0];
300  ohci_gtd_t *p_data = p_setup + 1;
301  ohci_gtd_t *p_status = p_setup + 2;
302 
303  //------------- SETUP Phase -------------//
304  gtd_init(p_setup, (void*) p_request, 8);
305  p_setup->index = dev_addr;
306  p_setup->pid = OHCI_PID_SETUP;
307  p_setup->data_toggle = BIN8(10); // DATA0
308  p_setup->next_td = (uint32_t) p_data;
309 
310  //------------- DATA Phase -------------//
311  if (p_request->wLength > 0)
312  {
313  gtd_init(p_data, data, p_request->wLength);
314  p_data->index = dev_addr;
315  p_data->pid = p_request->bmRequestType_bit.direction ? OHCI_PID_IN : OHCI_PID_OUT;
316  p_data->data_toggle = BIN8(11); // DATA1
317  }else
318  {
319  p_data = p_setup;
320  }
321  p_data->next_td = (uint32_t) p_status;
322 
323  //------------- STATUS Phase -------------//
324  gtd_init(p_status, NULL, 0); // zero-length data
325  p_status->index = dev_addr;
326  p_status->pid = p_request->bmRequestType_bit.direction ? OHCI_PID_OUT : OHCI_PID_IN; // reverse direction of data phase
327  p_status->data_toggle = BIN8(11); // DATA1
328  p_status->delay_interrupt = OHCI_INT_ON_COMPLETE_YES;
329 
330  //------------- Attach TDs list to Control Endpoint -------------//
331  p_ed->td_head.address = (uint32_t) p_setup;
332 
333  OHCI_REG->command_status_bit.control_list_filled = 1;
334 
335  return TUSB_ERROR_NONE;
336 }
337 
338 tusb_error_t hcd_pipe_control_close(uint8_t dev_addr)
339 {
340  ohci_ed_t* const p_ed = &ohci_data.control[dev_addr].ed;
341 
342  if ( dev_addr == 0 )
343  { // addr0 serves as static head --> only set skip bitx
344  p_ed->skip = 1;
345  }else
346  {
347  ed_list_remove( p_ed_head[ ed_get_xfer_type(p_ed)], p_ed );
348 
349  // TODO refractor to be USBH
350  usbh_devices[dev_addr].state = TUSB_DEVICE_STATE_UNPLUG;
351  }
352 
353  return TUSB_ERROR_NONE;
354 }
355 
356 //--------------------------------------------------------------------+
357 // BULK/INT/ISO PIPE API
358 //--------------------------------------------------------------------+
359 static inline uint8_t ed_get_index(ohci_ed_t const * const p_ed) ATTR_PURE ATTR_ALWAYS_INLINE;
360 static inline uint8_t ed_get_index(ohci_ed_t const * const p_ed)
361 {
362  return p_ed - ohci_data.device[p_ed->device_address-1].ed;
363 }
364 
365 static inline ohci_ed_t * ed_from_pipe_handle(pipe_handle_t pipe_hdl) ATTR_PURE ATTR_ALWAYS_INLINE;
366 static inline ohci_ed_t * ed_from_pipe_handle(pipe_handle_t pipe_hdl)
367 {
368  return &ohci_data.device[pipe_hdl.dev_addr-1].ed[pipe_hdl.index];
369 }
370 
371 static inline ohci_ed_t * ed_find_free(uint8_t dev_addr) ATTR_PURE ATTR_ALWAYS_INLINE;
372 static inline ohci_ed_t * ed_find_free(uint8_t dev_addr)
373 {
374  for(uint8_t i = 0; i < HCD_MAX_ENDPOINT; i++)
375  {
376  if ( !ohci_data.device[dev_addr-1].ed[i].used )
377  {
378  return &ohci_data.device[dev_addr-1].ed[i];
379  }
380  }
381 
382  return NULL;
383 }
384 
385 static ohci_ed_t * ed_list_find_previous(ohci_ed_t const * p_head, ohci_ed_t const * p_ed)
386 {
387  uint32_t max_loop = HCD_MAX_ENDPOINT;
388 
389  ohci_ed_t const * p_prev = p_head;
390 
391  ASSERT_PTR(p_prev, NULL);
392 
393  while ( align16(p_prev->next_ed) != 0 && /* not reach null */
394  align16(p_prev->next_ed) != (uint32_t) p_ed && /* not found yet */
395  max_loop > 0)
396  {
397  p_prev = (ohci_ed_t const *) align16(p_prev->next_ed);
398  max_loop--;
399  }
400 
401  return ( align16(p_prev->next_ed) == (uint32_t) p_ed ) ? (ohci_ed_t*) p_prev : NULL;
402 }
403 
404 static void ed_list_insert(ohci_ed_t * p_pre, ohci_ed_t * p_ed)
405 {
406  p_ed->next_ed |= p_pre->next_ed; // to reserve 4 lsb bits
407  p_pre->next_ed = (p_pre->next_ed & 0x0FUL) | ((uint32_t) p_ed);
408 }
409 
410 static void ed_list_remove(ohci_ed_t * p_head, ohci_ed_t * p_ed)
411 {
412  ohci_ed_t * const p_prev = ed_list_find_previous(p_head, p_ed);
413 
414  p_prev->next_ed = (p_prev->next_ed & 0x0fUL) | align16(p_ed->next_ed);
415  // point the removed ED's next pointer to list head to make sure HC can always safely move away from this ED
416  p_ed->next_ed = (uint32_t) p_head;
417  p_ed->used = 0; // free ED
418 }
419 
420 pipe_handle_t hcd_pipe_open(uint8_t dev_addr, tusb_descriptor_endpoint_t const * p_endpoint_desc, uint8_t class_code)
421 {
422  pipe_handle_t const null_handle = { .dev_addr = 0, .xfer_type = 0, .index = 0 };
423 
424  // TODO iso support
425  ASSERT(p_endpoint_desc->bmAttributes.xfer != TUSB_XFER_ISOCHRONOUS, null_handle );
426 
427  //------------- Prepare Queue Head -------------//
428  ohci_ed_t * const p_ed = ed_find_free(dev_addr);
429  ASSERT_PTR(p_ed, null_handle);
430 
431  ed_init( p_ed, dev_addr, p_endpoint_desc->wMaxPacketSize.size, p_endpoint_desc->bEndpointAddress,
432  p_endpoint_desc->bmAttributes.xfer, p_endpoint_desc->bInterval );
433  p_ed->td_tail.class_code = class_code;
434 
435  ed_list_insert( p_ed_head[p_endpoint_desc->bmAttributes.xfer], p_ed );
436 
437  return (pipe_handle_t)
438  {
439  .dev_addr = dev_addr,
440  .xfer_type = p_endpoint_desc->bmAttributes.xfer,
441  .index = ed_get_index(p_ed)
442  };
443 }
444 
445 static ohci_gtd_t * gtd_find_free(uint8_t dev_addr)
446 {
447  for(uint8_t i=0; i < HCD_MAX_XFER; i++)
448  {
449  if (!ohci_data.device[dev_addr-1].gtd[i].used)
450  {
451  return &ohci_data.device[dev_addr-1].gtd[i];
452  }
453  }
454 
455  return NULL;
456 }
457 
458 static void td_insert_to_ed(ohci_ed_t* p_ed, ohci_gtd_t * p_gtd)
459 {
460  // tail is always NULL
461  if ( align16(p_ed->td_head.address) == 0 )
462  { // TD queue is empty --> head = TD
463  p_ed->td_head.address |= (uint32_t) p_gtd;
464  }
465  else
466  { // TODO currently only support queue up to 2 TD each endpoint at a time
467  ((ohci_gtd_t*) align16(p_ed->td_head.address))->next_td = (uint32_t) p_gtd;
468  }
469 }
470 
471 static tusb_error_t pipe_queue_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete)
472 {
473  ohci_ed_t* const p_ed = ed_from_pipe_handle(pipe_hdl);
474 
475  if ( !p_ed->is_iso )
476  {
477  ohci_gtd_t * const p_gtd = gtd_find_free(pipe_hdl.dev_addr);
478  ASSERT_PTR(p_gtd, TUSB_ERROR_EHCI_NOT_ENOUGH_QTD); // TODO refractor error code
479 
480  gtd_init(p_gtd, buffer, total_bytes);
481  p_gtd->index = pipe_hdl.index;
482  if ( int_on_complete ) p_gtd->delay_interrupt = OHCI_INT_ON_COMPLETE_YES;
483 
484  td_insert_to_ed(p_ed, p_gtd);
485  }else
486  {
487  ASSERT_STATUS(TUSB_ERROR_NOT_SUPPORTED_YET);
488  }
489 
490  return TUSB_ERROR_NONE;
491 }
492 
493 tusb_error_t hcd_pipe_queue_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes)
494 {
495  return pipe_queue_xfer(pipe_hdl, buffer, total_bytes, false);
496 }
497 
498 tusb_error_t hcd_pipe_xfer(pipe_handle_t pipe_hdl, uint8_t buffer[], uint16_t total_bytes, bool int_on_complete)
499 {
500  ASSERT_STATUS( pipe_queue_xfer(pipe_hdl, buffer, total_bytes, true) );
501 
502  tusb_xfer_type_t xfer_type = ed_get_xfer_type( ed_from_pipe_handle(pipe_hdl) );
503 
504  if (TUSB_XFER_BULK == xfer_type) OHCI_REG->command_status_bit.bulk_list_filled = 1;
505 
506  return TUSB_ERROR_NONE;
507 }
508 
510 // endpoints are tied to an address, which only reclaim after a long delay when enumerating
511 // thus there is no need to make sure ED is not in HC's cahed as it will not for sure
512 tusb_error_t hcd_pipe_close(pipe_handle_t pipe_hdl)
513 {
514  ohci_ed_t * const p_ed = ed_from_pipe_handle(pipe_hdl);
515 
516  ed_list_remove( p_ed_head[ ed_get_xfer_type(p_ed)], p_ed );
517 
518  return TUSB_ERROR_FAILED;
519 }
520 
521 bool hcd_pipe_is_busy(pipe_handle_t pipe_hdl)
522 {
523  ohci_ed_t const * const p_ed = ed_from_pipe_handle(pipe_hdl);
524  return align16(p_ed->td_head.address) != align16(p_ed->td_tail.address);
525 }
526 
527 bool hcd_pipe_is_error(pipe_handle_t pipe_hdl)
528 {
529  ohci_ed_t const * const p_ed = ed_from_pipe_handle(pipe_hdl);
530  return p_ed->td_head.halted;
531 }
532 
533 bool hcd_pipe_is_stalled(pipe_handle_t pipe_hdl)
534 {
535  ohci_ed_t const * const p_ed = ed_from_pipe_handle(pipe_hdl);
536  return p_ed->td_head.halted && p_ed->is_stalled;
537 }
538 
539 uint8_t hcd_pipe_get_endpoint_addr(pipe_handle_t pipe_hdl)
540 {
541  ohci_ed_t const * const p_ed = ed_from_pipe_handle(pipe_hdl);
542  return p_ed->endpoint_number | (p_ed->direction == OHCI_PID_IN ? TUSB_DIR_DEV_TO_HOST_MASK : 0 );
543 }
544 
545 tusb_error_t hcd_pipe_clear_stall(pipe_handle_t pipe_hdl)
546 {
547  ohci_ed_t * const p_ed = ed_from_pipe_handle(pipe_hdl);
548 
549  p_ed->is_stalled = 0;
550  p_ed->td_tail.address &= 0x0Ful; // set tail pointer back to NULL
551 
552  p_ed->td_head.toggle = 0; // reset data toggle
553  p_ed->td_head.halted = 0;
554 
555  if ( TUSB_XFER_BULK == ed_get_xfer_type(p_ed) ) OHCI_REG->command_status_bit.bulk_list_filled = 1;
556 
557  return TUSB_ERROR_NONE;
558 }
559 
560 
561 //--------------------------------------------------------------------+
562 // OHCI Interrupt Handler
563 //--------------------------------------------------------------------+
564 static ohci_td_item_t* list_reverse(ohci_td_item_t* td_head)
565 {
566  ohci_td_item_t* td_reverse_head = NULL;
567 
568  while(td_head != NULL)
569  {
570  uint32_t next = td_head->next_td;
571 
572  // make current's item become reverse's first item
573  td_head->next_td = (uint32_t) td_reverse_head;
574  td_reverse_head = td_head;
575 
576  td_head = (ohci_td_item_t*) next; // advance to next item
577  }
578 
579  return td_reverse_head;
580 }
581 
582 static inline bool gtd_is_control(ohci_gtd_t const * const p_qtd) ATTR_CONST ATTR_ALWAYS_INLINE;
583 static inline bool gtd_is_control(ohci_gtd_t const * const p_qtd)
584 {
585  return ((uint32_t) p_qtd) < ((uint32_t) ohci_data.device); // check ohci_data_t for memory layout
586 }
587 
588 static inline ohci_ed_t* gtd_get_ed(ohci_gtd_t const * const p_qtd) ATTR_PURE ATTR_ALWAYS_INLINE;
589 static inline ohci_ed_t* gtd_get_ed(ohci_gtd_t const * const p_qtd)
590 {
591  if ( gtd_is_control(p_qtd) )
592  {
593  return &ohci_data.control[p_qtd->index].ed;
594  }else
595  {
596  uint8_t dev_addr_idx = (((uint32_t)p_qtd) - ((uint32_t)ohci_data.device)) / sizeof(ohci_data.device[0]);
597 
598  return &ohci_data.device[dev_addr_idx].ed[p_qtd->index];
599  }
600 }
601 
602 static inline uint32_t gtd_xfer_byte_left(uint32_t buffer_end, uint32_t current_buffer) ATTR_CONST ATTR_ALWAYS_INLINE;
603 static inline uint32_t gtd_xfer_byte_left(uint32_t buffer_end, uint32_t current_buffer)
604 { // 5.2.9 OHCI sample code
605  return (align4k(buffer_end ^ current_buffer) ? 0x1000 : 0) +
606  offset4k(buffer_end) - offset4k(current_buffer) + 1;
607 }
608 
609 static void done_queue_isr(uint8_t hostid)
610 {
611  uint8_t max_loop = (TUSB_CFG_HOST_DEVICE_MAX+1)*(HCD_MAX_XFER+OHCI_MAX_ITD);
612 
613  // done head is written in reversed order of completion --> need to reverse the done queue first
614  ohci_td_item_t* td_head = list_reverse ( (ohci_td_item_t*) align16(ohci_data.hcca.done_head) );
615 
616  while( td_head != NULL && max_loop > 0)
617  {
618  // TODO check if td_head is iso td
619  //------------- Non ISO transfer -------------//
620  ohci_gtd_t * const p_qtd = (ohci_gtd_t *) td_head;
621  tusb_event_t const event = (p_qtd->condition_code == OHCI_CCODE_NO_ERROR) ? TUSB_EVENT_XFER_COMPLETE :
622  (p_qtd->condition_code == OHCI_CCODE_STALL) ? TUSB_EVENT_XFER_STALLED : TUSB_EVENT_XFER_ERROR;
623 
624  p_qtd->used = 0; // free TD
625  if ( (p_qtd->delay_interrupt == OHCI_INT_ON_COMPLETE_YES) || (event != TUSB_EVENT_XFER_COMPLETE) )
626  {
627  ohci_ed_t * const p_ed = gtd_get_ed(p_qtd);
628 
629  uint32_t const xferred_bytes = p_qtd->expected_bytes - gtd_xfer_byte_left((uint32_t) p_qtd->buffer_end, (uint32_t) p_qtd->current_buffer_pointer);
630 
631  // NOTE Assuming the current list is BULK and there is no other EDs in the list has queued TDs.
632  // When there is a error resulting this ED is halted, and this EP still has other queued TD
633  // --> the Bulk list only has this halted EP queueing TDs (remaining)
634  // --> Bulk list will be considered as not empty by HC !!! while there is no attempt transaction on this list
635  // --> HC will not process Control list (due to service ratio when Bulk list not empty)
636  // To walk-around this, the halted ED will have TailP = HeadP (empty list condition), when clearing halt
637  // the TailP must be set back to NULL for processing remaining TDs
638  if ((event != TUSB_EVENT_XFER_COMPLETE))
639  {
640  p_ed->td_tail.address &= 0x0Ful;
641  p_ed->td_tail.address |= align16(p_ed->td_head.address); // mark halted EP as empty queue
642  if ( event == TUSB_EVENT_XFER_STALLED ) p_ed->is_stalled = 1;
643  }
644 
645  pipe_handle_t pipe_hdl =
646  {
647  .dev_addr = p_ed->device_address,
648  .xfer_type = ed_get_xfer_type(p_ed),
649  };
650 
651  if ( pipe_hdl.xfer_type != TUSB_XFER_CONTROL) pipe_hdl.index = ed_get_index(p_ed);
652 
653  usbh_xfer_isr(pipe_hdl, p_ed->td_tail.class_code, event, xferred_bytes);
654  }
655 
656  td_head = (ohci_td_item_t*) td_head->next_td;
657  max_loop--;
658  }
659 }
660 
661 void hcd_isr(uint8_t hostid)
662 {
663  uint32_t const int_en = OHCI_REG->interrupt_enable;
664  uint32_t const int_status = OHCI_REG->interrupt_status & int_en;
665 
666  if (int_status == 0) return;
667 
668  //------------- RootHub status -------------//
669  if ( int_status & OHCI_INT_RHPORT_STATUS_CHANGE_MASK )
670  {
671  uint32_t const rhport_status = OHCI_REG->rhport_status[0] & OHCI_RHPORT_ALL_CHANGE_MASK;
672 
673  // TODO dual port is not yet supported
674  if ( rhport_status & OHCI_RHPORT_CONNECT_STATUS_CHANGE_MASK )
675  {
676  // TODO check if remote wake-up
677  if ( OHCI_REG->rhport_status_bit[0].current_connect_status )
678  {
679  // TODO reset port immediately, without this controller will got 2-3 (debouncing connection status change)
680  OHCI_REG->rhport_status[0] = OHCI_RHPORT_PORT_RESET_STATUS_MASK;
681  usbh_hcd_rhport_plugged_isr(0);
682  }else
683  {
684  usbh_hcd_rhport_unplugged_isr(0);
685  }
686  }
687 
688  if ( rhport_status & OHCI_RHPORT_PORT_SUSPEND_CHANGE_MASK)
689  {
690 
691  }
692 
693  OHCI_REG->rhport_status[0] = rhport_status; // acknowledge all interrupt
694  }
695 
696  //------------- Transfer Complete -------------//
697  if ( int_status & OHCI_INT_WRITEBACK_DONEHEAD_MASK)
698  {
699  done_queue_isr(hostid);
700  }
701 
702  OHCI_REG->interrupt_status = int_status; // Acknowledge handled interrupt
703 }
704 //--------------------------------------------------------------------+
705 // HELPER
706 //--------------------------------------------------------------------+
707 
708 
709 #endif
710 
#define BIT_(n)
n-th Bit
Definition: binary.h:54
tusb_speed_t
defined base on EHCI specs value for Endpoint Speed
Definition: tusb_types.h:51
bool hcd_port_connect_status(uint8_t hostid) ATTR_PURE ATTR_WARN_UNUSED_RESULT
return the current connect status of roothub port
#define TUSB_CFG_HOST_DEVICE_MAX
Maximum number of device host stack can manage If hub class is not enabled, set this equal to numbe...
#define ATTR_PURE
Many functions have no effects except the return value and their return value depends only on the par...
Definition: compiler_gcc.h:96
struct tusb_descriptor_endpoint_t::@8 bmAttributes
This field describes the endpoint's attributes when it is configured using the bConfigurationValue. Bits 1..0: Transfer Type - 00 = Control - 01 = Isochronous - 10 = Bulk - 11 = Interrupt If not an isochronous endpoint, bits 5..2 are reserved and must be set to zero. If isochronous, they are defined as follows: Bits 3..2: Synchronization Type - 00 = No Synchronization - 01 = Asynchronous - 10 = Adaptive - 11 = Synchronous Bits 5..4: Usage Type - 00 = Data endpoint - 01 = Feedback endpoint - 10 = Implicit feedback Data endpoint - 11 = Reserved Refer to Chapter 5 of USB 2.0 specification for more information. All other bits are reserved and must be reset to zero. Reserved bits must be ignored by the host.
#define TUSB_CFG_ATTR_USBRAM
USB Standard Endpoint Descriptor (section 9.6.1 table 9-13)
uint8_t bInterval
Interval for polling endpoint for data transfers. Expressed in frames or microframes depending on the...
tusb_error_t
Error Code returned.
Definition: tusb_errors.h:100
#define ATTR_ALIGNED(Bytes)
This attribute specifies a minimum alignment for the variable or structure field, measured in bytes...
Definition: compiler_gcc.h:72
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
Definition: compiler_gcc.h:89
#define ATTR_CONST
Many functions do not examine any values except their arguments, and have no effects except the retur...
Definition: compiler_gcc.h:100
uint16_t size
Maximum packet size this endpoint is capable of sending or receiving when this configuration is selec...
tusb_xfer_type_t
defined base on USB Specs Endpoint's bmAttributes
Definition: tusb_types.h:58
uint8_t bEndpointAddress
The address of the endpoint on the USB device described by this descriptor. The address is encoded as...