hcd-uhci.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. /*
  2. * USB UHCI controller emulation
  3. *
  4. * Copyright (c) 2005 Fabrice Bellard
  5. *
  6. * Copyright (c) 2008 Max Krasnyansky
  7. * Magor rewrite of the UHCI data structures parser and frame processor
  8. * Support for fully async operation and multiple outstanding transactions
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include "qemu/osdep.h"
  29. #include "hw/usb.h"
  30. #include "hw/usb/uhci-regs.h"
  31. #include "migration/vmstate.h"
  32. #include "hw/pci/pci.h"
  33. #include "hw/irq.h"
  34. #include "hw/qdev-properties.h"
  35. #include "qapi/error.h"
  36. #include "qemu/timer.h"
  37. #include "qemu/iov.h"
  38. #include "sysemu/dma.h"
  39. #include "trace.h"
  40. #include "qemu/main-loop.h"
  41. #include "qemu/module.h"
  42. #include "qom/object.h"
  43. #include "hcd-uhci.h"
  44. #define FRAME_TIMER_FREQ 1000
  45. #define FRAME_MAX_LOOPS 256
  46. /* Must be large enough to handle 10 frame delay for initial isoc requests */
  47. #define QH_VALID 32
  48. #define MAX_FRAMES_PER_TICK (QH_VALID / 2)
  49. enum {
  50. TD_RESULT_STOP_FRAME = 10,
  51. TD_RESULT_COMPLETE,
  52. TD_RESULT_NEXT_QH,
  53. TD_RESULT_ASYNC_START,
  54. TD_RESULT_ASYNC_CONT,
  55. };
  56. typedef struct UHCIAsync UHCIAsync;
  57. struct UHCIPCIDeviceClass {
  58. PCIDeviceClass parent_class;
  59. UHCIInfo info;
  60. };
  61. /*
  62. * Pending async transaction.
  63. * 'packet' must be the first field because completion
  64. * handler does "(UHCIAsync *) pkt" cast.
  65. */
  66. struct UHCIAsync {
  67. USBPacket packet;
  68. uint8_t static_buf[64]; /* 64 bytes is enough, except for isoc packets */
  69. uint8_t *buf;
  70. UHCIQueue *queue;
  71. QTAILQ_ENTRY(UHCIAsync) next;
  72. uint32_t td_addr;
  73. uint8_t done;
  74. };
  75. struct UHCIQueue {
  76. uint32_t qh_addr;
  77. uint32_t token;
  78. UHCIState *uhci;
  79. USBEndpoint *ep;
  80. QTAILQ_ENTRY(UHCIQueue) next;
  81. QTAILQ_HEAD(, UHCIAsync) asyncs;
  82. int8_t valid;
  83. };
  84. typedef struct UHCI_TD {
  85. uint32_t link;
  86. uint32_t ctrl; /* see TD_CTRL_xxx */
  87. uint32_t token;
  88. uint32_t buffer;
  89. } UHCI_TD;
  90. typedef struct UHCI_QH {
  91. uint32_t link;
  92. uint32_t el_link;
  93. } UHCI_QH;
  94. static void uhci_async_cancel(UHCIAsync *async);
  95. static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td);
  96. static void uhci_resume(void *opaque);
  97. static inline int32_t uhci_queue_token(UHCI_TD *td)
  98. {
  99. if ((td->token & (0xf << 15)) == 0) {
  100. /* ctrl ep, cover ep and dev, not pid! */
  101. return td->token & 0x7ff00;
  102. } else {
  103. /* covers ep, dev, pid -> identifies the endpoint */
  104. return td->token & 0x7ffff;
  105. }
  106. }
  107. static UHCIQueue *uhci_queue_new(UHCIState *s, uint32_t qh_addr, UHCI_TD *td,
  108. USBEndpoint *ep)
  109. {
  110. UHCIQueue *queue;
  111. queue = g_new0(UHCIQueue, 1);
  112. queue->uhci = s;
  113. queue->qh_addr = qh_addr;
  114. queue->token = uhci_queue_token(td);
  115. queue->ep = ep;
  116. QTAILQ_INIT(&queue->asyncs);
  117. QTAILQ_INSERT_HEAD(&s->queues, queue, next);
  118. queue->valid = QH_VALID;
  119. trace_usb_uhci_queue_add(queue->token);
  120. return queue;
  121. }
  122. static void uhci_queue_free(UHCIQueue *queue, const char *reason)
  123. {
  124. UHCIState *s = queue->uhci;
  125. UHCIAsync *async;
  126. while (!QTAILQ_EMPTY(&queue->asyncs)) {
  127. async = QTAILQ_FIRST(&queue->asyncs);
  128. uhci_async_cancel(async);
  129. }
  130. usb_device_ep_stopped(queue->ep->dev, queue->ep);
  131. trace_usb_uhci_queue_del(queue->token, reason);
  132. QTAILQ_REMOVE(&s->queues, queue, next);
  133. g_free(queue);
  134. }
  135. static UHCIQueue *uhci_queue_find(UHCIState *s, UHCI_TD *td)
  136. {
  137. uint32_t token = uhci_queue_token(td);
  138. UHCIQueue *queue;
  139. QTAILQ_FOREACH(queue, &s->queues, next) {
  140. if (queue->token == token) {
  141. return queue;
  142. }
  143. }
  144. return NULL;
  145. }
  146. static bool uhci_queue_verify(UHCIQueue *queue, uint32_t qh_addr, UHCI_TD *td,
  147. uint32_t td_addr, bool queuing)
  148. {
  149. UHCIAsync *first = QTAILQ_FIRST(&queue->asyncs);
  150. uint32_t queue_token_addr = (queue->token >> 8) & 0x7f;
  151. return queue->qh_addr == qh_addr &&
  152. queue->token == uhci_queue_token(td) &&
  153. queue_token_addr == queue->ep->dev->addr &&
  154. (queuing || !(td->ctrl & TD_CTRL_ACTIVE) || first == NULL ||
  155. first->td_addr == td_addr);
  156. }
  157. static UHCIAsync *uhci_async_alloc(UHCIQueue *queue, uint32_t td_addr)
  158. {
  159. UHCIAsync *async = g_new0(UHCIAsync, 1);
  160. async->queue = queue;
  161. async->td_addr = td_addr;
  162. usb_packet_init(&async->packet);
  163. trace_usb_uhci_packet_add(async->queue->token, async->td_addr);
  164. return async;
  165. }
  166. static void uhci_async_free(UHCIAsync *async)
  167. {
  168. trace_usb_uhci_packet_del(async->queue->token, async->td_addr);
  169. usb_packet_cleanup(&async->packet);
  170. if (async->buf != async->static_buf) {
  171. g_free(async->buf);
  172. }
  173. g_free(async);
  174. }
  175. static void uhci_async_link(UHCIAsync *async)
  176. {
  177. UHCIQueue *queue = async->queue;
  178. QTAILQ_INSERT_TAIL(&queue->asyncs, async, next);
  179. trace_usb_uhci_packet_link_async(async->queue->token, async->td_addr);
  180. }
  181. static void uhci_async_unlink(UHCIAsync *async)
  182. {
  183. UHCIQueue *queue = async->queue;
  184. QTAILQ_REMOVE(&queue->asyncs, async, next);
  185. trace_usb_uhci_packet_unlink_async(async->queue->token, async->td_addr);
  186. }
  187. static void uhci_async_cancel(UHCIAsync *async)
  188. {
  189. uhci_async_unlink(async);
  190. trace_usb_uhci_packet_cancel(async->queue->token, async->td_addr,
  191. async->done);
  192. if (!async->done)
  193. usb_cancel_packet(&async->packet);
  194. uhci_async_free(async);
  195. }
  196. /*
  197. * Mark all outstanding async packets as invalid.
  198. * This is used for canceling them when TDs are removed by the HCD.
  199. */
  200. static void uhci_async_validate_begin(UHCIState *s)
  201. {
  202. UHCIQueue *queue;
  203. QTAILQ_FOREACH(queue, &s->queues, next) {
  204. queue->valid--;
  205. }
  206. }
  207. /*
  208. * Cancel async packets that are no longer valid
  209. */
  210. static void uhci_async_validate_end(UHCIState *s)
  211. {
  212. UHCIQueue *queue, *n;
  213. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
  214. if (!queue->valid) {
  215. uhci_queue_free(queue, "validate-end");
  216. }
  217. }
  218. }
  219. static void uhci_async_cancel_device(UHCIState *s, USBDevice *dev)
  220. {
  221. UHCIQueue *queue, *n;
  222. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, n) {
  223. if (queue->ep->dev == dev) {
  224. uhci_queue_free(queue, "cancel-device");
  225. }
  226. }
  227. }
  228. static void uhci_async_cancel_all(UHCIState *s)
  229. {
  230. UHCIQueue *queue, *nq;
  231. QTAILQ_FOREACH_SAFE(queue, &s->queues, next, nq) {
  232. uhci_queue_free(queue, "cancel-all");
  233. }
  234. }
  235. static UHCIAsync *uhci_async_find_td(UHCIState *s, uint32_t td_addr)
  236. {
  237. UHCIQueue *queue;
  238. UHCIAsync *async;
  239. QTAILQ_FOREACH(queue, &s->queues, next) {
  240. QTAILQ_FOREACH(async, &queue->asyncs, next) {
  241. if (async->td_addr == td_addr) {
  242. return async;
  243. }
  244. }
  245. }
  246. return NULL;
  247. }
  248. static void uhci_update_irq(UHCIState *s)
  249. {
  250. int level = 0;
  251. if (((s->status2 & 1) && (s->intr & (1 << 2))) ||
  252. ((s->status2 & 2) && (s->intr & (1 << 3))) ||
  253. ((s->status & UHCI_STS_USBERR) && (s->intr & (1 << 0))) ||
  254. ((s->status & UHCI_STS_RD) && (s->intr & (1 << 1))) ||
  255. (s->status & UHCI_STS_HSERR) ||
  256. (s->status & UHCI_STS_HCPERR)) {
  257. level = 1;
  258. }
  259. qemu_set_irq(s->irq, level);
  260. }
  261. static void uhci_reset(DeviceState *dev)
  262. {
  263. PCIDevice *d = PCI_DEVICE(dev);
  264. UHCIState *s = UHCI(d);
  265. uint8_t *pci_conf;
  266. int i;
  267. UHCIPort *port;
  268. trace_usb_uhci_reset();
  269. pci_conf = s->dev.config;
  270. pci_conf[0x6a] = 0x01; /* usb clock */
  271. pci_conf[0x6b] = 0x00;
  272. s->cmd = 0;
  273. s->status = UHCI_STS_HCHALTED;
  274. s->status2 = 0;
  275. s->intr = 0;
  276. s->fl_base_addr = 0;
  277. s->sof_timing = 64;
  278. for(i = 0; i < NB_PORTS; i++) {
  279. port = &s->ports[i];
  280. port->ctrl = 0x0080;
  281. if (port->port.dev && port->port.dev->attached) {
  282. usb_port_reset(&port->port);
  283. }
  284. }
  285. uhci_async_cancel_all(s);
  286. qemu_bh_cancel(s->bh);
  287. uhci_update_irq(s);
  288. }
  289. static const VMStateDescription vmstate_uhci_port = {
  290. .name = "uhci port",
  291. .version_id = 1,
  292. .minimum_version_id = 1,
  293. .fields = (VMStateField[]) {
  294. VMSTATE_UINT16(ctrl, UHCIPort),
  295. VMSTATE_END_OF_LIST()
  296. }
  297. };
  298. static int uhci_post_load(void *opaque, int version_id)
  299. {
  300. UHCIState *s = opaque;
  301. if (version_id < 2) {
  302. s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  303. (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ);
  304. }
  305. return 0;
  306. }
  307. static const VMStateDescription vmstate_uhci = {
  308. .name = "uhci",
  309. .version_id = 3,
  310. .minimum_version_id = 1,
  311. .post_load = uhci_post_load,
  312. .fields = (VMStateField[]) {
  313. VMSTATE_PCI_DEVICE(dev, UHCIState),
  314. VMSTATE_UINT8_EQUAL(num_ports_vmstate, UHCIState, NULL),
  315. VMSTATE_STRUCT_ARRAY(ports, UHCIState, NB_PORTS, 1,
  316. vmstate_uhci_port, UHCIPort),
  317. VMSTATE_UINT16(cmd, UHCIState),
  318. VMSTATE_UINT16(status, UHCIState),
  319. VMSTATE_UINT16(intr, UHCIState),
  320. VMSTATE_UINT16(frnum, UHCIState),
  321. VMSTATE_UINT32(fl_base_addr, UHCIState),
  322. VMSTATE_UINT8(sof_timing, UHCIState),
  323. VMSTATE_UINT8(status2, UHCIState),
  324. VMSTATE_TIMER_PTR(frame_timer, UHCIState),
  325. VMSTATE_INT64_V(expire_time, UHCIState, 2),
  326. VMSTATE_UINT32_V(pending_int_mask, UHCIState, 3),
  327. VMSTATE_END_OF_LIST()
  328. }
  329. };
  330. static void uhci_port_write(void *opaque, hwaddr addr,
  331. uint64_t val, unsigned size)
  332. {
  333. UHCIState *s = opaque;
  334. trace_usb_uhci_mmio_writew(addr, val);
  335. switch(addr) {
  336. case 0x00:
  337. if ((val & UHCI_CMD_RS) && !(s->cmd & UHCI_CMD_RS)) {
  338. /* start frame processing */
  339. trace_usb_uhci_schedule_start();
  340. s->expire_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  341. (NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ);
  342. timer_mod(s->frame_timer, s->expire_time);
  343. s->status &= ~UHCI_STS_HCHALTED;
  344. } else if (!(val & UHCI_CMD_RS)) {
  345. s->status |= UHCI_STS_HCHALTED;
  346. }
  347. if (val & UHCI_CMD_GRESET) {
  348. UHCIPort *port;
  349. int i;
  350. /* send reset on the USB bus */
  351. for(i = 0; i < NB_PORTS; i++) {
  352. port = &s->ports[i];
  353. usb_device_reset(port->port.dev);
  354. }
  355. uhci_reset(DEVICE(s));
  356. return;
  357. }
  358. if (val & UHCI_CMD_HCRESET) {
  359. uhci_reset(DEVICE(s));
  360. return;
  361. }
  362. s->cmd = val;
  363. if (val & UHCI_CMD_EGSM) {
  364. if ((s->ports[0].ctrl & UHCI_PORT_RD) ||
  365. (s->ports[1].ctrl & UHCI_PORT_RD)) {
  366. uhci_resume(s);
  367. }
  368. }
  369. break;
  370. case 0x02:
  371. s->status &= ~val;
  372. /* XXX: the chip spec is not coherent, so we add a hidden
  373. register to distinguish between IOC and SPD */
  374. if (val & UHCI_STS_USBINT)
  375. s->status2 = 0;
  376. uhci_update_irq(s);
  377. break;
  378. case 0x04:
  379. s->intr = val;
  380. uhci_update_irq(s);
  381. break;
  382. case 0x06:
  383. if (s->status & UHCI_STS_HCHALTED)
  384. s->frnum = val & 0x7ff;
  385. break;
  386. case 0x08:
  387. s->fl_base_addr &= 0xffff0000;
  388. s->fl_base_addr |= val & ~0xfff;
  389. break;
  390. case 0x0a:
  391. s->fl_base_addr &= 0x0000ffff;
  392. s->fl_base_addr |= (val << 16);
  393. break;
  394. case 0x0c:
  395. s->sof_timing = val & 0xff;
  396. break;
  397. case 0x10 ... 0x1f:
  398. {
  399. UHCIPort *port;
  400. USBDevice *dev;
  401. int n;
  402. n = (addr >> 1) & 7;
  403. if (n >= NB_PORTS)
  404. return;
  405. port = &s->ports[n];
  406. dev = port->port.dev;
  407. if (dev && dev->attached) {
  408. /* port reset */
  409. if ( (val & UHCI_PORT_RESET) &&
  410. !(port->ctrl & UHCI_PORT_RESET) ) {
  411. usb_device_reset(dev);
  412. }
  413. }
  414. port->ctrl &= UHCI_PORT_READ_ONLY;
  415. /* enabled may only be set if a device is connected */
  416. if (!(port->ctrl & UHCI_PORT_CCS)) {
  417. val &= ~UHCI_PORT_EN;
  418. }
  419. port->ctrl |= (val & ~UHCI_PORT_READ_ONLY);
  420. /* some bits are reset when a '1' is written to them */
  421. port->ctrl &= ~(val & UHCI_PORT_WRITE_CLEAR);
  422. }
  423. break;
  424. }
  425. }
  426. static uint64_t uhci_port_read(void *opaque, hwaddr addr, unsigned size)
  427. {
  428. UHCIState *s = opaque;
  429. uint32_t val;
  430. switch(addr) {
  431. case 0x00:
  432. val = s->cmd;
  433. break;
  434. case 0x02:
  435. val = s->status;
  436. break;
  437. case 0x04:
  438. val = s->intr;
  439. break;
  440. case 0x06:
  441. val = s->frnum;
  442. break;
  443. case 0x08:
  444. val = s->fl_base_addr & 0xffff;
  445. break;
  446. case 0x0a:
  447. val = (s->fl_base_addr >> 16) & 0xffff;
  448. break;
  449. case 0x0c:
  450. val = s->sof_timing;
  451. break;
  452. case 0x10 ... 0x1f:
  453. {
  454. UHCIPort *port;
  455. int n;
  456. n = (addr >> 1) & 7;
  457. if (n >= NB_PORTS)
  458. goto read_default;
  459. port = &s->ports[n];
  460. val = port->ctrl;
  461. }
  462. break;
  463. default:
  464. read_default:
  465. val = 0xff7f; /* disabled port */
  466. break;
  467. }
  468. trace_usb_uhci_mmio_readw(addr, val);
  469. return val;
  470. }
  471. /* signal resume if controller suspended */
  472. static void uhci_resume (void *opaque)
  473. {
  474. UHCIState *s = (UHCIState *)opaque;
  475. if (!s)
  476. return;
  477. if (s->cmd & UHCI_CMD_EGSM) {
  478. s->cmd |= UHCI_CMD_FGR;
  479. s->status |= UHCI_STS_RD;
  480. uhci_update_irq(s);
  481. }
  482. }
  483. static void uhci_attach(USBPort *port1)
  484. {
  485. UHCIState *s = port1->opaque;
  486. UHCIPort *port = &s->ports[port1->index];
  487. /* set connect status */
  488. port->ctrl |= UHCI_PORT_CCS | UHCI_PORT_CSC;
  489. /* update speed */
  490. if (port->port.dev->speed == USB_SPEED_LOW) {
  491. port->ctrl |= UHCI_PORT_LSDA;
  492. } else {
  493. port->ctrl &= ~UHCI_PORT_LSDA;
  494. }
  495. uhci_resume(s);
  496. }
  497. static void uhci_detach(USBPort *port1)
  498. {
  499. UHCIState *s = port1->opaque;
  500. UHCIPort *port = &s->ports[port1->index];
  501. uhci_async_cancel_device(s, port1->dev);
  502. /* set connect status */
  503. if (port->ctrl & UHCI_PORT_CCS) {
  504. port->ctrl &= ~UHCI_PORT_CCS;
  505. port->ctrl |= UHCI_PORT_CSC;
  506. }
  507. /* disable port */
  508. if (port->ctrl & UHCI_PORT_EN) {
  509. port->ctrl &= ~UHCI_PORT_EN;
  510. port->ctrl |= UHCI_PORT_ENC;
  511. }
  512. uhci_resume(s);
  513. }
  514. static void uhci_child_detach(USBPort *port1, USBDevice *child)
  515. {
  516. UHCIState *s = port1->opaque;
  517. uhci_async_cancel_device(s, child);
  518. }
  519. static void uhci_wakeup(USBPort *port1)
  520. {
  521. UHCIState *s = port1->opaque;
  522. UHCIPort *port = &s->ports[port1->index];
  523. if (port->ctrl & UHCI_PORT_SUSPEND && !(port->ctrl & UHCI_PORT_RD)) {
  524. port->ctrl |= UHCI_PORT_RD;
  525. uhci_resume(s);
  526. }
  527. }
  528. static USBDevice *uhci_find_device(UHCIState *s, uint8_t addr)
  529. {
  530. USBDevice *dev;
  531. int i;
  532. for (i = 0; i < NB_PORTS; i++) {
  533. UHCIPort *port = &s->ports[i];
  534. if (!(port->ctrl & UHCI_PORT_EN)) {
  535. continue;
  536. }
  537. dev = usb_find_device(&port->port, addr);
  538. if (dev != NULL) {
  539. return dev;
  540. }
  541. }
  542. return NULL;
  543. }
  544. static void uhci_read_td(UHCIState *s, UHCI_TD *td, uint32_t link)
  545. {
  546. pci_dma_read(&s->dev, link & ~0xf, td, sizeof(*td));
  547. le32_to_cpus(&td->link);
  548. le32_to_cpus(&td->ctrl);
  549. le32_to_cpus(&td->token);
  550. le32_to_cpus(&td->buffer);
  551. }
  552. static int uhci_handle_td_error(UHCIState *s, UHCI_TD *td, uint32_t td_addr,
  553. int status, uint32_t *int_mask)
  554. {
  555. uint32_t queue_token = uhci_queue_token(td);
  556. int ret;
  557. switch (status) {
  558. case USB_RET_NAK:
  559. td->ctrl |= TD_CTRL_NAK;
  560. return TD_RESULT_NEXT_QH;
  561. case USB_RET_STALL:
  562. td->ctrl |= TD_CTRL_STALL;
  563. trace_usb_uhci_packet_complete_stall(queue_token, td_addr);
  564. ret = TD_RESULT_NEXT_QH;
  565. break;
  566. case USB_RET_BABBLE:
  567. td->ctrl |= TD_CTRL_BABBLE | TD_CTRL_STALL;
  568. /* frame interrupted */
  569. trace_usb_uhci_packet_complete_babble(queue_token, td_addr);
  570. ret = TD_RESULT_STOP_FRAME;
  571. break;
  572. case USB_RET_IOERROR:
  573. case USB_RET_NODEV:
  574. default:
  575. td->ctrl |= TD_CTRL_TIMEOUT;
  576. td->ctrl &= ~(3 << TD_CTRL_ERROR_SHIFT);
  577. trace_usb_uhci_packet_complete_error(queue_token, td_addr);
  578. ret = TD_RESULT_NEXT_QH;
  579. break;
  580. }
  581. td->ctrl &= ~TD_CTRL_ACTIVE;
  582. s->status |= UHCI_STS_USBERR;
  583. if (td->ctrl & TD_CTRL_IOC) {
  584. *int_mask |= 0x01;
  585. }
  586. uhci_update_irq(s);
  587. return ret;
  588. }
  589. static int uhci_complete_td(UHCIState *s, UHCI_TD *td, UHCIAsync *async, uint32_t *int_mask)
  590. {
  591. int len = 0, max_len;
  592. uint8_t pid;
  593. max_len = ((td->token >> 21) + 1) & 0x7ff;
  594. pid = td->token & 0xff;
  595. if (td->ctrl & TD_CTRL_IOS)
  596. td->ctrl &= ~TD_CTRL_ACTIVE;
  597. if (async->packet.status != USB_RET_SUCCESS) {
  598. return uhci_handle_td_error(s, td, async->td_addr,
  599. async->packet.status, int_mask);
  600. }
  601. len = async->packet.actual_length;
  602. td->ctrl = (td->ctrl & ~0x7ff) | ((len - 1) & 0x7ff);
  603. /* The NAK bit may have been set by a previous frame, so clear it
  604. here. The docs are somewhat unclear, but win2k relies on this
  605. behavior. */
  606. td->ctrl &= ~(TD_CTRL_ACTIVE | TD_CTRL_NAK);
  607. if (td->ctrl & TD_CTRL_IOC)
  608. *int_mask |= 0x01;
  609. if (pid == USB_TOKEN_IN) {
  610. pci_dma_write(&s->dev, td->buffer, async->buf, len);
  611. if ((td->ctrl & TD_CTRL_SPD) && len < max_len) {
  612. *int_mask |= 0x02;
  613. /* short packet: do not update QH */
  614. trace_usb_uhci_packet_complete_shortxfer(async->queue->token,
  615. async->td_addr);
  616. return TD_RESULT_NEXT_QH;
  617. }
  618. }
  619. /* success */
  620. trace_usb_uhci_packet_complete_success(async->queue->token,
  621. async->td_addr);
  622. return TD_RESULT_COMPLETE;
  623. }
  624. static int uhci_handle_td(UHCIState *s, UHCIQueue *q, uint32_t qh_addr,
  625. UHCI_TD *td, uint32_t td_addr, uint32_t *int_mask)
  626. {
  627. int ret, max_len;
  628. bool spd;
  629. bool queuing = (q != NULL);
  630. uint8_t pid = td->token & 0xff;
  631. UHCIAsync *async;
  632. async = uhci_async_find_td(s, td_addr);
  633. if (async) {
  634. if (uhci_queue_verify(async->queue, qh_addr, td, td_addr, queuing)) {
  635. assert(q == NULL || q == async->queue);
  636. q = async->queue;
  637. } else {
  638. uhci_queue_free(async->queue, "guest re-used pending td");
  639. async = NULL;
  640. }
  641. }
  642. if (q == NULL) {
  643. q = uhci_queue_find(s, td);
  644. if (q && !uhci_queue_verify(q, qh_addr, td, td_addr, queuing)) {
  645. uhci_queue_free(q, "guest re-used qh");
  646. q = NULL;
  647. }
  648. }
  649. if (q) {
  650. q->valid = QH_VALID;
  651. }
  652. /* Is active ? */
  653. if (!(td->ctrl & TD_CTRL_ACTIVE)) {
  654. if (async) {
  655. /* Guest marked a pending td non-active, cancel the queue */
  656. uhci_queue_free(async->queue, "pending td non-active");
  657. }
  658. /*
  659. * ehci11d spec page 22: "Even if the Active bit in the TD is already
  660. * cleared when the TD is fetched ... an IOC interrupt is generated"
  661. */
  662. if (td->ctrl & TD_CTRL_IOC) {
  663. *int_mask |= 0x01;
  664. }
  665. return TD_RESULT_NEXT_QH;
  666. }
  667. switch (pid) {
  668. case USB_TOKEN_OUT:
  669. case USB_TOKEN_SETUP:
  670. case USB_TOKEN_IN:
  671. break;
  672. default:
  673. /* invalid pid : frame interrupted */
  674. s->status |= UHCI_STS_HCPERR;
  675. s->cmd &= ~UHCI_CMD_RS;
  676. uhci_update_irq(s);
  677. return TD_RESULT_STOP_FRAME;
  678. }
  679. if (async) {
  680. if (queuing) {
  681. /* we are busy filling the queue, we are not prepared
  682. to consume completed packages then, just leave them
  683. in async state */
  684. return TD_RESULT_ASYNC_CONT;
  685. }
  686. if (!async->done) {
  687. UHCI_TD last_td;
  688. UHCIAsync *last = QTAILQ_LAST(&async->queue->asyncs);
  689. /*
  690. * While we are waiting for the current td to complete, the guest
  691. * may have added more tds to the queue. Note we re-read the td
  692. * rather then caching it, as we want to see guest made changes!
  693. */
  694. uhci_read_td(s, &last_td, last->td_addr);
  695. uhci_queue_fill(async->queue, &last_td);
  696. return TD_RESULT_ASYNC_CONT;
  697. }
  698. uhci_async_unlink(async);
  699. goto done;
  700. }
  701. if (s->completions_only) {
  702. return TD_RESULT_ASYNC_CONT;
  703. }
  704. /* Allocate new packet */
  705. if (q == NULL) {
  706. USBDevice *dev;
  707. USBEndpoint *ep;
  708. dev = uhci_find_device(s, (td->token >> 8) & 0x7f);
  709. if (dev == NULL) {
  710. return uhci_handle_td_error(s, td, td_addr, USB_RET_NODEV,
  711. int_mask);
  712. }
  713. ep = usb_ep_get(dev, pid, (td->token >> 15) & 0xf);
  714. q = uhci_queue_new(s, qh_addr, td, ep);
  715. }
  716. async = uhci_async_alloc(q, td_addr);
  717. max_len = ((td->token >> 21) + 1) & 0x7ff;
  718. spd = (pid == USB_TOKEN_IN && (td->ctrl & TD_CTRL_SPD) != 0);
  719. usb_packet_setup(&async->packet, pid, q->ep, 0, td_addr, spd,
  720. (td->ctrl & TD_CTRL_IOC) != 0);
  721. if (max_len <= sizeof(async->static_buf)) {
  722. async->buf = async->static_buf;
  723. } else {
  724. async->buf = g_malloc(max_len);
  725. }
  726. usb_packet_addbuf(&async->packet, async->buf, max_len);
  727. switch(pid) {
  728. case USB_TOKEN_OUT:
  729. case USB_TOKEN_SETUP:
  730. pci_dma_read(&s->dev, td->buffer, async->buf, max_len);
  731. usb_handle_packet(q->ep->dev, &async->packet);
  732. if (async->packet.status == USB_RET_SUCCESS) {
  733. async->packet.actual_length = max_len;
  734. }
  735. break;
  736. case USB_TOKEN_IN:
  737. usb_handle_packet(q->ep->dev, &async->packet);
  738. break;
  739. default:
  740. abort(); /* Never to execute */
  741. }
  742. if (async->packet.status == USB_RET_ASYNC) {
  743. uhci_async_link(async);
  744. if (!queuing) {
  745. uhci_queue_fill(q, td);
  746. }
  747. return TD_RESULT_ASYNC_START;
  748. }
  749. done:
  750. ret = uhci_complete_td(s, td, async, int_mask);
  751. uhci_async_free(async);
  752. return ret;
  753. }
  754. static void uhci_async_complete(USBPort *port, USBPacket *packet)
  755. {
  756. UHCIAsync *async = container_of(packet, UHCIAsync, packet);
  757. UHCIState *s = async->queue->uhci;
  758. if (packet->status == USB_RET_REMOVE_FROM_QUEUE) {
  759. uhci_async_cancel(async);
  760. return;
  761. }
  762. async->done = 1;
  763. /* Force processing of this packet *now*, needed for migration */
  764. s->completions_only = true;
  765. qemu_bh_schedule(s->bh);
  766. }
  767. static int is_valid(uint32_t link)
  768. {
  769. return (link & 1) == 0;
  770. }
  771. static int is_qh(uint32_t link)
  772. {
  773. return (link & 2) != 0;
  774. }
  775. static int depth_first(uint32_t link)
  776. {
  777. return (link & 4) != 0;
  778. }
  779. /* QH DB used for detecting QH loops */
  780. #define UHCI_MAX_QUEUES 128
  781. typedef struct {
  782. uint32_t addr[UHCI_MAX_QUEUES];
  783. int count;
  784. } QhDb;
  785. static void qhdb_reset(QhDb *db)
  786. {
  787. db->count = 0;
  788. }
  789. /* Add QH to DB. Returns 1 if already present or DB is full. */
  790. static int qhdb_insert(QhDb *db, uint32_t addr)
  791. {
  792. int i;
  793. for (i = 0; i < db->count; i++)
  794. if (db->addr[i] == addr)
  795. return 1;
  796. if (db->count >= UHCI_MAX_QUEUES)
  797. return 1;
  798. db->addr[db->count++] = addr;
  799. return 0;
  800. }
  801. static void uhci_queue_fill(UHCIQueue *q, UHCI_TD *td)
  802. {
  803. uint32_t int_mask = 0;
  804. uint32_t plink = td->link;
  805. UHCI_TD ptd;
  806. int ret;
  807. while (is_valid(plink)) {
  808. uhci_read_td(q->uhci, &ptd, plink);
  809. if (!(ptd.ctrl & TD_CTRL_ACTIVE)) {
  810. break;
  811. }
  812. if (uhci_queue_token(&ptd) != q->token) {
  813. break;
  814. }
  815. trace_usb_uhci_td_queue(plink & ~0xf, ptd.ctrl, ptd.token);
  816. ret = uhci_handle_td(q->uhci, q, q->qh_addr, &ptd, plink, &int_mask);
  817. if (ret == TD_RESULT_ASYNC_CONT) {
  818. break;
  819. }
  820. assert(ret == TD_RESULT_ASYNC_START);
  821. assert(int_mask == 0);
  822. plink = ptd.link;
  823. }
  824. usb_device_flush_ep_queue(q->ep->dev, q->ep);
  825. }
  826. static void uhci_process_frame(UHCIState *s)
  827. {
  828. uint32_t frame_addr, link, old_td_ctrl, val, int_mask;
  829. uint32_t curr_qh, td_count = 0;
  830. int cnt, ret;
  831. UHCI_TD td;
  832. UHCI_QH qh;
  833. QhDb qhdb;
  834. frame_addr = s->fl_base_addr + ((s->frnum & 0x3ff) << 2);
  835. pci_dma_read(&s->dev, frame_addr, &link, 4);
  836. le32_to_cpus(&link);
  837. int_mask = 0;
  838. curr_qh = 0;
  839. qhdb_reset(&qhdb);
  840. for (cnt = FRAME_MAX_LOOPS; is_valid(link) && cnt; cnt--) {
  841. if (!s->completions_only && s->frame_bytes >= s->frame_bandwidth) {
  842. /* We've reached the usb 1.1 bandwidth, which is
  843. 1280 bytes/frame, stop processing */
  844. trace_usb_uhci_frame_stop_bandwidth();
  845. break;
  846. }
  847. if (is_qh(link)) {
  848. /* QH */
  849. trace_usb_uhci_qh_load(link & ~0xf);
  850. if (qhdb_insert(&qhdb, link)) {
  851. /*
  852. * We're going in circles. Which is not a bug because
  853. * HCD is allowed to do that as part of the BW management.
  854. *
  855. * Stop processing here if no transaction has been done
  856. * since we've been here last time.
  857. */
  858. if (td_count == 0) {
  859. trace_usb_uhci_frame_loop_stop_idle();
  860. break;
  861. } else {
  862. trace_usb_uhci_frame_loop_continue();
  863. td_count = 0;
  864. qhdb_reset(&qhdb);
  865. qhdb_insert(&qhdb, link);
  866. }
  867. }
  868. pci_dma_read(&s->dev, link & ~0xf, &qh, sizeof(qh));
  869. le32_to_cpus(&qh.link);
  870. le32_to_cpus(&qh.el_link);
  871. if (!is_valid(qh.el_link)) {
  872. /* QH w/o elements */
  873. curr_qh = 0;
  874. link = qh.link;
  875. } else {
  876. /* QH with elements */
  877. curr_qh = link;
  878. link = qh.el_link;
  879. }
  880. continue;
  881. }
  882. /* TD */
  883. uhci_read_td(s, &td, link);
  884. trace_usb_uhci_td_load(curr_qh & ~0xf, link & ~0xf, td.ctrl, td.token);
  885. old_td_ctrl = td.ctrl;
  886. ret = uhci_handle_td(s, NULL, curr_qh, &td, link, &int_mask);
  887. if (old_td_ctrl != td.ctrl) {
  888. /* update the status bits of the TD */
  889. val = cpu_to_le32(td.ctrl);
  890. pci_dma_write(&s->dev, (link & ~0xf) + 4, &val, sizeof(val));
  891. }
  892. switch (ret) {
  893. case TD_RESULT_STOP_FRAME: /* interrupted frame */
  894. goto out;
  895. case TD_RESULT_NEXT_QH:
  896. case TD_RESULT_ASYNC_CONT:
  897. trace_usb_uhci_td_nextqh(curr_qh & ~0xf, link & ~0xf);
  898. link = curr_qh ? qh.link : td.link;
  899. continue;
  900. case TD_RESULT_ASYNC_START:
  901. trace_usb_uhci_td_async(curr_qh & ~0xf, link & ~0xf);
  902. link = curr_qh ? qh.link : td.link;
  903. continue;
  904. case TD_RESULT_COMPLETE:
  905. trace_usb_uhci_td_complete(curr_qh & ~0xf, link & ~0xf);
  906. link = td.link;
  907. td_count++;
  908. s->frame_bytes += (td.ctrl & 0x7ff) + 1;
  909. if (curr_qh) {
  910. /* update QH element link */
  911. qh.el_link = link;
  912. val = cpu_to_le32(qh.el_link);
  913. pci_dma_write(&s->dev, (curr_qh & ~0xf) + 4, &val, sizeof(val));
  914. if (!depth_first(link)) {
  915. /* done with this QH */
  916. curr_qh = 0;
  917. link = qh.link;
  918. }
  919. }
  920. break;
  921. default:
  922. assert(!"unknown return code");
  923. }
  924. /* go to the next entry */
  925. }
  926. out:
  927. s->pending_int_mask |= int_mask;
  928. }
  929. static void uhci_bh(void *opaque)
  930. {
  931. UHCIState *s = opaque;
  932. uhci_process_frame(s);
  933. }
  934. static void uhci_frame_timer(void *opaque)
  935. {
  936. UHCIState *s = opaque;
  937. uint64_t t_now, t_last_run;
  938. int i, frames;
  939. const uint64_t frame_t = NANOSECONDS_PER_SECOND / FRAME_TIMER_FREQ;
  940. s->completions_only = false;
  941. qemu_bh_cancel(s->bh);
  942. if (!(s->cmd & UHCI_CMD_RS)) {
  943. /* Full stop */
  944. trace_usb_uhci_schedule_stop();
  945. timer_del(s->frame_timer);
  946. uhci_async_cancel_all(s);
  947. /* set hchalted bit in status - UHCI11D 2.1.2 */
  948. s->status |= UHCI_STS_HCHALTED;
  949. return;
  950. }
  951. /* We still store expire_time in our state, for migration */
  952. t_last_run = s->expire_time - frame_t;
  953. t_now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  954. /* Process up to MAX_FRAMES_PER_TICK frames */
  955. frames = (t_now - t_last_run) / frame_t;
  956. if (frames > s->maxframes) {
  957. int skipped = frames - s->maxframes;
  958. s->expire_time += skipped * frame_t;
  959. s->frnum = (s->frnum + skipped) & 0x7ff;
  960. frames -= skipped;
  961. }
  962. if (frames > MAX_FRAMES_PER_TICK) {
  963. frames = MAX_FRAMES_PER_TICK;
  964. }
  965. for (i = 0; i < frames; i++) {
  966. s->frame_bytes = 0;
  967. trace_usb_uhci_frame_start(s->frnum);
  968. uhci_async_validate_begin(s);
  969. uhci_process_frame(s);
  970. uhci_async_validate_end(s);
  971. /* The spec says frnum is the frame currently being processed, and
  972. * the guest must look at frnum - 1 on interrupt, so inc frnum now */
  973. s->frnum = (s->frnum + 1) & 0x7ff;
  974. s->expire_time += frame_t;
  975. }
  976. /* Complete the previous frame(s) */
  977. if (s->pending_int_mask) {
  978. s->status2 |= s->pending_int_mask;
  979. s->status |= UHCI_STS_USBINT;
  980. uhci_update_irq(s);
  981. }
  982. s->pending_int_mask = 0;
  983. timer_mod(s->frame_timer, t_now + frame_t);
  984. }
  985. static const MemoryRegionOps uhci_ioport_ops = {
  986. .read = uhci_port_read,
  987. .write = uhci_port_write,
  988. .valid.min_access_size = 1,
  989. .valid.max_access_size = 4,
  990. .impl.min_access_size = 2,
  991. .impl.max_access_size = 2,
  992. .endianness = DEVICE_LITTLE_ENDIAN,
  993. };
  994. static USBPortOps uhci_port_ops = {
  995. .attach = uhci_attach,
  996. .detach = uhci_detach,
  997. .child_detach = uhci_child_detach,
  998. .wakeup = uhci_wakeup,
  999. .complete = uhci_async_complete,
  1000. };
  1001. static USBBusOps uhci_bus_ops = {
  1002. };
  1003. void usb_uhci_common_realize(PCIDevice *dev, Error **errp)
  1004. {
  1005. Error *err = NULL;
  1006. UHCIPCIDeviceClass *u = UHCI_GET_CLASS(dev);
  1007. UHCIState *s = UHCI(dev);
  1008. uint8_t *pci_conf = s->dev.config;
  1009. int i;
  1010. pci_conf[PCI_CLASS_PROG] = 0x00;
  1011. /* TODO: reset value should be 0. */
  1012. pci_conf[USB_SBRN] = USB_RELEASE_1; /* release number */
  1013. pci_config_set_interrupt_pin(pci_conf, u->info.irq_pin + 1);
  1014. s->irq = pci_allocate_irq(dev);
  1015. if (s->masterbus) {
  1016. USBPort *ports[NB_PORTS];
  1017. for(i = 0; i < NB_PORTS; i++) {
  1018. ports[i] = &s->ports[i].port;
  1019. }
  1020. usb_register_companion(s->masterbus, ports, NB_PORTS,
  1021. s->firstport, s, &uhci_port_ops,
  1022. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL,
  1023. &err);
  1024. if (err) {
  1025. error_propagate(errp, err);
  1026. return;
  1027. }
  1028. } else {
  1029. usb_bus_new(&s->bus, sizeof(s->bus), &uhci_bus_ops, DEVICE(dev));
  1030. for (i = 0; i < NB_PORTS; i++) {
  1031. usb_register_port(&s->bus, &s->ports[i].port, s, i, &uhci_port_ops,
  1032. USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
  1033. }
  1034. }
  1035. s->bh = qemu_bh_new(uhci_bh, s);
  1036. s->frame_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, uhci_frame_timer, s);
  1037. s->num_ports_vmstate = NB_PORTS;
  1038. QTAILQ_INIT(&s->queues);
  1039. memory_region_init_io(&s->io_bar, OBJECT(s), &uhci_ioport_ops, s,
  1040. "uhci", 0x20);
  1041. /* Use region 4 for consistency with real hardware. BSD guests seem
  1042. to rely on this. */
  1043. pci_register_bar(&s->dev, 4, PCI_BASE_ADDRESS_SPACE_IO, &s->io_bar);
  1044. }
  1045. static void usb_uhci_exit(PCIDevice *dev)
  1046. {
  1047. UHCIState *s = UHCI(dev);
  1048. trace_usb_uhci_exit();
  1049. if (s->frame_timer) {
  1050. timer_free(s->frame_timer);
  1051. s->frame_timer = NULL;
  1052. }
  1053. if (s->bh) {
  1054. qemu_bh_delete(s->bh);
  1055. }
  1056. uhci_async_cancel_all(s);
  1057. if (!s->masterbus) {
  1058. usb_bus_release(&s->bus);
  1059. }
  1060. }
  1061. static Property uhci_properties_companion[] = {
  1062. DEFINE_PROP_STRING("masterbus", UHCIState, masterbus),
  1063. DEFINE_PROP_UINT32("firstport", UHCIState, firstport, 0),
  1064. DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
  1065. DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128),
  1066. DEFINE_PROP_END_OF_LIST(),
  1067. };
  1068. static Property uhci_properties_standalone[] = {
  1069. DEFINE_PROP_UINT32("bandwidth", UHCIState, frame_bandwidth, 1280),
  1070. DEFINE_PROP_UINT32("maxframes", UHCIState, maxframes, 128),
  1071. DEFINE_PROP_END_OF_LIST(),
  1072. };
  1073. static void uhci_class_init(ObjectClass *klass, void *data)
  1074. {
  1075. DeviceClass *dc = DEVICE_CLASS(klass);
  1076. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1077. k->class_id = PCI_CLASS_SERIAL_USB;
  1078. dc->vmsd = &vmstate_uhci;
  1079. dc->reset = uhci_reset;
  1080. set_bit(DEVICE_CATEGORY_USB, dc->categories);
  1081. }
  1082. static const TypeInfo uhci_pci_type_info = {
  1083. .name = TYPE_UHCI,
  1084. .parent = TYPE_PCI_DEVICE,
  1085. .instance_size = sizeof(UHCIState),
  1086. .class_size = sizeof(UHCIPCIDeviceClass),
  1087. .abstract = true,
  1088. .class_init = uhci_class_init,
  1089. .interfaces = (InterfaceInfo[]) {
  1090. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1091. { },
  1092. },
  1093. };
  1094. void uhci_data_class_init(ObjectClass *klass, void *data)
  1095. {
  1096. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  1097. DeviceClass *dc = DEVICE_CLASS(klass);
  1098. UHCIPCIDeviceClass *u = UHCI_CLASS(klass);
  1099. UHCIInfo *info = data;
  1100. k->realize = info->realize ? info->realize : usb_uhci_common_realize;
  1101. k->exit = info->unplug ? usb_uhci_exit : NULL;
  1102. k->vendor_id = info->vendor_id;
  1103. k->device_id = info->device_id;
  1104. k->revision = info->revision;
  1105. if (!info->unplug) {
  1106. /* uhci controllers in companion setups can't be hotplugged */
  1107. dc->hotpluggable = false;
  1108. device_class_set_props(dc, uhci_properties_companion);
  1109. } else {
  1110. device_class_set_props(dc, uhci_properties_standalone);
  1111. }
  1112. if (info->notuser) {
  1113. dc->user_creatable = false;
  1114. }
  1115. u->info = *info;
  1116. }
  1117. static UHCIInfo uhci_info[] = {
  1118. {
  1119. .name = TYPE_PIIX3_USB_UHCI,
  1120. .vendor_id = PCI_VENDOR_ID_INTEL,
  1121. .device_id = PCI_DEVICE_ID_INTEL_82371SB_2,
  1122. .revision = 0x01,
  1123. .irq_pin = 3,
  1124. .unplug = true,
  1125. },{
  1126. .name = TYPE_PIIX4_USB_UHCI,
  1127. .vendor_id = PCI_VENDOR_ID_INTEL,
  1128. .device_id = PCI_DEVICE_ID_INTEL_82371AB_2,
  1129. .revision = 0x01,
  1130. .irq_pin = 3,
  1131. .unplug = true,
  1132. },{
  1133. .name = TYPE_ICH9_USB_UHCI(1), /* 00:1d.0 */
  1134. .vendor_id = PCI_VENDOR_ID_INTEL,
  1135. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI1,
  1136. .revision = 0x03,
  1137. .irq_pin = 0,
  1138. .unplug = false,
  1139. },{
  1140. .name = TYPE_ICH9_USB_UHCI(2), /* 00:1d.1 */
  1141. .vendor_id = PCI_VENDOR_ID_INTEL,
  1142. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI2,
  1143. .revision = 0x03,
  1144. .irq_pin = 1,
  1145. .unplug = false,
  1146. },{
  1147. .name = TYPE_ICH9_USB_UHCI(3), /* 00:1d.2 */
  1148. .vendor_id = PCI_VENDOR_ID_INTEL,
  1149. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI3,
  1150. .revision = 0x03,
  1151. .irq_pin = 2,
  1152. .unplug = false,
  1153. },{
  1154. .name = TYPE_ICH9_USB_UHCI(4), /* 00:1a.0 */
  1155. .vendor_id = PCI_VENDOR_ID_INTEL,
  1156. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI4,
  1157. .revision = 0x03,
  1158. .irq_pin = 0,
  1159. .unplug = false,
  1160. },{
  1161. .name = TYPE_ICH9_USB_UHCI(5), /* 00:1a.1 */
  1162. .vendor_id = PCI_VENDOR_ID_INTEL,
  1163. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI5,
  1164. .revision = 0x03,
  1165. .irq_pin = 1,
  1166. .unplug = false,
  1167. },{
  1168. .name = TYPE_ICH9_USB_UHCI(6), /* 00:1a.2 */
  1169. .vendor_id = PCI_VENDOR_ID_INTEL,
  1170. .device_id = PCI_DEVICE_ID_INTEL_82801I_UHCI6,
  1171. .revision = 0x03,
  1172. .irq_pin = 2,
  1173. .unplug = false,
  1174. }
  1175. };
  1176. static void uhci_register_types(void)
  1177. {
  1178. TypeInfo uhci_type_info = {
  1179. .parent = TYPE_UHCI,
  1180. .class_init = uhci_data_class_init,
  1181. };
  1182. int i;
  1183. type_register_static(&uhci_pci_type_info);
  1184. for (i = 0; i < ARRAY_SIZE(uhci_info); i++) {
  1185. uhci_type_info.name = uhci_info[i].name;
  1186. uhci_type_info.class_data = uhci_info + i;
  1187. type_register(&uhci_type_info);
  1188. }
  1189. }
  1190. type_init(uhci_register_types)