fhci-sched.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 <asm/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_mod);
  123. clrbits8(&usb->fhci->regs->usb_mod, 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_event, 0xffff);
  133. /* enable the USB controller */
  134. out_8(&usb->fhci->regs->usb_mod, 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_del(node);
  235. list_add_tail(node, list);
  236. }
  237. }
  238. /*
  239. * This function goes through the endpoint list and schedules the
  240. * transactions within this list
  241. */
  242. static int scan_ed_list(struct fhci_usb *usb,
  243. struct list_head *list, enum fhci_tf_mode list_type)
  244. {
  245. static const int frame_part[4] = {
  246. [FHCI_TF_CTRL] = MAX_BYTES_PER_FRAME,
  247. [FHCI_TF_ISO] = (MAX_BYTES_PER_FRAME *
  248. MAX_PERIODIC_FRAME_USAGE) / 100,
  249. [FHCI_TF_BULK] = MAX_BYTES_PER_FRAME,
  250. [FHCI_TF_INTR] = (MAX_BYTES_PER_FRAME *
  251. MAX_PERIODIC_FRAME_USAGE) / 100
  252. };
  253. struct ed *ed;
  254. struct td *td;
  255. int ans = 1;
  256. u32 save_transaction_time = usb->sw_transaction_time;
  257. list_for_each_entry(ed, list, node) {
  258. td = ed->td_head;
  259. if (!td || (td && td->status == USB_TD_INPROGRESS))
  260. continue;
  261. if (ed->state != FHCI_ED_OPER) {
  262. if (ed->state == FHCI_ED_URB_DEL) {
  263. td->status = USB_TD_OK;
  264. fhci_move_td_from_ed_to_done_list(usb, ed);
  265. ed->state = FHCI_ED_SKIP;
  266. }
  267. continue;
  268. }
  269. /*
  270. * if it isn't interrupt pipe or it is not iso pipe and the
  271. * interval time passed
  272. */
  273. if ((list_type == FHCI_TF_INTR || list_type == FHCI_TF_ISO) &&
  274. (((usb->actual_frame->frame_num -
  275. td->start_frame) & 0x7ff) < td->interval))
  276. continue;
  277. if (add_packet(usb, ed, td) < 0)
  278. continue;
  279. /* update time stamps in the TD */
  280. td->start_frame = usb->actual_frame->frame_num;
  281. usb->sw_transaction_time += save_transaction_time;
  282. if (usb->actual_frame->total_bytes >=
  283. usb->max_bytes_per_frame) {
  284. usb->actual_frame->frame_status =
  285. FRAME_DATA_END_TRANSMISSION;
  286. fhci_push_dummy_bd(usb->ep0);
  287. ans = 0;
  288. break;
  289. }
  290. if (usb->actual_frame->total_bytes >= frame_part[list_type])
  291. break;
  292. }
  293. /* be fair to each ED(move list head around) */
  294. move_head_to_tail(list);
  295. usb->sw_transaction_time = save_transaction_time;
  296. return ans;
  297. }
  298. static u32 rotate_frames(struct fhci_usb *usb)
  299. {
  300. struct fhci_hcd *fhci = usb->fhci;
  301. if (!list_empty(&usb->actual_frame->tds_list)) {
  302. if ((((in_be16(&fhci->pram->frame_num) & 0x07ff) -
  303. usb->actual_frame->frame_num) & 0x7ff) > 5)
  304. fhci_flush_actual_frame(usb);
  305. else
  306. return -EINVAL;
  307. }
  308. usb->actual_frame->frame_status = FRAME_IS_PREPARED;
  309. usb->actual_frame->frame_num = in_be16(&fhci->pram->frame_num) & 0x7ff;
  310. usb->actual_frame->total_bytes = 0;
  311. return 0;
  312. }
  313. /*
  314. * This function schedule the USB transaction and will process the
  315. * endpoint in the following order: iso, interrupt, control and bulk.
  316. */
  317. void fhci_schedule_transactions(struct fhci_usb *usb)
  318. {
  319. int left = 1;
  320. if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
  321. if (rotate_frames(usb) != 0)
  322. return;
  323. if (usb->actual_frame->frame_status & FRAME_END_TRANSMISSION)
  324. return;
  325. if (usb->actual_frame->total_bytes == 0) {
  326. /*
  327. * schedule the next available ISO transfer
  328. *or next stage of the ISO transfer
  329. */
  330. scan_ed_list(usb, &usb->hc_list->iso_list, FHCI_TF_ISO);
  331. /*
  332. * schedule the next available interrupt transfer or
  333. * the next stage of the interrupt transfer
  334. */
  335. scan_ed_list(usb, &usb->hc_list->intr_list, FHCI_TF_INTR);
  336. /*
  337. * schedule the next available control transfer
  338. * or the next stage of the control transfer
  339. */
  340. left = scan_ed_list(usb, &usb->hc_list->ctrl_list,
  341. FHCI_TF_CTRL);
  342. }
  343. /*
  344. * schedule the next available bulk transfer or the next stage of the
  345. * bulk transfer
  346. */
  347. if (left > 0)
  348. scan_ed_list(usb, &usb->hc_list->bulk_list, FHCI_TF_BULK);
  349. }
  350. /* Handles SOF interrupt */
  351. static void sof_interrupt(struct fhci_hcd *fhci)
  352. {
  353. struct fhci_usb *usb = fhci->usb_lld;
  354. if ((usb->port_status == FHCI_PORT_DISABLED) &&
  355. (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_CONNECTION) &&
  356. !(usb->vroot_hub->port.wPortChange & USB_PORT_STAT_C_CONNECTION)) {
  357. if (usb->vroot_hub->port.wPortStatus & USB_PORT_STAT_LOW_SPEED)
  358. usb->port_status = FHCI_PORT_LOW;
  359. else
  360. usb->port_status = FHCI_PORT_FULL;
  361. /* Disable IDLE */
  362. usb->saved_msk &= ~USB_E_IDLE_MASK;
  363. out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
  364. }
  365. gtm_set_exact_timer16(fhci->timer, usb->max_frame_usage, false);
  366. fhci_host_transmit_actual_frame(usb);
  367. usb->actual_frame->frame_status = FRAME_IS_TRANSMITTED;
  368. fhci_schedule_transactions(usb);
  369. }
  370. /* Handles device disconnected interrupt on port */
  371. void fhci_device_disconnected_interrupt(struct fhci_hcd *fhci)
  372. {
  373. struct fhci_usb *usb = fhci->usb_lld;
  374. fhci_dbg(fhci, "-> %s\n", __func__);
  375. fhci_usb_disable_interrupt(usb);
  376. clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
  377. usb->port_status = FHCI_PORT_DISABLED;
  378. fhci_stop_sof_timer(fhci);
  379. /* Enable IDLE since we want to know if something comes along */
  380. usb->saved_msk |= USB_E_IDLE_MASK;
  381. out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
  382. usb->vroot_hub->port.wPortStatus &= ~USB_PORT_STAT_CONNECTION;
  383. usb->vroot_hub->port.wPortChange |= USB_PORT_STAT_C_CONNECTION;
  384. usb->max_bytes_per_frame = 0;
  385. fhci_usb_enable_interrupt(usb);
  386. fhci_dbg(fhci, "<- %s\n", __func__);
  387. }
  388. /* detect a new device connected on the USB port */
  389. void fhci_device_connected_interrupt(struct fhci_hcd *fhci)
  390. {
  391. struct fhci_usb *usb = fhci->usb_lld;
  392. int state;
  393. int ret;
  394. fhci_dbg(fhci, "-> %s\n", __func__);
  395. fhci_usb_disable_interrupt(usb);
  396. state = fhci_ioports_check_bus_state(fhci);
  397. /* low-speed device was connected to the USB port */
  398. if (state == 1) {
  399. ret = qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
  400. if (ret) {
  401. fhci_warn(fhci, "Low-Speed device is not supported, "
  402. "try use BRGx\n");
  403. goto out;
  404. }
  405. usb->port_status = FHCI_PORT_LOW;
  406. setbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
  407. usb->vroot_hub->port.wPortStatus |=
  408. (USB_PORT_STAT_LOW_SPEED |
  409. USB_PORT_STAT_CONNECTION);
  410. usb->vroot_hub->port.wPortChange |=
  411. USB_PORT_STAT_C_CONNECTION;
  412. usb->max_bytes_per_frame =
  413. (MAX_BYTES_PER_FRAME >> 3) - 7;
  414. fhci_port_enable(usb);
  415. } else if (state == 2) {
  416. ret = qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
  417. if (ret) {
  418. fhci_warn(fhci, "Full-Speed device is not supported, "
  419. "try use CLKx\n");
  420. goto out;
  421. }
  422. usb->port_status = FHCI_PORT_FULL;
  423. clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_LSS);
  424. usb->vroot_hub->port.wPortStatus &=
  425. ~USB_PORT_STAT_LOW_SPEED;
  426. usb->vroot_hub->port.wPortStatus |=
  427. USB_PORT_STAT_CONNECTION;
  428. usb->vroot_hub->port.wPortChange |=
  429. USB_PORT_STAT_C_CONNECTION;
  430. usb->max_bytes_per_frame = (MAX_BYTES_PER_FRAME - 15);
  431. fhci_port_enable(usb);
  432. }
  433. out:
  434. fhci_usb_enable_interrupt(usb);
  435. fhci_dbg(fhci, "<- %s\n", __func__);
  436. }
  437. irqreturn_t fhci_frame_limit_timer_irq(int irq, void *_hcd)
  438. {
  439. struct usb_hcd *hcd = _hcd;
  440. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  441. struct fhci_usb *usb = fhci->usb_lld;
  442. spin_lock(&fhci->lock);
  443. gtm_set_exact_timer16(fhci->timer, 1000, false);
  444. if (usb->actual_frame->frame_status == FRAME_IS_TRANSMITTED) {
  445. usb->actual_frame->frame_status = FRAME_TIMER_END_TRANSMISSION;
  446. fhci_push_dummy_bd(usb->ep0);
  447. }
  448. fhci_schedule_transactions(usb);
  449. spin_unlock(&fhci->lock);
  450. return IRQ_HANDLED;
  451. }
  452. /* Cancel transmission on the USB endpoint */
  453. static void abort_transmission(struct fhci_usb *usb)
  454. {
  455. fhci_dbg(usb->fhci, "-> %s\n", __func__);
  456. /* issue stop Tx command */
  457. qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
  458. /* flush Tx FIFOs */
  459. out_8(&usb->fhci->regs->usb_comm, USB_CMD_FLUSH_FIFO | EP_ZERO);
  460. udelay(1000);
  461. /* reset Tx BDs */
  462. fhci_flush_bds(usb);
  463. /* issue restart Tx command */
  464. qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB, EP_ZERO, 0);
  465. fhci_dbg(usb->fhci, "<- %s\n", __func__);
  466. }
  467. irqreturn_t fhci_irq(struct usb_hcd *hcd)
  468. {
  469. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  470. struct fhci_usb *usb;
  471. u16 usb_er = 0;
  472. unsigned long flags;
  473. spin_lock_irqsave(&fhci->lock, flags);
  474. usb = fhci->usb_lld;
  475. usb_er |= in_be16(&usb->fhci->regs->usb_event) &
  476. in_be16(&usb->fhci->regs->usb_mask);
  477. /* clear event bits for next time */
  478. out_be16(&usb->fhci->regs->usb_event, usb_er);
  479. fhci_dbg_isr(fhci, usb_er);
  480. if (usb_er & USB_E_RESET_MASK) {
  481. if ((usb->port_status == FHCI_PORT_FULL) ||
  482. (usb->port_status == FHCI_PORT_LOW)) {
  483. fhci_device_disconnected_interrupt(fhci);
  484. usb_er &= ~USB_E_IDLE_MASK;
  485. } else if (usb->port_status == FHCI_PORT_WAITING) {
  486. usb->port_status = FHCI_PORT_DISCONNECTING;
  487. /* Turn on IDLE since we want to disconnect */
  488. usb->saved_msk |= USB_E_IDLE_MASK;
  489. out_be16(&usb->fhci->regs->usb_event,
  490. usb->saved_msk);
  491. } else if (usb->port_status == FHCI_PORT_DISABLED) {
  492. if (fhci_ioports_check_bus_state(fhci) == 1)
  493. fhci_device_connected_interrupt(fhci);
  494. }
  495. usb_er &= ~USB_E_RESET_MASK;
  496. }
  497. if (usb_er & USB_E_MSF_MASK) {
  498. abort_transmission(fhci->usb_lld);
  499. usb_er &= ~USB_E_MSF_MASK;
  500. }
  501. if (usb_er & (USB_E_SOF_MASK | USB_E_SFT_MASK)) {
  502. sof_interrupt(fhci);
  503. usb_er &= ~(USB_E_SOF_MASK | USB_E_SFT_MASK);
  504. }
  505. if (usb_er & USB_E_TXB_MASK) {
  506. fhci_tx_conf_interrupt(fhci->usb_lld);
  507. usb_er &= ~USB_E_TXB_MASK;
  508. }
  509. if (usb_er & USB_E_TXE1_MASK) {
  510. fhci_tx_conf_interrupt(fhci->usb_lld);
  511. usb_er &= ~USB_E_TXE1_MASK;
  512. }
  513. if (usb_er & USB_E_IDLE_MASK) {
  514. if (usb->port_status == FHCI_PORT_DISABLED) {
  515. usb_er &= ~USB_E_RESET_MASK;
  516. fhci_device_connected_interrupt(fhci);
  517. } else if (usb->port_status ==
  518. FHCI_PORT_DISCONNECTING) {
  519. /* XXX usb->port_status = FHCI_PORT_WAITING; */
  520. /* Disable IDLE */
  521. usb->saved_msk &= ~USB_E_IDLE_MASK;
  522. out_be16(&usb->fhci->regs->usb_mask,
  523. usb->saved_msk);
  524. } else {
  525. fhci_dbg_isr(fhci, -1);
  526. }
  527. usb_er &= ~USB_E_IDLE_MASK;
  528. }
  529. spin_unlock_irqrestore(&fhci->lock, flags);
  530. return IRQ_HANDLED;
  531. }
  532. /*
  533. * Process normal completions(error or success) and clean the schedule.
  534. *
  535. * This is the main path for handing urbs back to drivers. The only other patth
  536. * is process_del_list(),which unlinks URBs by scanning EDs,instead of scanning
  537. * the (re-reversed) done list as this does.
  538. */
  539. static void process_done_list(unsigned long data)
  540. {
  541. struct urb *urb;
  542. struct ed *ed;
  543. struct td *td;
  544. struct urb_priv *urb_priv;
  545. struct fhci_hcd *fhci = (struct fhci_hcd *)data;
  546. disable_irq(fhci->timer->irq);
  547. disable_irq(fhci_to_hcd(fhci)->irq);
  548. spin_lock(&fhci->lock);
  549. td = fhci_remove_td_from_done_list(fhci->hc_list);
  550. while (td != NULL) {
  551. urb = td->urb;
  552. urb_priv = urb->hcpriv;
  553. ed = td->ed;
  554. /* update URB's length and status from TD */
  555. fhci_done_td(urb, td);
  556. urb_priv->tds_cnt++;
  557. /*
  558. * if all this urb's TDs are done, call complete()
  559. * Interrupt transfers are the onley special case:
  560. * they are reissued,until "deleted" by usb_unlink_urb
  561. * (real work done in a SOF intr, by process_del_list)
  562. */
  563. if (urb_priv->tds_cnt == urb_priv->num_of_tds) {
  564. fhci_urb_complete_free(fhci, urb);
  565. } else if (urb_priv->state == URB_DEL &&
  566. ed->state == FHCI_ED_SKIP) {
  567. fhci_del_ed_list(fhci, ed);
  568. ed->state = FHCI_ED_OPER;
  569. } else if (ed->state == FHCI_ED_HALTED) {
  570. urb_priv->state = URB_DEL;
  571. ed->state = FHCI_ED_URB_DEL;
  572. fhci_del_ed_list(fhci, ed);
  573. ed->state = FHCI_ED_OPER;
  574. }
  575. td = fhci_remove_td_from_done_list(fhci->hc_list);
  576. }
  577. spin_unlock(&fhci->lock);
  578. enable_irq(fhci->timer->irq);
  579. enable_irq(fhci_to_hcd(fhci)->irq);
  580. }
  581. DECLARE_TASKLET(fhci_tasklet, process_done_list, 0);
  582. /* transfer complted callback */
  583. u32 fhci_transfer_confirm_callback(struct fhci_hcd *fhci)
  584. {
  585. if (!fhci->process_done_task->state)
  586. tasklet_schedule(fhci->process_done_task);
  587. return 0;
  588. }
  589. /*
  590. * adds urb to the endpoint descriptor list
  591. * arguments:
  592. * fhci data structure for the Low level host controller
  593. * ep USB Host endpoint data structure
  594. * urb USB request block data structure
  595. */
  596. void fhci_queue_urb(struct fhci_hcd *fhci, struct urb *urb)
  597. {
  598. struct ed *ed = urb->ep->hcpriv;
  599. struct urb_priv *urb_priv = urb->hcpriv;
  600. u32 data_len = urb->transfer_buffer_length;
  601. int urb_state = 0;
  602. int toggle = 0;
  603. struct td *td;
  604. u8 *data;
  605. u16 cnt = 0;
  606. if (ed == NULL) {
  607. ed = fhci_get_empty_ed(fhci);
  608. ed->dev_addr = usb_pipedevice(urb->pipe);
  609. ed->ep_addr = usb_pipeendpoint(urb->pipe);
  610. switch (usb_pipetype(urb->pipe)) {
  611. case PIPE_CONTROL:
  612. ed->mode = FHCI_TF_CTRL;
  613. break;
  614. case PIPE_BULK:
  615. ed->mode = FHCI_TF_BULK;
  616. break;
  617. case PIPE_INTERRUPT:
  618. ed->mode = FHCI_TF_INTR;
  619. break;
  620. case PIPE_ISOCHRONOUS:
  621. ed->mode = FHCI_TF_ISO;
  622. break;
  623. default:
  624. break;
  625. }
  626. ed->speed = (urb->dev->speed == USB_SPEED_LOW) ?
  627. FHCI_LOW_SPEED : FHCI_FULL_SPEED;
  628. ed->max_pkt_size = usb_maxpacket(urb->dev,
  629. urb->pipe, usb_pipeout(urb->pipe));
  630. urb->ep->hcpriv = ed;
  631. fhci_dbg(fhci, "new ep speed=%d max_pkt_size=%d\n",
  632. ed->speed, ed->max_pkt_size);
  633. }
  634. /* for ISO transfer calculate start frame index */
  635. if (ed->mode == FHCI_TF_ISO && urb->transfer_flags & URB_ISO_ASAP)
  636. urb->start_frame = ed->td_head ? ed->last_iso + 1 :
  637. get_frame_num(fhci);
  638. /*
  639. * OHCI handles the DATA toggle itself,we just use the USB
  640. * toggle bits
  641. */
  642. if (usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  643. usb_pipeout(urb->pipe)))
  644. toggle = USB_TD_TOGGLE_CARRY;
  645. else {
  646. toggle = USB_TD_TOGGLE_DATA0;
  647. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  648. usb_pipeout(urb->pipe), 1);
  649. }
  650. urb_priv->tds_cnt = 0;
  651. urb_priv->ed = ed;
  652. if (data_len > 0)
  653. data = urb->transfer_buffer;
  654. else
  655. data = NULL;
  656. switch (ed->mode) {
  657. case FHCI_TF_BULK:
  658. if (urb->transfer_flags & URB_ZERO_PACKET &&
  659. urb->transfer_buffer_length > 0 &&
  660. ((urb->transfer_buffer_length %
  661. usb_maxpacket(urb->dev, urb->pipe,
  662. usb_pipeout(urb->pipe))) == 0))
  663. urb_state = US_BULK0;
  664. while (data_len > 4096) {
  665. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  666. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  667. FHCI_TA_IN,
  668. cnt ? USB_TD_TOGGLE_CARRY :
  669. toggle,
  670. data, 4096, 0, 0, true);
  671. data += 4096;
  672. data_len -= 4096;
  673. cnt++;
  674. }
  675. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  676. usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
  677. cnt ? USB_TD_TOGGLE_CARRY : toggle,
  678. data, data_len, 0, 0, true);
  679. cnt++;
  680. if (urb->transfer_flags & URB_ZERO_PACKET &&
  681. cnt < urb_priv->num_of_tds) {
  682. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  683. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  684. FHCI_TA_IN,
  685. USB_TD_TOGGLE_CARRY, NULL, 0, 0, 0, true);
  686. cnt++;
  687. }
  688. break;
  689. case FHCI_TF_INTR:
  690. urb->start_frame = get_frame_num(fhci) + 1;
  691. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  692. usb_pipeout(urb->pipe) ? FHCI_TA_OUT : FHCI_TA_IN,
  693. USB_TD_TOGGLE_DATA0, data, data_len,
  694. urb->interval, urb->start_frame, true);
  695. break;
  696. case FHCI_TF_CTRL:
  697. ed->dev_addr = usb_pipedevice(urb->pipe);
  698. ed->max_pkt_size = usb_maxpacket(urb->dev, urb->pipe,
  699. usb_pipeout(urb->pipe));
  700. /* setup stage */
  701. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++, FHCI_TA_SETUP,
  702. USB_TD_TOGGLE_DATA0, urb->setup_packet, 8, 0, 0, true);
  703. /* data stage */
  704. if (data_len > 0) {
  705. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  706. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  707. FHCI_TA_IN,
  708. USB_TD_TOGGLE_DATA1, data, data_len, 0, 0,
  709. true);
  710. }
  711. /* status stage */
  712. if (data_len > 0)
  713. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  714. (usb_pipeout(urb->pipe) ? FHCI_TA_IN :
  715. FHCI_TA_OUT),
  716. USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
  717. else
  718. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt++,
  719. FHCI_TA_IN,
  720. USB_TD_TOGGLE_DATA1, data, 0, 0, 0, true);
  721. urb_state = US_CTRL_SETUP;
  722. break;
  723. case FHCI_TF_ISO:
  724. for (cnt = 0; cnt < urb->number_of_packets; cnt++) {
  725. u16 frame = urb->start_frame;
  726. /*
  727. * FIXME scheduling should handle frame counter
  728. * roll-around ... exotic case (and OHCI has
  729. * a 2^16 iso range, vs other HCs max of 2^10)
  730. */
  731. frame += cnt * urb->interval;
  732. frame &= 0x07ff;
  733. td = fhci_td_fill(fhci, urb, urb_priv, ed, cnt,
  734. usb_pipeout(urb->pipe) ? FHCI_TA_OUT :
  735. FHCI_TA_IN,
  736. USB_TD_TOGGLE_DATA0,
  737. data + urb->iso_frame_desc[cnt].offset,
  738. urb->iso_frame_desc[cnt].length,
  739. urb->interval, frame, true);
  740. }
  741. break;
  742. default:
  743. break;
  744. }
  745. /*
  746. * set the state of URB
  747. * control pipe:3 states -- setup,data,status
  748. * interrupt and bulk pipe:1 state -- data
  749. */
  750. urb->pipe &= ~0x1f;
  751. urb->pipe |= urb_state & 0x1f;
  752. urb_priv->state = URB_INPROGRESS;
  753. if (!ed->td_head) {
  754. ed->state = FHCI_ED_OPER;
  755. switch (ed->mode) {
  756. case FHCI_TF_CTRL:
  757. list_add(&ed->node, &fhci->hc_list->ctrl_list);
  758. break;
  759. case FHCI_TF_BULK:
  760. list_add(&ed->node, &fhci->hc_list->bulk_list);
  761. break;
  762. case FHCI_TF_INTR:
  763. list_add(&ed->node, &fhci->hc_list->intr_list);
  764. break;
  765. case FHCI_TF_ISO:
  766. list_add(&ed->node, &fhci->hc_list->iso_list);
  767. break;
  768. default:
  769. break;
  770. }
  771. }
  772. fhci_add_tds_to_ed(ed, urb_priv->tds, urb_priv->num_of_tds);
  773. fhci->active_urbs++;
  774. }