Skip to contentSkip to conversation

Introduce printer subsystem and USB printer device

PATCH In review Ruien Zhang 2 participants 7 messages Jan 13, 2022 – Jan 17, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Introduce printer subsystem and USB printer device

From: zhangruien <[email protected]>

Currently, printer support in QEMU can generally be considered with these approaches:

1) USB passthrough & redirection, with the limitation of flexibility and transport-specific issues that come along with.

2) Network reachability with network printers, which is also driver-specific, thus less friendly to small systems.

Driverless Printing [1] may or may not be network-dependent, the former is the general case while the latter imposes less restraints on cloud environments, and it doesn't necessarily mean that we have to follow the methods in 1) to achieve this. Transport protocols targeted at devices such as USB printer class [2] with the extension of IPP-over-USB [3] and many others can be integrated into QEMU, presenting more flexibility and functionality.

This patchset introduces:

1) Skeleton of QEMU printer subsystem with a dummy builtin driver.

2) USB printer device emulation, with definitions in the extension of IPP-over- USB [3].

WIP:

1) QEMU printer subsystem interfaces, which will be finalized with a concrete backend driver.

2) IPP-over-USB implementation.

[1]: https://openprinting.github.io/driverless
[2]: https://www.usb.org/sites/default/files/usbprint11a021811.pdf
[3]: https://www.usb.org/document-library/ipp-protocol-10

zhangruien (2):
printer: Introduce printer subsystem
usb-printer: Introduce USB printer class

 MAINTAINERS                 |   7 +
 docs/system/devices/usb.rst |   3 +
 hw/usb/Kconfig              |   5 +
 hw/usb/dev-printer.c        | 423 ++++++++++++++++++++++++++++++++++++++++++++
 hw/usb/meson.build          |   1 +
 hw/usb/trace-events         |  11 ++
 include/hw/usb/printer.h    |  93 ++++++++++
 include/printer/printer.h   |  42 +++++
 meson.build                 |  12 +-
 meson_options.txt           |   3 +
 printer/builtin.c           |  61 +++++++
 printer/meson.build         |  14 ++
 printer/printer.c           | 191 ++++++++++++++++++++
 printer/trace-events        |   5 +
 printer/trace.h             |   1 +
 qapi/meson.build            |   1 +
 qapi/printer.json           |  47 +++++
 qapi/qapi-schema.json       |   1 +
 qemu-options.hx             |   8 +
 softmmu/vl.c                |   4 +
 20 files changed, 932 insertions(+), 1 deletion(-)
 create mode 100644 hw/usb/dev-printer.c
 create mode 100644 include/hw/usb/printer.h
 create mode 100644 include/printer/printer.h
 create mode 100644 printer/builtin.c
 create mode 100644 printer/meson.build
 create mode 100644 printer/printer.c
 create mode 100644 printer/trace-events
 create mode 100644 printer/trace.h
 create mode 100644 qapi/printer.json
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/2 printer: Introduce printer subsystem

From: zhangruien <[email protected]>

This patch describes the skeleton of QEMU printer subsystem with a dummy builtin driver.

---
 MAINTAINERS               |   7 ++
 include/printer/printer.h |  42 ++++++++++
 meson.build               |  12 ++-
 meson_options.txt         |   3 +
 printer/builtin.c         |  61 +++++++++++++++
 printer/meson.build       |  14 ++++
 printer/printer.c         | 191 ++++++++++++++++++++++++++++++++++++++++++++++
 printer/trace-events      |   5 ++
 printer/trace.h           |   1 +
 qapi/meson.build          |   1 +
 qapi/printer.json         |  47 ++++++++++++
 qapi/qapi-schema.json     |   1 +
 qemu-options.hx           |   8 ++
 softmmu/vl.c              |   4 +
 14 files changed, 396 insertions(+), 1 deletion(-)
 create mode 100644 include/printer/printer.h
 create mode 100644 printer/builtin.c
 create mode 100644 printer/meson.build
 create mode 100644 printer/printer.c
 create mode 100644 printer/trace-events
 create mode 100644 printer/trace.h
 create mode 100644 qapi/printer.json
MAINTAINERS +7 -0
index c98a61caee..689f20d740 100644
@@ -3086,6 +3086,13 @@ F: hw/core/clock-vmstate.c
 F: hw/core/qdev-clock.c
 F: docs/devel/clocks.rst
 
+Printer Subsystem
+M: Ruien Zhang <[email protected]>
+S: Maintained
+F: include/printer
+F: printer
+F: qapi/printer.json
+
 Usermode Emulation
 ------------------
 Overall usermode emulation
include/printer/printer.h new file +42 -0

42 lines not on this page · raw message

meson.build +11 -1
index c1b1db1e28..b3db26190d 100644
@@ -2397,6 +2397,7 @@ genh += hxdep
 authz_ss = ss.source_set()
 blockdev_ss = ss.source_set()
 block_ss = ss.source_set()
+printer_ss = ss.source_set()
 chardev_ss = ss.source_set()
 common_ss = ss.source_set()
 common_user_ss = ss.source_set()
@@ -2455,6 +2456,7 @@ if have_system
     'audio',
     'backends',
     'backends/tpm',
+    'printer',
     'chardev',
     'ebpf',
     'hw/9pfs',
@@ -2574,6 +2576,7 @@ endif
 
 subdir('audio')
 subdir('io')
+subdir('printer')
 subdir('chardev')
 subdir('fsdev')
 subdir('dump')
@@ -2843,6 +2846,13 @@ libqmp = static_library('qmp', qmp_ss.sources() + genh,
 
 qmp = declare_dependency(link_whole: [libqmp])
 
+printer_ss = printer_ss.apply(config_host, strict: false)
+libprinter = static_library('printer', printer_ss.sources() + genh,
+                            name_suffix: 'fa',
+                            build_by_default: false)
+
+printer = declare_dependency(link_whole: libprinter)
+
 libchardev = static_library('chardev', chardev_ss.sources() + genh,
                             name_suffix: 'fa',
                             dependencies: [gnutls],
@@ -2869,7 +2879,7 @@ foreach m : block_mods + softmmu_mods
                 install_dir: qemu_moddir)
 endforeach
 
-softmmu_ss.add(authz, blockdev, chardev, crypto, io, qmp)
+softmmu_ss.add(authz, blockdev, printer, chardev, crypto, io, qmp)
 common_ss.add(qom, qemuutil)
 
 common_ss.add_all(when: 'CONFIG_SOFTMMU', if_true: [softmmu_ss])
meson_options.txt +3 -0
index 921967eddb..5b3b502798 100644
@@ -208,3 +208,6 @@ option('fdt', type: 'combo', value: 'auto',
 
 option('selinux', type: 'feature', value: 'auto',
        description: 'SELinux support in qemu-nbd')
+
+option('printer', type: 'feature', value: 'auto',
+       description: 'Printer subsystem support')
printer/builtin.c new file +61 -0

61 lines not on this page · raw message

printer/meson.build new file +14 -0
new file mode 100644
index 0000000000..9814de2a57
@@ -0,0 +1,14 @@
+printer_ss.add([files(
+  'printer.c',
+)])
+
+printer_modules = {}
+foreach m : [
+  ['builtin', files('builtin.c')],
+]
+  module_ss = ss.source_set()
+  module_ss.add(m[1])
+  printer_modules += {m[0] : module_ss}
+endforeach
+
+modules += {'printer': printer_modules}
printer/printer.c new file +191 -0

191 lines not on this page · raw message

printer/trace-events new file +5 -0
new file mode 100644
index 0000000000..e453bbe691
@@ -0,0 +1,5 @@
+# See docs/devel/tracing.rst for syntax documentation.
+
+# printer.c
+qemu_printer_new(const char *dev, char *typename) "%s: new printer with type %s"
+qemu_printer_del(const char *dev) "%s: delete printer"
printer/trace.h new file +1 -0
new file mode 100644
index 0000000000..9717d37ac7
@@ -0,0 +1 @@
+#include "trace/trace-printer.h"
qapi/meson.build +1 -0
index c0c49c15e4..f85af6b7d6 100644
@@ -59,6 +59,7 @@ if have_system
     'rdma',
     'rocker',
     'tpm',
+    'printer',
   ]
 endif
 if have_system or have_tools
qapi/printer.json new file +47 -0

47 lines not on this page · raw message

qapi/qapi-schema.json +1 -0
index 4912b9744e..114b6a80cb 100644
@@ -93,3 +93,4 @@
 { 'include': 'audio.json' }
 { 'include': 'acpi.json' }
 { 'include': 'pci.json' }
+{ 'include': 'printer.json' }
qemu-options.hx +8 -0
index ec90505d84..448a456f86 100644
@@ -3564,6 +3564,14 @@ The available backends are:
     traffic identified by a name (preferably a fqdn).
 ERST
 
+DEFHEADING(Printer device options:)
+
+DEF("printerdev", HAS_ARG, QEMU_OPTION_printerdev,
+    "-printerdev help\n"
+    "-printerdev builtin,id=id\n"
+    , QEMU_ARCH_ALL
+)
+
 DEFHEADING()
 
 #ifdef CONFIG_TPM
softmmu/vl.c +4 -0
index a8cad43691..67b3c48fa1 100644
@@ -94,6 +94,7 @@
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
+#include "printer/printer.h"
 #include "sysemu/qtest.h"
 
 #include "disas/disas.h"
@@ -3247,6 +3248,9 @@ void qemu_init(int argc, char **argv, char **envp)
                              qemu_opt_get(opts, "mount_tag"), &error_abort);
                 break;
             }
+            case QEMU_OPTION_printerdev:
+                qemu_printer_new_from_opts(optarg);
+                break;
             case QEMU_OPTION_serial:
                 add_device_config(DEV_SERIAL, optarg);
                 default_serial = 0;
Signature · 1 line
2.11.0
#2 lore raw

PATCH 2/2 usb-printer: Introduce USB printer class

In reply to Ruien Zhang

From: zhangruien <[email protected]>

The USB printer device emulation is currently provided with:

1) Definitions and corresponding action handlers of class-specific requests with essential descriptors in USB Printer Class Specification 1.1 [1].

2) Extended definitions of interface protocol and class-specific descriptors in IPP-over-USB protocol 1.0 [2].

A usb printer device can be assembled with the following example of command-line arguments:

    -device piix4-usb-uhci,id=uhci,bus=pci.0 \
    -device usb-printer,id=usb-printer0,printerdev=printer0,bus=uhci.0,terminal=printer \
    -printerdev builtin,id=printer0

[1]: https://www.usb.org/sites/default/files/usbprint11a021811.pdf
[2]: https://www.usb.org/document-library/ipp-protocol-10

---
 docs/system/devices/usb.rst |   3 +
 hw/usb/Kconfig              |   5 +
 hw/usb/dev-printer.c        | 423 ++++++++++++++++++++++++++++++++++++++++++++
 hw/usb/meson.build          |   1 +
 hw/usb/trace-events         |  11 ++
 include/hw/usb/printer.h    |  93 ++++++++++
 6 files changed, 536 insertions(+)
 create mode 100644 hw/usb/dev-printer.c
 create mode 100644 include/hw/usb/printer.h
docs/system/devices/usb.rst +3 -0
index afb7d6c226..6e87c3be11 100644
@@ -199,6 +199,9 @@ option or the ``device_add`` monitor command. Available devices are:
 ``u2f-{emulated,passthru}``
    Universal Second Factor device
 
+``usb-printer``
+   USB printer device
+
 Physical port addressing
 ^^^^^^^^^^^^^^^^^^^^^^^^
 
hw/usb/Kconfig +5 -0
index 53f8283ffd..1b5a953cae 100644
@@ -133,3 +133,8 @@ config XLNX_USB_SUBSYS
     bool
     default y if XLNX_VERSAL
     select USB_DWC3
+
+config USB_PRINTER
+    bool
+    default y
+    depends on USB
hw/usb/dev-printer.c new file +423 -0

423 lines not on this page · raw message

hw/usb/meson.build +1 -0
index de853d780d..f79d5e1f74 100644
@@ -44,6 +44,7 @@ softmmu_ss.add(when: 'CONFIG_USB_STORAGE_UAS', if_true: files('dev-uas.c'))
 softmmu_ss.add(when: 'CONFIG_USB_AUDIO', if_true: files('dev-audio.c'))
 softmmu_ss.add(when: 'CONFIG_USB_SERIAL', if_true: files('dev-serial.c'))
 softmmu_ss.add(when: 'CONFIG_USB_NETWORK', if_true: files('dev-network.c'))
+softmmu_ss.add(when: 'CONFIG_USB_PRINTER', if_true: files('dev-printer.c'))
 softmmu_ss.add(when: ['CONFIG_POSIX', 'CONFIG_USB_STORAGE_MTP'], if_true: files('dev-mtp.c'))
 
 # smartcard
hw/usb/trace-events +11 -0
index b8287b63f1..e3fed30c43 100644
@@ -345,3 +345,14 @@ usb_serial_set_baud(int bus, int addr, int baud) "dev %d:%u baud rate %d"
 usb_serial_set_data(int bus, int addr, int parity, int data, int stop) "dev %d:%u parity %c, data bits %d, stop bits %d"
 usb_serial_set_flow_control(int bus, int addr, int index) "dev %d:%u flow control %d"
 usb_serial_set_xonxoff(int bus, int addr, uint8_t xon, uint8_t xoff) "dev %d:%u xon 0x%x xoff 0x%x"
+
+# dev-printer.c
+usb_printer_handle_reset(int bus, int addr) "dev %d:%u reset"
+usb_printer_get_device_id(int bus, int addr) "dev %d:%u get device id"
+usb_printer_get_port_status(int bus, int addr) "dev %d:%u get port status"
+usb_printer_handle_soft_reset(int bus, int addr) "dev %d:%u soft reset"
+usb_printer_handle_control_error(int bus, int addr, int request, int value, int index, int length) "dev %d:%u handle control error, request 0x%x, value 0x%x, index 0x%x, length 0x%x"
+usb_printer_handle_data(int bus, int addr, int pid, int ep) "dev %d:%u data, pid 0x%x, ep %d"
+usb_printer_handle_data_out(int bus, int addr, int size) "dev %d:%u data out, size %d"
+usb_printer_handle_data_in(int bus, int addr, int size) "dev %d:%u data in, size %d"
+usb_printer_set_interface(int bus, int addr, int iface, int old, int value) "dev %d:%u set interface %d, old %d, value %d"
include/hw/usb/printer.h new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.11.0
#3 lore raw
In reply to Ruien Zhang

Hi,

Quoted 7 lines

This patchset introduces:

1) Skeleton of QEMU printer subsystem with a dummy builtin driver.

2) USB printer device emulation, with definitions in the extension of IPP-over- USB [3].

Quoted 6 lines

WIP:

1) QEMU printer subsystem interfaces, which will be finalized with a concrete backend driver.

2) IPP-over-USB implementation.

Hmm, I'm wondering what uses cases you have in mind and whenever it makes sense to introduce a printer subsystem?

Having an ipp-over-usb device looks useful, but the only use case I can see is to allow guests access a network printer. I can't see the benefits of a printer subsystem, especially in a world where non-ipp printers are going extinct. We would most likely have just a single kind of printer backend, where the only job qemu will have is to forwarding requests and replies, maybe with some http header rewriting.

Likewise usb would be the one and only device (parallel ports are long gone in printers). So the indirection added by a printer subsystem doesn't buy us anything because we just don't need that flexibility. I'd suggest to pass the url directly to the device instead:

qemu -device usb-ipp-printer,url=ipp://hostname/ipp/printer

take care,
Gerd

#4 lore raw
Quoted 11 lines · Gerd Hoffmann
   Hi,
Quoted 9 lines

This patchset introduces:

1) Skeleton of QEMU printer subsystem with a dummy builtin driver.

2) USB printer device emulation, with definitions in the extension of IPP-over-

    USB [3].

WIP:

Quoted 8 lines
Quoted 4 lines

1) QEMU printer subsystem interfaces, which will be finalized with a concrete backend driver.

2) IPP-over-USB implementation.

Hmm, I'm wondering what uses cases you have in mind and whenever it makes sense to introduce a printer subsystem?

Simply for the "potential" backend diversity. I have to admit that I haven't figured out another backend which would be commonly-seen either, which is also one part of the reason why the interfaces are not firming up right now.

Quoted 12 lines

Having an ipp-over-usb device looks useful, but the only use case I can see is to allow guests access a network printer. I can't see the benefits of a printer subsystem, especially in a world where non-ipp printers are going extinct. We would most likely have just a single kind of printer backend, where the only job qemu will have is to forwarding requests and replies, maybe with some http header rewriting.

Likewise usb would be the one and only device (parallel ports are long gone in printers). So the indirection added by a printer subsystem doesn't buy us anything because we just don't need that flexibility. I'd suggest to pass the url directly to the device instead:

Quoted 5 lines

qemu -device usb-ipp-printer,url=ipp://hostname/ipp/printer

take care,
Gerd

Indeed, the subsystem is an over-abstraction. The forwarding way is much neater, considering how things really work nowadays.

Anyway, thanks for the practical suggestion, it will be revised, along with other designs around the path I'm currently working on.

Regards,
Ruien

#5 lore raw
In reply to Gerd Hoffmann
Quoted 11 lines · Gerd Hoffmann
   Hi,
Quoted 9 lines

This patchset introduces:

1) Skeleton of QEMU printer subsystem with a dummy builtin driver.

2) USB printer device emulation, with definitions in the extension of IPP-over-

    USB [3].

WIP:

Quoted 7 lines
Quoted 4 lines

1) QEMU printer subsystem interfaces, which will be finalized with a concrete backend driver.

2) IPP-over-USB implementation.

Hmm, I'm wondering what uses cases you have in mind and whenever it makes sense to introduce a printer subsystem?

I'm having an idea about the use case, let's discuss a bit more about it here.

If I want to expose some Virtual Device Interfaces (VDI) on USB-IPP printer device to remote desktop service like spice-server, is it rational to register these interfaces to the printer subsystem which will play as a middle layer? A concrete example is QXL virtual GPU with VDI between QEMU and spice-server. What if other QEMU-emulated devices also want to take part in the spice service routine? This can be achieved naturally by the registered handlers in the subsystems, which further exchange data with the underlying devices (USB-IPP printer here, for example). Nevertheless, I totally agree that it is straightforward to make devices like USB-IPP printer to be passed-through in local environments, which prompts me to add the uri-option. But I want it to be compatible with other use cases, like this one.

I'll give this idea a try.

Quoted 13 lines

Having an ipp-over-usb device looks useful, but the only use case I can see is to allow guests access a network printer. I can't see the benefits of a printer subsystem, especially in a world where non-ipp printers are going extinct. We would most likely have just a single kind of printer backend, where the only job qemu will have is to forwarding requests and replies, maybe with some http header rewriting.

Likewise usb would be the one and only device (parallel ports are long gone in printers). So the indirection added by a printer subsystem doesn't buy us anything because we just don't need that flexibility. I'd suggest to pass the url directly to the device instead:

Quoted 5 lines

qemu -device usb-ipp-printer,url=ipp://hostname/ipp/printer

take care,
Gerd

Regards,
Ruien

#6 lore raw

Hi,

Quoted 4 lines

If I want to expose some Virtual Device Interfaces (VDI) on USB-IPP printer device to remote desktop service like spice-server, is it rational to register these interfaces to the printer subsystem which will play as a middle layer?

I'd suggest to implement the usb printer in the spice client then and connect it to the virtual machine using the usb redirection protocol, similar to the existing usb storage emulation for cdrom redirection.

take care,
Gerd