PageRenderTime 128ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/cpu/arm/at91sam7s/usb-arch.c

https://gitlab.com/rdeterre/contiki
C | 878 lines | 699 code | 108 blank | 71 comment | 114 complexity | 73bf9da120581f166268a305bb3dea6e MD5 | raw file
  1. #include <usb-arch.h>
  2. #include <usb-interrupt.h>
  3. #include <AT91SAM7S64.h>
  4. #include <stdio.h>
  5. #include <debug-uart.h>
  6. /* #define DEBUG */
  7. #ifdef DEBUG
  8. #define PRINTF(...) printf(__VA_ARGS__)
  9. #else
  10. #define PRINTF(...)
  11. #endif
  12. #define USB_PULLUP_PIN AT91C_PIO_PA16
  13. #ifndef AT91C_UDP_STALLSENT
  14. #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
  15. #endif
  16. /* Bits that won't effect the state if they're written at a specific level.
  17. */
  18. /* Bits that should be written as 1 */
  19. #define NO_EFFECT_BITS (AT91C_UDP_TXCOMP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RXSETUP \
  20. | AT91C_UDP_ISOERROR | AT91C_UDP_RX_DATA_BK1)
  21. /* Also includes bits that should be written as 0 */
  22. #define NO_EFFECT_MASK (NO_EFFECT_BITS | AT91C_UDP_TXPKTRDY)
  23. #define RXBYTECNT(s) (((s)>>16)&0x7ff)
  24. static inline void
  25. udp_set_ep_ctrl_flags(AT91_REG *reg, unsigned int flags,
  26. unsigned int write_mask, unsigned int check_mask)
  27. {
  28. while ( (*reg & check_mask) != (flags & check_mask)) {
  29. *reg = (*reg & ~write_mask) | flags;
  30. }
  31. }
  32. #define UDP_SET_EP_CTRL_FLAGS(reg, flags, mask) \
  33. udp_set_ep_ctrl_flags((reg), \
  34. (NO_EFFECT_BITS & ~(mask)) | ((flags) & (mask)), (mask) | NO_EFFECT_MASK,\
  35. (mask))
  36. #define USB_DISABLE_INT *AT91C_AIC_IDCR = (1 << AT91C_ID_UDP)
  37. #define USB_ENABLE_INT *AT91C_AIC_IECR = (1 << AT91C_ID_UDP)
  38. #define USB_DISABLE_EP_INT(hw_ep) *AT91C_UDP_IDR = (1 << (hw_ep))
  39. #define USB_ENABLE_EP_INT(hw_ep) *AT91C_UDP_IER = (1 << (hw_ep))
  40. #if CTRL_EP_SIZE > 8
  41. #error Control endpoint size too big
  42. #endif
  43. #if USB_EP1_SIZE > 64
  44. #error Endpoint 1 size too big
  45. #endif
  46. #if USB_EP2_SIZE > 64
  47. #error Endpoint 2 size too big
  48. #endif
  49. #if USB_EP3_SIZE > 64
  50. #error Endpoint 3 size too big
  51. #endif
  52. static const uint16_t ep_xfer_size[8] =
  53. {
  54. CTRL_EP_SIZE,
  55. USB_EP1_SIZE,
  56. USB_EP2_SIZE,
  57. USB_EP3_SIZE
  58. };
  59. #define USB_EP_XFER_SIZE(ep) ep_xfer_size[ep]
  60. typedef struct _USBEndpoint USBEndpoint;
  61. struct _USBEndpoint
  62. {
  63. uint16_t status;
  64. uint8_t addr;
  65. uint8_t flags;
  66. USBBuffer *buffer; /* NULL if no current buffer */
  67. struct process *event_process;
  68. unsigned int events;
  69. uint16_t xfer_size;
  70. };
  71. #define USB_EP_FLAGS_TYPE_MASK 0x03
  72. #define USB_EP_FLAGS_TYPE_BULK 0x00
  73. #define USB_EP_FLAGS_TYPE_CONTROL 0x01
  74. #define USB_EP_FLAGS_TYPE_ISO 0x02
  75. #define USB_EP_FLAGS_TYPE_INTERRUPT 0x03
  76. #define EP_TYPE(ep) ((ep)->flags & USB_EP_FLAGS_TYPE_MASK)
  77. #define IS_EP_TYPE(ep, type) (EP_TYPE(ep) == (type))
  78. #define IS_CONTROL_EP(ep) IS_EP_TYPE(ep, USB_EP_FLAGS_TYPE_CONTROL)
  79. #define IS_BULK_EP(ep) IS_EP_TYPE(ep, USB_EP_FLAGS_TYPE_BULK)
  80. #define USB_EP_FLAGS_ENABLED 0x04
  81. /* A packet has been received but the data is still in hardware buffer */
  82. #define USB_EP_FLAGS_RECV_PENDING 0x08
  83. /* The pending packet is a SETUP packet */
  84. #define USB_EP_FLAGS_SETUP_PENDING 0x10
  85. /* The data in the hardware buffer is being transmitted */
  86. #define USB_EP_FLAGS_TRANSMITTING 0x20
  87. /* The receiver is waiting for a packet */
  88. #define USB_EP_FLAGS_RECEIVING 0x40
  89. /* For bulk endpoints. Both buffers are busy are in use, either by
  90. hardware or software. */
  91. #define USB_EP_FLAGS_DOUBLE 0x80
  92. /* The next packet received should be read from bank 1 if possible */
  93. #define USB_EP_FLAGS_BANK_1_RECV_NEXT 0x10
  94. /* States for double buffered reception:
  95. Packets being received 0 1 2 1 0 0
  96. Packets pending 0 0 0 1 2 1
  97. RECVING 0 1 1 1 0 0
  98. RECV_PENDING 0 0 0 1 1 1
  99. DOUBLE 0 0 1 0 1 0
  100. */
  101. /* States for double buffered transmission:
  102. Packets being transmitted 0 1 2
  103. TRANSMITTING 0 1 1
  104. DOUBLE 0 0 1
  105. */
  106. /* Index in endpoint array */
  107. #define EP_INDEX(addr) ((addr) & 0x7f)
  108. /* Get address of endpoint struct */
  109. #define EP_STRUCT(addr) &usb_endpoints[EP_INDEX(addr)];
  110. /* Number of hardware endpoint */
  111. #define EP_HW_NUM(addr) ((addr) & 0x7f)
  112. static USBEndpoint usb_endpoints[USB_MAX_ENDPOINTS];
  113. struct process *event_process = 0;
  114. volatile unsigned int events = 0;
  115. static void
  116. notify_process(unsigned int e)
  117. {
  118. events |= e;
  119. if (event_process) {
  120. process_poll(event_process);
  121. }
  122. }
  123. static void
  124. notify_ep_process(USBEndpoint *ep, unsigned int e)
  125. {
  126. ep->events |= e;
  127. if (ep->event_process) {
  128. process_poll(ep->event_process);
  129. }
  130. }
  131. static void
  132. usb_arch_reset(void)
  133. {
  134. unsigned int e;
  135. for (e = 0; e < USB_MAX_ENDPOINTS; e++) {
  136. if (usb_endpoints[e].flags &USB_EP_FLAGS_ENABLED) {
  137. USBBuffer *buffer = usb_endpoints[e].buffer;
  138. usb_endpoints[e].flags = 0;
  139. usb_disable_endpoint(e);
  140. while(buffer) {
  141. buffer->flags &= ~USB_BUFFER_SUBMITTED;
  142. buffer = buffer->next;
  143. }
  144. }
  145. }
  146. usb_arch_setup_control_endpoint(0);
  147. }
  148. void
  149. usb_arch_setup(void)
  150. {
  151. unsigned int i;
  152. /* Assume 96MHz PLL frequency */
  153. *AT91C_CKGR_PLLR = ((*AT91C_CKGR_PLLR & ~AT91C_CKGR_USBDIV)
  154. | AT91C_CKGR_USBDIV_1);
  155. /* Enable 48MHz USB clock */
  156. *AT91C_PMC_SCER = AT91C_PMC_UDP;
  157. /* Enable USB main clock */
  158. *AT91C_PMC_PCER = (1 << AT91C_ID_UDP);
  159. /* Enable pullup */
  160. *AT91C_PIOA_PER = USB_PULLUP_PIN;
  161. *AT91C_PIOA_OER = USB_PULLUP_PIN;
  162. *AT91C_PIOA_CODR = USB_PULLUP_PIN;
  163. for(i = 0; i < USB_MAX_ENDPOINTS; i++) {
  164. usb_endpoints[i].flags = 0;
  165. usb_endpoints[i].event_process = 0;
  166. }
  167. usb_arch_reset();
  168. /* Enable usb_interrupt */
  169. AT91C_AIC_SMR[AT91C_ID_UDP] = AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | 4;
  170. AT91C_AIC_SVR[AT91C_ID_UDP] = (unsigned long) usb_int;
  171. *AT91C_AIC_IECR = (1 << AT91C_ID_UDP);
  172. }
  173. static void
  174. usb_arch_setup_endpoint(unsigned char addr, unsigned int hw_type)
  175. {
  176. unsigned int ei = EP_HW_NUM(addr);
  177. USBEndpoint *ep = EP_STRUCT(addr);
  178. ep->status = 0;
  179. ep->flags = USB_EP_FLAGS_ENABLED;
  180. ep->buffer = 0;
  181. ep->addr = addr;
  182. ep->events = 0;
  183. ep->xfer_size = 0;
  184. *AT91C_UDP_IDR = 1<<ei;
  185. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[ei], hw_type | AT91C_UDP_EPEDS,
  186. AT91C_UDP_EPTYPE | AT91C_UDP_EPEDS);
  187. *AT91C_UDP_IER = 1<<ei;
  188. };
  189. void
  190. usb_arch_setup_control_endpoint(unsigned char addr)
  191. {
  192. unsigned int ei = EP_HW_NUM(addr);
  193. USBEndpoint *ep = EP_STRUCT(addr);
  194. usb_arch_setup_endpoint(addr, AT91C_UDP_EPTYPE_CTRL);
  195. ep->flags |= USB_EP_FLAGS_TYPE_CONTROL;
  196. ep->xfer_size = ep_xfer_size[ei];
  197. }
  198. void
  199. usb_arch_setup_bulk_endpoint(unsigned char addr)
  200. {
  201. unsigned int ei = EP_HW_NUM(addr);
  202. USBEndpoint *ep = EP_STRUCT(addr);
  203. usb_arch_setup_endpoint(addr, ((addr & 0x80)
  204. ? AT91C_UDP_EPTYPE_BULK_IN
  205. : AT91C_UDP_EPTYPE_BULK_OUT));
  206. ep->flags |= USB_EP_FLAGS_TYPE_BULK;
  207. ep->xfer_size = ep_xfer_size[ei];
  208. }
  209. void
  210. usb_arch_setup_interrupt_endpoint(unsigned char addr)
  211. {
  212. unsigned int ei = EP_HW_NUM(addr);
  213. USBEndpoint *ep = EP_STRUCT(addr);
  214. usb_arch_setup_endpoint(addr, ((addr & 0x80)
  215. ? AT91C_UDP_EPTYPE_INT_IN
  216. : AT91C_UDP_EPTYPE_INT_OUT));
  217. ep->flags |= USB_EP_FLAGS_TYPE_BULK;
  218. ep->xfer_size = ep_xfer_size[ei];
  219. }
  220. void
  221. usb_arch_disable_endpoint(uint8_t addr)
  222. {
  223. USBEndpoint *ep = EP_STRUCT(addr);
  224. ep->flags &= ~USB_EP_FLAGS_ENABLED;
  225. *AT91C_UDP_IDR = 1<<EP_HW_NUM(addr);
  226. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[EP_HW_NUM(addr)], 0, AT91C_UDP_EPEDS);
  227. }
  228. #define USB_READ_BLOCK 0x01 /* The currently submitted buffers
  229. can't hold the received data, wait
  230. for more buffers. No data was read
  231. from the hardware buffer */
  232. #define USB_READ_NOTIFY 0x02 /* Some buffers that had the
  233. USB_BUFFER_NOTIFY flags set were
  234. released */
  235. #define USB_READ_FAIL 0x04 /* The received data doesn't match the
  236. submitted buffers. The hardware
  237. buffer is discarded. */
  238. /* Skip buffers until mask and flags matches*/
  239. static USBBuffer *
  240. skip_buffers_until(USBBuffer *buffer, unsigned int mask, unsigned int flags,
  241. unsigned int *resp)
  242. {
  243. while(buffer && !((buffer->flags & mask) == flags)) {
  244. USBBuffer *next = buffer->next;
  245. buffer->flags &= ~USB_BUFFER_SUBMITTED ;
  246. buffer->flags |= USB_BUFFER_FAILED;
  247. if (buffer->flags & USB_BUFFER_NOTIFY) *resp |= USB_READ_NOTIFY;
  248. buffer = next;
  249. }
  250. return buffer;
  251. }
  252. static void
  253. read_hw_buffer(uint8_t *data, unsigned int hw_ep, unsigned int len)
  254. {
  255. AT91_REG *fdr;
  256. fdr = &AT91C_UDP_FDR[hw_ep];
  257. while(len-- > 0) {
  258. *data++ = *fdr;
  259. }
  260. }
  261. #define USB_WRITE_BLOCK 0x01
  262. #define USB_WRITE_NOTIFY 0x02
  263. void
  264. write_hw_buffer(const uint8_t *data, unsigned int hw_ep, unsigned int len)
  265. {
  266. AT91_REG *fdr;
  267. fdr = &AT91C_UDP_FDR[hw_ep];
  268. /* PRINTF("Write %d\n", len); */
  269. while(len-- > 0) {
  270. *fdr = *data++;
  271. }
  272. }
  273. static unsigned int
  274. get_receive_capacity(USBBuffer *buffer)
  275. {
  276. unsigned int capacity = 0;
  277. while(buffer && !(buffer->flags & (USB_BUFFER_IN| USB_BUFFER_SETUP|USB_BUFFER_HALT))) {
  278. capacity += buffer->left;
  279. buffer = buffer->next;
  280. }
  281. return capacity;
  282. }
  283. static int
  284. handle_pending_receive(USBEndpoint *ep)
  285. {
  286. int short_packet;
  287. unsigned int len;
  288. unsigned int copy;
  289. unsigned int res = 0;
  290. unsigned int hw_ep = EP_HW_NUM(ep->addr);
  291. USBBuffer *buffer = ep->buffer;
  292. uint8_t *setup_data = NULL;
  293. unsigned int flags = ep->flags;
  294. if (!(flags & USB_EP_FLAGS_ENABLED) || !buffer) return USB_READ_BLOCK;
  295. len = RXBYTECNT(AT91C_UDP_CSR[hw_ep]);
  296. PRINTF("handle_pending_receive: %d\n", len);
  297. switch(flags & USB_EP_FLAGS_TYPE_MASK) {
  298. case USB_EP_FLAGS_TYPE_CONTROL:
  299. if (flags & USB_EP_FLAGS_SETUP_PENDING) {
  300. /* Discard buffers until we find a SETUP buffer */
  301. buffer =
  302. skip_buffers_until(buffer, USB_BUFFER_SETUP, USB_BUFFER_SETUP, &res);
  303. ep->buffer = buffer;
  304. if (!buffer || buffer->left < len) {
  305. res |= USB_READ_BLOCK;
  306. return res;
  307. }
  308. /* SETUP packet must fit in a single buffer */
  309. if (buffer->left < len) {
  310. buffer->flags |= USB_BUFFER_FAILED;
  311. buffer->flags &= ~USB_BUFFER_SUBMITTED ;
  312. if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
  313. ep->buffer = buffer->next;
  314. res |= USB_READ_FAIL;
  315. return res;
  316. }
  317. setup_data = buffer->data;
  318. } else {
  319. if (buffer->flags & (USB_BUFFER_SETUP|USB_BUFFER_IN)) {
  320. buffer->flags |= USB_BUFFER_FAILED;
  321. buffer->flags &= ~USB_BUFFER_SUBMITTED ;
  322. if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
  323. ep->buffer = buffer->next;
  324. res |= USB_READ_FAIL;
  325. return res;
  326. }
  327. if (len == 0) {
  328. /* Status OUT */
  329. if (buffer->left > 0) {
  330. buffer->flags |= USB_BUFFER_FAILED;
  331. res |= USB_READ_FAIL;
  332. }
  333. buffer->flags &= ~USB_BUFFER_SUBMITTED ;
  334. if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
  335. ep->buffer = buffer->next;
  336. return res;
  337. }
  338. if (get_receive_capacity(buffer) < len) return USB_READ_BLOCK;
  339. }
  340. break;
  341. case USB_EP_FLAGS_TYPE_INTERRUPT:
  342. case USB_EP_FLAGS_TYPE_BULK:
  343. case USB_EP_FLAGS_TYPE_ISO:
  344. if (get_receive_capacity(buffer) < len) {
  345. return USB_READ_BLOCK;
  346. }
  347. break;
  348. }
  349. short_packet = len < ep->xfer_size;
  350. do {
  351. if (buffer->left < len) {
  352. copy = buffer->left;
  353. } else {
  354. copy = len;
  355. }
  356. len -= copy;
  357. buffer->left -= copy;
  358. read_hw_buffer(buffer->data, hw_ep, copy);
  359. buffer->data += copy;
  360. if (len == 0) break;
  361. /* Release buffer */
  362. buffer->flags &= ~(USB_BUFFER_SUBMITTED | USB_BUFFER_SHORT_PACKET);
  363. if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
  364. /* Use next buffer. */
  365. buffer = buffer->next;
  366. } while(1);
  367. if (short_packet) {
  368. buffer->flags |= USB_BUFFER_SHORT_PACKET;
  369. }
  370. if ((buffer->left == 0)
  371. || (buffer->flags & USB_BUFFER_PACKET_END)
  372. || (short_packet && (buffer->flags & USB_BUFFER_SHORT_END))) {
  373. /* Release buffer */
  374. buffer->flags &= ~USB_BUFFER_SUBMITTED;
  375. if (buffer->flags & USB_BUFFER_NOTIFY) res |= USB_READ_NOTIFY;
  376. /* Use next buffer. */
  377. buffer = buffer->next;
  378. }
  379. ep->buffer = buffer;
  380. if (setup_data) {
  381. /* Set direction according to request */
  382. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[0],
  383. ((setup_data[0] & 0x80)
  384. ? AT91C_UDP_DIR : 0), AT91C_UDP_DIR);
  385. }
  386. return res;
  387. }
  388. static void
  389. start_receive(USBEndpoint *ep)
  390. {
  391. ep->flags |= USB_EP_FLAGS_RECEIVING;
  392. }
  393. #if 0
  394. static unsigned int
  395. get_transmit_length(USBBuffer *buffer)
  396. {
  397. unsigned int length = 0;
  398. while(buffer && (buffer->flags & USB_BUFFER_IN)) {
  399. length += buffer->left;
  400. buffer = buffer->next;
  401. }
  402. return length;
  403. }
  404. #endif
  405. static int
  406. start_transmit(USBEndpoint *ep)
  407. {
  408. unsigned int res = 0;
  409. USBBuffer *buffer = ep->buffer;
  410. unsigned int len;
  411. unsigned int hw_ep = EP_HW_NUM(ep->addr);
  412. unsigned int ep_flags = ep->flags;
  413. len = ep->xfer_size;
  414. if (!(ep_flags & USB_EP_FLAGS_ENABLED) || !buffer) return USB_WRITE_BLOCK;
  415. switch(ep_flags & USB_EP_FLAGS_TYPE_MASK) {
  416. case USB_EP_FLAGS_TYPE_BULK:
  417. if (buffer->flags & USB_BUFFER_HALT) {
  418. if (ep->status & 0x01) return USB_WRITE_BLOCK;
  419. ep->status |= 0x01;
  420. if (!(ep->flags & USB_EP_FLAGS_TRANSMITTING)) {
  421. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],
  422. AT91C_UDP_FORCESTALL, AT91C_UDP_FORCESTALL);
  423. PRINTF("HALT IN\n");
  424. }
  425. return USB_WRITE_BLOCK;
  426. }
  427. case USB_EP_FLAGS_TYPE_ISO:
  428. if (!(ep->flags & USB_EP_FLAGS_TRANSMITTING)) {
  429. if (AT91C_UDP_CSR[hw_ep] & AT91C_UDP_TXPKTRDY) return USB_WRITE_BLOCK;
  430. }
  431. break;
  432. default:
  433. if (AT91C_UDP_CSR[hw_ep] & AT91C_UDP_TXPKTRDY) return USB_WRITE_BLOCK;
  434. }
  435. while (buffer) {
  436. unsigned int copy;
  437. if (buffer->left < len) {
  438. copy = buffer->left;
  439. } else {
  440. copy = len;
  441. }
  442. len -= copy;
  443. buffer->left -= copy;
  444. write_hw_buffer(buffer->data, hw_ep, copy);
  445. buffer->data += copy;
  446. if (buffer->left == 0) {
  447. if (buffer->flags & USB_BUFFER_SHORT_END) {
  448. if (len == 0) {
  449. /* Send zero length packet. */
  450. break;
  451. } else {
  452. len = 0;
  453. }
  454. }
  455. /* Release buffer */
  456. buffer->flags &= ~USB_BUFFER_SUBMITTED;
  457. if (buffer->flags & USB_BUFFER_NOTIFY) res = USB_WRITE_NOTIFY;
  458. /* Use next buffer. */
  459. buffer = buffer->next;
  460. }
  461. if (len == 0) break;
  462. }
  463. ep->buffer = buffer;
  464. if (ep->flags & USB_EP_FLAGS_TRANSMITTING) {
  465. ep->flags |= USB_EP_FLAGS_DOUBLE;
  466. } else {
  467. ep->flags |= USB_EP_FLAGS_TRANSMITTING;
  468. }
  469. PRINTF("start_transmit: sent %08x\n",AT91C_UDP_CSR[hw_ep]);
  470. /* Start transmission */
  471. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],
  472. AT91C_UDP_TXPKTRDY, AT91C_UDP_TXPKTRDY);
  473. return res;
  474. }
  475. static void
  476. start_transfer(USBEndpoint *ep)
  477. {
  478. unsigned int hw_ep = EP_HW_NUM(ep->addr);
  479. int res;
  480. while (1) {
  481. if (!(ep->addr & 0x80)) {
  482. if (ep->buffer && (ep->buffer->flags & USB_BUFFER_HALT)) {
  483. if (ep->status & 0x01) return ;
  484. ep->status |= 0x01;
  485. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[EP_HW_NUM(ep->addr)],
  486. AT91C_UDP_FORCESTALL, AT91C_UDP_FORCESTALL);
  487. PRINTF("HALT OUT\n");
  488. *AT91C_UDP_IDR = 1<<hw_ep;
  489. return;
  490. }
  491. }
  492. if (!(ep->flags & USB_EP_FLAGS_RECV_PENDING)) break;
  493. res = handle_pending_receive(ep);
  494. if (res & USB_READ_NOTIFY) {
  495. notify_ep_process(ep, USB_EP_EVENT_NOTIFICATION);
  496. }
  497. PRINTF("received res = %d\n", res);
  498. if (res & USB_READ_BLOCK) {
  499. *AT91C_UDP_IDR = 1<<hw_ep;
  500. return;
  501. }
  502. if (AT91C_UDP_CSR[hw_ep] & AT91C_UDP_RXSETUP) {
  503. /* Acknowledge SETUP */
  504. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],0, AT91C_UDP_RXSETUP);
  505. } else if (AT91C_UDP_CSR[hw_ep] & (AT91C_UDP_RX_DATA_BK1)) {
  506. /* Ping-pong */
  507. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],0,
  508. (ep->flags & USB_EP_FLAGS_BANK_1_RECV_NEXT)
  509. ? AT91C_UDP_RX_DATA_BK1
  510. : AT91C_UDP_RX_DATA_BK0);
  511. ep->flags ^= USB_EP_FLAGS_BANK_1_RECV_NEXT;
  512. } else {
  513. /* Ping-pong or single buffer */
  514. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],0,
  515. AT91C_UDP_RX_DATA_BK0);
  516. ep->flags |= USB_EP_FLAGS_BANK_1_RECV_NEXT;
  517. }
  518. if (ep->flags & USB_EP_FLAGS_DOUBLE) {
  519. ep->flags &= ~USB_EP_FLAGS_DOUBLE;
  520. } else if IS_CONTROL_EP(ep) {
  521. ep->flags &= ~(USB_EP_FLAGS_RECV_PENDING|USB_EP_FLAGS_SETUP_PENDING);
  522. } else {
  523. ep->flags &= ~USB_EP_FLAGS_RECV_PENDING;
  524. }
  525. if (res & USB_READ_FAIL) {
  526. /* Only fails for control endpoints */
  527. usb_arch_control_stall(ep->addr);
  528. return;
  529. }
  530. *AT91C_UDP_IER = 1<<hw_ep;
  531. }
  532. if (ep->flags & (USB_EP_FLAGS_TRANSMITTING | USB_EP_FLAGS_RECEIVING)) {
  533. #if 0
  534. if (!IS_BULK_EP(ep) || (ep->flags & USB_EP_FLAGS_DOUBLE)) {
  535. #else
  536. if(1) {
  537. #endif
  538. PRINTF("Busy\n");
  539. return;
  540. }
  541. }
  542. if (ep->status & 0x01) return; /* Don't start transfer if halted */
  543. if (ep->buffer) {
  544. if (ep->buffer->flags & USB_BUFFER_IN) {
  545. res = start_transmit(ep);
  546. if (res & USB_WRITE_NOTIFY) {
  547. notify_ep_process(ep, USB_EP_EVENT_NOTIFICATION);
  548. }
  549. } else {
  550. start_receive(ep);
  551. }
  552. }
  553. }
  554. void
  555. usb_arch_transfer_complete(unsigned int hw_ep)
  556. {
  557. unsigned int status = AT91C_UDP_CSR[hw_ep];
  558. USBEndpoint *ep = &usb_endpoints[hw_ep];
  559. PRINTF("transfer_complete: %d\n", hw_ep);
  560. if (status & AT91C_UDP_STALLSENT) {
  561. /* Acknowledge */
  562. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],0, AT91C_UDP_STALLSENT);
  563. }
  564. if (status & (AT91C_UDP_RXSETUP
  565. | AT91C_UDP_RX_DATA_BK1 | AT91C_UDP_RX_DATA_BK0)) {
  566. if (status & AT91C_UDP_RXSETUP) {
  567. PRINTF("SETUP\n");
  568. ep->flags |= USB_EP_FLAGS_SETUP_PENDING;
  569. }
  570. if (ep->flags & USB_EP_FLAGS_DOUBLE) {
  571. ep->flags &= ~USB_EP_FLAGS_DOUBLE;
  572. } else {
  573. ep->flags &= ~USB_EP_FLAGS_RECEIVING;
  574. }
  575. if ( ep->flags & USB_EP_FLAGS_RECV_PENDING) {
  576. ep->flags |= USB_EP_FLAGS_DOUBLE;
  577. } else {
  578. ep->flags |= USB_EP_FLAGS_RECV_PENDING;
  579. }
  580. start_transfer(ep);
  581. }
  582. if (status & AT91C_UDP_TXCOMP) {
  583. PRINTF("Sent packet\n");
  584. if (ep->flags & USB_EP_FLAGS_DOUBLE) {
  585. ep->flags &= ~USB_EP_FLAGS_DOUBLE;
  586. } else {
  587. ep->flags &= ~USB_EP_FLAGS_TRANSMITTING;
  588. }
  589. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],0, AT91C_UDP_TXCOMP);
  590. if (ep->status & 0x01) {
  591. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[hw_ep],
  592. AT91C_UDP_FORCESTALL, AT91C_UDP_FORCESTALL);
  593. PRINTF("HALT IN\n");
  594. } else {
  595. start_transfer(ep);
  596. }
  597. }
  598. }
  599. void
  600. usb_set_ep_event_process(unsigned char addr, struct process *p)
  601. {
  602. USBEndpoint *ep = &usb_endpoints[EP_INDEX(addr)];
  603. ep->event_process = p;
  604. }
  605. /* Select what process should be polled when a global event occurs */
  606. void
  607. usb_arch_set_global_event_process(struct process *p)
  608. {
  609. event_process = p;
  610. }
  611. unsigned int
  612. usb_arch_get_global_events(void)
  613. {
  614. unsigned int e;
  615. USB_DISABLE_INT;
  616. e = events;
  617. events = 0;
  618. USB_ENABLE_INT;
  619. return e;
  620. }
  621. unsigned int
  622. usb_get_ep_events(unsigned char addr)
  623. {
  624. unsigned int e;
  625. unsigned int ei = EP_HW_NUM(addr);
  626. USB_DISABLE_INT;
  627. e = usb_endpoints[ei].events;
  628. usb_endpoints[ei].events = 0;
  629. USB_ENABLE_INT;
  630. return e;
  631. }
  632. void
  633. usb_submit_recv_buffer(unsigned char ep_addr, USBBuffer *buffer)
  634. {
  635. USBBuffer **tailp;
  636. USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
  637. if (!(ep->flags & USB_EP_FLAGS_ENABLED)) return;
  638. /* PRINTF("buffer: %p\n", ep->buffer); */
  639. /* dbg_drain(); */
  640. USB_DISABLE_INT;
  641. tailp = (USBBuffer**)&ep->buffer;
  642. while(*tailp) {
  643. tailp = &(*tailp)->next;
  644. }
  645. *tailp = buffer;
  646. while(buffer) {
  647. buffer->flags |= USB_BUFFER_SUBMITTED;
  648. buffer = buffer->next;
  649. }
  650. start_transfer(ep);
  651. USB_ENABLE_INT;
  652. }
  653. void
  654. usb_submit_xmit_buffer(unsigned char ep_addr, USBBuffer *buffer)
  655. {
  656. USBBuffer **tailp;
  657. USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
  658. if (!(ep->flags & USB_EP_FLAGS_ENABLED)) return;
  659. /* PRINTF("usb_submit_xmit_buffer %d\n", buffer->left); */
  660. USB_DISABLE_INT;
  661. tailp = (USBBuffer**)&ep->buffer;
  662. while(*tailp) {
  663. tailp = &(*tailp)->next;
  664. }
  665. *tailp = buffer;
  666. while(buffer) {
  667. buffer->flags |= USB_BUFFER_SUBMITTED | USB_BUFFER_IN;
  668. buffer = buffer->next;
  669. }
  670. start_transfer(ep);
  671. USB_ENABLE_INT;
  672. }
  673. void
  674. usb_arch_discard_all_buffers(unsigned char ep_addr)
  675. {
  676. USBBuffer *buffer;
  677. volatile USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
  678. USB_DISABLE_EP_INT(EP_HW_NUM(ep_addr));
  679. buffer = ep->buffer;
  680. ep->buffer = NULL;
  681. USB_ENABLE_EP_INT(EP_HW_NUM(ep_addr));
  682. while(buffer) {
  683. buffer->flags &= ~USB_BUFFER_SUBMITTED;
  684. buffer = buffer->next;
  685. }
  686. }
  687. uint16_t
  688. usb_arch_get_ep_status(uint8_t addr)
  689. {
  690. if (EP_INDEX(addr) > USB_MAX_ENDPOINTS) return 0;
  691. return usb_endpoints[EP_INDEX(addr)].status;
  692. }
  693. void
  694. usb_arch_set_configuration(uint8_t usb_configuration_value)
  695. {
  696. /* Nothing needs to be done */
  697. }
  698. void
  699. usb_arch_control_stall(unsigned char addr)
  700. {
  701. if (EP_INDEX(addr) > USB_MAX_ENDPOINTS) return;
  702. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[EP_HW_NUM(addr)],
  703. AT91C_UDP_FORCESTALL, AT91C_UDP_FORCESTALL);
  704. }
  705. /* Not for control endpoints */
  706. void
  707. usb_arch_halt_endpoint(unsigned char ep_addr, int halt)
  708. {
  709. if (EP_INDEX(ep_addr) > USB_MAX_ENDPOINTS) return;
  710. if (!usb_endpoints[EP_INDEX(ep_addr)].flags & USB_EP_FLAGS_ENABLED) return;
  711. *AT91C_UDP_IDR = 1<<EP_HW_NUM(ep_addr);
  712. if (halt) {
  713. usb_endpoints[EP_INDEX(ep_addr)].status |= 0x01;
  714. /* Delay stall if a transmission is i progress */
  715. if (!(usb_endpoints[EP_INDEX(ep_addr)].flags & USB_EP_FLAGS_TRANSMITTING)){
  716. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[EP_HW_NUM(ep_addr)],
  717. AT91C_UDP_FORCESTALL, AT91C_UDP_FORCESTALL);
  718. }
  719. } else {
  720. USBEndpoint *ep = &usb_endpoints[EP_INDEX(ep_addr)];
  721. ep->status &= ~0x01;
  722. *AT91C_UDP_IDR = 1<<EP_HW_NUM(ep_addr);
  723. UDP_SET_EP_CTRL_FLAGS(&AT91C_UDP_CSR[EP_HW_NUM(ep_addr)],
  724. 0, AT91C_UDP_FORCESTALL);
  725. *AT91C_UDP_RSTEP = 1<<EP_HW_NUM(ep_addr);
  726. *AT91C_UDP_RSTEP = 0;
  727. /* Release HALT buffer */
  728. if (ep->buffer && (ep->buffer->flags & USB_BUFFER_HALT)) {
  729. ep->buffer->flags &= ~USB_BUFFER_SUBMITTED;
  730. if (ep->buffer->flags & USB_BUFFER_NOTIFY) {
  731. notify_ep_process(ep,USB_EP_EVENT_NOTIFICATION);
  732. }
  733. ep->buffer = ep->buffer->next;
  734. }
  735. /* Restart transmission */
  736. start_transfer(&usb_endpoints[EP_INDEX(ep_addr)]);
  737. }
  738. *AT91C_UDP_IER = 1<<EP_HW_NUM(ep_addr);
  739. }
  740. int
  741. usb_arch_send_pending(uint8_t ep_addr)
  742. {
  743. return usb_endpoints[EP_INDEX(ep_addr)].flags & USB_EP_FLAGS_TRANSMITTING;
  744. }
  745. void
  746. usb_arch_set_address(unsigned char addr)
  747. {
  748. *AT91C_UDP_FADDR = AT91C_UDP_FEN | addr;
  749. *AT91C_UDP_GLBSTATE |= AT91C_UDP_FADDEN;
  750. }
  751. void
  752. usb_arch_reset_int()
  753. {
  754. usb_arch_reset();
  755. notify_process(USB_EVENT_RESET);
  756. }
  757. void
  758. usb_arch_suspend_int()
  759. {
  760. notify_process(USB_EVENT_SUSPEND);
  761. }
  762. void
  763. usb_arch_resume_int()
  764. {
  765. notify_process(USB_EVENT_RESUME);
  766. }