Skip to content

Instantly share code, notes, and snippets.

@parkerlreed
Last active June 10, 2024 20:58
Show Gist options
  • Save parkerlreed/17409aecfe6b6174c4042bdc66de76d9 to your computer and use it in GitHub Desktop.
Save parkerlreed/17409aecfe6b6174c4042bdc66de76d9 to your computer and use it in GitHub Desktop.
Use your Steam Deck as a USB printer
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/poll.h>
#include <sys/ioctl.h>
#include <linux/usb/g_printer.h>
#define PRINTER_FILE "/dev/g_printer0"
#define BUF_SIZE 512
/*
* 'usage()' - Show program usage.
*/
static void
usage(const char *option) /* I - Option string or NULL */
{
if (option) {
fprintf(stderr,"prn_example: Unknown option \"%s\"!\n",
option);
}
fputs("\n", stderr);
fputs("Usage: prn_example -[options]\n", stderr);
fputs("Options:\n", stderr);
fputs("\n", stderr);
fputs("-get_status Get the current printer status.\n", stderr);
fputs("-selected Set the selected status to selected.\n", stderr);
fputs("-not_selected Set the selected status to NOT selected.\n",
stderr);
fputs("-error Set the error status to error.\n", stderr);
fputs("-no_error Set the error status to NO error.\n", stderr);
fputs("-paper_out Set the paper status to paper out.\n", stderr);
fputs("-paper_loaded Set the paper status to paper loaded.\n",
stderr);
fputs("-read_data Read printer data from driver.\n", stderr);
fputs("-write_data Write printer sata to driver.\n", stderr);
fputs("-NB_read_data (Non-Blocking) Read printer data from driver.\n",
stderr);
fputs("\n\n", stderr);
exit(1);
}
static int
read_printer_data()
{
struct pollfd fd[1];
/* Open device file for printer gadget. */
fd[0].fd = open(PRINTER_FILE, O_RDWR);
if (fd[0].fd < 0) {
printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);
close(fd[0].fd);
return(-1);
}
fd[0].events = POLLIN | POLLRDNORM;
while (1) {
static char buf[BUF_SIZE];
int bytes_read;
int retval;
/* Wait for up to 1 second for data. */
retval = poll(fd, 1, 1000);
if (retval && (fd[0].revents & POLLRDNORM)) {
/* Read data from printer gadget driver. */
bytes_read = read(fd[0].fd, buf, BUF_SIZE);
if (bytes_read < 0) {
printf("Error %d reading from %s\n",
fd[0].fd, PRINTER_FILE);
close(fd[0].fd);
return(-1);
} else if (bytes_read > 0) {
/* Write data to standard OUTPUT (stdout). */
fwrite(buf, 1, bytes_read, stdout);
fflush(stdout);
}
} else if (bytes_read > 0) {
break;
}
}
/* Close the device file. */
close(fd[0].fd);
return 0;
}
static int
write_printer_data()
{
struct pollfd fd[1];
/* Open device file for printer gadget. */
fd[0].fd = open (PRINTER_FILE, O_RDWR);
if (fd[0].fd < 0) {
printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);
close(fd[0].fd);
return(-1);
}
fd[0].events = POLLOUT | POLLWRNORM;
while (1) {
int retval;
static char buf[BUF_SIZE];
/* Read data from standard INPUT (stdin). */
int bytes_read = fread(buf, 1, BUF_SIZE, stdin);
if (!bytes_read) {
break;
}
while (bytes_read) {
/* Wait for up to 1 second to sent data. */
retval = poll(fd, 1, 1000);
/* Write data to printer gadget driver. */
if (retval && (fd[0].revents & POLLWRNORM)) {
retval = write(fd[0].fd, buf, bytes_read);
if (retval < 0) {
printf("Error %d writing to %s\n",
fd[0].fd,
PRINTER_FILE);
close(fd[0].fd);
return(-1);
} else {
bytes_read -= retval;
}
}
}
}
/* Wait until the data has been sent. */
fsync(fd[0].fd);
/* Close the device file. */
close(fd[0].fd);
return 0;
}
static int
read_NB_printer_data()
{
int fd;
static char buf[BUF_SIZE];
int bytes_read;
/* Open device file for printer gadget. */
fd = open(PRINTER_FILE, O_RDWR|O_NONBLOCK);
if (fd < 0) {
printf("Error %d opening %s\n", fd, PRINTER_FILE);
close(fd);
return(-1);
}
while (1) {
/* Read data from printer gadget driver. */
bytes_read = read(fd, buf, BUF_SIZE);
if (bytes_read <= 0) {
break;
}
/* Write data to standard OUTPUT (stdout). */
fwrite(buf, 1, bytes_read, stdout);
fflush(stdout);
}
/* Close the device file. */
close(fd);
return 0;
}
static int
get_printer_status()
{
int retval;
int fd;
/* Open device file for printer gadget. */
fd = open(PRINTER_FILE, O_RDWR);
if (fd < 0) {
printf("Error %d opening %s\n", fd, PRINTER_FILE);
close(fd);
return(-1);
}
/* Make the IOCTL call. */
retval = ioctl(fd, GADGET_GET_PRINTER_STATUS);
if (retval < 0) {
fprintf(stderr, "ERROR: Failed to set printer status\n");
return(-1);
}
/* Close the device file. */
close(fd);
return(retval);
}
static int
set_printer_status(unsigned char buf, int clear_printer_status_bit)
{
int retval;
int fd;
retval = get_printer_status();
if (retval < 0) {
fprintf(stderr, "ERROR: Failed to get printer status\n");
return(-1);
}
/* Open device file for printer gadget. */
fd = open(PRINTER_FILE, O_RDWR);
if (fd < 0) {
printf("Error %d opening %s\n", fd, PRINTER_FILE);
close(fd);
return(-1);
}
if (clear_printer_status_bit) {
retval &= ~buf;
} else {
retval |= buf;
}
/* Make the IOCTL call. */
if (ioctl(fd, GADGET_SET_PRINTER_STATUS, (unsigned char)retval)) {
fprintf(stderr, "ERROR: Failed to set printer status\n");
return(-1);
}
/* Close the device file. */
close(fd);
return 0;
}
static int
display_printer_status()
{
char printer_status;
printer_status = get_printer_status();
if (printer_status < 0) {
fprintf(stderr, "ERROR: Failed to get printer status\n");
return(-1);
}
printf("Printer status is:\n");
if (printer_status & PRINTER_SELECTED) {
printf(" Printer is Selected\n");
} else {
printf(" Printer is NOT Selected\n");
}
if (printer_status & PRINTER_PAPER_EMPTY) {
printf(" Paper is Out\n");
} else {
printf(" Paper is Loaded\n");
}
if (printer_status & PRINTER_NOT_ERROR) {
printf(" Printer OK\n");
} else {
printf(" Printer ERROR\n");
}
return(0);
}
int
main(int argc, char *argv[])
{
int i; /* Looping var */
int retval = 0;
/* No Args */
if (argc == 1) {
usage(0);
exit(0);
}
for (i = 1; i < argc && !retval; i ++) {
if (argv[i][0] != '-') {
continue;
}
if (!strcmp(argv[i], "-get_status")) {
if (display_printer_status()) {
retval = 1;
}
} else if (!strcmp(argv[i], "-paper_loaded")) {
if (set_printer_status(PRINTER_PAPER_EMPTY, 1)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-paper_out")) {
if (set_printer_status(PRINTER_PAPER_EMPTY, 0)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-selected")) {
if (set_printer_status(PRINTER_SELECTED, 0)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-not_selected")) {
if (set_printer_status(PRINTER_SELECTED, 1)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-error")) {
if (set_printer_status(PRINTER_NOT_ERROR, 1)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-no_error")) {
if (set_printer_status(PRINTER_NOT_ERROR, 0)) {
retval = 1;
}
} else if (!strcmp(argv[i], "-read_data")) {
if (read_printer_data()) {
retval = 1;
}
} else if (!strcmp(argv[i], "-write_data")) {
if (write_printer_data()) {
retval = 1;
}
} else if (!strcmp(argv[i], "-NB_read_data")) {
if (read_NB_printer_data()) {
retval = 1;
}
} else {
usage(argv[i]);
retval = 1;
}
}
exit(retval);
}
@vazhnov
Copy link

vazhnov commented Feb 1, 2023

Cool!

https://hackaday.com/2023/01/31/steamdeck-become-printer/

Could you please add a license information (I think it is GPL v2) and convert it into a "normal" repository?

@cheater
Copy link

cheater commented Feb 4, 2023

Hey Parker, if you've figured out how to make the Deck show up as a printer in whatever it's connected to... maybe you could do the following as well?

  1. Show up as an Ethernet port: i've lived in 3 places now where my SD can't connect to wifi, but my desktop or laptop can. So I need to hard wire it (hassle) or use a wifi hotspot (often doesn't work on Windows if you are getting your internet from wifi). However, connecting a usbc port on the pc to the usb c port on the steam deck, maybe you could make the deck show up as an Ethernet port. It would be very fast - you could probably emulate a 10gbit port, although it would obviously be slower in practice. Most importantly it would be a simple way of doing things that doesn't cost much in hardware. To make a hotspot, you need an AP or a wifi dongle, that's $50-100. To make it run via ethernet, you need a dock for the steam deck that has ethernet (at least $40), an ethernet cable ($10), and an ethernet card for the computer ($30 for a good one), or possibly a switch ($30 as well). Meanwhile everyone has a usb c cable, so that's "free". It would also let you share stuff via SMB so you can upload files to the deck more easily. In my mind, the Steam Deck would show up as an Ethernet port on the pc it's connected to, and then on the Steam Deck, you would have another "virtual" ethernet port that connects to that. Essentially it would be as if both the steam deck and the other computer had an usb c ethernet dongle plugged in, and they were both connected together using a short 10cm ethernet cable. Just without the extra hardware - only a usb c cable required. This might be possible to accomplish via this: https://linuxlink.timesys.com/docs/wiki/engineering/HOWTO_Use_USB_Gadget_Ethernet
  2. show up as a storage device: simply enable browsing the file system of the steam deck, or at least pre-selected directory (eg the home dir or a subdir inside it). This would make managing files, uploading stuff, and editing config files soooo much easier. This would be extra good if the sd card were available as well. Maybe it could be accomplished via this. https://www.kernel.org/doc/Documentation/usb/mass-storage.rst
  3. Show up as a steam controller... this would be super cool.
  4. As a bonus: make 1 and 2 work both at once by using a composite

@parkerlreed
Copy link
Author

parkerlreed commented Feb 4, 2023

@cheater All three already exist... :D

Ethernet https://gist.github.com/dafta/0aadeba3aa8bcbbc8b92a233977571ed

Mass storage only supports file images but there's two things here

Normal mass storage gadget with a file backend. As dead simple as it gets

https://docs.kernel.org/usb/mass-storage.html

Exposing the filesystem over MTP (closer to what you want)

https://github.com/dafta/DeckMTP/tree/main/backend/src

Controller

https://github.com/Frederic98/SteamDeckGadget

Or the in progress Valve backed passthrough

https://gitlab.com/evlaV/usbhid-gadget-passthru

@cheater
Copy link

cheater commented Feb 4, 2023

Wow, insane. You're a much better sleuth than I am. Let me try it right now!!

@cheater
Copy link

cheater commented Feb 5, 2023

@parkerlreed note that the Ethernet gadget is for sharing the Deck's internet with other devices, not for letting the Deck use the internet that other devices have. I asked the OP of the link you provided about that... let's see if they can suggest something:

https://www.reddit.com/r/SteamDeck/comments/10rifa4/comment/j794fot/?context=3

@parkerlreed
Copy link
Author

parkerlreed commented Feb 5, 2023

All it does is create a link and start the DHCP server. If you remove the DHCP bits you could then plug it into another machine and on that machine tell it to share Internet to the new connection.

@cheater
Copy link

cheater commented Feb 5, 2023

@parkerlreed i didn't remove the dhcp bits, i ran ./usb-ether.sh start, and connected via a usb cable to my macbook running windows 10, however, no new network device showed up in the control panel. i think the dhcp would not change that, what do you think?

@cheater
Copy link

cheater commented Feb 5, 2023

while still connected to the laptop via a usb c cable, i ran ./usb-ether.sh stop and then ./usb-ether.sh start again. (all under sudo). to no avail, as no new network connections showed up in windows.

@cheater
Copy link

cheater commented Feb 5, 2023

When you said "remove the dhcp bits", did you mean those lines?

https://gist.github.com/dafta/0aadeba3aa8bcbbc8b92a233977571ed#file-usb-ether-sh-L53-L84

@cheater
Copy link

cheater commented Feb 5, 2023

i think something in the script might be failing. when i start it, it's silent (no output), but when I stop it, bash says:

./usb-ether.sh: line 173: echo: write error: No such device

as a quick reminder, this is line 173: echo "" > UDC

I don't really know why it's failing like this. It seems like UDC - whatever it is - doesn't seem to work and quickly goes down right after it's enabled using start.

@parkerlreed
Copy link
Author

parkerlreed commented Feb 5, 2023

i think something in the script might be failing. when i start it, it's silent (no output), but when I stop it, bash says:

./usb-ether.sh: line 173: echo: write error: No such device

as a quick reminder, this is line 173: echo "" > UDC

I don't really know why it's failing like this. It seems like UDC - whatever it is - doesn't seem to work and quickly goes down right after it's enabled using start.

Did you enable the dual role in BIOS as mentioned?

Also this can't be started while docked. It has to be either unplugged or plugged in already to another machine.

@cheater
Copy link

cheater commented Feb 5, 2023

That was one issue, but ultimately the network also had to be configured differently. We've managed to figure it out, though :) Here's the result:

https://gist.github.com/cheater/00279e5b3dea743e14cf7bcd57f6c7fa

@parkerlreed
Copy link
Author

Cool!

https://hackaday.com/2023/01/31/steamdeck-become-printer/

Could you please add a license information (I think it is GPL v2) and convert it into a "normal" repository?

You can convert gists?

I linked to the source of the C (and realistically it was just one line added so it exits after the chunk of data is sent)

I'm not sure of the license of the original as it's not noted in the text file

@vazhnov
Copy link

vazhnov commented Feb 5, 2023

@parkerlreed , you can use this https://stackoverflow.com/a/37144960/5287257 to convert your gist into a normal repo.
At least there will be better "Issues" tab instead of plain comments.

@vazhnov
Copy link

vazhnov commented Feb 5, 2023

About a license — I thought everything connected with the Linux kernel is GPL v2, but I can't find proof about the documentation examples.

@cheater
Copy link

cheater commented Feb 5, 2023

It looks like the RNDIS gadget isn't going to work for sharing internet from Windows after all - looks like Windows support is dodgy:

https://gist.github.com/dafta/0aadeba3aa8bcbbc8b92a233977571ed?permalink_comment_id=4460779#gistcomment-4460779

Maybe you + dafta can figure out a way to use the ethernet gadget?

@parkerlreed
Copy link
Author

@parkerlreed , you can use this https://stackoverflow.com/a/37144960/5287257 to convert your gist into a normal repo. At least there will be better "Issues" tab instead of plain comments.

https://github.com/parkerlreed/gadget-print

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment