usb-uhci.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:84k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* 
  2.  * Universal Host Controller Interface driver for USB (take II).
  3.  *
  4.  * (c) 1999-2001 Georg Acher, acher@in.tum.de (executive slave) (base guitar)
  5.  *               Deti Fliegl, deti@fliegl.de (executive slave) (lead voice)
  6.  *               Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader)
  7.  *               Roman Weissgaerber, weissg@vienna.at (virt root hub) (studio porter)
  8.  * (c) 2000      Yggdrasil Computing, Inc. (port of new PCI interface support
  9.  *               from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
  10.  * (C) 2000      David Brownell, david-b@pacbell.net (usb-ohci.c)
  11.  *          
  12.  * HW-initalization based on material of
  13.  *
  14.  * (C) Copyright 1999 Linus Torvalds
  15.  * (C) Copyright 1999 Johannes Erdfelt
  16.  * (C) Copyright 1999 Randy Dunlap
  17.  * (C) Copyright 1999 Gregory P. Smith
  18.  *
  19.  * $Id: usb-uhci.c,v 1.275 2002/01/19 20:57:33 acher Exp $
  20.  */
  21. #include <linux/config.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/ioport.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/errno.h>
  31. #include <linux/unistd.h>
  32. #include <linux/interrupt.h> /* for in_interrupt() */
  33. #include <linux/init.h>
  34. #include <linux/version.h>
  35. #include <linux/pm.h>
  36. #include <linux/timer.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. /* This enables more detailed sanity checks in submit_iso */
  42. //#define ISO_SANITY_CHECK
  43. /* This enables debug printks */
  44. #define DEBUG
  45. /* This enables all symbols to be exported, to ease debugging oopses */
  46. //#define DEBUG_SYMBOLS
  47. /* This enables an extra UHCI slab for memory debugging */
  48. #define DEBUG_SLAB
  49. #define VERSTR "$Revision: 1.275 $ time " __TIME__ " " __DATE__
  50. #include <linux/usb.h>
  51. #include "usb-uhci.h"
  52. #include "usb-uhci-debug.h"
  53. #include "hcd.h"
  54. /*
  55.  * Version Information
  56.  */
  57. #define DRIVER_VERSION "v1.275"
  58. #define DRIVER_AUTHOR "Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber"
  59. #define DRIVER_DESC "USB Universal Host Controller Interface driver"
  60. #undef DEBUG
  61. #undef dbg
  62. #define dbg(format, arg...) do {} while (0)
  63. #define DEBUG_SYMBOLS
  64. #ifdef DEBUG_SYMBOLS
  65. #define _static
  66. #ifndef EXPORT_SYMTAB
  67. #define EXPORT_SYMTAB
  68. #endif
  69. #else
  70. #define _static static
  71. #endif
  72. #define queue_dbg dbg //err
  73. #define async_dbg dbg //err
  74. #ifdef DEBUG_SLAB
  75. static kmem_cache_t *urb_priv_kmem;
  76. #endif
  77. #define SLAB_FLAG     (in_interrupt () || current->state != TASK_RUNNING ? SLAB_ATOMIC : SLAB_KERNEL)
  78. #define KMALLOC_FLAG  (in_interrupt () || current->state != TASK_RUNNING ? GFP_ATOMIC : GFP_KERNEL)
  79. /* CONFIG_USB_UHCI_HIGH_BANDWITH turns on Full Speed Bandwidth
  80.  * Reclamation: feature that puts loop on descriptor loop when
  81.  * there's some transfer going on. With FSBR, USB performance
  82.  * is optimal, but PCI can be slowed down up-to 5 times, slowing down
  83.  * system performance (eg. framebuffer devices).
  84.  */
  85. #define CONFIG_USB_UHCI_HIGH_BANDWIDTH
  86. /* *_DEPTH_FIRST puts descriptor in depth-first mode. This has
  87.  * somehow similar effect to FSBR (higher speed), but does not
  88.  * slow PCI down. OTOH USB performace is slightly slower than
  89.  * in FSBR case and single device could hog whole USB, starving
  90.  * other devices.
  91.  */
  92. #define USE_CTRL_DEPTH_FIRST 0  // 0: Breadth first, 1: Depth first
  93. #define USE_BULK_DEPTH_FIRST 0  // 0: Breadth first, 1: Depth first
  94. /* Turning off both CONFIG_USB_UHCI_HIGH_BANDWITH and *_DEPTH_FIRST
  95.  * will lead to <64KB/sec performance over USB for bulk transfers targeting
  96.  * one device's endpoint. You probably do not want to do that.
  97.  */
  98. // stop bandwidth reclamation after (roughly) 50ms
  99. #define IDLE_TIMEOUT  (HZ/20)
  100. // Suppress HC interrupt error messages for 5s
  101. #define ERROR_SUPPRESSION_TIME (HZ*5)
  102. _static int rh_submit_urb (struct urb *urb);
  103. _static int rh_unlink_urb (struct urb *urb);
  104. _static int delete_qh (uhci_t *s, uhci_desc_t *qh);
  105. _static int process_transfer (uhci_t *s, struct urb *urb, int mode);
  106. _static int process_interrupt (uhci_t *s, struct urb *urb);
  107. _static int process_iso (uhci_t *s, struct urb *urb, int force);
  108. // How much URBs with ->next are walked
  109. #define MAX_NEXT_COUNT 2048
  110. static uhci_t *devs = NULL;
  111. /* used by userspace UHCI data structure dumper */
  112. uhci_t **uhci_devices = &devs;
  113. /*-------------------------------------------------------------------*/
  114. // Cleans up collected QHs, but not more than 100 in one go
  115. void clean_descs(uhci_t *s, int force)
  116. {
  117. struct list_head *q;
  118. uhci_desc_t *qh;
  119. int now=UHCI_GET_CURRENT_FRAME(s), n=0;
  120. q=s->free_desc.prev;
  121. while (q != &s->free_desc && (force || n<100)) {
  122. qh = list_entry (q, uhci_desc_t, horizontal);
  123. q=qh->horizontal.prev;
  124. if ((qh->last_used!=now) || force)
  125. delete_qh(s,qh);
  126. n++;
  127. }
  128. }
  129. /*-------------------------------------------------------------------*/
  130. _static void uhci_switch_timer_int(uhci_t *s)
  131. {
  132. if (!list_empty(&s->urb_unlinked))
  133. set_td_ioc(s->td1ms);
  134. else
  135. clr_td_ioc(s->td1ms);
  136. if (s->timeout_urbs)
  137. set_td_ioc(s->td32ms);
  138. else
  139. clr_td_ioc(s->td32ms);
  140. wmb();
  141. }
  142. /*-------------------------------------------------------------------*/
  143. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  144. _static void enable_desc_loop(uhci_t *s, struct urb *urb)
  145. {
  146. unsigned long flags;
  147. if (urb->transfer_flags & USB_NO_FSBR)
  148. return;
  149. spin_lock_irqsave (&s->qh_lock, flags);
  150. s->chain_end->hw.qh.head&=cpu_to_le32(~UHCI_PTR_TERM);
  151. mb();
  152. s->loop_usage++;
  153. ((urb_priv_t*)urb->hcpriv)->use_loop=1;
  154. spin_unlock_irqrestore (&s->qh_lock, flags);
  155. }
  156. /*-------------------------------------------------------------------*/
  157. _static void disable_desc_loop(uhci_t *s, struct urb *urb)
  158. {
  159. unsigned long flags;
  160. if (urb->transfer_flags & USB_NO_FSBR)
  161. return;
  162. spin_lock_irqsave (&s->qh_lock, flags);
  163. if (((urb_priv_t*)urb->hcpriv)->use_loop) {
  164. s->loop_usage--;
  165. if (!s->loop_usage) {
  166. s->chain_end->hw.qh.head|=cpu_to_le32(UHCI_PTR_TERM);
  167. mb();
  168. }
  169. ((urb_priv_t*)urb->hcpriv)->use_loop=0;
  170. }
  171. spin_unlock_irqrestore (&s->qh_lock, flags);
  172. }
  173. #endif
  174. /*-------------------------------------------------------------------*/
  175. _static void queue_urb_unlocked (uhci_t *s, struct urb *urb)
  176. {
  177. struct list_head *p=&urb->urb_list;
  178. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  179. {
  180. int type;
  181. type=usb_pipetype (urb->pipe);
  182. if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
  183. enable_desc_loop(s, urb);
  184. }
  185. #endif
  186. urb->status = -EINPROGRESS;
  187. ((urb_priv_t*)urb->hcpriv)->started=jiffies;
  188. list_add (p, &s->urb_list);
  189. if (urb->timeout)
  190. s->timeout_urbs++;
  191. uhci_switch_timer_int(s);
  192. }
  193. /*-------------------------------------------------------------------*/
  194. _static void queue_urb (uhci_t *s, struct urb *urb)
  195. {
  196. unsigned long flags=0;
  197. spin_lock_irqsave (&s->urb_list_lock, flags);
  198. queue_urb_unlocked(s,urb);
  199. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  200. }
  201. /*-------------------------------------------------------------------*/
  202. _static void dequeue_urb (uhci_t *s, struct urb *urb)
  203. {
  204. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  205. int type;
  206. type=usb_pipetype (urb->pipe);
  207. if ((type == PIPE_BULK) || (type == PIPE_CONTROL))
  208. disable_desc_loop(s, urb);
  209. #endif
  210. list_del (&urb->urb_list);
  211. if (urb->timeout && s->timeout_urbs)
  212. s->timeout_urbs--;
  213. }
  214. /*-------------------------------------------------------------------*/
  215. _static int alloc_td (uhci_t *s, uhci_desc_t ** new, int flags)
  216. {
  217. dma_addr_t dma_handle;
  218. *new = pci_pool_alloc(s->desc_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
  219. if (!*new)
  220. return -ENOMEM;
  221. memset (*new, 0, sizeof (uhci_desc_t));
  222. (*new)->dma_addr = dma_handle;
  223. set_td_link((*new), UHCI_PTR_TERM | (flags & UHCI_PTR_BITS)); // last by default
  224. (*new)->type = TD_TYPE;
  225. mb();
  226. INIT_LIST_HEAD (&(*new)->vertical);
  227. INIT_LIST_HEAD (&(*new)->horizontal);
  228. return 0;
  229. }
  230. /*-------------------------------------------------------------------*/
  231. // append a qh to td.link physically, the SW linkage is not affected
  232. _static void append_qh(uhci_t *s, uhci_desc_t *td, uhci_desc_t* qh, int  flags)
  233. {
  234. unsigned long xxx;
  235. spin_lock_irqsave (&s->td_lock, xxx);
  236. set_td_link(td, qh->dma_addr | (flags & UHCI_PTR_DEPTH) | UHCI_PTR_QH);
  237.        
  238. mb();
  239. spin_unlock_irqrestore (&s->td_lock, xxx);
  240. }
  241. /*-------------------------------------------------------------------*/
  242. /* insert td at last position in td-list of qh (vertical) */
  243. _static int insert_td (uhci_t *s, uhci_desc_t *qh, uhci_desc_t* new, int flags)
  244. {
  245. uhci_desc_t *prev;
  246. unsigned long xxx;
  247. spin_lock_irqsave (&s->td_lock, xxx);
  248. list_add_tail (&new->vertical, &qh->vertical);
  249. prev = list_entry (new->vertical.prev, uhci_desc_t, vertical);
  250. if (qh == prev ) {
  251. // virgin qh without any tds
  252. set_qh_element(qh, new->dma_addr | UHCI_PTR_TERM);
  253. }
  254. else {
  255. // already tds inserted, implicitely remove TERM bit of prev
  256. set_td_link(prev, new->dma_addr | (flags & UHCI_PTR_DEPTH));
  257. }
  258. mb();
  259. spin_unlock_irqrestore (&s->td_lock, xxx);
  260. return 0;
  261. }
  262. /*-------------------------------------------------------------------*/
  263. /* insert new_td after td (horizontal) */
  264. _static int insert_td_horizontal (uhci_t *s, uhci_desc_t *td, uhci_desc_t* new)
  265. {
  266. uhci_desc_t *next;
  267. unsigned long flags;
  268. spin_lock_irqsave (&s->td_lock, flags);
  269. next = list_entry (td->horizontal.next, uhci_desc_t, horizontal);
  270. list_add (&new->horizontal, &td->horizontal);
  271. new->hw.td.link = td->hw.td.link;
  272. set_td_link(td, new->dma_addr);
  273. mb();
  274. spin_unlock_irqrestore (&s->td_lock, flags);
  275. return 0;
  276. }
  277. /*-------------------------------------------------------------------*/
  278. _static int unlink_td (uhci_t *s, uhci_desc_t *element, int phys_unlink)
  279. {
  280. uhci_desc_t *next, *prev;
  281. int dir = 0;
  282. unsigned long flags;
  283. spin_lock_irqsave (&s->td_lock, flags);
  284. next = list_entry (element->vertical.next, uhci_desc_t, vertical);
  285. if (next == element) {
  286. dir = 1;
  287. prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
  288. }
  289. else 
  290. prev = list_entry (element->vertical.prev, uhci_desc_t, vertical);
  291. if (phys_unlink) {
  292. // really remove HW linking
  293. if (prev->type == TD_TYPE)
  294. prev->hw.td.link = element->hw.td.link;
  295. else
  296. prev->hw.qh.element = element->hw.td.link;
  297. }
  298. mb ();
  299. if (dir == 0)
  300. list_del (&element->vertical);
  301. else
  302. list_del (&element->horizontal);
  303. spin_unlock_irqrestore (&s->td_lock, flags);
  304. return 0;
  305. }
  306. /*-------------------------------------------------------------------*/
  307. _static int delete_desc (uhci_t *s, uhci_desc_t *element)
  308. {
  309. pci_pool_free(s->desc_pool, element, element->dma_addr);
  310. return 0;
  311. }
  312. /*-------------------------------------------------------------------*/
  313. // Allocates qh element
  314. _static int alloc_qh (uhci_t *s, uhci_desc_t ** new)
  315. {
  316. dma_addr_t dma_handle;
  317. *new = pci_pool_alloc(s->desc_pool, GFP_DMA | GFP_ATOMIC, &dma_handle);
  318. if (!*new)
  319. return -ENOMEM;
  320. memset (*new, 0, sizeof (uhci_desc_t));
  321. (*new)->dma_addr = dma_handle;
  322. set_qh_head(*new, UHCI_PTR_TERM);
  323. set_qh_element(*new, UHCI_PTR_TERM);
  324. (*new)->type = QH_TYPE;
  325. mb();
  326. INIT_LIST_HEAD (&(*new)->horizontal);
  327. INIT_LIST_HEAD (&(*new)->vertical);
  328. dbg("Allocated qh @ %p", *new);
  329. return 0;
  330. }
  331. /*-------------------------------------------------------------------*/
  332. // inserts new qh before/after the qh at pos
  333. // flags: 0: insert before pos, 1: insert after pos (for low speed transfers)
  334. _static int insert_qh (uhci_t *s, uhci_desc_t *pos, uhci_desc_t *new, int order)
  335. {
  336. uhci_desc_t *old;
  337. unsigned long flags;
  338. spin_lock_irqsave (&s->qh_lock, flags);
  339. if (!order) {
  340. // (OLD) (POS) -> (OLD) (NEW) (POS)
  341. old = list_entry (pos->horizontal.prev, uhci_desc_t, horizontal);
  342. list_add_tail (&new->horizontal, &pos->horizontal);
  343. set_qh_head(new, MAKE_QH_ADDR (pos)) ;
  344. if (!(old->hw.qh.head & cpu_to_le32(UHCI_PTR_TERM)))
  345. set_qh_head(old, MAKE_QH_ADDR (new)) ;
  346. }
  347. else {
  348. // (POS) (OLD) -> (POS) (NEW) (OLD)
  349. old = list_entry (pos->horizontal.next, uhci_desc_t, horizontal);
  350. list_add (&new->horizontal, &pos->horizontal);
  351. set_qh_head(new, MAKE_QH_ADDR (old));
  352. set_qh_head(pos, MAKE_QH_ADDR (new)) ;
  353. }
  354. mb ();
  355. spin_unlock_irqrestore (&s->qh_lock, flags);
  356. return 0;
  357. }
  358. /*-------------------------------------------------------------------*/
  359. _static int unlink_qh (uhci_t *s, uhci_desc_t *element)
  360. {
  361. uhci_desc_t  *prev;
  362. unsigned long flags;
  363. spin_lock_irqsave (&s->qh_lock, flags);
  364. prev = list_entry (element->horizontal.prev, uhci_desc_t, horizontal);
  365. prev->hw.qh.head = element->hw.qh.head;
  366. dbg("unlink qh %p, pqh %p, nxqh %p, to %08x", element, prev, 
  367.     list_entry (element->horizontal.next, uhci_desc_t, horizontal),le32_to_cpu(element->hw.qh.head) &~15);
  368. list_del(&element->horizontal);
  369. mb ();
  370. spin_unlock_irqrestore (&s->qh_lock, flags);
  371. return 0;
  372. }
  373. /*-------------------------------------------------------------------*/
  374. _static int delete_qh (uhci_t *s, uhci_desc_t *qh)
  375. {
  376. uhci_desc_t *td;
  377. struct list_head *p;
  378. list_del (&qh->horizontal);
  379. while ((p = qh->vertical.next) != &qh->vertical) {
  380. td = list_entry (p, uhci_desc_t, vertical);
  381. dbg("unlink td @ %p",td);
  382. unlink_td (s, td, 0); // no physical unlink
  383. delete_desc (s, td);
  384. }
  385. delete_desc (s, qh);
  386. return 0;
  387. }
  388. /*-------------------------------------------------------------------*/
  389. _static void clean_td_chain (uhci_t *s, uhci_desc_t *td)
  390. {
  391. struct list_head *p;
  392. uhci_desc_t *td1;
  393. if (!td)
  394. return;
  395. while ((p = td->horizontal.next) != &td->horizontal) {
  396. td1 = list_entry (p, uhci_desc_t, horizontal);
  397. delete_desc (s, td1);
  398. }
  399. delete_desc (s, td);
  400. }
  401. /*-------------------------------------------------------------------*/
  402. _static void fill_td (uhci_desc_t *td, int status, int info, __u32 buffer)
  403. {
  404. td->hw.td.status = cpu_to_le32(status);
  405. td->hw.td.info = cpu_to_le32(info);
  406. td->hw.td.buffer = cpu_to_le32(buffer);
  407. }
  408. /*-------------------------------------------------------------------*/
  409. // Removes ALL qhs in chain (paranoia!)
  410. _static void cleanup_skel (uhci_t *s)
  411. {
  412. unsigned int n;
  413. uhci_desc_t *td;
  414. dbg("cleanup_skel");
  415. clean_descs(s,1);
  416. if (s->td32ms) {
  417. unlink_td(s,s->td32ms,1);
  418. delete_desc(s, s->td32ms);
  419. }
  420. for (n = 0; n < 8; n++) {
  421. td = s->int_chain[n];
  422. clean_td_chain (s, td);
  423. }
  424. if (s->iso_td) {
  425. for (n = 0; n < 1024; n++) {
  426. td = s->iso_td[n];
  427. clean_td_chain (s, td);
  428. }
  429. kfree (s->iso_td);
  430. }
  431. if (s->framelist)
  432. pci_free_consistent(s->uhci_pci, PAGE_SIZE,
  433.     s->framelist, s->framelist_dma);
  434. if (s->control_chain) {
  435. // completed init_skel?
  436. struct list_head *p;
  437. uhci_desc_t *qh, *qh1;
  438. qh = s->control_chain;
  439. while ((p = qh->horizontal.next) != &qh->horizontal) {
  440. qh1 = list_entry (p, uhci_desc_t, horizontal);
  441. delete_qh (s, qh1);
  442. }
  443. delete_qh (s, qh);
  444. }
  445. else {
  446. if (s->ls_control_chain)
  447. delete_desc (s, s->ls_control_chain);
  448. if (s->control_chain)
  449. delete_desc (s, s->control_chain);
  450. if (s->bulk_chain)
  451. delete_desc (s, s->bulk_chain);
  452. if (s->chain_end)
  453. delete_desc (s, s->chain_end);
  454. }
  455. if (s->desc_pool) {
  456. pci_pool_destroy(s->desc_pool);
  457. s->desc_pool = NULL;
  458. }
  459. dbg("cleanup_skel finished");
  460. }
  461. /*-------------------------------------------------------------------*/
  462. // allocates framelist and qh-skeletons
  463. // only HW-links provide continous linking, SW-links stay in their domain (ISO/INT)
  464. _static int init_skel (uhci_t *s)
  465. {
  466. int n, ret;
  467. uhci_desc_t *qh, *td;
  468. dbg("init_skel");
  469. s->framelist = pci_alloc_consistent(s->uhci_pci, PAGE_SIZE,
  470.     &s->framelist_dma);
  471. if (!s->framelist)
  472. return -ENOMEM;
  473. memset (s->framelist, 0, 4096);
  474. dbg("creating descriptor pci_pool");
  475. s->desc_pool = pci_pool_create("uhci_desc", s->uhci_pci,
  476.        sizeof(uhci_desc_t), 16, 0,
  477.        GFP_DMA | GFP_ATOMIC);
  478. if (!s->desc_pool)
  479. goto init_skel_cleanup;
  480. dbg("allocating iso desc pointer list");
  481. s->iso_td = (uhci_desc_t **) kmalloc (1024 * sizeof (uhci_desc_t*), GFP_KERNEL);
  482. if (!s->iso_td)
  483. goto init_skel_cleanup;
  484. s->ls_control_chain = NULL;
  485. s->control_chain = NULL;
  486. s->bulk_chain = NULL;
  487. s->chain_end = NULL;
  488. dbg("allocating iso descs");
  489. for (n = 0; n < 1024; n++) {
  490.   // allocate skeleton iso/irq-tds
  491. if (alloc_td (s, &td, 0))
  492. goto init_skel_cleanup;
  493. s->iso_td[n] = td;
  494. s->framelist[n] = cpu_to_le32((__u32) td->dma_addr);
  495. }
  496. dbg("allocating qh: chain_end");
  497. if (alloc_qh (s, &qh))
  498. goto init_skel_cleanup;
  499. s->chain_end = qh;
  500. if (alloc_td (s, &td, 0))
  501. goto init_skel_cleanup;
  502. fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 1ms interrupt (enabled on demand)
  503. insert_td (s, qh, td, 0);
  504. qh->hw.qh.element &= cpu_to_le32(~UHCI_PTR_TERM); // remove TERM bit
  505. s->td1ms=td;
  506. dbg("allocating qh: bulk_chain");
  507. if (alloc_qh (s, &qh))
  508. goto init_skel_cleanup;
  509. insert_qh (s, s->chain_end, qh, 0);
  510. s->bulk_chain = qh;
  511. dbg("allocating qh: control_chain");
  512. ret = alloc_qh (s, &qh);
  513. if (ret)
  514. goto init_skel_cleanup;
  515. insert_qh (s, s->bulk_chain, qh, 0);
  516. s->control_chain = qh;
  517. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  518. // disabled reclamation loop
  519. set_qh_head(s->chain_end, s->control_chain->dma_addr | UHCI_PTR_QH | UHCI_PTR_TERM);
  520. #endif
  521. dbg("allocating qh: ls_control_chain");
  522. if (alloc_qh (s, &qh))
  523. goto init_skel_cleanup;
  524. insert_qh (s, s->control_chain, qh, 0);
  525. s->ls_control_chain = qh;
  526. for (n = 0; n < 8; n++)
  527. s->int_chain[n] = 0;
  528. dbg("allocating skeleton INT-TDs");
  529. for (n = 0; n < 8; n++) {
  530. uhci_desc_t *td;
  531. if (alloc_td (s, &td, 0))
  532. goto init_skel_cleanup;
  533. s->int_chain[n] = td;
  534. if (n == 0) {
  535. set_td_link(s->int_chain[0], s->ls_control_chain->dma_addr | UHCI_PTR_QH);
  536. }
  537. else {
  538. set_td_link(s->int_chain[n], s->int_chain[0]->dma_addr);
  539. }
  540. }
  541. dbg("Linking skeleton INT-TDs");
  542. for (n = 0; n < 1024; n++) {
  543. // link all iso-tds to the interrupt chains
  544. int m, o;
  545. dbg("framelist[%i]=%x",n,le32_to_cpu(s->framelist[n]));
  546. if ((n&127)==127) 
  547. ((uhci_desc_t*) s->iso_td[n])->hw.td.link = cpu_to_le32(s->int_chain[0]->dma_addr);
  548. else 
  549. for (o = 1, m = 2; m <= 128; o++, m += m)
  550. if ((n & (m - 1)) == ((m - 1) / 2))
  551. set_td_link(((uhci_desc_t*) s->iso_td[n]), s->int_chain[o]->dma_addr);
  552. }
  553. if (alloc_td (s, &td, 0))
  554. goto init_skel_cleanup;
  555. fill_td (td, 0 * TD_CTRL_IOC, 0, 0); // generate 32ms interrupt (activated later)
  556. s->td32ms=td;
  557. insert_td_horizontal (s, s->int_chain[5], td);
  558. mb();
  559. //uhci_show_queue(s->control_chain);   
  560. dbg("init_skel exit");
  561. return 0;
  562.       init_skel_cleanup:
  563. cleanup_skel (s);
  564. return -ENOMEM;
  565. }
  566. /*-------------------------------------------------------------------*/
  567. //                         LOW LEVEL STUFF
  568. //          assembles QHs und TDs for control, bulk and iso
  569. /*-------------------------------------------------------------------*/
  570. _static int uhci_submit_control_urb (struct urb *urb)
  571. {
  572. uhci_desc_t *qh, *td;
  573. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  574. urb_priv_t *urb_priv = urb->hcpriv;
  575. unsigned long destination, status;
  576. int maxsze = usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe));
  577. unsigned long len;
  578. char *data;
  579. int depth_first=USE_CTRL_DEPTH_FIRST;  // UHCI descriptor chasing method
  580. dbg("uhci_submit_control start");
  581. if (alloc_qh (s, &qh)) // alloc qh for this request
  582. return -ENOMEM;
  583. if (alloc_td (s, &td, UHCI_PTR_DEPTH * depth_first)) // get td for setup stage
  584. {
  585. delete_qh (s, qh);
  586. return -ENOMEM;
  587. }
  588. /* The "pipe" thing contains the destination in bits 8--18 */
  589. destination = (urb->pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP;
  590. /* 3 errors */
  591. status = (urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
  592. (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
  593. /*  Build the TD for the control request, try forever, 8 bytes of data */
  594. fill_td (td, status, destination | (7 << 21), urb_priv->setup_packet_dma);
  595. insert_td (s, qh, td, 0); // queue 'setup stage'-td in qh
  596. #if 0
  597. {
  598. char *sp=urb->setup_packet;
  599. dbg("SETUP to pipe %x: %x %x %x %x %x %x %x %x", urb->pipe,
  600.     sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
  601. }
  602. //uhci_show_td(td);
  603. #endif
  604. len = urb->transfer_buffer_length;
  605. data = urb->transfer_buffer;
  606. /* If direction is "send", change the frame from SETUP (0x2D)
  607.    to OUT (0xE1). Else change it from SETUP to IN (0x69). */
  608. destination = (urb->pipe & PIPE_DEVEP_MASK) | (usb_pipeout (urb->pipe)?USB_PID_OUT:USB_PID_IN);
  609. while (len > 0) {
  610. int pktsze = len;
  611. if (alloc_td (s, &td, UHCI_PTR_DEPTH * depth_first))
  612. goto fail_unmap_enomem;
  613. if (pktsze > maxsze)
  614. pktsze = maxsze;
  615. destination ^= 1 << TD_TOKEN_TOGGLE; // toggle DATA0/1
  616. // Status, pktsze bytes of data
  617. fill_td (td, status, destination | ((pktsze - 1) << 21),
  618.  urb_priv->transfer_buffer_dma + (data - (char *)urb->transfer_buffer));
  619. insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue 'data stage'-td in qh
  620. data += pktsze;
  621. len -= pktsze;
  622. }
  623. /* Build the final TD for control status */
  624. /* It's only IN if the pipe is out AND we aren't expecting data */
  625. destination &= ~UHCI_PID;
  626. if (usb_pipeout (urb->pipe) || (urb->transfer_buffer_length == 0))
  627. destination |= USB_PID_IN;
  628. else
  629. destination |= USB_PID_OUT;
  630. destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
  631. if (alloc_td (s, &td, UHCI_PTR_DEPTH))
  632. goto fail_unmap_enomem;
  633. status &=~TD_CTRL_SPD;
  634. /* no limit on errors on final packet , 0 bytes of data */
  635. fill_td (td, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),
  636.  0);
  637. insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first); // queue status td
  638. list_add (&qh->desc_list, &urb_priv->desc_list);
  639. queue_urb (s, urb); // queue before inserting in desc chain
  640. qh->hw.qh.element &= cpu_to_le32(~UHCI_PTR_TERM);
  641. //uhci_show_queue(qh);
  642. /* Start it up... put low speed first */
  643. if (urb->pipe & TD_CTRL_LS)
  644. insert_qh (s, s->control_chain, qh, 0);
  645. else
  646. insert_qh (s, s->bulk_chain, qh, 0);
  647. dbg("uhci_submit_control end");
  648. return 0;
  649. fail_unmap_enomem:
  650. delete_qh(s, qh);
  651. return -ENOMEM;
  652. }
  653. /*-------------------------------------------------------------------*/
  654. // For queued bulk transfers, two additional QH helpers are allocated (nqh, bqh)
  655. // Due to the linking with other bulk urbs, it has to be locked with urb_list_lock!
  656. _static int uhci_submit_bulk_urb (struct urb *urb, struct urb *bulk_urb)
  657. {
  658. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  659. urb_priv_t *urb_priv = urb->hcpriv, *upriv, *bpriv=NULL;
  660. uhci_desc_t *qh, *td, *nqh=NULL, *bqh=NULL, *first_td=NULL;
  661. unsigned long destination, status;
  662. char *data;
  663. unsigned int pipe = urb->pipe;
  664. int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
  665. int info, len, last;
  666. int depth_first=USE_BULK_DEPTH_FIRST;  // UHCI descriptor chasing method
  667. if (usb_endpoint_halted (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)))
  668. return -EPIPE;
  669. queue_dbg("uhci_submit_bulk_urb: urb %p, old %p, pipe %08x, len %i",
  670.   urb,bulk_urb,urb->pipe,urb->transfer_buffer_length);
  671. upriv = (urb_priv_t*)urb->hcpriv;
  672. if (!bulk_urb) {
  673. if (alloc_qh (s, &qh)) // get qh for this request
  674. return -ENOMEM;
  675. if (urb->transfer_flags & USB_QUEUE_BULK) {
  676. if (alloc_qh(s, &nqh)) // placeholder for clean unlink
  677. {
  678. delete_desc (s, qh);
  679. return -ENOMEM;
  680. }
  681. upriv->next_qh = nqh;
  682. queue_dbg("new next qh %p",nqh);
  683. }
  684. }
  685. else { 
  686. bpriv = (urb_priv_t*)bulk_urb->hcpriv;
  687. qh = bpriv->bottom_qh;  // re-use bottom qh and next qh
  688. nqh = bpriv->next_qh;
  689. upriv->next_qh=nqh;
  690. upriv->prev_queued_urb=bulk_urb;
  691. }
  692. if (urb->transfer_flags & USB_QUEUE_BULK) {
  693. if (alloc_qh (s, &bqh))  // "bottom" QH
  694. {
  695. if (!bulk_urb) { 
  696. delete_desc(s, qh);
  697. delete_desc(s, nqh);
  698. }
  699. return -ENOMEM;
  700. }
  701. set_qh_element(bqh, UHCI_PTR_TERM);
  702. set_qh_head(bqh, nqh->dma_addr | UHCI_PTR_QH); // element
  703. upriv->bottom_qh = bqh;
  704. }
  705. queue_dbg("uhci_submit_bulk: qh %p bqh %p nqh %p",qh, bqh, nqh);
  706. /* The "pipe" thing contains the destination in bits 8--18. */
  707. destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
  708. /* 3 errors */
  709. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE |
  710. ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27);
  711. /* Build the TDs for the bulk request */
  712. len = urb->transfer_buffer_length;
  713. data = urb->transfer_buffer;
  714. do { // TBD: Really allow zero-length packets?
  715. int pktsze = len;
  716. if (alloc_td (s, &td, UHCI_PTR_DEPTH * depth_first))
  717. {
  718. delete_qh (s, qh);
  719. return -ENOMEM;
  720. }
  721. if (pktsze > maxsze)
  722. pktsze = maxsze;
  723. // pktsze bytes of data 
  724. info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
  725. (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
  726. fill_td (td, status, info,
  727.  urb_priv->transfer_buffer_dma + (data - (char *)urb->transfer_buffer));
  728. data += pktsze;
  729. len -= pktsze;
  730. // Use USB_ZERO_PACKET to finish bulk OUTs always with a zero length packet
  731. last = (len == 0 && (usb_pipein(pipe) || pktsze < maxsze || !(urb->transfer_flags & USB_ZERO_PACKET)));
  732. if (last)
  733. set_td_ioc(td); // last one generates INT
  734. insert_td (s, qh, td, UHCI_PTR_DEPTH * depth_first);
  735. if (!first_td)
  736. first_td=td;
  737. usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
  738. } while (!last);
  739. if (bulk_urb && bpriv)   // everything went OK, link with old bulk URB
  740. bpriv->next_queued_urb=urb;
  741. list_add (&qh->desc_list, &urb_priv->desc_list);
  742. if (urb->transfer_flags & USB_QUEUE_BULK)
  743. append_qh(s, td, bqh, UHCI_PTR_DEPTH * depth_first);
  744. queue_urb_unlocked (s, urb);
  745. if (urb->transfer_flags & USB_QUEUE_BULK)
  746. set_qh_element(qh, first_td->dma_addr);
  747. else
  748. qh->hw.qh.element &= cpu_to_le32(~UHCI_PTR_TERM);    // arm QH
  749. if (!bulk_urb) {  // new bulk queue
  750. if (urb->transfer_flags & USB_QUEUE_BULK) {
  751. spin_lock (&s->td_lock); // both QHs in one go
  752. insert_qh (s, s->chain_end, qh, 0); // Main QH
  753. insert_qh (s, s->chain_end, nqh, 0); // Helper QH
  754. spin_unlock (&s->td_lock);
  755. }
  756. else
  757. insert_qh (s, s->chain_end, qh, 0);
  758. }
  759. //uhci_show_queue(s->bulk_chain);
  760. //dbg("uhci_submit_bulk_urb: exitn");
  761. return 0;
  762. }
  763. /*-------------------------------------------------------------------*/
  764. _static void uhci_clean_iso_step1(uhci_t *s, urb_priv_t *urb_priv)
  765. {
  766. struct list_head *p;
  767. uhci_desc_t *td;
  768. for (p = urb_priv->desc_list.next; p != &urb_priv->desc_list; p = p->next) {
  769. td = list_entry (p, uhci_desc_t, desc_list);
  770. unlink_td (s, td, 1);
  771. }
  772. }
  773. /*-------------------------------------------------------------------*/
  774. _static void uhci_clean_iso_step2(uhci_t *s, urb_priv_t *urb_priv)
  775. {
  776. struct list_head *p;
  777. uhci_desc_t *td;
  778. while ((p = urb_priv->desc_list.next) != &urb_priv->desc_list) {
  779. td = list_entry (p, uhci_desc_t, desc_list);
  780. list_del (p);
  781. delete_desc (s, td);
  782. }
  783. }
  784. /*-------------------------------------------------------------------*/
  785. /* mode: CLEAN_TRANSFER_NO_DELETION: unlink but no deletion mark (step 1 of async_unlink)
  786.          CLEAN_TRANSFER_REGULAR: regular (unlink/delete-mark)
  787.          CLEAN_TRANSFER_DELETION_MARK: deletion mark for QH (step 2 of async_unlink)
  788.  looks a bit complicated because of all the bulk queueing goodies
  789. */
  790. _static void uhci_clean_transfer (uhci_t *s, struct urb *urb, uhci_desc_t *qh, int mode)
  791. {
  792. uhci_desc_t *bqh, *nqh, *prevqh, *prevtd;
  793. int now;
  794. urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
  795. now=UHCI_GET_CURRENT_FRAME(s);
  796. bqh=priv->bottom_qh;
  797. if (!priv->next_queued_urb)  { // no more appended bulk queues
  798. queue_dbg("uhci_clean_transfer: No more bulks for urb %p, qh %p, bqh %p, nqh %p", urb, qh, bqh, priv->next_qh);
  799. if (priv->prev_queued_urb && mode != CLEAN_TRANSFER_DELETION_MARK) {  // qh not top of the queue
  800. unsigned long flags; 
  801. urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
  802. spin_lock_irqsave (&s->qh_lock, flags);
  803. prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
  804. prevtd = list_entry (prevqh->vertical.prev, uhci_desc_t, vertical);
  805. set_td_link(prevtd, priv->bottom_qh->dma_addr | UHCI_PTR_QH); // skip current qh
  806. mb();
  807. queue_dbg("uhci_clean_transfer: relink pqh %p, ptd %p",prevqh, prevtd);
  808. spin_unlock_irqrestore (&s->qh_lock, flags);
  809. ppriv->bottom_qh = priv->bottom_qh;
  810. ppriv->next_queued_urb = NULL;
  811. }
  812. else {   // queue is dead, qh is top of the queue
  813. if (mode != CLEAN_TRANSFER_DELETION_MARK) 
  814. unlink_qh(s, qh); // remove qh from horizontal chain
  815. if (bqh) {  // remove remainings of bulk queue
  816. nqh=priv->next_qh;
  817. if (mode != CLEAN_TRANSFER_DELETION_MARK) 
  818. unlink_qh(s, nqh);  // remove nqh from horizontal chain
  819. if (mode != CLEAN_TRANSFER_NO_DELETION) {  // add helper QHs to free desc list
  820. nqh->last_used = bqh->last_used = now;
  821. list_add_tail (&nqh->horizontal, &s->free_desc);
  822. list_add_tail (&bqh->horizontal, &s->free_desc);
  823. }
  824. }
  825. }
  826. }
  827. else { // there are queued urbs following
  828.   queue_dbg("uhci_clean_transfer: urb %p, prevurb %p, nexturb %p, qh %p, bqh %p, nqh %p",
  829.        urb, priv->prev_queued_urb,  priv->next_queued_urb, qh, bqh, priv->next_qh);
  830.        
  831. if (mode != CLEAN_TRANSFER_DELETION_MARK) { // no work for cleanup at unlink-completion
  832. struct urb *nurb;
  833. unsigned long flags;
  834. nurb = priv->next_queued_urb;
  835. spin_lock_irqsave (&s->qh_lock, flags);
  836. if (!priv->prev_queued_urb) { // top QH
  837. prevqh = list_entry (qh->horizontal.prev, uhci_desc_t, horizontal);
  838. set_qh_head(prevqh, bqh->dma_addr | UHCI_PTR_QH);
  839. list_del (&qh->horizontal);  // remove this qh form horizontal chain
  840. list_add (&bqh->horizontal, &prevqh->horizontal); // insert next bqh in horizontal chain
  841. }
  842. else { // intermediate QH
  843. urb_priv_t* ppriv=(urb_priv_t*)priv->prev_queued_urb->hcpriv;
  844. urb_priv_t* npriv=(urb_priv_t*)nurb->hcpriv;
  845. uhci_desc_t * bnqh;
  846. bnqh = list_entry (npriv->desc_list.next, uhci_desc_t, desc_list);
  847. ppriv->bottom_qh = bnqh;
  848. ppriv->next_queued_urb = nurb;
  849. prevqh = list_entry (ppriv->desc_list.next, uhci_desc_t, desc_list);
  850. set_qh_head(prevqh, bqh->dma_addr | UHCI_PTR_QH);
  851. }
  852. mb();
  853. ((urb_priv_t*)nurb->hcpriv)->prev_queued_urb=priv->prev_queued_urb;
  854. spin_unlock_irqrestore (&s->qh_lock, flags);
  855. }
  856. }
  857. if (mode != CLEAN_TRANSFER_NO_DELETION) {
  858. qh->last_used = now;
  859. list_add_tail (&qh->horizontal, &s->free_desc); // mark qh for later deletion/kfree
  860. }
  861. }
  862. /*-------------------------------------------------------------------*/
  863. // Release bandwidth for Interrupt or Isoc. transfers 
  864. _static void uhci_release_bandwidth(struct urb *urb)
  865. {       
  866. if (urb->bandwidth) {
  867. switch (usb_pipetype(urb->pipe)) {
  868. case PIPE_INTERRUPT:
  869. usb_release_bandwidth (urb->dev, urb, 0);
  870. break;
  871. case PIPE_ISOCHRONOUS:
  872. usb_release_bandwidth (urb->dev, urb, 1);
  873. break;
  874. default:
  875. break;
  876. }
  877. }
  878. }
  879. _static void uhci_urb_dma_sync(uhci_t *s, struct urb *urb, urb_priv_t *urb_priv)
  880. {
  881. if (urb_priv->setup_packet_dma)
  882. pci_dma_sync_single(s->uhci_pci, urb_priv->setup_packet_dma,
  883.     sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE);
  884. if (urb_priv->transfer_buffer_dma)
  885. pci_dma_sync_single(s->uhci_pci, urb_priv->transfer_buffer_dma,
  886.     urb->transfer_buffer_length,
  887.     usb_pipein(urb->pipe) ?
  888.     PCI_DMA_FROMDEVICE :
  889.     PCI_DMA_TODEVICE);
  890. }
  891. _static void uhci_urb_dma_unmap(uhci_t *s, struct urb *urb, urb_priv_t *urb_priv)
  892. {
  893. if (urb_priv->setup_packet_dma) {
  894. pci_unmap_single(s->uhci_pci, urb_priv->setup_packet_dma,
  895.  sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE);
  896. urb_priv->setup_packet_dma = 0;
  897. }
  898. if (urb_priv->transfer_buffer_dma) {
  899. pci_unmap_single(s->uhci_pci, urb_priv->transfer_buffer_dma,
  900.  urb->transfer_buffer_length,
  901.  usb_pipein(urb->pipe) ?
  902.  PCI_DMA_FROMDEVICE :
  903.  PCI_DMA_TODEVICE);
  904. urb_priv->transfer_buffer_dma = 0;
  905. }
  906. }
  907. /*-------------------------------------------------------------------*/
  908. /* needs urb_list_lock!
  909.    mode: UNLINK_ASYNC_STORE_URB: unlink and move URB into unlinked list
  910.          UNLINK_ASYNC_DONT_STORE: unlink, don't move URB into unlinked list
  911. */
  912. _static int uhci_unlink_urb_async (uhci_t *s,struct urb *urb, int mode)
  913. {
  914. uhci_desc_t *qh;
  915. urb_priv_t *urb_priv;
  916. async_dbg("unlink_urb_async called %p",urb);
  917. if ((urb->status == -EINPROGRESS) ||
  918.     ((usb_pipetype (urb->pipe) ==  PIPE_INTERRUPT) && ((urb_priv_t*)urb->hcpriv)->flags))
  919. {
  920. ((urb_priv_t*)urb->hcpriv)->started = ~0;  // mark
  921. dequeue_urb (s, urb);
  922. if (mode==UNLINK_ASYNC_STORE_URB)
  923. list_add_tail (&urb->urb_list, &s->urb_unlinked); // store urb
  924. uhci_switch_timer_int(s);
  925.         s->unlink_urb_done = 1;
  926. uhci_release_bandwidth(urb);
  927. urb->status = -ECONNABORTED; // mark urb as "waiting to be killed"
  928. urb_priv = (urb_priv_t*)urb->hcpriv;
  929. switch (usb_pipetype (urb->pipe)) {
  930. case PIPE_INTERRUPT:
  931. usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
  932. case PIPE_ISOCHRONOUS:
  933. uhci_clean_iso_step1 (s, urb_priv);
  934. break;
  935. case PIPE_BULK:
  936. case PIPE_CONTROL:
  937. qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
  938. uhci_clean_transfer (s, urb, qh, CLEAN_TRANSFER_NO_DELETION);
  939. break;
  940. }
  941. ((urb_priv_t*)urb->hcpriv)->started = UHCI_GET_CURRENT_FRAME(s);
  942. return -EINPROGRESS;  // completion will follow
  943. }
  944. return 0;    // URB already dead
  945. }
  946. /*-------------------------------------------------------------------*/
  947. // kills an urb by unlinking descriptors and waiting for at least one frame
  948. _static int uhci_unlink_urb_sync (uhci_t *s, struct urb *urb)
  949. {
  950. uhci_desc_t *qh;
  951. urb_priv_t *urb_priv;
  952. unsigned long flags=0;
  953. struct usb_device *usb_dev;
  954. spin_lock_irqsave (&s->urb_list_lock, flags);
  955. if (urb->status == -EINPROGRESS) {
  956. // move descriptors out of the running chains, dequeue urb
  957. uhci_unlink_urb_async(s, urb, UNLINK_ASYNC_DONT_STORE);
  958. urb_priv = urb->hcpriv;
  959. urb->status = -ENOENT; // prevent from double deletion after unlock
  960. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  961. // cleanup the rest
  962. switch (usb_pipetype (urb->pipe)) {
  963. case PIPE_INTERRUPT:
  964. case PIPE_ISOCHRONOUS:
  965. uhci_wait_ms(1);
  966. uhci_clean_iso_step2(s, urb_priv);
  967. break;
  968. case PIPE_BULK:
  969. case PIPE_CONTROL:
  970. qh = list_entry (urb_priv->desc_list.next, uhci_desc_t, desc_list);
  971. uhci_clean_transfer(s, urb, qh, CLEAN_TRANSFER_DELETION_MARK);
  972. uhci_wait_ms(1);
  973. }
  974. urb->status = -ENOENT; // mark urb as killed
  975. uhci_urb_dma_unmap(s, urb, urb->hcpriv);
  976. #ifdef DEBUG_SLAB
  977. kmem_cache_free (urb_priv_kmem, urb->hcpriv);
  978. #else
  979. kfree (urb->hcpriv);
  980. #endif
  981. usb_dev = urb->dev;
  982. if (urb->complete) {
  983. dbg("unlink_urb: calling completion");
  984. urb->dev = NULL;
  985. urb->complete ((struct urb *) urb);
  986. }
  987. usb_dec_dev_use (usb_dev);
  988. }
  989. else
  990. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  991. return 0;
  992. }
  993. /*-------------------------------------------------------------------*/
  994. // async unlink_urb completion/cleanup work
  995. // has to be protected by urb_list_lock!
  996. // features: if set in transfer_flags, the resulting status of the killed
  997. // transaction is not overwritten
  998. _static void uhci_cleanup_unlink(uhci_t *s, int force)
  999. {
  1000. struct list_head *q;
  1001. struct urb *urb;
  1002. struct usb_device *dev;
  1003. int now, type;
  1004. urb_priv_t *urb_priv;
  1005. q=s->urb_unlinked.next;
  1006. now=UHCI_GET_CURRENT_FRAME(s);
  1007. while (q != &s->urb_unlinked) {
  1008. urb = list_entry (q, struct urb, urb_list);
  1009. urb_priv = (urb_priv_t*)urb->hcpriv;
  1010. q = urb->urb_list.next;
  1011. if (!urb_priv) // avoid crash when URB is corrupted
  1012. break;
  1013. if (force || ((urb_priv->started != ~0) && (urb_priv->started != now))) {
  1014. async_dbg("async cleanup %p",urb);
  1015. type=usb_pipetype (urb->pipe);
  1016. switch (type) { // process descriptors
  1017. case PIPE_CONTROL:
  1018. process_transfer (s, urb, CLEAN_TRANSFER_DELETION_MARK);  // don't unlink (already done)
  1019. break;
  1020. case PIPE_BULK:
  1021. if (!s->avoid_bulk.counter)
  1022. process_transfer (s, urb, CLEAN_TRANSFER_DELETION_MARK); // don't unlink (already done)
  1023. else
  1024. continue;
  1025. break;
  1026. case PIPE_ISOCHRONOUS:
  1027. process_iso (s, urb, PROCESS_ISO_FORCE); // force, don't unlink
  1028. break;
  1029. case PIPE_INTERRUPT:
  1030. process_interrupt (s, urb);
  1031. break;
  1032. }
  1033. if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
  1034.    urb->status = -ECONNRESET; // mark as asynchronously killed
  1035. dev = urb->dev; // completion may destroy all...
  1036. urb_priv = urb->hcpriv;
  1037. list_del (&urb->urb_list);
  1038. uhci_urb_dma_sync(s, urb, urb_priv);
  1039. if (urb->complete) {
  1040. spin_unlock(&s->urb_list_lock);
  1041. urb->dev = NULL;
  1042. urb->complete ((struct urb *) urb);
  1043. spin_lock(&s->urb_list_lock);
  1044. }
  1045. if (!(urb->transfer_flags & USB_TIMEOUT_KILLED))
  1046. urb->status = -ENOENT;  // now the urb is really dead
  1047. switch (type) {
  1048. case PIPE_ISOCHRONOUS:
  1049. case PIPE_INTERRUPT:
  1050. uhci_clean_iso_step2(s, urb_priv);
  1051. break;
  1052. }
  1053. uhci_urb_dma_unmap(s, urb, urb_priv);
  1054. usb_dec_dev_use (dev);
  1055. #ifdef DEBUG_SLAB
  1056. kmem_cache_free (urb_priv_kmem, urb_priv);
  1057. #else
  1058. kfree (urb_priv);
  1059. #endif
  1060. }
  1061. }
  1062. }
  1063.  
  1064. /*-------------------------------------------------------------------*/
  1065. _static int uhci_unlink_urb (struct urb *urb)
  1066. {
  1067. uhci_t *s;
  1068. unsigned long flags=0;
  1069. dbg("uhci_unlink_urb called for %p",urb);
  1070. if (!urb || !urb->dev) // you never know...
  1071. return -EINVAL;
  1072. s = (uhci_t*) urb->dev->bus->hcpriv;
  1073. if (usb_pipedevice (urb->pipe) == s->rh.devnum)
  1074. return rh_unlink_urb (urb);
  1075. if (!urb->hcpriv)
  1076. return -EINVAL;
  1077. if (urb->transfer_flags & USB_ASYNC_UNLINK) {
  1078. int ret;
  1079.         spin_lock_irqsave (&s->urb_list_lock, flags);
  1080.        
  1081. uhci_release_bandwidth(urb);
  1082. ret = uhci_unlink_urb_async(s, urb, UNLINK_ASYNC_STORE_URB);
  1083. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1084. return ret;
  1085. }
  1086. else
  1087. return uhci_unlink_urb_sync(s, urb);
  1088. }
  1089. /*-------------------------------------------------------------------*/
  1090. // In case of ASAP iso transfer, search the URB-list for already queued URBs
  1091. // for this EP and calculate the earliest start frame for the new
  1092. // URB (easy seamless URB continuation!)
  1093. _static int find_iso_limits (struct urb *urb, unsigned int *start, unsigned int *end)
  1094. {
  1095. struct urb *u, *last_urb = NULL;
  1096. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  1097. struct list_head *p;
  1098. int ret=-1;
  1099. unsigned long flags;
  1100. spin_lock_irqsave (&s->urb_list_lock, flags);
  1101. p=s->urb_list.prev;
  1102. for (; p != &s->urb_list; p = p->prev) {
  1103. u = list_entry (p, struct urb, urb_list);
  1104. // look for pending URBs with identical pipe handle
  1105. // works only because iso doesn't toggle the data bit!
  1106. if ((urb->pipe == u->pipe) && (urb->dev == u->dev) && (u->status == -EINPROGRESS)) {
  1107. if (!last_urb)
  1108. *start = u->start_frame;
  1109. last_urb = u;
  1110. }
  1111. }
  1112. if (last_urb) {
  1113. *end = (last_urb->start_frame + last_urb->number_of_packets) & 1023;
  1114. ret=0;
  1115. }
  1116. spin_unlock_irqrestore(&s->urb_list_lock, flags);
  1117. return ret;
  1118. }
  1119. /*-------------------------------------------------------------------*/
  1120. // adjust start_frame according to scheduling constraints (ASAP etc)
  1121. _static int iso_find_start (struct urb *urb)
  1122. {
  1123. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  1124. unsigned int now;
  1125. unsigned int start_limit = 0, stop_limit = 0, queued_size;
  1126. int limits;
  1127. now = UHCI_GET_CURRENT_FRAME (s) & 1023;
  1128. if ((unsigned) urb->number_of_packets > 900)
  1129. return -EFBIG;
  1130. limits = find_iso_limits (urb, &start_limit, &stop_limit);
  1131. queued_size = (stop_limit - start_limit) & 1023;
  1132. if (urb->transfer_flags & USB_ISO_ASAP) {
  1133. // first iso
  1134. if (limits) {
  1135. // 10ms setup should be enough //FIXME!
  1136. urb->start_frame = (now + 10) & 1023;
  1137. }
  1138. else {
  1139. urb->start_frame = stop_limit; //seamless linkage
  1140. if (((now - urb->start_frame) & 1023) <= (unsigned) urb->number_of_packets) {
  1141. info("iso_find_start: gap in seamless isochronous scheduling");
  1142. dbg("iso_find_start: now %u start_frame %u number_of_packets %u pipe 0x%08x",
  1143. now, urb->start_frame, urb->number_of_packets, urb->pipe);
  1144. urb->start_frame = (now + 5) & 1023; // 5ms setup should be enough //FIXME!
  1145. }
  1146. }
  1147. }
  1148. else {
  1149. urb->start_frame &= 1023;
  1150. if (((now - urb->start_frame) & 1023) < (unsigned) urb->number_of_packets) {
  1151. dbg("iso_find_start: now between start_frame and end");
  1152. return -EAGAIN;
  1153. }
  1154. }
  1155. /* check if either start_frame or start_frame+number_of_packets-1 lies between start_limit and stop_limit */
  1156. if (limits)
  1157. return 0;
  1158. if (((urb->start_frame - start_limit) & 1023) < queued_size ||
  1159.     ((urb->start_frame + urb->number_of_packets - 1 - start_limit) & 1023) < queued_size) {
  1160. dbg("iso_find_start: start_frame %u number_of_packets %u start_limit %u stop_limit %u",
  1161. urb->start_frame, urb->number_of_packets, start_limit, stop_limit);
  1162. return -EAGAIN;
  1163. }
  1164. return 0;
  1165. }
  1166. /*-------------------------------------------------------------------*/
  1167. // submits USB interrupt (ie. polling ;-) 
  1168. // ASAP-flag set implicitely
  1169. // if period==0, the transfer is only done once
  1170. _static int uhci_submit_int_urb (struct urb *urb)
  1171. {
  1172. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  1173. urb_priv_t *urb_priv = urb->hcpriv;
  1174. int nint, n;
  1175. uhci_desc_t *td;
  1176. int status, destination;
  1177. int info;
  1178. unsigned int pipe = urb->pipe;
  1179. if (urb->interval < 0 || urb->interval >= 256)
  1180. return -EINVAL;
  1181. if (urb->interval == 0)
  1182. nint = 0;
  1183. else {
  1184. for (nint = 0, n = 1; nint <= 8; nint++, n += n) // round interval down to 2^n
  1185.  {
  1186. if (urb->interval < n) {
  1187. urb->interval = n / 2;
  1188. break;
  1189. }
  1190. }
  1191. nint--;
  1192. }
  1193. dbg("Rounded interval to %i, chain  %i", urb->interval, nint);
  1194. urb->start_frame = UHCI_GET_CURRENT_FRAME (s) & 1023; // remember start frame, just in case...
  1195. urb->number_of_packets = 1;
  1196. // INT allows only one packet
  1197. if (urb->transfer_buffer_length > usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe)))
  1198. return -EINVAL;
  1199. if (alloc_td (s, &td, UHCI_PTR_DEPTH))
  1200. return -ENOMEM;
  1201. status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
  1202. (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
  1203. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe) |
  1204. (((urb->transfer_buffer_length - 1) & 0x7ff) << 21);
  1205. info = destination | (usb_gettoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
  1206. fill_td (td, status, info, urb_priv->transfer_buffer_dma);
  1207. list_add_tail (&td->desc_list, &urb_priv->desc_list);
  1208. queue_urb (s, urb);
  1209. insert_td_horizontal (s, s->int_chain[nint], td); // store in INT-TDs
  1210. usb_dotoggle (urb->dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
  1211. return 0;
  1212. }
  1213. /*-------------------------------------------------------------------*/
  1214. _static int uhci_submit_iso_urb (struct urb *urb)
  1215. {
  1216. uhci_t *s = (uhci_t*) urb->dev->bus->hcpriv;
  1217. urb_priv_t *urb_priv = urb->hcpriv;
  1218. #ifdef ISO_SANITY_CHECK
  1219. int pipe=urb->pipe;
  1220. int maxsze = usb_maxpacket (urb->dev, pipe, usb_pipeout (pipe));
  1221. #endif
  1222. int n, ret, last=0;
  1223. uhci_desc_t *td, **tdm;
  1224. int status, destination;
  1225. unsigned long flags;
  1226. __save_flags(flags);
  1227. __cli();       // Disable IRQs to schedule all ISO-TDs in time
  1228. ret = iso_find_start (urb); // adjusts urb->start_frame for later use
  1229. if (ret)
  1230. goto err;
  1231. tdm = (uhci_desc_t **) kmalloc (urb->number_of_packets * sizeof (uhci_desc_t*), KMALLOC_FLAG);
  1232. if (!tdm) {
  1233. ret = -ENOMEM;
  1234. goto err;
  1235. }
  1236. memset(tdm, 0, urb->number_of_packets * sizeof (uhci_desc_t*));
  1237. // First try to get all TDs. Cause: Removing already inserted TDs can only be done 
  1238. // racefree in three steps: unlink TDs, wait one frame, delete TDs. 
  1239. // So, this solutions seems simpler...
  1240. for (n = 0; n < urb->number_of_packets; n++) {
  1241. dbg("n:%d urb->iso_frame_desc[n].length:%d", n, urb->iso_frame_desc[n].length);
  1242. if (!urb->iso_frame_desc[n].length)
  1243. continue;  // allows ISO striping by setting length to zero in iso_descriptor
  1244. #ifdef ISO_SANITY_CHECK
  1245. if(urb->iso_frame_desc[n].length > maxsze) {
  1246. err("submit_iso: urb->iso_frame_desc[%d].length(%d)>%d",n , urb->iso_frame_desc[n].length, maxsze);
  1247. ret=-EINVAL;
  1248. }
  1249. else
  1250. #endif
  1251. if (alloc_td (s, &td, UHCI_PTR_DEPTH)) {
  1252. int i; // Cleanup allocated TDs
  1253. for (i = 0; i < n; n++)
  1254. if (tdm[i])
  1255.  delete_desc(s, tdm[i]);
  1256. kfree (tdm);
  1257. goto err;
  1258. }
  1259. last=n;
  1260. tdm[n] = td;
  1261. }
  1262. status = TD_CTRL_ACTIVE | TD_CTRL_IOS;
  1263. destination = (urb->pipe & PIPE_DEVEP_MASK) | usb_packetid (urb->pipe);
  1264. // Queue all allocated TDs
  1265. for (n = 0; n < urb->number_of_packets; n++) {
  1266. td = tdm[n];
  1267. if (!td)
  1268. continue;
  1269. if (n  == last) {
  1270. status |= TD_CTRL_IOC;
  1271. queue_urb (s, urb);
  1272. }
  1273. fill_td (td, status, destination | (((urb->iso_frame_desc[n].length - 1) & 0x7ff) << 21),
  1274.  urb_priv->transfer_buffer_dma + urb->iso_frame_desc[n].offset);
  1275. list_add_tail (&td->desc_list, &urb_priv->desc_list);
  1276. insert_td_horizontal (s, s->iso_td[(urb->start_frame + n) & 1023], td); // store in iso-tds
  1277. }
  1278. kfree (tdm);
  1279. dbg("ISO-INT# %i, start %i, now %i", urb->number_of_packets, urb->start_frame, UHCI_GET_CURRENT_FRAME (s) & 1023);
  1280. ret = 0;
  1281.       err:
  1282. __restore_flags(flags);
  1283. return ret;
  1284. }
  1285. /*-------------------------------------------------------------------*/
  1286. // returns: 0 (no transfer queued), urb* (this urb already queued)
  1287.  
  1288. _static struct urb* search_dev_ep (uhci_t *s, struct urb *urb)
  1289. {
  1290. struct list_head *p;
  1291. struct urb *tmp;
  1292. unsigned int mask = usb_pipecontrol(urb->pipe) ? (~USB_DIR_IN) : (~0);
  1293. dbg("search_dev_ep:");
  1294. p=s->urb_list.next;
  1295. for (; p != &s->urb_list; p = p->next) {
  1296. tmp = list_entry (p, struct urb, urb_list);
  1297. dbg("urb: %p", tmp);
  1298. // we can accept this urb if it is not queued at this time 
  1299. // or if non-iso transfer requests should be scheduled for the same device and pipe
  1300. if ((!usb_pipeisoc(urb->pipe) && (tmp->dev == urb->dev) && !((tmp->pipe ^ urb->pipe) & mask)) ||
  1301.     (urb == tmp)) {
  1302. return tmp; // found another urb already queued for processing
  1303. }
  1304. }
  1305. return 0;
  1306. }
  1307. /*-------------------------------------------------------------------*/
  1308. _static int uhci_submit_urb (struct urb *urb)
  1309. {
  1310. uhci_t *s;
  1311. urb_priv_t *urb_priv;
  1312. int ret = 0, type;
  1313. unsigned long flags;
  1314. struct urb *queued_urb=NULL;
  1315. int bustime;
  1316. if (!urb->dev || !urb->dev->bus)
  1317. return -ENODEV;
  1318. s = (uhci_t*) urb->dev->bus->hcpriv;
  1319. //dbg("submit_urb: %p type %d",urb,usb_pipetype(urb->pipe));
  1320. if (!s->running)
  1321. return -ENODEV;
  1322. type = usb_pipetype (urb->pipe);
  1323. if (usb_pipedevice (urb->pipe) == s->rh.devnum)
  1324. return rh_submit_urb (urb); /* virtual root hub */
  1325. // Sanity checks
  1326. if (usb_maxpacket (urb->dev, urb->pipe, usb_pipeout (urb->pipe)) <= 0) {
  1327. err("uhci_submit_urb: pipesize for pipe %x is zero", urb->pipe);
  1328. return -EMSGSIZE;
  1329. }
  1330. if (urb->transfer_buffer_length < 0 && type != PIPE_ISOCHRONOUS) {
  1331. err("uhci_submit_urb: Negative transfer length for urb %p", urb);
  1332. return -EINVAL;
  1333. }
  1334. usb_inc_dev_use (urb->dev);
  1335. spin_lock_irqsave (&s->urb_list_lock, flags);
  1336. queued_urb = search_dev_ep (s, urb); // returns already queued urb for that pipe
  1337. if (queued_urb) {
  1338. queue_dbg("found bulk urb %pn", queued_urb);
  1339. if (( type != PIPE_BULK) ||
  1340.     ((type == PIPE_BULK) &&
  1341.      (!(urb->transfer_flags & USB_QUEUE_BULK) || !(queued_urb->transfer_flags & USB_QUEUE_BULK)))) {
  1342. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1343. usb_dec_dev_use (urb->dev);
  1344. err("ENXIO %08x, flags %x, urb %p, burb %p",urb->pipe,urb->transfer_flags,urb,queued_urb);
  1345. return -ENXIO; // urb already queued
  1346. }
  1347. }
  1348. #ifdef DEBUG_SLAB
  1349. urb_priv = kmem_cache_alloc(urb_priv_kmem, SLAB_FLAG);
  1350. #else
  1351. urb_priv = kmalloc (sizeof (urb_priv_t), KMALLOC_FLAG);
  1352. #endif
  1353. if (!urb_priv) {
  1354. usb_dec_dev_use (urb->dev);
  1355. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1356. return -ENOMEM;
  1357. }
  1358. memset(urb_priv, 0, sizeof(urb_priv_t));
  1359. urb->hcpriv = urb_priv;
  1360. INIT_LIST_HEAD (&urb_priv->desc_list);
  1361. dbg("submit_urb: scheduling %p", urb);
  1362. if (type == PIPE_CONTROL)
  1363. urb_priv->setup_packet_dma = pci_map_single(s->uhci_pci, urb->setup_packet,
  1364.     sizeof(struct usb_ctrlrequest), PCI_DMA_TODEVICE);
  1365. if (urb->transfer_buffer_length)
  1366. urb_priv->transfer_buffer_dma = pci_map_single(s->uhci_pci,
  1367.        urb->transfer_buffer,
  1368.        urb->transfer_buffer_length,
  1369.        usb_pipein(urb->pipe) ?
  1370.        PCI_DMA_FROMDEVICE :
  1371.        PCI_DMA_TODEVICE);
  1372. if (type == PIPE_BULK) {
  1373. if (queued_urb) {
  1374. while (((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb)  // find last queued bulk
  1375. queued_urb=((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb;
  1376. ((urb_priv_t*)queued_urb->hcpriv)->next_queued_urb=urb;
  1377. }
  1378. atomic_inc (&s->avoid_bulk);
  1379. ret = uhci_submit_bulk_urb (urb, queued_urb);
  1380. atomic_dec (&s->avoid_bulk);
  1381. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1382. }
  1383. else {
  1384. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1385. switch (type) {
  1386. case PIPE_ISOCHRONOUS:
  1387. if (urb->bandwidth == 0) {      /* not yet checked/allocated */
  1388. if (urb->number_of_packets <= 0) {
  1389. ret = -EINVAL;
  1390. break;
  1391. }
  1392. bustime = usb_check_bandwidth (urb->dev, urb);
  1393. if (bustime < 0) 
  1394. ret = bustime;
  1395. else {
  1396. ret = uhci_submit_iso_urb(urb);
  1397. if (ret == 0)
  1398. usb_claim_bandwidth (urb->dev, urb, bustime, 1);
  1399. }
  1400. } else {        /* bandwidth is already set */
  1401. ret = uhci_submit_iso_urb(urb);
  1402. }
  1403. break;
  1404. case PIPE_INTERRUPT:
  1405. if (urb->bandwidth == 0) {      /* not yet checked/allocated */
  1406. bustime = usb_check_bandwidth (urb->dev, urb);
  1407. if (bustime < 0)
  1408. ret = bustime;
  1409. else {
  1410. ret = uhci_submit_int_urb(urb);
  1411. if (ret == 0)
  1412. usb_claim_bandwidth (urb->dev, urb, bustime, 0);
  1413. }
  1414. } else {        /* bandwidth is already set */
  1415. ret = uhci_submit_int_urb(urb);
  1416. }
  1417. break;
  1418. case PIPE_CONTROL:
  1419. ret = uhci_submit_control_urb (urb);
  1420. break;
  1421. default:
  1422. ret = -EINVAL;
  1423. }
  1424. }
  1425. dbg("submit_urb: scheduled with ret: %d", ret);
  1426. if (ret != 0) {
  1427. uhci_urb_dma_unmap(s, urb, urb_priv);
  1428. usb_dec_dev_use (urb->dev);
  1429. #ifdef DEBUG_SLAB
  1430. kmem_cache_free(urb_priv_kmem, urb_priv);
  1431. #else
  1432. kfree (urb_priv);
  1433. #endif
  1434. return ret;
  1435. }
  1436. return 0;
  1437. }
  1438. // Checks for URB timeout and removes bandwidth reclamation if URB idles too long
  1439. _static void uhci_check_timeouts(uhci_t *s)
  1440. {
  1441. struct list_head *p,*p2;
  1442. struct urb *urb;
  1443. int type;
  1444. p = s->urb_list.prev;
  1445. while (p != &s->urb_list) {
  1446. urb_priv_t *hcpriv;
  1447. p2 = p;
  1448. p = p->prev;
  1449. urb = list_entry (p2, struct urb, urb_list);
  1450. type = usb_pipetype (urb->pipe);
  1451. hcpriv = (urb_priv_t*)urb->hcpriv;
  1452. if ( urb->timeout && time_after(jiffies, hcpriv->started + urb->timeout)) {
  1453. urb->transfer_flags |= USB_TIMEOUT_KILLED | USB_ASYNC_UNLINK;
  1454. async_dbg("uhci_check_timeout: timeout for %p",urb);
  1455. uhci_unlink_urb_async(s, urb, UNLINK_ASYNC_STORE_URB);
  1456. }
  1457. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  1458. else if (((type == PIPE_BULK) || (type == PIPE_CONTROL)) &&  
  1459.  (hcpriv->use_loop) && time_after(jiffies, hcpriv->started + IDLE_TIMEOUT))
  1460. disable_desc_loop(s, urb);
  1461. #endif
  1462. }
  1463. s->timeout_check=jiffies;
  1464. }
  1465. /*-------------------------------------------------------------------
  1466.  Virtual Root Hub
  1467.  -------------------------------------------------------------------*/
  1468. _static __u8 root_hub_dev_des[] =
  1469. {
  1470. 0x12, /*  __u8  bLength; */
  1471. 0x01, /*  __u8  bDescriptorType; Device */
  1472. 0x00, /*  __u16 bcdUSB; v1.0 */
  1473. 0x01,
  1474. 0x09, /*  __u8  bDeviceClass; HUB_CLASSCODE */
  1475. 0x00, /*  __u8  bDeviceSubClass; */
  1476. 0x00, /*  __u8  bDeviceProtocol; */
  1477. 0x08, /*  __u8  bMaxPacketSize0; 8 Bytes */
  1478. 0x00, /*  __u16 idVendor; */
  1479. 0x00,
  1480. 0x00, /*  __u16 idProduct; */
  1481. 0x00,
  1482. 0x00, /*  __u16 bcdDevice; */
  1483. 0x00,
  1484. 0x00, /*  __u8  iManufacturer; */
  1485. 0x02, /*  __u8  iProduct; */
  1486. 0x01, /*  __u8  iSerialNumber; */
  1487. 0x01 /*  __u8  bNumConfigurations; */
  1488. };
  1489. /* Configuration descriptor */
  1490. _static __u8 root_hub_config_des[] =
  1491. {
  1492. 0x09, /*  __u8  bLength; */
  1493. 0x02, /*  __u8  bDescriptorType; Configuration */
  1494. 0x19, /*  __u16 wTotalLength; */
  1495. 0x00,
  1496. 0x01, /*  __u8  bNumInterfaces; */
  1497. 0x01, /*  __u8  bConfigurationValue; */
  1498. 0x00, /*  __u8  iConfiguration; */
  1499. 0x40, /*  __u8  bmAttributes; 
  1500.    Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
  1501. 0x00, /*  __u8  MaxPower; */
  1502.      /* interface */
  1503. 0x09, /*  __u8  if_bLength; */
  1504. 0x04, /*  __u8  if_bDescriptorType; Interface */
  1505. 0x00, /*  __u8  if_bInterfaceNumber; */
  1506. 0x00, /*  __u8  if_bAlternateSetting; */
  1507. 0x01, /*  __u8  if_bNumEndpoints; */
  1508. 0x09, /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
  1509. 0x00, /*  __u8  if_bInterfaceSubClass; */
  1510. 0x00, /*  __u8  if_bInterfaceProtocol; */
  1511. 0x00, /*  __u8  if_iInterface; */
  1512.      /* endpoint */
  1513. 0x07, /*  __u8  ep_bLength; */
  1514. 0x05, /*  __u8  ep_bDescriptorType; Endpoint */
  1515. 0x81, /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
  1516. 0x03, /*  __u8  ep_bmAttributes; Interrupt */
  1517. 0x08, /*  __u16 ep_wMaxPacketSize; 8 Bytes */
  1518. 0x00,
  1519. 0xff /*  __u8  ep_bInterval; 255 ms */
  1520. };
  1521. _static __u8 root_hub_hub_des[] =
  1522. {
  1523. 0x09, /*  __u8  bLength; */
  1524. 0x29, /*  __u8  bDescriptorType; Hub-descriptor */
  1525. 0x02, /*  __u8  bNbrPorts; */
  1526. 0x00, /* __u16  wHubCharacteristics; */
  1527. 0x00,
  1528. 0x01, /*  __u8  bPwrOn2pwrGood; 2ms */
  1529. 0x00, /*  __u8  bHubContrCurrent; 0 mA */
  1530. 0x00, /*  __u8  DeviceRemovable; *** 7 Ports max *** */
  1531. 0xff /*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
  1532. };
  1533. /*-------------------------------------------------------------------------*/
  1534. /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
  1535. _static int rh_send_irq (struct urb *urb)
  1536. {
  1537. int len = 1;
  1538. int i;
  1539. uhci_t *uhci = urb->dev->bus->hcpriv;
  1540. unsigned int io_addr = uhci->io_addr;
  1541. __u16 data = 0;
  1542. for (i = 0; i < uhci->rh.numports; i++) {
  1543. data |= ((inw (io_addr + USBPORTSC1 + i * 2) & 0xa) > 0 ? (1 << (i + 1)) : 0);
  1544. len = (i + 1) / 8 + 1;
  1545. }
  1546. *(__u16 *) urb->transfer_buffer = cpu_to_le16 (data);
  1547. urb->actual_length = len;
  1548. urb->status = 0;
  1549. if ((data > 0) && (uhci->rh.send != 0)) {
  1550. dbg("Root-Hub INT complete: port1: %x port2: %x data: %x",
  1551.      inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2), data);
  1552. urb->complete (urb);
  1553. }
  1554. return 0;
  1555. }
  1556. /*-------------------------------------------------------------------------*/
  1557. /* Virtual Root Hub INTs are polled by this timer every "intervall" ms */
  1558. _static int rh_init_int_timer (struct urb *urb);
  1559. _static void rh_int_timer_do (unsigned long ptr)
  1560. {
  1561. int len;
  1562. struct urb *urb = (struct urb*) ptr;
  1563. uhci_t *uhci = urb->dev->bus->hcpriv;
  1564. if (uhci->rh.send) {
  1565. len = rh_send_irq (urb);
  1566. if (len > 0) {
  1567. urb->actual_length = len;
  1568. if (urb->complete)
  1569. urb->complete (urb);
  1570. }
  1571. }
  1572. rh_init_int_timer (urb);
  1573. }
  1574. /*-------------------------------------------------------------------------*/
  1575. /* Root Hub INTs are polled by this timer, polling interval 20ms */
  1576. _static int rh_init_int_timer (struct urb *urb)
  1577. {
  1578. uhci_t *uhci = urb->dev->bus->hcpriv;
  1579. uhci->rh.interval = urb->interval;
  1580. init_timer (&uhci->rh.rh_int_timer);
  1581. uhci->rh.rh_int_timer.function = rh_int_timer_do;
  1582. uhci->rh.rh_int_timer.data = (unsigned long) urb;
  1583. uhci->rh.rh_int_timer.expires = jiffies + (HZ * 20) / 1000;
  1584. add_timer (&uhci->rh.rh_int_timer);
  1585. return 0;
  1586. }
  1587. /*-------------------------------------------------------------------------*/
  1588. #define OK(x)  len = (x); break
  1589. #define CLR_RH_PORTSTAT(x) 
  1590. status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); 
  1591. status = (status & 0xfff5) & ~(x); 
  1592. outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
  1593. #define SET_RH_PORTSTAT(x) 
  1594. status = inw(io_addr+USBPORTSC1+2*(wIndex-1)); 
  1595. status = (status & 0xfff5) | (x); 
  1596. outw(status, io_addr+USBPORTSC1+2*(wIndex-1))
  1597. /*-------------------------------------------------------------------------*/
  1598. /****
  1599.  ** Root Hub Control Pipe
  1600.  *************************/
  1601. _static int rh_submit_urb (struct urb *urb)
  1602. {
  1603. struct usb_device *usb_dev = urb->dev;
  1604. uhci_t *uhci = usb_dev->bus->hcpriv;
  1605. unsigned int pipe = urb->pipe;
  1606. struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *) urb->setup_packet;
  1607. void *data = urb->transfer_buffer;
  1608. int leni = urb->transfer_buffer_length;
  1609. int len = 0;
  1610. int status = 0;
  1611. int stat = 0;
  1612. int i;
  1613. unsigned int io_addr = uhci->io_addr;
  1614. __u16 cstatus;
  1615. __u16 bmRType_bReq;
  1616. __u16 wValue;
  1617. __u16 wIndex;
  1618. __u16 wLength;
  1619. if (usb_pipetype (pipe) == PIPE_INTERRUPT) {
  1620. dbg("Root-Hub submit IRQ: every %d ms", urb->interval);
  1621. uhci->rh.urb = urb;
  1622. uhci->rh.send = 1;
  1623. uhci->rh.interval = urb->interval;
  1624. rh_init_int_timer (urb);
  1625. return 0;
  1626. }
  1627. bmRType_bReq = cmd->bRequestType | cmd->bRequest << 8;
  1628. wValue = le16_to_cpu (cmd->wValue);
  1629. wIndex = le16_to_cpu (cmd->wIndex);
  1630. wLength = le16_to_cpu (cmd->wLength);
  1631. for (i = 0; i < 8; i++)
  1632. uhci->rh.c_p_r[i] = 0;
  1633. dbg("Root-Hub: adr: %2x cmd(%1x): %04x %04x %04x %04x",
  1634.      uhci->rh.devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
  1635. switch (bmRType_bReq) {
  1636. /* Request Destination:
  1637.    without flags: Device, 
  1638.    RH_INTERFACE: interface, 
  1639.    RH_ENDPOINT: endpoint,
  1640.    RH_CLASS means HUB here, 
  1641.    RH_OTHER | RH_CLASS  almost ever means HUB_PORT here 
  1642.  */
  1643. case RH_GET_STATUS:
  1644. *(__u16 *) data = cpu_to_le16 (1);
  1645. OK (2);
  1646. case RH_GET_STATUS | RH_INTERFACE:
  1647. *(__u16 *) data = cpu_to_le16 (0);
  1648. OK (2);
  1649. case RH_GET_STATUS | RH_ENDPOINT:
  1650. *(__u16 *) data = cpu_to_le16 (0);
  1651. OK (2);
  1652. case RH_GET_STATUS | RH_CLASS:
  1653. *(__u32 *) data = cpu_to_le32 (0);
  1654. OK (4); /* hub power ** */
  1655. case RH_GET_STATUS | RH_OTHER | RH_CLASS:
  1656. status = inw (io_addr + USBPORTSC1 + 2 * (wIndex - 1));
  1657. cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
  1658. ((status & USBPORTSC_PEC) >> (3 - 1)) |
  1659. (uhci->rh.c_p_r[wIndex - 1] << (0 + 4));
  1660. status = (status & USBPORTSC_CCS) |
  1661. ((status & USBPORTSC_PE) >> (2 - 1)) |
  1662. ((status & USBPORTSC_SUSP) >> (12 - 2)) |
  1663. ((status & USBPORTSC_PR) >> (9 - 4)) |
  1664. (1 << 8) | /* power on ** */
  1665. ((status & USBPORTSC_LSDA) << (-8 + 9));
  1666. *(__u16 *) data = cpu_to_le16 (status);
  1667. *(__u16 *) (data + 2) = cpu_to_le16 (cstatus);
  1668. OK (4);
  1669. case RH_CLEAR_FEATURE | RH_ENDPOINT:
  1670. switch (wValue) {
  1671. case (RH_ENDPOINT_STALL):
  1672. OK (0);
  1673. }
  1674. break;
  1675. case RH_CLEAR_FEATURE | RH_CLASS:
  1676. switch (wValue) {
  1677. case (RH_C_HUB_OVER_CURRENT):
  1678. OK (0); /* hub power over current ** */
  1679. }
  1680. break;
  1681. case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
  1682. switch (wValue) {
  1683. case (RH_PORT_ENABLE):
  1684. CLR_RH_PORTSTAT (USBPORTSC_PE);
  1685. OK (0);
  1686. case (RH_PORT_SUSPEND):
  1687. CLR_RH_PORTSTAT (USBPORTSC_SUSP);
  1688. OK (0);
  1689. case (RH_PORT_POWER):
  1690. OK (0); /* port power ** */
  1691. case (RH_C_PORT_CONNECTION):
  1692. SET_RH_PORTSTAT (USBPORTSC_CSC);
  1693. OK (0);
  1694. case (RH_C_PORT_ENABLE):
  1695. SET_RH_PORTSTAT (USBPORTSC_PEC);
  1696. OK (0);
  1697. case (RH_C_PORT_SUSPEND):
  1698. /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
  1699. OK (0);
  1700. case (RH_C_PORT_OVER_CURRENT):
  1701. OK (0); /* port power over current ** */
  1702. case (RH_C_PORT_RESET):
  1703. uhci->rh.c_p_r[wIndex - 1] = 0;
  1704. OK (0);
  1705. }
  1706. break;
  1707. case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
  1708. switch (wValue) {
  1709. case (RH_PORT_SUSPEND):
  1710. SET_RH_PORTSTAT (USBPORTSC_SUSP);
  1711. OK (0);
  1712. case (RH_PORT_RESET):
  1713. SET_RH_PORTSTAT (USBPORTSC_PR);
  1714. uhci_wait_ms (10);
  1715. uhci->rh.c_p_r[wIndex - 1] = 1;
  1716. CLR_RH_PORTSTAT (USBPORTSC_PR);
  1717. udelay (10);
  1718. SET_RH_PORTSTAT (USBPORTSC_PE);
  1719. uhci_wait_ms (10);
  1720. SET_RH_PORTSTAT (0xa);
  1721. OK (0);
  1722. case (RH_PORT_POWER):
  1723. OK (0); /* port power ** */
  1724. case (RH_PORT_ENABLE):
  1725. SET_RH_PORTSTAT (USBPORTSC_PE);
  1726. OK (0);
  1727. }
  1728. break;
  1729. case RH_SET_ADDRESS:
  1730. uhci->rh.devnum = wValue;
  1731. OK (0);
  1732. case RH_GET_DESCRIPTOR:
  1733. switch ((wValue & 0xff00) >> 8) {
  1734. case (0x01): /* device descriptor */
  1735. len = min_t(unsigned int, leni,
  1736.   min_t(unsigned int,
  1737.       sizeof (root_hub_dev_des), wLength));
  1738. memcpy (data, root_hub_dev_des, len);
  1739. OK (len);
  1740. case (0x02): /* configuration descriptor */
  1741. len = min_t(unsigned int, leni,
  1742.   min_t(unsigned int,
  1743.       sizeof (root_hub_config_des), wLength));
  1744. memcpy (data, root_hub_config_des, len);
  1745. OK (len);
  1746. case (0x03): /* string descriptors */
  1747. len = usb_root_hub_string (wValue & 0xff,
  1748.         uhci->io_addr, "UHCI",
  1749. data, wLength);
  1750. if (len > 0) {
  1751. OK(min_t(int, leni, len));
  1752. } else 
  1753. stat = -EPIPE;
  1754. }
  1755. break;
  1756. case RH_GET_DESCRIPTOR | RH_CLASS:
  1757. root_hub_hub_des[2] = uhci->rh.numports;
  1758. len = min_t(unsigned int, leni,
  1759.   min_t(unsigned int, sizeof (root_hub_hub_des), wLength));
  1760. memcpy (data, root_hub_hub_des, len);
  1761. OK (len);
  1762. case RH_GET_CONFIGURATION:
  1763. *(__u8 *) data = 0x01;
  1764. OK (1);
  1765. case RH_SET_CONFIGURATION:
  1766. OK (0);
  1767. default:
  1768. stat = -EPIPE;
  1769. }
  1770. dbg("Root-Hub stat port1: %x port2: %x",
  1771.      inw (io_addr + USBPORTSC1), inw (io_addr + USBPORTSC2));
  1772. urb->actual_length = len;
  1773. urb->status = stat;
  1774. urb->dev=NULL;
  1775. if (urb->complete)
  1776. urb->complete (urb);
  1777. return 0;
  1778. }
  1779. /*-------------------------------------------------------------------------*/
  1780. _static int rh_unlink_urb (struct urb *urb)
  1781. {
  1782. uhci_t *uhci = urb->dev->bus->hcpriv;
  1783. if (uhci->rh.urb==urb) {
  1784. dbg("Root-Hub unlink IRQ");
  1785. uhci->rh.send = 0;
  1786. del_timer (&uhci->rh.rh_int_timer);
  1787. }
  1788. return 0;
  1789. }
  1790. /*-------------------------------------------------------------------*/
  1791. /*
  1792.  * Map status to standard result codes
  1793.  *
  1794.  * <status> is (td->status & 0xFE0000) [a.k.a. uhci_status_bits(td->status)
  1795.  * <dir_out> is True for output TDs and False for input TDs.
  1796.  */
  1797. _static int uhci_map_status (int status, int dir_out)
  1798. {
  1799. if (!status)
  1800. return 0;
  1801. if (status & TD_CTRL_BITSTUFF) /* Bitstuff error */
  1802. return -EPROTO;
  1803. if (status & TD_CTRL_CRCTIMEO) { /* CRC/Timeout */
  1804. if (dir_out)
  1805. return -ETIMEDOUT;
  1806. else
  1807. return -EILSEQ;
  1808. }
  1809. if (status & TD_CTRL_NAK) /* NAK */
  1810. return -ETIMEDOUT;
  1811. if (status & TD_CTRL_BABBLE) /* Babble */
  1812. return -EOVERFLOW;
  1813. if (status & TD_CTRL_DBUFERR) /* Buffer error */
  1814. return -ENOSR;
  1815. if (status & TD_CTRL_STALLED) /* Stalled */
  1816. return -EPIPE;
  1817. if (status & TD_CTRL_ACTIVE) /* Active */
  1818. return 0;
  1819. return -EPROTO;
  1820. }
  1821. /*
  1822.  * Only the USB core should call uhci_alloc_dev and uhci_free_dev
  1823.  */
  1824. _static int uhci_alloc_dev (struct usb_device *usb_dev)
  1825. {
  1826. return 0;
  1827. }
  1828. _static void uhci_unlink_urbs(uhci_t *s, struct usb_device *usb_dev, int remove_all)
  1829. {
  1830. unsigned long flags;
  1831. struct list_head *p;
  1832. struct list_head *p2;
  1833. struct urb *urb;
  1834. spin_lock_irqsave (&s->urb_list_lock, flags);
  1835. p = s->urb_list.prev;
  1836. while (p != &s->urb_list) {
  1837. p2 = p;
  1838. p = p->prev ;
  1839. urb = list_entry (p2, struct urb, urb_list);
  1840. dbg("urb: %p, dev %p, %p", urb, usb_dev,urb->dev);
  1841. //urb->transfer_flags |=USB_ASYNC_UNLINK; 
  1842. if (remove_all || (usb_dev == urb->dev)) {
  1843. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1844. warn("forced removing of queued URB %p due to disconnect",urb);
  1845. uhci_unlink_urb(urb);
  1846. urb->dev = NULL; // avoid further processing of this URB
  1847. spin_lock_irqsave (&s->urb_list_lock, flags);
  1848. p = s->urb_list.prev;
  1849. }
  1850. }
  1851. spin_unlock_irqrestore (&s->urb_list_lock, flags);
  1852. }
  1853. _static int uhci_free_dev (struct usb_device *usb_dev)
  1854. {
  1855. uhci_t *s;
  1856. if(!usb_dev || !usb_dev->bus || !usb_dev->bus->hcpriv)
  1857. return -EINVAL;
  1858. s=(uhci_t*) usb_dev->bus->hcpriv;
  1859. uhci_unlink_urbs(s, usb_dev, 0);
  1860. return 0;
  1861. }
  1862. /*
  1863.  * uhci_get_current_frame_number()
  1864.  *
  1865.  * returns the current frame number for a USB bus/controller.
  1866.  */
  1867. _static int uhci_get_current_frame_number (struct usb_device *usb_dev)
  1868. {
  1869. return UHCI_GET_CURRENT_FRAME ((uhci_t*) usb_dev->bus->hcpriv);
  1870. }
  1871. struct usb_operations uhci_device_operations =
  1872. {
  1873. uhci_alloc_dev,
  1874. uhci_free_dev,
  1875. uhci_get_current_frame_number,
  1876. uhci_submit_urb,
  1877. uhci_unlink_urb
  1878. };
  1879. _static void correct_data_toggles(struct urb *urb)
  1880. {
  1881. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), 
  1882.        !usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)));
  1883. while(urb) {
  1884. urb_priv_t *priv=urb->hcpriv;
  1885. uhci_desc_t *qh = list_entry (priv->desc_list.next, uhci_desc_t, desc_list);
  1886. struct list_head *p = qh->vertical.next;
  1887. uhci_desc_t *td;
  1888. dbg("URB to correct %pn", urb);
  1889. for (; p != &qh->vertical; p = p->next) {
  1890. td = list_entry (p, uhci_desc_t, vertical);
  1891. td->hw.td.info^=cpu_to_le32(1<<TD_TOKEN_TOGGLE);
  1892. }
  1893. urb=priv->next_queued_urb;
  1894. }
  1895. }
  1896. /* 
  1897.  * For IN-control transfers, process_transfer gets a bit more complicated,
  1898.  * since there are devices that return less data (eg. strings) than they
  1899.  * have announced. This leads to a queue abort due to the short packet,
  1900.  * the status stage is not executed. If this happens, the status stage
  1901.  * is manually re-executed.
  1902.  * mode: PROCESS_TRANSFER_REGULAR: regular (unlink QH)
  1903.  *       PROCESS_TRANSFER_DONT_UNLINK: QHs already unlinked (for async unlink_urb)
  1904.  */
  1905. _static int process_transfer (uhci_t *s, struct urb *urb, int mode)
  1906. {
  1907. int ret = 0;
  1908. urb_priv_t *urb_priv = urb->hcpriv;
  1909. struct list_head *qhl = urb_priv->desc_list.next;
  1910. uhci_desc_t *qh = list_entry (qhl, uhci_desc_t, desc_list);
  1911. struct list_head *p = qh->vertical.next;
  1912. uhci_desc_t *desc= list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
  1913. uhci_desc_t *last_desc = list_entry (desc->vertical.prev, uhci_desc_t, vertical);
  1914. int data_toggle = usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe)); // save initial data_toggle
  1915. int maxlength;  // extracted and remapped info from TD
  1916. int actual_length;
  1917. int status = 0;
  1918. //dbg("process_transfer: urb %p, urb_priv %p, qh %p last_desc %pn",urb,urb_priv, qh, last_desc);
  1919. /* if the status phase has been retriggered and the
  1920.    queue is empty or the last status-TD is inactive, the retriggered
  1921.    status stage is completed
  1922.  */
  1923. if (urb_priv->flags && 
  1924.     ((qh->hw.qh.element == cpu_to_le32(UHCI_PTR_TERM)) || !is_td_active(desc)))
  1925. goto transfer_finished;
  1926. urb->actual_length=0;
  1927. for (; p != &qh->vertical; p = p->next) {
  1928. desc = list_entry (p, uhci_desc_t, vertical);
  1929. if (is_td_active(desc)) { // do not process active TDs
  1930. if (mode == CLEAN_TRANSFER_DELETION_MARK) // if called from async_unlink
  1931. uhci_clean_transfer(s, urb, qh, CLEAN_TRANSFER_DELETION_MARK);
  1932. return ret;
  1933. }
  1934. actual_length = uhci_actual_length(le32_to_cpu(desc->hw.td.status)); // extract transfer parameters from TD
  1935. maxlength = (((le32_to_cpu(desc->hw.td.info) >> 21) & 0x7ff) + 1) & 0x7ff;
  1936. status = uhci_map_status (uhci_status_bits (le32_to_cpu(desc->hw.td.status)), usb_pipeout (urb->pipe));
  1937. if (status == -EPIPE) {  // see if EP is stalled
  1938. // set up stalled condition
  1939. usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
  1940. }
  1941. if (status && (status != -EPIPE)) { // if any error occurred stop processing of further TDs
  1942. // only set ret if status returned an error
  1943.   is_error:
  1944. ret = status;
  1945. urb->error_count++;
  1946. break;
  1947. }
  1948. else if ((le32_to_cpu(desc->hw.td.info) & 0xff) != USB_PID_SETUP)
  1949. urb->actual_length += actual_length;
  1950. // got less data than requested
  1951. if ( (actual_length < maxlength)) {
  1952. if (urb->transfer_flags & USB_DISABLE_SPD) {
  1953. status = -EREMOTEIO; // treat as real error
  1954. dbg("process_transfer: SPD!!");
  1955. break; // exit after this TD because SP was detected
  1956. }
  1957. // short read during control-IN: re-start status stage
  1958. if ((usb_pipetype (urb->pipe) == PIPE_CONTROL)) {
  1959. if (uhci_packetid(le32_to_cpu(last_desc->hw.td.info)) == USB_PID_OUT) {
  1960. set_qh_element(qh, last_desc->dma_addr);  // re-trigger status stage
  1961. dbg("short packet during control transfer, retrigger status stage @ %p",last_desc);
  1962. urb_priv->flags = 1; // mark as short control packet
  1963. return 0;
  1964. }
  1965. }
  1966. // all other cases: short read is OK
  1967. data_toggle = uhci_toggle (le32_to_cpu(desc->hw.td.info));
  1968. break;
  1969. }
  1970. else if (status)
  1971. goto is_error;
  1972. data_toggle = uhci_toggle (le32_to_cpu(desc->hw.td.info));
  1973. queue_dbg("process_transfer: len:%d status:%x mapped:%x toggle:%d", actual_length, le32_to_cpu(desc->hw.td.status),status, data_toggle);      
  1974. }
  1975. if (usb_pipetype (urb->pipe) == PIPE_BULK ) {  /* toggle correction for short bulk transfers (nonqueued/queued) */
  1976. urb_priv_t *priv=(urb_priv_t*)urb->hcpriv;
  1977. struct urb *next_queued_urb=priv->next_queued_urb;
  1978. if (next_queued_urb) {
  1979. urb_priv_t *next_priv=(urb_priv_t*)next_queued_urb->hcpriv;
  1980. uhci_desc_t *qh = list_entry (next_priv->desc_list.next, uhci_desc_t, desc_list);
  1981. uhci_desc_t *first_td=list_entry (qh->vertical.next, uhci_desc_t, vertical);
  1982. if (data_toggle == uhci_toggle (le32_to_cpu(first_td->hw.td.info))) {
  1983. err("process_transfer: fixed toggle");
  1984. correct_data_toggles(next_queued_urb);
  1985. }
  1986. }
  1987. else
  1988. usb_settoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe), !data_toggle);
  1989. }
  1990.  transfer_finished:
  1991. uhci_clean_transfer(s, urb, qh, mode);
  1992. urb->status = status;
  1993. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  1994. disable_desc_loop(s,urb);
  1995. #endif
  1996. queue_dbg("process_transfer: (end) urb %p, wanted len %d, len %d status %x err %d",
  1997. urb,urb->transfer_buffer_length,urb->actual_length, urb->status, urb->error_count);
  1998. return ret;
  1999. }
  2000. _static int process_interrupt (uhci_t *s, struct urb *urb)
  2001. {
  2002. int i, ret = -EINPROGRESS;
  2003. urb_priv_t *urb_priv = urb->hcpriv;
  2004. struct list_head *p = urb_priv->desc_list.next;
  2005. uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
  2006. int actual_length;
  2007. int status = 0;
  2008. //dbg("urb contains interrupt request");
  2009. for (i = 0; p != &urb_priv->desc_list; p = p->next, i++) // Maybe we allow more than one TD later ;-)
  2010. {
  2011. desc = list_entry (p, uhci_desc_t, desc_list);
  2012. if (is_td_active(desc)) {
  2013. // do not process active TDs
  2014. //dbg("TD ACT Status @%p %08x",desc,le32_to_cpu(desc->hw.td.status));
  2015. break;
  2016. }
  2017. if (!(desc->hw.td.status & cpu_to_le32(TD_CTRL_IOC))) {
  2018. // do not process one-shot TDs, no recycling
  2019. break;
  2020. }
  2021. // extract transfer parameters from TD
  2022. actual_length = uhci_actual_length(le32_to_cpu(desc->hw.td.status));
  2023. status = uhci_map_status (uhci_status_bits (le32_to_cpu(desc->hw.td.status)), usb_pipeout (urb->pipe));
  2024. // see if EP is stalled
  2025. if (status == -EPIPE) {
  2026. // set up stalled condition
  2027. usb_endpoint_halt (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
  2028. }
  2029. // if any error occurred: ignore this td, and continue
  2030. if (status != 0) {
  2031. //uhci_show_td (desc);
  2032. urb->error_count++;
  2033. goto recycle;
  2034. }
  2035. else
  2036. urb->actual_length = actual_length;
  2037. recycle:
  2038. uhci_urb_dma_sync(s, urb, urb->hcpriv);
  2039. if (urb->complete) {
  2040. //dbg("process_interrupt: calling completion, status %i",status);
  2041. urb->status = status;
  2042. ((urb_priv_t*)urb->hcpriv)->flags=1; // if unlink_urb is called during completion
  2043. spin_unlock(&s->urb_list_lock);
  2044. urb->complete ((struct urb *) urb);
  2045. spin_lock(&s->urb_list_lock);
  2046. ((urb_priv_t*)urb->hcpriv)->flags=0;        
  2047. }
  2048. if ((urb->status != -ECONNABORTED) && (urb->status != ECONNRESET) &&
  2049.     (urb->status != -ENOENT)) {
  2050. urb->status = -EINPROGRESS;
  2051. // Recycle INT-TD if interval!=0, else mark TD as one-shot
  2052. if (urb->interval) {
  2053. desc->hw.td.info &= cpu_to_le32(~(1 << TD_TOKEN_TOGGLE));
  2054. if (status==0) {
  2055. ((urb_priv_t*)urb->hcpriv)->started=jiffies;
  2056. desc->hw.td.info |= cpu_to_le32((usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
  2057.     usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE));
  2058. usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
  2059. } else {
  2060. desc->hw.td.info |= cpu_to_le32((!usb_gettoggle (urb->dev, usb_pipeendpoint (urb->pipe),
  2061.      usb_pipeout (urb->pipe)) << TD_TOKEN_TOGGLE));
  2062. }
  2063. desc->hw.td.status= cpu_to_le32((urb->pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC |
  2064. (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27));
  2065. mb();
  2066. }
  2067. else {
  2068. uhci_unlink_urb_async(s, urb, UNLINK_ASYNC_STORE_URB);
  2069. // correct toggle after unlink
  2070. usb_dotoggle (urb->dev, usb_pipeendpoint (urb->pipe), usb_pipeout (urb->pipe));
  2071. clr_td_ioc(desc); // inactivate TD
  2072. }
  2073. }
  2074. }
  2075. return ret;
  2076. }
  2077. // mode: PROCESS_ISO_REGULAR: processing only for done TDs, unlink TDs
  2078. // mode: PROCESS_ISO_FORCE: force processing, don't unlink TDs (already unlinked)
  2079. _static int process_iso (uhci_t *s, struct urb *urb, int mode)
  2080. {
  2081. int i;
  2082. int ret = 0;
  2083. urb_priv_t *urb_priv = urb->hcpriv;
  2084. struct list_head *p = urb_priv->desc_list.next, *p_tmp;
  2085. uhci_desc_t *desc = list_entry (urb_priv->desc_list.prev, uhci_desc_t, desc_list);
  2086. dbg("urb contains iso request");
  2087. if (is_td_active(desc) && mode==PROCESS_ISO_REGULAR)
  2088. return -EXDEV; // last TD not finished
  2089. urb->error_count = 0;
  2090. urb->actual_length = 0;
  2091. urb->status = 0;
  2092. dbg("process iso urb %p, %li, %i, %i, %i %08x",urb,jiffies,UHCI_GET_CURRENT_FRAME(s),
  2093.     urb->number_of_packets,mode,le32_to_cpu(desc->hw.td.status));
  2094. for (i = 0; p != &urb_priv->desc_list;  i++) {
  2095. desc = list_entry (p, uhci_desc_t, desc_list);
  2096. //uhci_show_td(desc);
  2097. if (is_td_active(desc)) {
  2098. // means we have completed the last TD, but not the TDs before
  2099. desc->hw.td.status &= cpu_to_le32(~TD_CTRL_ACTIVE);
  2100. dbg("TD still active (%x)- grrr. paranoia!", le32_to_cpu(desc->hw.td.status));
  2101. ret = -EXDEV;
  2102. urb->iso_frame_desc[i].status = ret;
  2103. unlink_td (s, desc, 1);
  2104. // FIXME: immediate deletion may be dangerous
  2105. goto err;
  2106. }
  2107. if (mode == PROCESS_ISO_REGULAR)
  2108. unlink_td (s, desc, 1);
  2109. if (urb->number_of_packets <= i) {
  2110. dbg("urb->number_of_packets (%d)<=(%d)", urb->number_of_packets, i);
  2111. ret = -EINVAL;
  2112. goto err;
  2113. }
  2114. urb->iso_frame_desc[i].actual_length = uhci_actual_length(le32_to_cpu(desc->hw.td.status));
  2115. urb->iso_frame_desc[i].status = uhci_map_status (uhci_status_bits (le32_to_cpu(desc->hw.td.status)), usb_pipeout (urb->pipe));
  2116. urb->actual_length += urb->iso_frame_desc[i].actual_length;
  2117.       err:
  2118. if (urb->iso_frame_desc[i].status != 0) {
  2119. urb->error_count++;
  2120. urb->status = urb->iso_frame_desc[i].status;
  2121. }
  2122. dbg("process_iso: %i: len:%d %08x status:%x",
  2123.      i, urb->iso_frame_desc[i].actual_length, le32_to_cpu(desc->hw.td.status),urb->iso_frame_desc[i].status);
  2124. p_tmp = p;
  2125. p = p->next;
  2126. list_del (p_tmp);
  2127. delete_desc (s, desc);
  2128. }
  2129. dbg("process_iso: exit %i (%d), actual_len %i", i, ret,urb->actual_length);
  2130. return ret;
  2131. }
  2132. _static int process_urb (uhci_t *s, struct list_head *p)
  2133. {
  2134. int ret = 0;
  2135. struct urb *urb;
  2136. urb=list_entry (p, struct urb, urb_list);
  2137. //dbg("process_urb: found queued urb: %p", urb);
  2138. switch (usb_pipetype (urb->pipe)) {
  2139. case PIPE_CONTROL:
  2140. ret = process_transfer (s, urb, CLEAN_TRANSFER_REGULAR);
  2141. break;
  2142. case PIPE_BULK:
  2143. if (!s->avoid_bulk.counter)
  2144. ret = process_transfer (s, urb, CLEAN_TRANSFER_REGULAR);
  2145. else
  2146. return 0;
  2147. break;
  2148. case PIPE_ISOCHRONOUS:
  2149. ret = process_iso (s, urb, PROCESS_ISO_REGULAR);
  2150. break;
  2151. case PIPE_INTERRUPT:
  2152. ret = process_interrupt (s, urb);
  2153. break;
  2154. }
  2155. if (urb->status != -EINPROGRESS) {
  2156. urb_priv_t *urb_priv;
  2157. struct usb_device *usb_dev;
  2158. usb_dev=urb->dev;
  2159. /* Release bandwidth for Interrupt or Iso transfers */
  2160. if (urb->bandwidth) {
  2161. if (usb_pipetype(urb->pipe)==PIPE_ISOCHRONOUS)
  2162. usb_release_bandwidth (urb->dev, urb, 1);
  2163. else if (usb_pipetype(urb->pipe)==PIPE_INTERRUPT && urb->interval)
  2164. usb_release_bandwidth (urb->dev, urb, 0);
  2165. }
  2166. dbg("dequeued urb: %p", urb);
  2167. dequeue_urb (s, urb);
  2168. urb_priv = urb->hcpriv;
  2169. uhci_urb_dma_unmap(s, urb, urb_priv);
  2170. #ifdef DEBUG_SLAB
  2171. kmem_cache_free(urb_priv_kmem, urb_priv);
  2172. #else
  2173. kfree (urb_priv);
  2174. #endif
  2175. if ((usb_pipetype (urb->pipe) != PIPE_INTERRUPT)) {  // process_interrupt does completion on its own
  2176. struct urb *next_urb = urb->next;
  2177. int is_ring = 0;
  2178. int contains_killed = 0;
  2179. int loop_count=0;
  2180. if (next_urb) {
  2181. // Find out if the URBs are linked to a ring
  2182. while  (next_urb != NULL && next_urb != urb && loop_count < MAX_NEXT_COUNT) {
  2183. if (next_urb->status == -ENOENT) {// killed URBs break ring structure & resubmission
  2184. contains_killed = 1;
  2185. break;
  2186. }
  2187. next_urb = next_urb->next;
  2188. loop_count++;
  2189. }
  2190. if (loop_count == MAX_NEXT_COUNT)
  2191. err("process_urb: Too much linked URBs in ring detection!");
  2192. if (next_urb == urb)
  2193. is_ring=1;
  2194. }
  2195. // Submit idle/non-killed URBs linked with urb->next
  2196. // Stop before the current URB
  2197. next_urb = urb->next;
  2198. if (next_urb && !contains_killed) {
  2199. int ret_submit;
  2200. next_urb = urb->next;
  2201. loop_count=0;
  2202. while (next_urb != NULL && next_urb != urb && loop_count < MAX_NEXT_COUNT) {
  2203. if (next_urb->status != -EINPROGRESS) {
  2204. if (next_urb->status == -ENOENT) 
  2205. break;
  2206. spin_unlock(&s->urb_list_lock);
  2207. ret_submit=uhci_submit_urb(next_urb);
  2208. spin_lock(&s->urb_list_lock);
  2209. if (ret_submit)
  2210. break;
  2211. }
  2212. loop_count++;
  2213. next_urb = next_urb->next;
  2214. }
  2215. if (loop_count == MAX_NEXT_COUNT)
  2216. err("process_urb: Too much linked URBs in resubmission!");
  2217. }
  2218. // Completion
  2219. if (urb->complete) {
  2220. int was_unlinked = (urb->status == -ENOENT);
  2221. urb->dev = NULL;
  2222. spin_unlock(&s->urb_list_lock);
  2223. urb->complete ((struct urb *) urb);
  2224. // Re-submit the URB if ring-linked
  2225. if (is_ring && !was_unlinked && !contains_killed) {
  2226. urb->dev=usb_dev;
  2227. uhci_submit_urb (urb);
  2228. }
  2229. spin_lock(&s->urb_list_lock);
  2230. }
  2231. usb_dec_dev_use (usb_dev);
  2232. }
  2233. }
  2234. return ret;
  2235. }
  2236. _static void uhci_interrupt (int irq, void *__uhci, struct pt_regs *regs)
  2237. {
  2238. uhci_t *s = __uhci;
  2239. unsigned int io_addr = s->io_addr;
  2240. unsigned short status;
  2241. struct list_head *p, *p2;
  2242. int restarts, work_done;
  2243. /*
  2244.  * Read the interrupt status, and write it back to clear the
  2245.  * interrupt cause
  2246.  */
  2247. status = inw (io_addr + USBSTS);
  2248. if (!status) /* shared interrupt, not mine */
  2249. return;
  2250. dbg("interrupt");
  2251. if (status != 1) {
  2252. // Avoid too much error messages at a time
  2253. if (time_after(jiffies, s->last_error_time + ERROR_SUPPRESSION_TIME)) {
  2254. warn("interrupt, status %x, frame# %i", status, 
  2255.      UHCI_GET_CURRENT_FRAME(s));
  2256. s->last_error_time = jiffies;
  2257. }
  2258. // remove host controller halted state
  2259. if ((status&0x20) && (s->running)) {
  2260. err("Host controller halted, trying to restart.");
  2261. outw (USBCMD_RS | inw(io_addr + USBCMD), io_addr + USBCMD);
  2262. }
  2263. //uhci_show_status (s);
  2264. }
  2265. /*
  2266.  * traverse the list in *reverse* direction, because new entries
  2267.  * may be added at the end.
  2268.  * also, because process_urb may unlink the current urb,
  2269.  * we need to advance the list before
  2270.  * New: check for max. workload and restart count
  2271.  */
  2272. spin_lock (&s->urb_list_lock);
  2273. restarts=0;
  2274. work_done=0;
  2275. restart:
  2276. s->unlink_urb_done=0;
  2277. p = s->urb_list.prev;
  2278. while (p != &s->urb_list && (work_done < 1024)) {
  2279. p2 = p;
  2280. p = p->prev;
  2281. process_urb (s, p2);
  2282. work_done++;
  2283. if (s->unlink_urb_done) {
  2284. s->unlink_urb_done=0;
  2285. restarts++;
  2286. if (restarts<16) // avoid endless restarts
  2287. goto restart;
  2288. else 
  2289. break;
  2290. }
  2291. }
  2292. if (time_after(jiffies, s->timeout_check + (HZ/30)))
  2293. uhci_check_timeouts(s);
  2294. clean_descs(s, CLEAN_NOT_FORCED);
  2295. uhci_cleanup_unlink(s, CLEAN_NOT_FORCED);
  2296. uhci_switch_timer_int(s);
  2297. spin_unlock (&s->urb_list_lock);
  2298. outw (status, io_addr + USBSTS);
  2299. //dbg("uhci_interrupt: done");
  2300. }
  2301. _static void reset_hc (uhci_t *s)
  2302. {
  2303. unsigned int io_addr = s->io_addr;
  2304. s->apm_state = 0;
  2305. /* Global reset for 50ms */
  2306. outw (USBCMD_GRESET, io_addr + USBCMD);
  2307. uhci_wait_ms (50);
  2308. outw (0, io_addr + USBCMD);
  2309. uhci_wait_ms (10);
  2310. }
  2311. _static void start_hc (uhci_t *s)
  2312. {
  2313. unsigned int io_addr = s->io_addr;
  2314. int timeout = 10;
  2315. /*
  2316.  * Reset the HC - this will force us to get a
  2317.  * new notification of any already connected
  2318.  * ports due to the virtual disconnect that it
  2319.  * implies.
  2320.  */
  2321. outw (USBCMD_HCRESET, io_addr + USBCMD);
  2322. while (inw (io_addr + USBCMD) & USBCMD_HCRESET) {
  2323. if (!--timeout) {
  2324. err("USBCMD_HCRESET timed out!");
  2325. break;
  2326. }
  2327. udelay(1);
  2328. }
  2329. /* Turn on all interrupts */
  2330. outw (USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP, io_addr + USBINTR);
  2331. /* Start at frame 0 */
  2332. outw (0, io_addr + USBFRNUM);
  2333. outl (s->framelist_dma, io_addr + USBFLBASEADD);
  2334. /* Run and mark it configured with a 64-byte max packet */
  2335. outw (USBCMD_RS | USBCMD_CF | USBCMD_MAXP, io_addr + USBCMD);
  2336. s->apm_state = 1;
  2337. s->running = 1;
  2338. }
  2339. /* No  __devexit, since it maybe called from alloc_uhci() */
  2340. _static void
  2341. uhci_pci_remove (struct pci_dev *dev)
  2342. {
  2343. uhci_t *s = pci_get_drvdata(dev);
  2344. struct usb_device *root_hub = s->bus->root_hub;
  2345. s->running = 0;     // Don't allow submit_urb
  2346. if (root_hub)
  2347. usb_disconnect (&root_hub);
  2348. reset_hc (s);
  2349. wait_ms (1);
  2350. uhci_unlink_urbs (s, 0, CLEAN_FORCED);  // Forced unlink of remaining URBs
  2351. uhci_cleanup_unlink (s, CLEAN_FORCED);  // force cleanup of async killed URBs
  2352. usb_deregister_bus (s->bus);
  2353. release_region (s->io_addr, s->io_size);
  2354. free_irq (s->irq, s);
  2355. usb_free_bus (s->bus);
  2356. cleanup_skel (s);
  2357. kfree (s);
  2358. }
  2359. _static int __init uhci_start_usb (uhci_t *s)
  2360. { /* start it up */
  2361. /* connect the virtual root hub */
  2362. struct usb_device *usb_dev;
  2363. usb_dev = usb_alloc_dev (NULL, s->bus);
  2364. if (!usb_dev)
  2365. return -1;
  2366. s->bus->root_hub = usb_dev;
  2367. usb_connect (usb_dev);
  2368. if (usb_new_device (usb_dev) != 0) {
  2369. usb_free_dev (usb_dev);
  2370. return -1;
  2371. }
  2372. return 0;
  2373. }
  2374. #ifdef CONFIG_PM
  2375. _static int
  2376. uhci_pci_suspend (struct pci_dev *dev, u32 state)
  2377. {
  2378. reset_hc((uhci_t *) pci_get_drvdata(dev));
  2379. return 0;
  2380. }
  2381. _static int
  2382. uhci_pci_resume (struct pci_dev *dev)
  2383. {
  2384. start_hc((uhci_t *) pci_get_drvdata(dev));
  2385. return 0;
  2386. }
  2387. #endif
  2388. _static int __devinit alloc_uhci (struct pci_dev *dev, int irq, unsigned int io_addr, unsigned int io_size)
  2389. {
  2390. uhci_t *s;
  2391. struct usb_bus *bus;
  2392. char buf[8], *bufp = buf;
  2393. #ifndef __sparc__
  2394. sprintf(buf, "%d", irq);
  2395. #else
  2396. bufp = __irq_itoa(irq);
  2397. #endif
  2398. printk(KERN_INFO __FILE__ ": USB UHCI at I/O 0x%x, IRQ %sn",
  2399. io_addr, bufp);
  2400. s = kmalloc (sizeof (uhci_t), GFP_KERNEL);
  2401. if (!s)
  2402. return -1;
  2403. memset (s, 0, sizeof (uhci_t));
  2404. INIT_LIST_HEAD (&s->free_desc);
  2405. INIT_LIST_HEAD (&s->urb_list);
  2406. INIT_LIST_HEAD (&s->urb_unlinked);
  2407. spin_lock_init (&s->urb_list_lock);
  2408. spin_lock_init (&s->qh_lock);
  2409. spin_lock_init (&s->td_lock);
  2410. atomic_set(&s->avoid_bulk, 0);
  2411. s->irq = -1;
  2412. s->io_addr = io_addr;
  2413. s->io_size = io_size;
  2414. s->uhci_pci=dev;
  2415. bus = usb_alloc_bus (&uhci_device_operations);
  2416. if (!bus) {
  2417. kfree (s);
  2418. return -1;
  2419. }
  2420. s->bus = bus;
  2421. bus->bus_name = dev->slot_name;
  2422. bus->hcpriv = s;
  2423. /* UHCI specs says devices must have 2 ports, but goes on to say */
  2424. /* they may have more but give no way to determine how many they */
  2425. /* have, so default to 2 */
  2426. /* According to the UHCI spec, Bit 7 is always set to 1. So we try */
  2427. /* to use this to our advantage */
  2428. for (s->maxports = 0; s->maxports < (io_size - 0x10) / 2; s->maxports++) {
  2429. unsigned int portstatus;
  2430. portstatus = inw (io_addr + 0x10 + (s->maxports * 2));
  2431. dbg("port %i, adr %x status %x", s->maxports,
  2432. io_addr + 0x10 + (s->maxports * 2), portstatus);
  2433. if (!(portstatus & 0x0080))
  2434. break;
  2435. }
  2436. warn("Detected %d ports", s->maxports);
  2437. /* This is experimental so anything less than 2 or greater than 8 is */
  2438. /*  something weird and we'll ignore it */
  2439. if (s->maxports < 2 || s->maxports > 8) {
  2440. dbg("Port count misdetected, forcing to 2 ports");
  2441. s->maxports = 2;
  2442. }
  2443. s->rh.numports = s->maxports;
  2444. s->loop_usage=0;
  2445. if (init_skel (s)) {
  2446. usb_free_bus (bus);
  2447. kfree(s);
  2448. return -1;
  2449. }
  2450. request_region (s->io_addr, io_size, MODNAME);
  2451. reset_hc (s);
  2452. usb_register_bus (s->bus);
  2453. start_hc (s);
  2454. if (request_irq (irq, uhci_interrupt, SA_SHIRQ, MODNAME, s)) {
  2455. err("request_irq %d failed!",irq);
  2456. usb_free_bus (bus);
  2457. reset_hc (s);
  2458. release_region (s->io_addr, s->io_size);
  2459. cleanup_skel(s);
  2460. kfree(s);
  2461. return -1;
  2462. }
  2463. /* Enable PIRQ */
  2464. pci_write_config_word (dev, USBLEGSUP, USBLEGSUP_DEFAULT);
  2465. s->irq = irq;
  2466. if(uhci_start_usb (s) < 0) {
  2467. uhci_pci_remove(dev);
  2468. return -1;
  2469. }
  2470. //chain new uhci device into global list
  2471. pci_set_drvdata(dev, s);
  2472. devs=s;
  2473. return 0;
  2474. }
  2475. _static int __devinit
  2476. uhci_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
  2477. {
  2478. int i;
  2479. if (pci_enable_device(dev) < 0)
  2480. return -ENODEV;
  2481. if (!dev->irq) {
  2482. err("found UHCI device with no IRQ assigned. check BIOS settings!");
  2483. return -ENODEV;
  2484. }
  2485. pci_set_master(dev);
  2486. /* Search for the IO base address.. */
  2487. for (i = 0; i < 6; i++) {
  2488. unsigned int io_addr = pci_resource_start(dev, i);
  2489. unsigned int io_size = pci_resource_len(dev, i);
  2490. if (!(pci_resource_flags(dev,i) & IORESOURCE_IO))
  2491. continue;
  2492. /* Is it already in use? */
  2493. if (check_region (io_addr, io_size))
  2494. break;
  2495. /* disable legacy emulation */
  2496. pci_write_config_word (dev, USBLEGSUP, 0);
  2497. return alloc_uhci(dev, dev->irq, io_addr, io_size);
  2498. }
  2499. return -ENODEV;
  2500. }
  2501. /*-------------------------------------------------------------------------*/
  2502. static const struct pci_device_id __devinitdata uhci_pci_ids [] = { {
  2503. /* handle any USB UHCI controller */
  2504. class:  ((PCI_CLASS_SERIAL_USB << 8) | 0x00),
  2505. class_mask:  ~0,
  2506. /* no matter who makes it */
  2507. vendor: PCI_ANY_ID,
  2508. device: PCI_ANY_ID,
  2509. subvendor: PCI_ANY_ID,
  2510. subdevice: PCI_ANY_ID,
  2511. }, { /* end: all zeroes */ }
  2512. };
  2513. MODULE_DEVICE_TABLE (pci, uhci_pci_ids);
  2514. static struct pci_driver uhci_pci_driver = {
  2515. name: "usb-uhci",
  2516. id_table: &uhci_pci_ids [0],
  2517. probe: uhci_pci_probe,
  2518. remove: uhci_pci_remove,
  2519. #ifdef CONFIG_PM
  2520. suspend: uhci_pci_suspend,
  2521. resume: uhci_pci_resume,
  2522. #endif /* PM */
  2523. };
  2524. /*-------------------------------------------------------------------------*/
  2525. static int __init uhci_hcd_init (void) 
  2526. {
  2527. int retval;
  2528. #ifdef DEBUG_SLAB
  2529. urb_priv_kmem = kmem_cache_create("urb_priv", sizeof(urb_priv_t), 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  2530. if(!urb_priv_kmem) {
  2531. err("kmem_cache_create for urb_priv_t failed (out of memory)");
  2532. return -ENOMEM;
  2533. }
  2534. #endif
  2535. info(VERSTR);
  2536. #ifdef CONFIG_USB_UHCI_HIGH_BANDWIDTH
  2537. info("High bandwidth mode enabled");
  2538. #endif
  2539. retval = pci_module_init (&uhci_pci_driver);
  2540. #ifdef DEBUG_SLAB
  2541. if (retval < 0 ) {
  2542. if (kmem_cache_destroy(urb_priv_kmem))
  2543. err("urb_priv_kmem remained");
  2544. }
  2545. #endif
  2546. info(DRIVER_VERSION ":" DRIVER_DESC);
  2547. return retval;
  2548. }
  2549. static void __exit uhci_hcd_cleanup (void) 
  2550. {      
  2551. pci_unregister_driver (&uhci_pci_driver);
  2552. #ifdef DEBUG_SLAB
  2553. if(kmem_cache_destroy(urb_priv_kmem))
  2554. err("urb_priv_kmem remained");
  2555. #endif
  2556. }
  2557. module_init (uhci_hcd_init);
  2558. module_exit (uhci_hcd_cleanup);
  2559. MODULE_AUTHOR( DRIVER_AUTHOR );
  2560. MODULE_DESCRIPTION( DRIVER_DESC );
  2561. MODULE_LICENSE("GPL");