bus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. #include "qemu/osdep.h"
  2. #include "hw/qdev-properties.h"
  3. #include "hw/usb.h"
  4. #include "qapi/error.h"
  5. #include "qapi/qapi-commands-machine.h"
  6. #include "qapi/type-helpers.h"
  7. #include "qemu/error-report.h"
  8. #include "qemu/module.h"
  9. #include "sysemu/sysemu.h"
  10. #include "migration/vmstate.h"
  11. #include "monitor/monitor.h"
  12. #include "trace.h"
  13. #include "qemu/cutils.h"
  14. static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
  15. static char *usb_get_dev_path(DeviceState *dev);
  16. static char *usb_get_fw_dev_path(DeviceState *qdev);
  17. static void usb_qdev_unrealize(DeviceState *qdev);
  18. static Property usb_props[] = {
  19. DEFINE_PROP_STRING("port", USBDevice, port_path),
  20. DEFINE_PROP_STRING("serial", USBDevice, serial),
  21. DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
  22. USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
  23. DEFINE_PROP_STRING("pcap", USBDevice, pcap_filename),
  24. DEFINE_PROP_END_OF_LIST()
  25. };
  26. static void usb_bus_class_init(ObjectClass *klass, void *data)
  27. {
  28. BusClass *k = BUS_CLASS(klass);
  29. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  30. k->print_dev = usb_bus_dev_print;
  31. k->get_dev_path = usb_get_dev_path;
  32. k->get_fw_dev_path = usb_get_fw_dev_path;
  33. hc->unplug = qdev_simple_device_unplug_cb;
  34. }
  35. static const TypeInfo usb_bus_info = {
  36. .name = TYPE_USB_BUS,
  37. .parent = TYPE_BUS,
  38. .instance_size = sizeof(USBBus),
  39. .class_init = usb_bus_class_init,
  40. .interfaces = (InterfaceInfo[]) {
  41. { TYPE_HOTPLUG_HANDLER },
  42. { }
  43. }
  44. };
  45. static int next_usb_bus = 0;
  46. static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
  47. static int usb_device_post_load(void *opaque, int version_id)
  48. {
  49. USBDevice *dev = opaque;
  50. if (dev->state == USB_STATE_NOTATTACHED) {
  51. dev->attached = false;
  52. } else {
  53. dev->attached = true;
  54. }
  55. return 0;
  56. }
  57. const VMStateDescription vmstate_usb_device = {
  58. .name = "USBDevice",
  59. .version_id = 1,
  60. .minimum_version_id = 1,
  61. .post_load = usb_device_post_load,
  62. .fields = (VMStateField[]) {
  63. VMSTATE_UINT8(addr, USBDevice),
  64. VMSTATE_INT32(state, USBDevice),
  65. VMSTATE_INT32(remote_wakeup, USBDevice),
  66. VMSTATE_INT32(setup_state, USBDevice),
  67. VMSTATE_INT32(setup_len, USBDevice),
  68. VMSTATE_INT32(setup_index, USBDevice),
  69. VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
  70. VMSTATE_END_OF_LIST(),
  71. }
  72. };
  73. void usb_bus_new(USBBus *bus, size_t bus_size,
  74. USBBusOps *ops, DeviceState *host)
  75. {
  76. qbus_init(bus, bus_size, TYPE_USB_BUS, host, NULL);
  77. qbus_set_bus_hotplug_handler(BUS(bus));
  78. bus->ops = ops;
  79. bus->busnr = next_usb_bus++;
  80. QTAILQ_INIT(&bus->free);
  81. QTAILQ_INIT(&bus->used);
  82. QTAILQ_INSERT_TAIL(&busses, bus, next);
  83. }
  84. void usb_bus_release(USBBus *bus)
  85. {
  86. assert(next_usb_bus > 0);
  87. QTAILQ_REMOVE(&busses, bus, next);
  88. }
  89. USBBus *usb_bus_find(int busnr)
  90. {
  91. USBBus *bus;
  92. if (-1 == busnr)
  93. return QTAILQ_FIRST(&busses);
  94. QTAILQ_FOREACH(bus, &busses, next) {
  95. if (bus->busnr == busnr)
  96. return bus;
  97. }
  98. return NULL;
  99. }
  100. static void usb_device_realize(USBDevice *dev, Error **errp)
  101. {
  102. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  103. if (klass->realize) {
  104. klass->realize(dev, errp);
  105. }
  106. }
  107. USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
  108. {
  109. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  110. if (klass->find_device) {
  111. return klass->find_device(dev, addr);
  112. }
  113. return NULL;
  114. }
  115. static void usb_device_unrealize(USBDevice *dev)
  116. {
  117. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  118. if (klass->unrealize) {
  119. klass->unrealize(dev);
  120. }
  121. }
  122. void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
  123. {
  124. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  125. if (klass->cancel_packet) {
  126. klass->cancel_packet(dev, p);
  127. }
  128. }
  129. void usb_device_handle_attach(USBDevice *dev)
  130. {
  131. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  132. if (klass->handle_attach) {
  133. klass->handle_attach(dev);
  134. }
  135. }
  136. void usb_device_handle_reset(USBDevice *dev)
  137. {
  138. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  139. if (klass->handle_reset) {
  140. klass->handle_reset(dev);
  141. }
  142. }
  143. void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
  144. int value, int index, int length, uint8_t *data)
  145. {
  146. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  147. if (klass->handle_control) {
  148. klass->handle_control(dev, p, request, value, index, length, data);
  149. }
  150. }
  151. void usb_device_handle_data(USBDevice *dev, USBPacket *p)
  152. {
  153. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  154. if (klass->handle_data) {
  155. klass->handle_data(dev, p);
  156. }
  157. }
  158. const char *usb_device_get_product_desc(USBDevice *dev)
  159. {
  160. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  161. return klass->product_desc;
  162. }
  163. const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
  164. {
  165. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  166. if (dev->usb_desc) {
  167. return dev->usb_desc;
  168. }
  169. return klass->usb_desc;
  170. }
  171. void usb_device_set_interface(USBDevice *dev, int interface,
  172. int alt_old, int alt_new)
  173. {
  174. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  175. if (klass->set_interface) {
  176. klass->set_interface(dev, interface, alt_old, alt_new);
  177. }
  178. }
  179. void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
  180. {
  181. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  182. if (klass->flush_ep_queue) {
  183. klass->flush_ep_queue(dev, ep);
  184. }
  185. }
  186. void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
  187. {
  188. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  189. if (klass->ep_stopped) {
  190. klass->ep_stopped(dev, ep);
  191. }
  192. }
  193. int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
  194. int streams)
  195. {
  196. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  197. if (klass->alloc_streams) {
  198. return klass->alloc_streams(dev, eps, nr_eps, streams);
  199. }
  200. return 0;
  201. }
  202. void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
  203. {
  204. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  205. if (klass->free_streams) {
  206. klass->free_streams(dev, eps, nr_eps);
  207. }
  208. }
  209. static void usb_qdev_realize(DeviceState *qdev, Error **errp)
  210. {
  211. USBDevice *dev = USB_DEVICE(qdev);
  212. Error *local_err = NULL;
  213. pstrcpy(dev->product_desc, sizeof(dev->product_desc),
  214. usb_device_get_product_desc(dev));
  215. dev->auto_attach = 1;
  216. QLIST_INIT(&dev->strings);
  217. usb_ep_init(dev);
  218. usb_claim_port(dev, &local_err);
  219. if (local_err) {
  220. error_propagate(errp, local_err);
  221. return;
  222. }
  223. usb_device_realize(dev, &local_err);
  224. if (local_err) {
  225. usb_release_port(dev);
  226. error_propagate(errp, local_err);
  227. return;
  228. }
  229. if (dev->auto_attach) {
  230. usb_device_attach(dev, &local_err);
  231. if (local_err) {
  232. usb_qdev_unrealize(qdev);
  233. error_propagate(errp, local_err);
  234. return;
  235. }
  236. }
  237. if (dev->pcap_filename) {
  238. int fd = qemu_open_old(dev->pcap_filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  239. if (fd < 0) {
  240. error_setg(errp, "open %s failed", dev->pcap_filename);
  241. usb_qdev_unrealize(qdev);
  242. return;
  243. }
  244. dev->pcap = fdopen(fd, "w");
  245. usb_pcap_init(dev->pcap);
  246. }
  247. }
  248. static void usb_qdev_unrealize(DeviceState *qdev)
  249. {
  250. USBDevice *dev = USB_DEVICE(qdev);
  251. USBDescString *s, *next;
  252. QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
  253. QLIST_REMOVE(s, next);
  254. g_free(s->str);
  255. g_free(s);
  256. }
  257. if (dev->pcap) {
  258. fclose(dev->pcap);
  259. }
  260. if (dev->attached) {
  261. usb_device_detach(dev);
  262. }
  263. usb_device_unrealize(dev);
  264. if (dev->port) {
  265. usb_release_port(dev);
  266. }
  267. }
  268. typedef struct LegacyUSBFactory
  269. {
  270. const char *name;
  271. const char *usbdevice_name;
  272. USBDevice *(*usbdevice_init)(void);
  273. } LegacyUSBFactory;
  274. static GSList *legacy_usb_factory;
  275. void usb_legacy_register(const char *typename, const char *usbdevice_name,
  276. USBDevice *(*usbdevice_init)(void))
  277. {
  278. if (usbdevice_name) {
  279. LegacyUSBFactory *f = g_malloc0(sizeof(*f));
  280. f->name = typename;
  281. f->usbdevice_name = usbdevice_name;
  282. f->usbdevice_init = usbdevice_init;
  283. legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
  284. }
  285. }
  286. USBDevice *usb_new(const char *name)
  287. {
  288. return USB_DEVICE(qdev_new(name));
  289. }
  290. static USBDevice *usb_try_new(const char *name)
  291. {
  292. return USB_DEVICE(qdev_try_new(name));
  293. }
  294. bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp)
  295. {
  296. return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
  297. }
  298. USBDevice *usb_create_simple(USBBus *bus, const char *name)
  299. {
  300. USBDevice *dev = usb_new(name);
  301. usb_realize_and_unref(dev, bus, &error_abort);
  302. return dev;
  303. }
  304. static void usb_fill_port(USBPort *port, void *opaque, int index,
  305. USBPortOps *ops, int speedmask)
  306. {
  307. port->opaque = opaque;
  308. port->index = index;
  309. port->ops = ops;
  310. port->speedmask = speedmask;
  311. usb_port_location(port, NULL, index + 1);
  312. }
  313. void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
  314. USBPortOps *ops, int speedmask)
  315. {
  316. usb_fill_port(port, opaque, index, ops, speedmask);
  317. QTAILQ_INSERT_TAIL(&bus->free, port, next);
  318. bus->nfree++;
  319. }
  320. void usb_register_companion(const char *masterbus, USBPort *ports[],
  321. uint32_t portcount, uint32_t firstport,
  322. void *opaque, USBPortOps *ops, int speedmask,
  323. Error **errp)
  324. {
  325. USBBus *bus;
  326. int i;
  327. QTAILQ_FOREACH(bus, &busses, next) {
  328. if (strcmp(bus->qbus.name, masterbus) == 0) {
  329. break;
  330. }
  331. }
  332. if (!bus) {
  333. error_setg(errp, "USB bus '%s' not found", masterbus);
  334. return;
  335. }
  336. if (!bus->ops->register_companion) {
  337. error_setg(errp, "Can't use USB bus '%s' as masterbus,"
  338. " it doesn't support companion controllers",
  339. masterbus);
  340. return;
  341. }
  342. for (i = 0; i < portcount; i++) {
  343. usb_fill_port(ports[i], opaque, i, ops, speedmask);
  344. }
  345. bus->ops->register_companion(bus, ports, portcount, firstport, errp);
  346. }
  347. void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
  348. {
  349. if (upstream) {
  350. int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
  351. upstream->path, portnr);
  352. /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
  353. assert(l < sizeof(downstream->path));
  354. downstream->hubcount = upstream->hubcount + 1;
  355. } else {
  356. snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
  357. downstream->hubcount = 0;
  358. }
  359. }
  360. void usb_unregister_port(USBBus *bus, USBPort *port)
  361. {
  362. if (port->dev) {
  363. object_unparent(OBJECT(port->dev));
  364. }
  365. QTAILQ_REMOVE(&bus->free, port, next);
  366. bus->nfree--;
  367. }
  368. void usb_claim_port(USBDevice *dev, Error **errp)
  369. {
  370. USBBus *bus = usb_bus_from_device(dev);
  371. USBPort *port;
  372. USBDevice *hub;
  373. assert(dev->port == NULL);
  374. if (dev->port_path) {
  375. QTAILQ_FOREACH(port, &bus->free, next) {
  376. if (strcmp(port->path, dev->port_path) == 0) {
  377. break;
  378. }
  379. }
  380. if (port == NULL) {
  381. error_setg(errp, "usb port %s (bus %s) not found (in use?)",
  382. dev->port_path, bus->qbus.name);
  383. return;
  384. }
  385. } else {
  386. if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
  387. /* Create a new hub and chain it on */
  388. hub = usb_try_new("usb-hub");
  389. if (hub) {
  390. usb_realize_and_unref(hub, bus, NULL);
  391. }
  392. }
  393. if (bus->nfree == 0) {
  394. error_setg(errp, "tried to attach usb device %s to a bus "
  395. "with no free ports", dev->product_desc);
  396. return;
  397. }
  398. port = QTAILQ_FIRST(&bus->free);
  399. }
  400. trace_usb_port_claim(bus->busnr, port->path);
  401. QTAILQ_REMOVE(&bus->free, port, next);
  402. bus->nfree--;
  403. dev->port = port;
  404. port->dev = dev;
  405. QTAILQ_INSERT_TAIL(&bus->used, port, next);
  406. bus->nused++;
  407. }
  408. void usb_release_port(USBDevice *dev)
  409. {
  410. USBBus *bus = usb_bus_from_device(dev);
  411. USBPort *port = dev->port;
  412. assert(port != NULL);
  413. trace_usb_port_release(bus->busnr, port->path);
  414. QTAILQ_REMOVE(&bus->used, port, next);
  415. bus->nused--;
  416. dev->port = NULL;
  417. port->dev = NULL;
  418. QTAILQ_INSERT_TAIL(&bus->free, port, next);
  419. bus->nfree++;
  420. }
  421. static void usb_mask_to_str(char *dest, size_t size,
  422. unsigned int speedmask)
  423. {
  424. static const struct {
  425. unsigned int mask;
  426. const char *name;
  427. } speeds[] = {
  428. { .mask = USB_SPEED_MASK_FULL, .name = "full" },
  429. { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
  430. { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
  431. };
  432. int i, pos = 0;
  433. for (i = 0; i < ARRAY_SIZE(speeds); i++) {
  434. if (speeds[i].mask & speedmask) {
  435. pos += snprintf(dest + pos, size - pos, "%s%s",
  436. pos ? "+" : "",
  437. speeds[i].name);
  438. }
  439. }
  440. if (pos == 0) {
  441. snprintf(dest, size, "unknown");
  442. }
  443. }
  444. void usb_check_attach(USBDevice *dev, Error **errp)
  445. {
  446. USBBus *bus = usb_bus_from_device(dev);
  447. USBPort *port = dev->port;
  448. char devspeed[32], portspeed[32];
  449. assert(port != NULL);
  450. assert(!dev->attached);
  451. usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
  452. usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
  453. trace_usb_port_attach(bus->busnr, port->path,
  454. devspeed, portspeed);
  455. if (!(port->speedmask & dev->speedmask)) {
  456. error_setg(errp, "Warning: speed mismatch trying to attach"
  457. " usb device \"%s\" (%s speed)"
  458. " to bus \"%s\", port \"%s\" (%s speed)",
  459. dev->product_desc, devspeed,
  460. bus->qbus.name, port->path, portspeed);
  461. return;
  462. }
  463. }
  464. void usb_device_attach(USBDevice *dev, Error **errp)
  465. {
  466. USBPort *port = dev->port;
  467. Error *local_err = NULL;
  468. usb_check_attach(dev, &local_err);
  469. if (local_err) {
  470. error_propagate(errp, local_err);
  471. return;
  472. }
  473. dev->attached = true;
  474. usb_attach(port);
  475. }
  476. int usb_device_detach(USBDevice *dev)
  477. {
  478. USBBus *bus = usb_bus_from_device(dev);
  479. USBPort *port = dev->port;
  480. assert(port != NULL);
  481. assert(dev->attached);
  482. trace_usb_port_detach(bus->busnr, port->path);
  483. usb_detach(port);
  484. dev->attached = false;
  485. return 0;
  486. }
  487. static const char *usb_speed(unsigned int speed)
  488. {
  489. static const char *txt[] = {
  490. [ USB_SPEED_LOW ] = "1.5",
  491. [ USB_SPEED_FULL ] = "12",
  492. [ USB_SPEED_HIGH ] = "480",
  493. [ USB_SPEED_SUPER ] = "5000",
  494. };
  495. if (speed >= ARRAY_SIZE(txt))
  496. return "?";
  497. return txt[speed];
  498. }
  499. static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
  500. {
  501. USBDevice *dev = USB_DEVICE(qdev);
  502. USBBus *bus = usb_bus_from_device(dev);
  503. monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
  504. indent, "", bus->busnr, dev->addr,
  505. dev->port ? dev->port->path : "-",
  506. usb_speed(dev->speed), dev->product_desc,
  507. dev->attached ? ", attached" : "");
  508. }
  509. static char *usb_get_dev_path(DeviceState *qdev)
  510. {
  511. USBDevice *dev = USB_DEVICE(qdev);
  512. DeviceState *hcd = qdev->parent_bus->parent;
  513. char *id = qdev_get_dev_path(hcd);
  514. if (id) {
  515. char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
  516. g_free(id);
  517. return ret;
  518. } else {
  519. return g_strdup(dev->port->path);
  520. }
  521. }
  522. static char *usb_get_fw_dev_path(DeviceState *qdev)
  523. {
  524. USBDevice *dev = USB_DEVICE(qdev);
  525. char *fw_path, *in;
  526. ssize_t pos = 0, fw_len;
  527. long nr;
  528. fw_len = 32 + strlen(dev->port->path) * 6;
  529. fw_path = g_malloc(fw_len);
  530. in = dev->port->path;
  531. while (fw_len - pos > 0) {
  532. nr = strtol(in, &in, 10);
  533. if (in[0] == '.') {
  534. /* some hub between root port and device */
  535. pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
  536. in++;
  537. } else {
  538. /* the device itself */
  539. snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
  540. qdev_fw_name(qdev), nr);
  541. break;
  542. }
  543. }
  544. return fw_path;
  545. }
  546. HumanReadableText *qmp_x_query_usb(Error **errp)
  547. {
  548. g_autoptr(GString) buf = g_string_new("");
  549. USBBus *bus;
  550. USBDevice *dev;
  551. USBPort *port;
  552. if (QTAILQ_EMPTY(&busses)) {
  553. error_setg(errp, "USB support not enabled");
  554. return NULL;
  555. }
  556. QTAILQ_FOREACH(bus, &busses, next) {
  557. QTAILQ_FOREACH(port, &bus->used, next) {
  558. dev = port->dev;
  559. if (!dev)
  560. continue;
  561. g_string_append_printf(buf,
  562. " Device %d.%d, Port %s, Speed %s Mb/s, "
  563. "Product %s%s%s\n",
  564. bus->busnr, dev->addr, port->path,
  565. usb_speed(dev->speed), dev->product_desc,
  566. dev->qdev.id ? ", ID: " : "",
  567. dev->qdev.id ?: "");
  568. }
  569. }
  570. return human_readable_text_from_str(buf);
  571. }
  572. /* handle legacy -usbdevice cmd line option */
  573. USBDevice *usbdevice_create(const char *driver)
  574. {
  575. USBBus *bus = usb_bus_find(-1 /* any */);
  576. LegacyUSBFactory *f = NULL;
  577. Error *err = NULL;
  578. GSList *i;
  579. USBDevice *dev;
  580. if (strchr(driver, ':')) {
  581. error_report("usbdevice parameters are not supported anymore");
  582. return NULL;
  583. }
  584. for (i = legacy_usb_factory; i; i = i->next) {
  585. f = i->data;
  586. if (strcmp(f->usbdevice_name, driver) == 0) {
  587. break;
  588. }
  589. }
  590. if (i == NULL) {
  591. #if 0
  592. /* no error because some drivers are not converted (yet) */
  593. error_report("usbdevice %s not found", driver);
  594. #endif
  595. return NULL;
  596. }
  597. if (!bus) {
  598. error_report("Error: no usb bus to attach usbdevice %s, "
  599. "please try -machine usb=on and check that "
  600. "the machine model supports USB", driver);
  601. return NULL;
  602. }
  603. dev = f->usbdevice_init ? f->usbdevice_init() : usb_new(f->name);
  604. if (!dev) {
  605. error_report("Failed to create USB device '%s'", f->name);
  606. return NULL;
  607. }
  608. if (!usb_realize_and_unref(dev, bus, &err)) {
  609. error_reportf_err(err, "Failed to initialize USB device '%s': ",
  610. f->name);
  611. object_unparent(OBJECT(dev));
  612. return NULL;
  613. }
  614. return dev;
  615. }
  616. static bool usb_get_attached(Object *obj, Error **errp)
  617. {
  618. USBDevice *dev = USB_DEVICE(obj);
  619. return dev->attached;
  620. }
  621. static void usb_set_attached(Object *obj, bool value, Error **errp)
  622. {
  623. USBDevice *dev = USB_DEVICE(obj);
  624. if (dev->attached == value) {
  625. return;
  626. }
  627. if (value) {
  628. usb_device_attach(dev, errp);
  629. } else {
  630. usb_device_detach(dev);
  631. }
  632. }
  633. static void usb_device_instance_init(Object *obj)
  634. {
  635. USBDevice *dev = USB_DEVICE(obj);
  636. USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
  637. if (klass->attached_settable) {
  638. object_property_add_bool(obj, "attached",
  639. usb_get_attached, usb_set_attached);
  640. } else {
  641. object_property_add_bool(obj, "attached",
  642. usb_get_attached, NULL);
  643. }
  644. }
  645. static void usb_device_class_init(ObjectClass *klass, void *data)
  646. {
  647. DeviceClass *k = DEVICE_CLASS(klass);
  648. k->bus_type = TYPE_USB_BUS;
  649. k->realize = usb_qdev_realize;
  650. k->unrealize = usb_qdev_unrealize;
  651. device_class_set_props(k, usb_props);
  652. }
  653. static const TypeInfo usb_device_type_info = {
  654. .name = TYPE_USB_DEVICE,
  655. .parent = TYPE_DEVICE,
  656. .instance_size = sizeof(USBDevice),
  657. .instance_init = usb_device_instance_init,
  658. .abstract = true,
  659. .class_size = sizeof(USBDeviceClass),
  660. .class_init = usb_device_class_init,
  661. };
  662. static void usb_register_types(void)
  663. {
  664. type_register_static(&usb_bus_info);
  665. type_register_static(&usb_device_type_info);
  666. }
  667. type_init(usb_register_types)