fhci-sched.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale QUICC Engine USB Host Controller Driver
  4. *
  5. * Copyright (c) Freescale Semicondutor, Inc. 2006, 2011.
  6. * Shlomi Gridish <gridish@freescale.com>
  7. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  8. * Copyright (c) Logic Product Development, Inc. 2007
  9. * Peter Barada <peterb@logicpd.com>
  10. * Copyright (c) MontaVista Software, Inc. 2008.
  11. * Anton Vorontsov <avorontsov@ru.mvista.com>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/list.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include <soc/fsl/qe/qe.h>
  24. #include <asm/fsl_gtm.h>
  25. #include "fhci.h"
  26. static void recycle_frame(struct fhci_usb *usb, struct packet *pkt)
  27. {
  28. pkt->data = NULL;
  29. pkt->len = 0;
  30. pkt->status = USB_TD_OK;
  31. pkt->info = 0;
  32. pkt->priv_data = NULL;
  33. cq_put(&usb->ep0->empty_frame_Q, pkt);
  34. }
  35. /* confirm submitted packet */
  36. void fhci_transaction_confirm(struct fhci_usb *usb, struct packet *pkt)
  37. {
  38. struct td *td;
  39. struct packet *td_pkt;
  40. struct ed *ed;
  41. u32 trans_len;
  42. bool td_done = false;
  43. td = fhci_remove_td_from_frame(usb->actual_frame);
  44. td_pkt = td->pkt;
  45. trans_len = pkt->len;
  46. td->status = pkt->status;
  47. if (td->type == FHCI_TA_IN && td_pkt->info & PKT_DUMMY_PACKET) {
  48. if ((td->data + td->actual_len) && trans_len)
  49. memcpy(td->data + td->actual_len, pkt->data,
  50. trans_len);
  51. cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
  52. }
  53. recycle_frame(usb, pkt);
  54. ed = td->ed;
  55. if (ed->mode == FHCI_TF_ISO) {
  56. if (ed->td_list.next->next != &ed->td_list) {
  57. struct td *td_next =
  58. list_entry(ed->td_list.next->next, struct td,
  59. node);
  60. td_next->start_frame = usb->actual_frame->frame_num;
  61. }
  62. td->actual_len = trans_len;
  63. td_done = true;
  64. } else if ((td->status & USB_TD_ERROR) &&
  65. !(td->status & USB_TD_TX_ER_NAK)) {
  66. /*
  67. * There was an error on the transaction (but not NAK).
  68. * If it is fatal error (data underrun, stall, bad pid or 3
  69. * errors exceeded), mark this TD as done.
  70. */
  71. if ((td->status & USB_TD_RX_DATA_UNDERUN) ||
  72. (td->status & USB_TD_TX_ER_STALL) ||
  73. (td->status & USB_TD_RX_ER_PID) ||
  74. (++td->error_cnt >= 3)) {
  75. ed->state = FHCI_ED_HALTED;
  76. td_done = true;
  77. if (td->status & USB_TD_RX_DATA_UNDERUN) {
  78. fhci_dbg(usb->fhci, "td err fu\n");
  79. td->toggle = !td->toggle;
  80. td->actual_len += trans_len;
  81. } else {
  82. fhci_dbg(usb->fhci, "td err f!u\n");
  83. }
  84. } else {
  85. fhci_dbg(usb->fhci, "td err !f\n");
  86. /* it is not a fatal error -retry this transaction */
  87. td->nak_cnt = 0;
  88. td->error_cnt++;
  89. td->status = USB_TD_OK;
  90. }
  91. } else if (td->status & USB_TD_TX_ER_NAK) {
  92. /* there was a NAK response */
  93. fhci_vdbg(usb->fhci, "td nack\n");
  94. td->nak_cnt++;
  95. td->error_cnt = 0;
  96. td->status = USB_TD_OK;
  97. } else {
  98. /* there was no error on transaction */
  99. td->error_cnt = 0;
  100. td->nak_cnt = 0;
  101. td->toggle = !td->toggle;
  102. td->actual_len += trans_len;
  103. if (td->len == td->actual_len)
  104. td_done = true;
  105. }
  106. if (td_done)
  107. fhci_move_td_from_ed_to_done_list(usb, ed);
  108. }
  109. /*
  110. * Flush all transmitted packets from BDs
  111. * This routine is called when disabling the USB port to flush all
  112. * transmissions that are already scheduled in the BDs
  113. */
  114. void fhci_flush_all_transmissions(struct fhci_usb *usb)
  115. {
  116. u8 mode;
  117. struct td *td;
  118. mode = in_8(&usb->fhci->regs->usb_usmod);
  119. clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
  120. fhci_flush_bds(usb);
  121. while ((td = fhci_peek_td_from_frame(usb->actual_frame)) != NULL) {
  122. struct packet *pkt = td->pkt;
  123. pkt->status = USB_TD_TX_ER_TIMEOUT;
  124. fhci_transaction_confirm(usb, pkt);
  125. }
  126. usb->actual_frame->frame_status = FRAME_END_TRANSMISSION;
  127. /* reset the event register */
  128. out_be16(&usb->fhci->regs->usb_usber, 0xffff);
  129. /* enable the USB controller */
  130. out_8(&usb->fhci->regs->usb_usmod, mode | USB_MODE_EN);
  131. }
  132. /*
  133. * This function forms the packet and transmit the packet. This function
  134. * will handle all endpoint type:ISO,interrupt,control and bulk
  135. */
  136. static int add_packet(struct fhci_usb *usb, struct ed *ed, struct td *td)
  137. {
  138. u32 fw_transaction_time, len = 0;
  139. struct packet *pkt;
  140. u8 *data = NULL;
  141. /* calcalate data address,len and toggle and then add the transaction */
  142. if (td->toggle == USB_TD_TOGGLE_CARRY)
  143. td->toggle = ed->toggle_carry;
  144. switch (ed->mode) {
  145. case FHCI_TF_ISO:
  146. len = td->len;
  147. if (td->type != FHCI_TA_IN)
  148. data = td->data;
  149. break;
  150. case FHCI_TF_CTRL:
  151. case FHCI_TF_BULK:
  152. len = min(td->len - td->actual_len, ed->max_pkt_size);
  153. if (!((td->type == FHCI_TA_IN) &&
  154. ((len + td->actual_len) == td->len)))
  155. data = td->data + td->actual_len;
  156. break;
  157. case FHCI_TF_INTR:
  158. len = min(td->len, ed->max_pkt_size);
  159. if (!((td->type == FHCI_TA_IN) &&
  160. ((td->len + CRC_SIZE) >= ed->max_pkt_size)))
  161. data = td->data;
  162. break;
  163. default:
  164. break;
  165. }
  166. if (usb->port_status == FHCI_PORT_FULL)
  167. fw_transaction_time = (((len + PROTOCOL_OVERHEAD) * 11) >> 4);
  168. else
  169. fw_transaction_time = ((len + PROTOCOL_OVERHEAD) * 6);
  170. /* check if there's enough space in this frame to submit this TD */
  171. if (usb->actual_frame->total_bytes + len + PROTOCOL_OVERHEAD >=
  172. usb->max_bytes_per_frame) {
  173. fhci_vdbg(usb->fhci, "not enough space in this frame: "
  174. "%d %d %d\n", usb->actual_frame->total_bytes, len,
  175. usb->max_bytes_per_frame);
  176. return -1;
  177. }
  178. /* check if there's enough time in this frame to submit this TD */
  179. if (usb->actual_frame->frame_status != FRAME_IS_PREPARED &&
  180. (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION ||
  181. (fw_transaction_time + usb->sw_transaction_time >=
  182. 1000 - fhci_get_sof_timer_count(usb)))) {
  183. fhci_dbg(usb->fhci, "not enough time in this frame\n");
  184. return -1;
  185. }
  186. /* update frame object fields before transmitting */
  187. pkt = cq_get(&usb->ep0->empty_frame_Q);
  188. if (!pkt) {
  189. fhci_dbg(usb->fhci, "there is no empty frame\n");
  190. return -1;
  191. }
  192. td->pkt = pkt;
  193. pkt->info = 0;
  194. if (data == NULL) {
  195. data = cq_get(&usb->ep0->dummy_packets_Q);
  196. BUG_ON(!data);
  197. pkt->info = PKT_DUMMY_PACKET;
  198. }
  199. pkt->data = data;
  200. pkt->len = len;
  201. pkt->status = USB_TD_OK;
  202. /* update TD status field before transmitting */
  203. td->status = USB_TD_INPROGRESS;
  204. /* update actual frame time object with the actual transmission */
  205. usb->actual_frame->total_bytes += (len + PROTOCOL_OVERHEAD);
  206. fhci_add_td_to_frame(usb->actual_frame, td);
  207. if (usb->port_status != FHCI_PORT_FULL &&
  208. usb->port_status != FHCI_PORT_LOW) {
  209. pkt->status = USB_TD_TX_ER_TIMEOUT;
  210. pkt->len = 0;
  211. fhci_transaction_confirm(usb, pkt);
  212. } else if (fhci_host_transaction(usb, pkt, td->type, ed->dev_addr,
  213. ed->ep_addr, ed->mode, ed->speed, td->toggle)) {
  214. /* remove TD from actual frame */
  215. list_del_init(&td->frame_lh);
  216. td->status = USB_TD_OK;
  217. if (pkt->info & PKT_DUMMY_PACKET)
  218. cq_put(&usb->ep0->dummy_packets_Q, pkt->data);
  219. recycle_frame(usb, pkt);
  220. usb->actual_frame->total_bytes -= (len + PROTOCOL_OVERHEAD);
  221. fhci_err(usb->fhci, "host transaction failed\n");
  222. return -1;
  223. }
  224. return len;
  225. }
  226. static void move_head_to_tail(struct list_head *list)
  227. {
  228. struct list_head *node = list->next;
  229. if (!list_empty(list)) {
  230. list_move_tail(node, list);
  231. }
  232. }
  233. /*
  234. * This function goes through the endpoint list and schedules the
  235. * transactions within this list
  236. */
  237. static int scan_ed_list(struct fhci_usb *usb,
  238. struct list_head *list, enum fhci_tf_mode list_type)
  239. {
  240. static const int frame_part[4] = {
  241. [FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
  242. [FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
  243. MAX_PERIODIC_FRAME_USAGE) / 100,
  244. [FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
  245. [FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
  246. MAX_PERIODIC_FRAME_USAGE) / 100
  247. };
  248. struct ed *ed;
  249. struct td *td;
  250. int ans = 1;
  251. u32 save_transaction_time = usb->sw_transaction_time;
  252. list_for_each_entry(ed, list, node) {
  253. td = ed->td_head;
  254. if (!td || td->status == USB_TD_INPROGRESS)
  255. continue;
  256. if (ed->state != FHCI_ED_OPER) {
  257. if (ed->state == FHCI_ED_URB_DEL) {
  258. td->status = USB_TD_OK;
  259. fhci_move_td_from_ed_to_done_list(usb, ed);
  260. ed->state = FHCI_ED_SKIP;
  261. }
  262. continue;
  263. }
  264. /*
  265. * if it isn't interrupt pipe or it is not iso pipe and the
  266. * interval time passed
  267. */
  268. if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
  269. (((usb->actual_frame->frame_num -
  270. td->start_frame) & 0x7ff) < td->interval))
  271. continue;
  272. if (add_packet(usb, ed, td) < 0)
  273. continue;
  274. /* update time stamps in the TD */
  275. td->start_frame = usb->actual_frame->frame_num;
  276. usb->sw_transaction_time += save_transaction_time;
  277. if (usb->actual_frame->total_bytes >=
  278. usb->max_bytes_per_frame) {
  279. usb->actual_frame->frame_status =
  280. FRAME_DATA_END_TRANSMISSION;
  281. fhci_push_dummy_bd(usb->ep0);
  282. ans = 0;
  283. break;
  284. }
  285. if (usb->actual_frame->total_bytes >= frame_part[list_type])
  286. break;
  287. }
  288. /* be fair to each ED(move list head around) */
  289. move_head_to_tail(list);
  290. usb->sw_transaction_time = save_transaction_time;
  291. return ans;
  292. }
  293. static u32 rotate_frames(struct fhci_usb *usb)
  294. {
  295. struct fhci_hcd *fhci = usb->fhci;
  296. if (!list_empty(&usb->actual_frame->tds_list)) {
  297. if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
  298. usb->actual_frame->frame_num) & 0x7ff) > 5)
  299. fhci_flush_actual_frame(usb);
  300. else
  301. return -EINVAL;
  302. }
  303. usb->actual_frame->frame_status = FRAME_IS_PREPARED;
  304. usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
  305. usb->actual_frame->total_bytes = 0;
  306. return 0;
  307. }
  308. /*
  309. * This function schedule the USB transaction and will process the
  310. * endpoint in the following order: iso, interrupt, control and bulk.
  311. */
  312. void fhci_schedule_transactions(struct fhci_usb *usb)
  313. {
  314. int left = 1;
  315. if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
  316. if (rotate_frames(usb) != 0)
  317. return;
  318. if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
  319. return;
  320. if (usb->actual_frame->total_bytes == 0) {
  321. /*
  322. * schedule the next available ISO transfer
  323. *or next stage of the ISO transfer
  324. */
  325. scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
  326. /*
  327. * schedule the next available interrupt transfer or
  328. * the next stage of the interrupt transfer
  329. */
  330. scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
  331. /*
  332. * schedule the next available control transfer
  333. * or the next stage of the control transfer
  334. */
  335. left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
  336. FHCI_TF_CTRL);
  337. }
  338. /*
  339. * schedule the next available bulk transfer or the next stage of the
  340. * bulk transfer
  341. */
  342. if (left > 0)
  343. scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
  344. }
  345. /* Handles SOF interrupt */
  346. static void sof_interrupt(struct fhci_hcd *fhci)
  347. {
  348. struct fhci_usb *usb = fhci->usb_lld;
  349. if ((usb->port_status == FHCI_PORT_DISABLED) &&
  350. (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
  351. !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
  352. if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
  353. usb->port_status = FHCI_PORT_LOW;
  354. else
  355. usb->port_status = FHCI_PORT_FULL;
  356. /* Disable IDLE */
  357. usb->saved_msk &= ~USB_E_IDLE_MASK;
  358. out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
  359. }
  360. gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
  361. fhci_host_transmit_actual_frame(usb);
  362. usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
  363. fhci_schedule_transactions(usb);
  364. }
  365. /* Handles device disconnected interrupt on port */
  366. void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
  367. {
  368. struct fhci_usb *usb = fhci->usb_lld;
  369. fhci_dbg(fhci, "-> %s\n", __func__);
  370. fhci_usb_disable_interrupt(usb);
  371. clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
  372. usb->port_status = FHCI_PORT_DISABLED;
  373. fhci_stop_sof_timer(fhci);
  374. /* Enable IDLE since we want to know if something comes along */
  375. usb->saved_msk |= USB_E_IDLE_MASK;
  376. out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
  377. usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
  378. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
  379. usb->max_bytes_per_frame = 0;
  380. fhci_usb_enable_interrupt(usb);
  381. fhci_dbg(fhci, "<- %s\n", __func__);
  382. }
  383. /* detect a new device connected on the USB port */
  384. void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
  385. {
  386. struct fhci_usb *usb = fhci->usb_lld;
  387. int state;
  388. int ret;
  389. fhci_dbg(fhci, "-> %s\n", __func__);
  390. fhci_usb_disable_interrupt(usb);
  391. state = fhci_ioports_check_bus_state(fhci);
  392. /* low-speed device was connected to the USB port */
  393. if (state == 1) {
  394. ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
  395. if (ret) {
  396. fhci_warn(fhci, "Low-Speed device is not supported, "
  397. "try use BRGx\n");
  398. goto out;
  399. }
  400. usb->port_status = FHCI_PORT_LOW;
  401. setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
  402. usb->vroot_hub->port.wPortStatus |=
  403. (USB_PORT_STAT_LOW_SPEED |
  404. USB_PORT_STAT_CONNECTION);
  405. usb->vroot_hub->port.wPortChange |=
  406. USB_PORT_STAT_C_CONNECTION;
  407. usb->max_bytes_per_frame =
  408. (MAX_BYTES_PER_FRAME >> 3) - 7;
  409. fhci_port_enable(usb);
  410. } else if (state == 2) {
  411. ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
  412. if (ret) {
  413. fhci_warn(fhci, "Full-Speed device is not supported, "
  414. "try use CLKx\n");
  415. goto out;
  416. }
  417. usb->port_status = FHCI_PORT_FULL;
  418. clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_LSS);
  419. usb->vroot_hub->port.wPortStatus &=
  420. ~USB_PORT_STAT_LOW_SPEED;
  421. usb->vroot_hub->port.wPortStatus |=
  422. USB_PORT_STAT_CONNECTION;
  423. usb->vroot_hub->port.wPortChange |=
  424. USB_PORT_STAT_C_CONNECTION;
  425. usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
  426. fhci_port_enable(usb);
  427. }
  428. out:
  429. fhci_usb_enable_interrupt(usb);
  430. fhci_dbg(fhci, "<- %s\n", __func__);
  431. }
  432. irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
  433. {
  434. struct usb_hcd *hcd = _hcd;
  435. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  436. struct fhci_usb *usb = fhci->usb_lld;
  437. spin_lock(&fhci->lock);
  438. gtm_set_exact_timer16(fhci->timer, 1000, false);
  439. if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
  440. usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
  441. fhci_push_dummy_bd(usb->ep0);
  442. }
  443. fhci_schedule_transactions(usb);
  444. spin_unlock(&fhci->lock);
  445. return IRQ_HANDLED;
  446. }
  447. /* Cancel transmission on the USB endpoint */
  448. static void abort_transmission(struct fhci_usb *usb)
  449. {
  450. fhci_dbg(usb->fhci, "-> %s\n", __func__);
  451. /* issue stop Tx command */
  452. qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
  453. /* flush Tx FIFOs */
  454. out_8(&usb->fhci->regs->usb_uscom, USB_CMD_FLUSH_FIFO | EP_ZERO);
  455. udelay(1000);
  456. /* reset Tx BDs */
  457. fhci_flush_bds(usb);
  458. /* issue restart Tx command */
  459. qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
  460. fhci_dbg(usb->fhci, "<- %s\n", __func__);
  461. }
  462. irqreturn_t fhci_irq(struct usb_hcd *hcd)
  463. {
  464. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  465. struct fhci_usb *usb;
  466. u16 usb_er = 0;
  467. unsigned long flags;
  468. spin_lock_irqsave(&fhci->lock, flags);
  469. usb = fhci->usb_lld;
  470. usb_er |= in_be16(&usb->fhci->regs->usb_usber) &
  471. in_be16(&usb->fhci->regs->usb_usbmr);
  472. /* clear event bits for next time */
  473. out_be16(&usb->fhci->regs->usb_usber, usb_er);
  474. fhci_dbg_isr(fhci, usb_er);
  475. if (usb_er & USB_E_RESET_MASK) {
  476. if ((usb->port_status == FHCI_PORT_FULL) ||
  477. (usb->port_status == FHCI_PORT_LOW)) {
  478. fhci_device_disconnected_interrupt(fhci);
  479. usb_er &= ~USB_E_IDLE_MASK;
  480. } else if (usb->port_status == FHCI_PORT_WAITING) {
  481. usb->port_status = FHCI_PORT_DISCONNECTING;
  482. /* Turn on IDLE since we want to disconnect */
  483. usb->saved_msk |= USB_E_IDLE_MASK;
  484. out_be16(&usb->fhci->regs->usb_usber,
  485. usb->saved_msk);
  486. } else if (usb->port_status == FHCI_PORT_DISABLED) {
  487. if (fhci_ioports_check_bus_state(fhci) == 1)
  488. fhci_device_connected_interrupt(fhci);
  489. }
  490. usb_er &= ~USB_E_RESET_MASK;
  491. }
  492. if (usb_er & USB_E_MSF_MASK) {
  493. abort_transmission(fhci->usb_lld);
  494. usb_er &= ~USB_E_MSF_MASK;
  495. }
  496. if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
  497. sof_interrupt(fhci);
  498. usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
  499. }
  500. if (usb_er & USB_E_TXB_MASK) {
  501. fhci_tx_conf_interrupt(fhci->usb_lld);
  502. usb_er &= ~USB_E_TXB_MASK;
  503. }
  504. if (usb_er & USB_E_TXE1_MASK) {
  505. fhci_tx_conf_interrupt(fhci->usb_lld);
  506. usb_er &= ~USB_E_TXE1_MASK;
  507. }
  508. if (usb_er & USB_E_IDLE_MASK) {
  509. if (usb->port_status == FHCI_PORT_DISABLED) {
  510. usb_er &= ~USB_E_RESET_MASK;
  511. fhci_device_connected_interrupt(fhci);
  512. } else if (usb->port_status ==
  513. FHCI_PORT_DISCONNECTING) {
  514. /* XXX usb->port_status = FHCI_PORT_WAITING; */
  515. /* Disable IDLE */
  516. usb->saved_msk &= ~USB_E_IDLE_MASK;
  517. out_be16(&usb->fhci->regs->usb_usbmr,
  518. usb->saved_msk);
  519. } else {
  520. fhci_dbg_isr(fhci, -1);
  521. }
  522. usb_er &= ~USB_E_IDLE_MASK;
  523. }
  524. spin_unlock_irqrestore(&fhci->lock, flags);
  525. return IRQ_HANDLED;
  526. }
  527. /*
  528. * Process normal completions(error or success) and clean the schedule.
  529. *
  530. * This is the main path for handing urbs back to drivers. The only other patth
  531. * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
  532. * the (re-reversed) done list as this does.
  533. */
  534. static void process_done_list(unsigned long data)
  535. {
  536. struct urb *urb;
  537. struct ed *ed;
  538. struct td *td;
  539. struct urb_priv *urb_priv;
  540. struct fhci_hcd *fhci = (struct fhci_hcd *)data;
  541. disable_irq(fhci->timer->irq);
  542. disable_irq(fhci_to_hcd(fhci)->irq);
  543. spin_lock(&fhci->lock);
  544. td = fhci_remove_td_from_done_list(fhci->hc_list);
  545. while (td != NULL) {
  546. urb = td->urb;
  547. urb_priv = urb->hcpriv;
  548. ed = td->ed;
  549. /* update URB's length and status from TD */
  550. fhci_done_td(urb, td);
  551. urb_priv->tds_cnt++;
  552. /*
  553. * if all this urb's TDs are done, call complete()
  554. * Interrupt transfers are the onley special case:
  555. * they are reissued,until "deleted" by usb_unlink_urb
  556. * (real work done in a SOF intr, by process_del_list)
  557. */
  558. if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
  559. fhci_urb_complete_free(fhci, urb);
  560. } else if (urb_priv->state == URB_DEL &&
  561. ed->state == FHCI_ED_SKIP) {
  562. fhci_del_ed_list(fhci, ed);
  563. ed->state = FHCI_ED_OPER;
  564. } else if (ed->state == FHCI_ED_HALTED) {
  565. urb_priv->state = URB_DEL;
  566. ed->state = FHCI_ED_URB_DEL;
  567. fhci_del_ed_list(fhci, ed);
  568. ed->state = FHCI_ED_OPER;
  569. }
  570. td = fhci_remove_td_from_done_list(fhci->hc_list);
  571. }
  572. spin_unlock(&fhci->lock);
  573. enable_irq(fhci->timer->irq);
  574. enable_irq(fhci_to_hcd(fhci)->irq);
  575. }
  576. DECLARE_TASKLET(fhci_tasklet, process_done_list, 0);
  577. /* transfer complted callback */
  578. u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
  579. {
  580. if (!fhci->process_done_task->state)
  581. tasklet_schedule(fhci->process_done_task);
  582. return 0;
  583. }
  584. /*
  585. * adds urb to the endpoint descriptor list
  586. * arguments:
  587. * fhci data structure for the Low level host controller
  588. * ep USB Host endpoint data structure
  589. * urb USB request block data structure
  590. */
  591. void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
  592. {
  593. struct ed *ed = urb->ep->hcpriv;
  594. struct urb_priv *urb_priv = urb->hcpriv;
  595. u32 data_len = urb->transfer_buffer_length;
  596. int urb_state = 0;
  597. int toggle = 0;
  598. struct td *td;
  599. u8 *data;
  600. u16 cnt = 0;
  601. if (ed == NULL) {
  602. ed = fhci_get_empty_ed(fhci);
  603. ed->dev_addr = usb_pipedevice(urb->pipe);
  604. ed->ep_addr = usb_pipeendpoint(urb->pipe);
  605. switch (usb_pipetype(urb->pipe)) {
  606. case PIPE_CONTROL:
  607. ed->mode = FHCI_TF_CTRL;
  608. break;
  609. case PIPE_BULK:
  610. ed->mode = FHCI_TF_BULK;
  611. break;
  612. case PIPE_INTERRUPT:
  613. ed->mode = FHCI_TF_INTR;
  614. break;
  615. case PIPE_ISOCHRONOUS:
  616. ed->mode = FHCI_TF_ISO;
  617. break;
  618. default:
  619. break;
  620. }
  621. ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
  622. FHCI_LOW_SPEED : FHCI_FULL_SPEED;
  623. ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
  624. urb->ep->hcpriv = ed;
  625. fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
  626. ed->speed, ed->max_pkt_size);
  627. }
  628. /* for ISO transfer calculate start frame index */
  629. if (ed->mode == FHCI_TF_ISO) {
  630. /* Ignore the possibility of underruns */
  631. urb->start_frame = ed->td_head ? ed->next_iso :
  632. get_frame_num(fhci);
  633. ed->next_iso = (urb->start_frame + urb->interval *
  634. urb->number_of_packets) & 0x07ff;
  635. }
  636. /*
  637. * OHCI handles the DATA toggle itself,we just use the USB
  638. * toggle bits
  639. */
  640. if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  641. usb_pipeout(urb->pipe)))
  642. toggle = USB_TD_TOGGLE_CARRY;
  643. else {
  644. toggle = USB_TD_TOGGLE_DATA0;
  645. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  646. usb_pipeout(urb->pipe), 1);
  647. }
  648. urb_priv->tds_cnt = 0;
  649. urb_priv->ed = ed;
  650. if (data_len > 0)
  651. data = urb->transfer_buffer;
  652. else
  653. data = NULL;
  654. switch (ed->mode) {
  655. case FHCI_TF_BULK:
  656. if (urb->transfer_flags & URB_ZERO_PACKET &&
  657. urb->transfer_buffer_length > 0 &&
  658. ((urb->transfer_buffer_length %
  659. usb_endpoint_maxp(&urb->ep->desc)) == 0))
  660. urb_state = US_BULK0;
  661. while (data_len > 4096) {
  662. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  663. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  664. FHCI_TA_IN,
  665. cnt ? USB_TD_TOGGLE_CARRY :
  666. toggle,
  667. data, 4096, 0, 0, true);
  668. data += 4096;
  669. data_len -= 4096;
  670. cnt++;
  671. }
  672. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  673. usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
  674. cnt ? USB_TD_TOGGLE_CARRY : toggle,
  675. data, data_len, 0, 0, true);
  676. cnt++;
  677. if (urb->transfer_flags & URB_ZERO_PACKET &&
  678. cnt < urb_priv->num_of_tds) {
  679. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  680. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  681. FHCI_TA_IN,
  682. USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
  683. cnt++;
  684. }
  685. break;
  686. case FHCI_TF_INTR:
  687. urb->start_frame = get_frame_num(fhci) + 1;
  688. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  689. usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
  690. USB_TD_TOGGLE_DATA0, data, data_len,
  691. urb->interval, urb->start_frame, true);
  692. break;
  693. case FHCI_TF_CTRL:
  694. ed->dev_addr = usb_pipedevice(urb->pipe);
  695. ed->max_pkt_size = usb_endpoint_maxp(&urb->ep->desc);
  696. /* setup stage */
  697. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
  698. USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
  699. /* data stage */
  700. if (data_len > 0) {
  701. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  702. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  703. FHCI_TA_IN,
  704. USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
  705. true);
  706. }
  707. /* status stage */
  708. if (data_len > 0)
  709. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  710. (usb_pipeout(urb->pipe) ? FHCI_TA_IN :
  711. FHCI_TA_OUT),
  712. USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
  713. else
  714. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  715. FHCI_TA_IN,
  716. USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
  717. urb_state = US_CTRL_SETUP;
  718. break;
  719. case FHCI_TF_ISO:
  720. for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
  721. u16 frame = urb->start_frame;
  722. /*
  723. * FIXME scheduling should handle frame counter
  724. * roll-around ... exotic case (and OHCI has
  725. * a 2^16 iso range, vs other HCs max of 2^10)
  726. */
  727. frame += cnt * urb->interval;
  728. frame &= 0x07ff;
  729. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  730. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  731. FHCI_TA_IN,
  732. USB_TD_TOGGLE_DATA0,
  733. data + urb->iso_frame_desc[cnt].offset,
  734. urb->iso_frame_desc[cnt].length,
  735. urb->interval, frame, true);
  736. }
  737. break;
  738. default:
  739. break;
  740. }
  741. /*
  742. * set the state of URB
  743. * control pipe:3 states -- setup,data,status
  744. * interrupt and bulk pipe:1 state -- data
  745. */
  746. urb->pipe &= ~0x1f;
  747. urb->pipe |= urb_state & 0x1f;
  748. urb_priv->state = URB_INPROGRESS;
  749. if (!ed->td_head) {
  750. ed->state = FHCI_ED_OPER;
  751. switch (ed->mode) {
  752. case FHCI_TF_CTRL:
  753. list_add(&ed->node, &fhci->hc_list->ctrl_list);
  754. break;
  755. case FHCI_TF_BULK:
  756. list_add(&ed->node, &fhci->hc_list->bulk_list);
  757. break;
  758. case FHCI_TF_INTR:
  759. list_add(&ed->node, &fhci->hc_list->intr_list);
  760. break;
  761. case FHCI_TF_ISO:
  762. list_add(&ed->node, &fhci->hc_list->iso_list);
  763. break;
  764. default:
  765. break;
  766. }
  767. }
  768. fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
  769. fhci->active_urbs++;
  770. }