fhci-sched.c 24 KB

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