€•oœŒsphinx.addnodes”Œdocument”“”)”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)”}”(hhh]”(hŒ pending_xref”“”)”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…””}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ&/translations/zh_CN/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)”}”(hhh]”hŒChinese (Traditional)”…””}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/zh_TW/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒItalian”…””}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/it_IT/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒJapanese”…””}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/ja_JP/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒKorean”…””}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/ko_KR/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)”}”(hhh]”hŒSpanish”…””}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ&/translations/sp_SP/usb/gadget_printer”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)”}”(hhh]”(hŒtitle”“”)”}”(hŒLinux USB Printer Gadget Driver”h]”hŒLinux USB Printer Gadget Driver”…””}”(hh¨hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hh£hžhhŸŒ@/var/lib/git/docbuild/linux/Documentation/usb/gadget_printer.rst”h KubhŒ paragraph”“”)”}”(hŒ 06/04/2007”h]”hŒ 06/04/2007”…””}”(hh¹hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h Khh£hžhubh¸)”}”(hŒ4Copyright (C) 2007 Craig W. Nadler ”h]”(hŒ$Copyright (C) 2007 Craig W. Nadler <”…””}”(hhÇhžhhŸNh NubhŒ reference”“”)”}”(hŒcraig@nadler.us”h]”hŒcraig@nadler.us”…””}”(hhÑhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”Œrefuri”Œmailto:craig@nadler.us”uh1hÏhhÇubhŒ>”…””}”(hhÇhžhhŸNh Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h Khh£hžhubh¢)”}”(hhh]”(h§)”}”(hŒGeneral”h]”hŒGeneral”…””}”(hhîhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hhëhžhhŸh¶h K ubh¸)”}”(hŒ«This driver may be used if you are writing printer firmware using Linux as the embedded OS. This driver has nothing to do with using a printer with your Linux host system.”h]”hŒ«This driver may be used if you are writing printer firmware using Linux as the embedded OS. This driver has nothing to do with using a printer with your Linux host system.”…””}”(hhühžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h Khhëhžhubh¸)”}”(hXDYou will need a USB device controller and a Linux driver for it that accepts a gadget / "device class" driver using the Linux USB Gadget API. After the USB device controller driver is loaded then load the printer gadget driver. This will present a printer interface to the USB Host that your USB Device port is connected to.”h]”hXHYou will need a USB device controller and a Linux driver for it that accepts a gadget / “device class†driver using the Linux USB Gadget API. After the USB device controller driver is loaded then load the printer gadget driver. This will present a printer interface to the USB Host that your USB Device port is connected to.”…””}”(hj hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h Khhëhžhubh¸)”}”(hXÌThis driver is structured for printer firmware that runs in user mode. The user mode printer firmware will read and write data from the kernel mode printer gadget driver using a device file. The printer returns a printer status byte when the USB HOST sends a device request to get the printer status. The user space firmware can read or write this status byte using a device file /dev/g_printer . Both blocking and non-blocking read/write calls are supported.”h]”hXÌThis driver is structured for printer firmware that runs in user mode. The user mode printer firmware will read and write data from the kernel mode printer gadget driver using a device file. The printer returns a printer status byte when the USB HOST sends a device request to get the printer status. The user space firmware can read or write this status byte using a device file /dev/g_printer . Both blocking and non-blocking read/write calls are supported.”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h Khhëhžhubeh}”(h]”Œgeneral”ah ]”h"]”Œgeneral”ah$]”h&]”uh1h¡hh£hžhhŸh¶h K ubh¢)”}”(hhh]”(h§)”}”(hŒHowto Use This Driver”h]”hŒHowto Use This Driver”…””}”(hj1hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hj.hžhhŸh¶h K#ubh¸)”}”(hŒ’To load the USB device controller driver and the printer gadget driver. The following example uses the Netchip 2280 USB device controller driver::”h]”hŒ‘To load the USB device controller driver and the printer gadget driver. The following example uses the Netchip 2280 USB device controller driver:”…””}”(hj?hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K%hj.hžhubhŒ literal_block”“”)”}”(hŒ#modprobe net2280 modprobe g_printer”h]”hŒ#modprobe net2280 modprobe g_printer”…””}”hjOsbah}”(h]”h ]”h"]”h$]”h&]”Œ xml:space”Œpreserve”uh1jMhŸh¶h K(hj.hžhubh¸)”}”(hŒ‰The follow command line parameter can be used when loading the printer gadget (ex: modprobe g_printer idVendor=0x0525 idProduct=0xa4a8 ):”h]”hŒ‰The follow command line parameter can be used when loading the printer gadget (ex: modprobe g_printer idVendor=0x0525 idProduct=0xa4a8 ):”…””}”(hj_hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K,hj.hžhubhŒdefinition_list”“”)”}”(hhh]”(hŒdefinition_list_item”“”)”}”(hX'idVendor This is the Vendor ID used in the device descriptor. The default is the Netchip vendor id 0x0525. YOU MUST CHANGE TO YOUR OWN VENDOR ID BEFORE RELEASING A PRODUCT. If you plan to release a product and don't already have a Vendor ID please see www.usb.org for details on how to get one. ”h]”(hŒterm”“”)”}”(hŒidVendor”h]”hŒidVendor”…””}”(hjzhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h K4hjtubhŒ definition”“”)”}”(hhh]”h¸)”}”(hXThis is the Vendor ID used in the device descriptor. The default is the Netchip vendor id 0x0525. YOU MUST CHANGE TO YOUR OWN VENDOR ID BEFORE RELEASING A PRODUCT. If you plan to release a product and don't already have a Vendor ID please see www.usb.org for details on how to get one.”h]”hXThis is the Vendor ID used in the device descriptor. The default is the Netchip vendor id 0x0525. YOU MUST CHANGE TO YOUR OWN VENDOR ID BEFORE RELEASING A PRODUCT. If you plan to release a product and don’t already have a Vendor ID please see www.usb.org for details on how to get one.”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K0hjŠubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjtubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h K4hjoubjs)”}”(hX idProduct This is the Product ID used in the device descriptor. The default is 0xa4a8, you should change this to an ID that's not used by any of your other USB products if you have any. It would be a good idea to start numbering your products starting with say 0x0001. ”h]”(jy)”}”(hŒ idProduct”h]”hŒ idProduct”…””}”(hj«hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h K:hj§ubj‰)”}”(hhh]”h¸)”}”(hXThis is the Product ID used in the device descriptor. The default is 0xa4a8, you should change this to an ID that's not used by any of your other USB products if you have any. It would be a good idea to start numbering your products starting with say 0x0001.”h]”hXThis is the Product ID used in the device descriptor. The default is 0xa4a8, you should change this to an ID that’s not used by any of your other USB products if you have any. It would be a good idea to start numbering your products starting with say 0x0001.”…””}”(hj¼hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K7hj¹ubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhj§ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h K:hjohžhubjs)”}”(hŒqbcdDevice This is the version number of your product. It would be a good idea to put your firmware version here. ”h]”(jy)”}”(hŒ bcdDevice”h]”hŒ bcdDevice”…””}”(hjÚhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h K>hjÖubj‰)”}”(hhh]”h¸)”}”(hŒfThis is the version number of your product. It would be a good idea to put your firmware version here.”h]”hŒfThis is the version number of your product. It would be a good idea to put your firmware version here.”…””}”(hjëhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K=hjèubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjÖubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h K>hjohžhubjs)”}”(hŒ:iManufacturer A string containing the name of the Vendor. ”h]”(jy)”}”(hŒ iManufacturer”h]”hŒ iManufacturer”…””}”(hj hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h KAhjubj‰)”}”(hhh]”h¸)”}”(hŒ+A string containing the name of the Vendor.”h]”hŒ+A string containing the name of the Vendor.”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KAhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h KAhjohžhubjs)”}”(hŒ/iProduct A string containing the Product Name. ”h]”(jy)”}”(hŒiProduct”h]”hŒiProduct”…””}”(hj8hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h KDhj4ubj‰)”}”(hhh]”h¸)”}”(hŒ%A string containing the Product Name.”h]”hŒ%A string containing the Product Name.”…””}”(hjIhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KDhjFubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhj4ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h KDhjohžhubjs)”}”(hŒhiSerialNum A string containing the Serial Number. This should be changed for each unit of your product. ”h]”(jy)”}”(hŒ iSerialNum”h]”hŒ iSerialNum”…””}”(hjghžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h KHhjcubj‰)”}”(hhh]”h¸)”}”(hŒ\A string containing the Serial Number. This should be changed for each unit of your product.”h]”hŒ\A string containing the Serial Number. This should be changed for each unit of your product.”…””}”(hjxhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KGhjuubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjcubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h KHhjohžhubjs)”}”(hŒ¢iPNPstring The PNP ID string used for this printer. You will want to set either on the command line or hard code the PNP ID string used for your printer product. ”h]”(jy)”}”(hŒ iPNPstring”h]”hŒ iPNPstring”…””}”(hj–hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h KMhj’ubj‰)”}”(hhh]”h¸)”}”(hŒ–The PNP ID string used for this printer. You will want to set either on the command line or hard code the PNP ID string used for your printer product.”h]”hŒ–The PNP ID string used for this printer. You will want to set either on the command line or hard code the PNP ID string used for your printer product.”…””}”(hj§hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KKhj¤ubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhj’ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h KMhjohžhubjs)”}”(hŒ´qlen The number of 8k buffers to use per endpoint. The default is 10, you should tune this for your product. You may also want to tune the size of each buffer for your product. ”h]”(jy)”}”(hŒqlen”h]”hŒqlen”…””}”(hjÅhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1jxhŸh¶h KUhjÁubj‰)”}”(hhh]”h¸)”}”(hŒ«The number of 8k buffers to use per endpoint. The default is 10, you should tune this for your product. You may also want to tune the size of each buffer for your product.”h]”hŒ«The number of 8k buffers to use per endpoint. The default is 10, you should tune this for your product. You may also want to tune the size of each buffer for your product.”…””}”(hjÖhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KPhjÓubah}”(h]”h ]”h"]”h$]”h&]”uh1jˆhjÁubeh}”(h]”h ]”h"]”h$]”h&]”uh1jrhŸh¶h KUhjohžhubeh}”(h]”h ]”h"]”h$]”h&]”uh1jmhj.hžhhŸh¶h Nubeh}”(h]”Œhowto-use-this-driver”ah ]”h"]”Œhowto use this driver”ah$]”h&]”uh1h¡hh£hžhhŸh¶h K#ubh¢)”}”(hhh]”(h§)”}”(hŒUsing The Example Code”h]”hŒUsing The Example Code”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hjþhžhhŸh¶h KXubh¸)”}”(hŒ=This example code talks to stdout, instead of a print engine.”h]”hŒ=This example code talks to stdout, instead of a print engine.”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KZhjþhžhubh¸)”}”(hŒTo compile the test code below:”h]”hŒTo compile the test code below:”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K\hjþhžhubhŒenumerated_list”“”)”}”(hhh]”(hŒ list_item”“”)”}”(hŒ&save it to a file called prn_example.c”h]”h¸)”}”(hj4h]”hŒ&save it to a file called prn_example.c”…””}”(hj6hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K^hj2ubah}”(h]”h ]”h"]”h$]”h&]”uh1j0hj-hžhhŸh¶h Nubj1)”}”(hŒUcompile the code with the follow command:: gcc prn_example.c -o prn_example ”h]”(h¸)”}”(hŒ*compile the code with the follow command::”h]”hŒ)compile the code with the follow command:”…””}”(hjMhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K_hjIubjN)”}”(hŒ gcc prn_example.c -o prn_example”h]”hŒ gcc prn_example.c -o prn_example”…””}”hj[sbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h KahjIubeh}”(h]”h ]”h"]”h$]”h&]”uh1j0hj-hžhhŸh¶h Nubeh}”(h]”h ]”h"]”h$]”h&]”Œenumtype”Œarabic”Œprefix”hŒsuffix”Œ)”uh1j+hjþhžhhŸh¶h K^ubh¸)”}”(hŒ.To read printer data from the host to stdout::”h]”hŒ-To read printer data from the host to stdout:”…””}”(hjzhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KehjþhžhubjN)”}”(hŒ# prn_example -read_data”h]”hŒ# prn_example -read_data”…””}”hjˆsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h Kghjþhžhubh¸)”}”(hŒ;To write printer data from a file (data_file) to the host::”h]”hŒ:To write printer data from a file (data_file) to the host:”…””}”(hj–hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KjhjþhžhubjN)”}”(hŒ)# cat data_file | prn_example -write_data”h]”hŒ)# cat data_file | prn_example -write_data”…””}”hj¤sbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h Klhjþhžhubh¸)”}”(hŒ:To get the current printer status for the gadget driver:::”h]”hŒ9To get the current printer status for the gadget driver::”…””}”(hj²hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KohjþhžhubjN)”}”(hŒl# prn_example -get_status Printer status is: Printer is NOT Selected Paper is Out Printer OK”h]”hŒl# prn_example -get_status Printer status is: Printer is NOT Selected Paper is Out Printer OK”…””}”hjÀsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h Kqhjþhžhubh¸)”}”(hŒ$To set printer to Selected/On-line::”h]”hŒ#To set printer to Selected/On-line:”…””}”(hjÎhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KyhjþhžhubjN)”}”(hŒ# prn_example -selected”h]”hŒ# prn_example -selected”…””}”hjÜsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h K{hjþhžhubh¸)”}”(hŒ)To set printer to Not Selected/Off-line::”h]”hŒ(To set printer to Not Selected/Off-line:”…””}”(hjêhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K~hjþhžhubjN)”}”(hŒ# prn_example -not_selected”h]”hŒ# prn_example -not_selected”…””}”hjøsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h K€hjþhžhubh¸)”}”(hŒ"To set paper status to paper out::”h]”hŒ!To set paper status to paper out:”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KƒhjþhžhubjN)”}”(hŒ# prn_example -paper_out”h]”hŒ# prn_example -paper_out”…””}”hjsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h K…hjþhžhubh¸)”}”(hŒ%To set paper status to paper loaded::”h]”hŒ$To set paper status to paper loaded:”…””}”(hj"hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KˆhjþhžhubjN)”}”(hŒ# prn_example -paper_loaded”h]”hŒ# prn_example -paper_loaded”…””}”hj0sbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h KŠhjþhžhubh¸)”}”(hŒ#To set error status to printer OK::”h]”hŒ"To set error status to printer OK:”…””}”(hj>hžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h KhjþhžhubjN)”}”(hŒ# prn_example -no_error”h]”hŒ# prn_example -no_error”…””}”hjLsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h Khjþhžhubh¸)”}”(hŒTo set error status to ERROR::”h]”hŒTo set error status to ERROR:”…””}”(hjZhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h·hŸh¶h K’hjþhžhubjN)”}”(hŒ# prn_example -error”h]”hŒ# prn_example -error”…””}”hjhsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h K”hjþhžhubeh}”(h]”Œusing-the-example-code”ah ]”h"]”Œusing the example code”ah$]”h&]”uh1h¡hh£hžhhŸh¶h KXubh¢)”}”(hhh]”(h§)”}”(hŒ Example Code”h]”hŒ Example Code”…””}”(hjhžhhŸNh Nubah}”(h]”h ]”h"]”h$]”h&]”uh1h¦hj~hžhhŸh¶h KšubjN)”}”(hXÁ'#include #include #include #include #include #include #define PRINTER_FILE "/dev/g_printer" #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); } } } /* 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); }”h]”hXÁ'#include #include #include #include #include #include #define PRINTER_FILE "/dev/g_printer" #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); } } } /* 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); }”…””}”hjsbah}”(h]”h ]”h"]”h$]”h&]”j]j^uh1jMhŸh¶h KŸhj~hžhubeh}”(h]”Œ example-code”ah ]”h"]”Œ example code”ah$]”h&]”uh1h¡hh£hžhhŸh¶h Kšubeh}”(h]”Œlinux-usb-printer-gadget-driver”ah ]”h"]”Œlinux usb printer gadget driver”ah$]”h&]”uh1h¡hhhžhhŸh¶h Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”h¶uh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)”}”(h¦NŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”Œentry”Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jÐŒerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”h¶Œ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(jªj§j+j(jûjøj{jxj¢jŸuŒ nametypes”}”(jª‰j+‰jû‰j{‰j¢‰uh}”(j§h£j(hëjøj.jxjþjŸj~uŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”Œtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nhžhub.