2
0

bus.c 20 KB

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