gadget_printer.txt 11 KB

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