gadget_printer.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. ===============================
  2. Linux USB Printer Gadget Driver
  3. ===============================
  4. 06/04/2007
  5. Copyright (C) 2007 Craig W. Nadler <[email protected]>
  6. General
  7. =======
  8. This driver may be used if you are writing printer firmware using Linux as
  9. the embedded OS. This driver has nothing to do with using a printer with
  10. your Linux host system.
  11. You will need a USB device controller and a Linux driver for it that accepts
  12. a gadget / "device class" driver using the Linux USB Gadget API. After the
  13. USB device controller driver is loaded then load the printer gadget driver.
  14. This will present a printer interface to the USB Host that your USB Device
  15. port is connected to.
  16. This driver is structured for printer firmware that runs in user mode. The
  17. user mode printer firmware will read and write data from the kernel mode
  18. printer gadget driver using a device file. The printer returns a printer status
  19. byte when the USB HOST sends a device request to get the printer status. The
  20. user space firmware can read or write this status byte using a device file
  21. /dev/g_printer . Both blocking and non-blocking read/write calls are supported.
  22. Howto Use This Driver
  23. =====================
  24. To load the USB device controller driver and the printer gadget driver. The
  25. following example uses the Netchip 2280 USB device controller driver::
  26. modprobe net2280
  27. modprobe g_printer
  28. The follow command line parameter can be used when loading the printer gadget
  29. (ex: modprobe g_printer idVendor=0x0525 idProduct=0xa4a8 ):
  30. idVendor
  31. This is the Vendor ID used in the device descriptor. The default is
  32. the Netchip vendor id 0x0525. YOU MUST CHANGE TO YOUR OWN VENDOR ID
  33. BEFORE RELEASING A PRODUCT. If you plan to release a product and don't
  34. already have a Vendor ID please see www.usb.org for details on how to
  35. get one.
  36. idProduct
  37. This is the Product ID used in the device descriptor. The default
  38. is 0xa4a8, you should change this to an ID that's not used by any of
  39. your other USB products if you have any. It would be a good idea to
  40. start numbering your products starting with say 0x0001.
  41. bcdDevice
  42. This is the version number of your product. It would be a good idea
  43. to put your firmware version here.
  44. iManufacturer
  45. A string containing the name of the Vendor.
  46. iProduct
  47. A string containing the Product Name.
  48. iSerialNum
  49. A string containing the Serial Number. This should be changed for
  50. each unit of your product.
  51. iPNPstring
  52. The PNP ID string used for this printer. You will want to set
  53. either on the command line or hard code the PNP ID string used for
  54. your printer product.
  55. qlen
  56. The number of 8k buffers to use per endpoint. The default is 10, you
  57. should tune this for your product. You may also want to tune the
  58. size of each buffer for your product.
  59. Using The Example Code
  60. ======================
  61. This example code talks to stdout, instead of a print engine.
  62. To compile the test code below:
  63. 1) save it to a file called prn_example.c
  64. 2) compile the code with the follow command::
  65. gcc prn_example.c -o prn_example
  66. To read printer data from the host to stdout::
  67. # prn_example -read_data
  68. To write printer data from a file (data_file) to the host::
  69. # cat data_file | prn_example -write_data
  70. To get the current printer status for the gadget driver:::
  71. # prn_example -get_status
  72. Printer status is:
  73. Printer is NOT Selected
  74. Paper is Out
  75. Printer OK
  76. To set printer to Selected/On-line::
  77. # prn_example -selected
  78. To set printer to Not Selected/Off-line::
  79. # prn_example -not_selected
  80. To set paper status to paper out::
  81. # prn_example -paper_out
  82. To set paper status to paper loaded::
  83. # prn_example -paper_loaded
  84. To set error status to printer OK::
  85. # prn_example -no_error
  86. To set error status to ERROR::
  87. # prn_example -error
  88. Example Code
  89. ============
  90. ::
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <fcntl.h>
  94. #include <linux/poll.h>
  95. #include <sys/ioctl.h>
  96. #include <linux/usb/g_printer.h>
  97. #define PRINTER_FILE "/dev/g_printer"
  98. #define BUF_SIZE 512
  99. /*
  100. * 'usage()' - Show program usage.
  101. */
  102. static void
  103. usage(const char *option) /* I - Option string or NULL */
  104. {
  105. if (option) {
  106. fprintf(stderr,"prn_example: Unknown option \"%s\"!\n",
  107. option);
  108. }
  109. fputs("\n", stderr);
  110. fputs("Usage: prn_example -[options]\n", stderr);
  111. fputs("Options:\n", stderr);
  112. fputs("\n", stderr);
  113. fputs("-get_status Get the current printer status.\n", stderr);
  114. fputs("-selected Set the selected status to selected.\n", stderr);
  115. fputs("-not_selected Set the selected status to NOT selected.\n",
  116. stderr);
  117. fputs("-error Set the error status to error.\n", stderr);
  118. fputs("-no_error Set the error status to NO error.\n", stderr);
  119. fputs("-paper_out Set the paper status to paper out.\n", stderr);
  120. fputs("-paper_loaded Set the paper status to paper loaded.\n",
  121. stderr);
  122. fputs("-read_data Read printer data from driver.\n", stderr);
  123. fputs("-write_data Write printer sata to driver.\n", stderr);
  124. fputs("-NB_read_data (Non-Blocking) Read printer data from driver.\n",
  125. stderr);
  126. fputs("\n\n", stderr);
  127. exit(1);
  128. }
  129. static int
  130. read_printer_data()
  131. {
  132. struct pollfd fd[1];
  133. /* Open device file for printer gadget. */
  134. fd[0].fd = open(PRINTER_FILE, O_RDWR);
  135. if (fd[0].fd < 0) {
  136. printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);
  137. close(fd[0].fd);
  138. return(-1);
  139. }
  140. fd[0].events = POLLIN | POLLRDNORM;
  141. while (1) {
  142. static char buf[BUF_SIZE];
  143. int bytes_read;
  144. int retval;
  145. /* Wait for up to 1 second for data. */
  146. retval = poll(fd, 1, 1000);
  147. if (retval && (fd[0].revents & POLLRDNORM)) {
  148. /* Read data from printer gadget driver. */
  149. bytes_read = read(fd[0].fd, buf, BUF_SIZE);
  150. if (bytes_read < 0) {
  151. printf("Error %d reading from %s\n",
  152. fd[0].fd, PRINTER_FILE);
  153. close(fd[0].fd);
  154. return(-1);
  155. } else if (bytes_read > 0) {
  156. /* Write data to standard OUTPUT (stdout). */
  157. fwrite(buf, 1, bytes_read, stdout);
  158. fflush(stdout);
  159. }
  160. }
  161. }
  162. /* Close the device file. */
  163. close(fd[0].fd);
  164. return 0;
  165. }
  166. static int
  167. write_printer_data()
  168. {
  169. struct pollfd fd[1];
  170. /* Open device file for printer gadget. */
  171. fd[0].fd = open (PRINTER_FILE, O_RDWR);
  172. if (fd[0].fd < 0) {
  173. printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);
  174. close(fd[0].fd);
  175. return(-1);
  176. }
  177. fd[0].events = POLLOUT | POLLWRNORM;
  178. while (1) {
  179. int retval;
  180. static char buf[BUF_SIZE];
  181. /* Read data from standard INPUT (stdin). */
  182. int bytes_read = fread(buf, 1, BUF_SIZE, stdin);
  183. if (!bytes_read) {
  184. break;
  185. }
  186. while (bytes_read) {
  187. /* Wait for up to 1 second to sent data. */
  188. retval = poll(fd, 1, 1000);
  189. /* Write data to printer gadget driver. */
  190. if (retval && (fd[0].revents & POLLWRNORM)) {
  191. retval = write(fd[0].fd, buf, bytes_read);
  192. if (retval < 0) {
  193. printf("Error %d writing to %s\n",
  194. fd[0].fd,
  195. PRINTER_FILE);
  196. close(fd[0].fd);
  197. return(-1);
  198. } else {
  199. bytes_read -= retval;
  200. }
  201. }
  202. }
  203. }
  204. /* Wait until the data has been sent. */
  205. fsync(fd[0].fd);
  206. /* Close the device file. */
  207. close(fd[0].fd);
  208. return 0;
  209. }
  210. static int
  211. read_NB_printer_data()
  212. {
  213. int fd;
  214. static char buf[BUF_SIZE];
  215. int bytes_read;
  216. /* Open device file for printer gadget. */
  217. fd = open(PRINTER_FILE, O_RDWR|O_NONBLOCK);
  218. if (fd < 0) {
  219. printf("Error %d opening %s\n", fd, PRINTER_FILE);
  220. close(fd);
  221. return(-1);
  222. }
  223. while (1) {
  224. /* Read data from printer gadget driver. */
  225. bytes_read = read(fd, buf, BUF_SIZE);
  226. if (bytes_read <= 0) {
  227. break;
  228. }
  229. /* Write data to standard OUTPUT (stdout). */
  230. fwrite(buf, 1, bytes_read, stdout);
  231. fflush(stdout);
  232. }
  233. /* Close the device file. */
  234. close(fd);
  235. return 0;
  236. }
  237. static int
  238. get_printer_status()
  239. {
  240. int retval;
  241. int fd;
  242. /* Open device file for printer gadget. */
  243. fd = open(PRINTER_FILE, O_RDWR);
  244. if (fd < 0) {
  245. printf("Error %d opening %s\n", fd, PRINTER_FILE);
  246. close(fd);
  247. return(-1);
  248. }
  249. /* Make the IOCTL call. */
  250. retval = ioctl(fd, GADGET_GET_PRINTER_STATUS);
  251. if (retval < 0) {
  252. fprintf(stderr, "ERROR: Failed to set printer status\n");
  253. return(-1);
  254. }
  255. /* Close the device file. */
  256. close(fd);
  257. return(retval);
  258. }
  259. static int
  260. set_printer_status(unsigned char buf, int clear_printer_status_bit)
  261. {
  262. int retval;
  263. int fd;
  264. retval = get_printer_status();
  265. if (retval < 0) {
  266. fprintf(stderr, "ERROR: Failed to get printer status\n");
  267. return(-1);
  268. }
  269. /* Open device file for printer gadget. */
  270. fd = open(PRINTER_FILE, O_RDWR);
  271. if (fd < 0) {
  272. printf("Error %d opening %s\n", fd, PRINTER_FILE);
  273. close(fd);
  274. return(-1);
  275. }
  276. if (clear_printer_status_bit) {
  277. retval &= ~buf;
  278. } else {
  279. retval |= buf;
  280. }
  281. /* Make the IOCTL call. */
  282. if (ioctl(fd, GADGET_SET_PRINTER_STATUS, (unsigned char)retval)) {
  283. fprintf(stderr, "ERROR: Failed to set printer status\n");
  284. return(-1);
  285. }
  286. /* Close the device file. */
  287. close(fd);
  288. return 0;
  289. }
  290. static int
  291. display_printer_status()
  292. {
  293. char printer_status;
  294. printer_status = get_printer_status();
  295. if (printer_status < 0) {
  296. fprintf(stderr, "ERROR: Failed to get printer status\n");
  297. return(-1);
  298. }
  299. printf("Printer status is:\n");
  300. if (printer_status & PRINTER_SELECTED) {
  301. printf(" Printer is Selected\n");
  302. } else {
  303. printf(" Printer is NOT Selected\n");
  304. }
  305. if (printer_status & PRINTER_PAPER_EMPTY) {
  306. printf(" Paper is Out\n");
  307. } else {
  308. printf(" Paper is Loaded\n");
  309. }
  310. if (printer_status & PRINTER_NOT_ERROR) {
  311. printf(" Printer OK\n");
  312. } else {
  313. printf(" Printer ERROR\n");
  314. }
  315. return(0);
  316. }
  317. int
  318. main(int argc, char *argv[])
  319. {
  320. int i; /* Looping var */
  321. int retval = 0;
  322. /* No Args */
  323. if (argc == 1) {
  324. usage(0);
  325. exit(0);
  326. }
  327. for (i = 1; i < argc && !retval; i ++) {
  328. if (argv[i][0] != '-') {
  329. continue;
  330. }
  331. if (!strcmp(argv[i], "-get_status")) {
  332. if (display_printer_status()) {
  333. retval = 1;
  334. }
  335. } else if (!strcmp(argv[i], "-paper_loaded")) {
  336. if (set_printer_status(PRINTER_PAPER_EMPTY, 1)) {
  337. retval = 1;
  338. }
  339. } else if (!strcmp(argv[i], "-paper_out")) {
  340. if (set_printer_status(PRINTER_PAPER_EMPTY, 0)) {
  341. retval = 1;
  342. }
  343. } else if (!strcmp(argv[i], "-selected")) {
  344. if (set_printer_status(PRINTER_SELECTED, 0)) {
  345. retval = 1;
  346. }
  347. } else if (!strcmp(argv[i], "-not_selected")) {
  348. if (set_printer_status(PRINTER_SELECTED, 1)) {
  349. retval = 1;
  350. }
  351. } else if (!strcmp(argv[i], "-error")) {
  352. if (set_printer_status(PRINTER_NOT_ERROR, 1)) {
  353. retval = 1;
  354. }
  355. } else if (!strcmp(argv[i], "-no_error")) {
  356. if (set_printer_status(PRINTER_NOT_ERROR, 0)) {
  357. retval = 1;
  358. }
  359. } else if (!strcmp(argv[i], "-read_data")) {
  360. if (read_printer_data()) {
  361. retval = 1;
  362. }
  363. } else if (!strcmp(argv[i], "-write_data")) {
  364. if (write_printer_data()) {
  365. retval = 1;
  366. }
  367. } else if (!strcmp(argv[i], "-NB_read_data")) {
  368. if (read_NB_printer_data()) {
  369. retval = 1;
  370. }
  371. } else {
  372. usage(argv[i]);
  373. retval = 1;
  374. }
  375. }
  376. exit(retval);
  377. }