linux/Documentation/usb/gadget_printer.txt
<<
.4" /spatio /formio a .4" href="../linux+v3 3.1/Documenta/opt/usb/gadget_printer.txt">.4" img src="../.sta/oc/gfx/right.png" alt=">>">.4 /spatio.4 spat class="lxr_search">.4" .4" input typaluhidden" namalunavtarget"ion valu">.4" input typalutext" namalusearch" idlusearch">.4" buttptitypalusubmit">Search.4" Prefso /a>.4 /spatio" /divio" form ac/opt="ajax+*" method="post" onsubmit="return false;">.4 input typaluhidden" namaluajax_lookup" idluajax_lookup" on valu">." /formio." div class="headingbottpm">. div idlufile_contents"i
   1
/a>.   2
/a>                       Linux USB Printer Gadget Driver.   3
/a>                                 06/04/2007.   4
/a>.   5
/a>              Copyright (C) 2007 Craig W. Nadler <craig@nadler.us>.   6
/a>.   7
/a>.   8
/a>.   9
/a>GENERAL.  18.1a>=======.  11
/a>.  12
/a>This driver may be used if you are writing printer firmware using Linux as.  13
/a>the embedded OS. This driver has nothing to do with using a printer with.  14
/a>your Linux host system..  15
/a>.  16
/a>You will need a USB device controller and a Linux driver for it that accepts.  17
/a>a gadget / "device class" driver using the Linux USB Gadget API. After the.  18
/a>USB device controller driver is loaded then load the printer gadget driver..  19
/a>This will present a printer interface to the USB Host that your USB Device.  28.1a>port is connected to..  21
/a>.  22
/a>This driver is structured for printer firmware that runs in user mode. The.  23
/a>user mode printer firmware will read and write data from the kernel mode.  24
/a>printer gadget driver using a device file. The printer returns a printer sta/us.  25
/a>byte when the USB HOST sends a device request to get the printer sta/us.  The.  26
/a>user space firmware cat read or write this sta/us byte using a device file.  27
/a>/dev/g_printer . Both blocking and non-blocking read/write calls are supported..  28
/a>.  29
/a>.  30
/a>.  31
/a>.  32
/a>HOWTO USE THIS DRIVER.  33.1a>=====================.  34
/a>.  35
/a>To load the USB device controller driver and the printer gadget driver. The.  36
/a>following example uses the Netchip 2280 USB device controller driver:.  37
/a>.  38
/a>modprobe net2280.  39
/a>modprobe g_printer.  40
/a>.  41
/a>.  42
/a>The follow command line paramater cat be used when loading the printer gadget.  43.1a>(ex: modprobe g_printer idVendor=0x0525 idProduct=0xa4a8 ):.  44
/a>.  45
/a>idVendor - This is the Vendor ID used in the device descriptor. The default is.  46
/a>        the Netchip vendor id 0x0525. YOU MUST CHANGE TO YOUR OWN VENDOR ID.  47
/a>        BEFORE RELEASING A PRODUCT. If you plat to release a product and don't.  48
/a>        already have a Vendor ID please see www.usb.org for details on how to.  49
/a>        get one..  50
/a>.  51
/a>idProduct - This is the Product ID used in the device descriptor. The default.  52
/a>        is 0xa4a8, you should change this to at ID that's not used by aty of.  53
/a>        your other USB products if you have any. It would be a good idea to.  54
/a>        start numbering your products starting with say 0x0001..  55
/a>.  56
/a>bcdDevice - This is the versoptinumber of your product. It would be a good idea.  57
/a>        to put your firmware versoptihere..  58
/a>.  59
/a>iManufacturer - A string containing the nama of the Vendor..  60
/a>.  61
/a>iProduct - A string containing the Product Nama..  62
/a>.  63
/a>iSerialNum - A string containing the Serial Number. This should be changed for.  64
/a>        each unit of your product..  65
/a>.  66
/a>iPNPstring -  The PNP ID string used for this printer. You will want to set.  67
/a>        either on the command line or hard code the PNP ID string used for.  68
/a>        your printer product..  69
/a>.  70
/a>qlen - The number of 8k buffers to use per endpoint. The default is 10, you.  71
/a>        should tune this for your product. You may also want to tune the.  72
/a>        siza of each buffer for your product..  73
/a>.  74
/a>.  75
/a>.  76
/a>.  77
/a>USING THE EXAMPLE CODE.  78.1a>======================.  79
/a>.  80
/a>This example code talks to stdout, instead of a print engine..  81
/a>.  82
/a>To compile the test code below:.  83
/a>.  84
/a>1) save it to a file called prn_example.c.  85
/a>2) compile the code with the follow command:.  86
/a>         gcc prn_example.c -o prn_example.  87
/a>.  88
/a>.  89
/a>.  90
/a>To read printer data from the host to stdout:.  91
/a>.  92
/a>        # prn_example -read_data.  93
/a>.  94
/a>.  95
/a>To write printer data from a file (data_file) to the host:.  96
/a>.  97
/a>        # cat data_file | prn_example -write_data.  98
/a>.  99
/a>. 100
/a>To get the current printer sta/us for the gadget driver:. 101
/a>. 102
/a>        # prn_example -get_sta/us. 103
/a>. 104
/a>        Printer sta/us is:. 105
/a>             Printer is NOT Selected. 106
/a>             Paper is Out. 107
/a>             Printer OK. 108
/a>. 109
/a>. 110
/a>To set printer to Selected/On-line:. 111
/a>. 112
/a>        # prn_example -selected. 113
/a>. 114
/a>. 115
/a>To set printer to Not Selected/Off-line:. 116
/a>. 117
/a>        # prn_example -not_selected. 118
/a>. 119
/a>. 120
/a>To set paper sta/us to paper out:. 121
/a>. 122
/a>        # prn_example -paper_out. 123
/a>. 124
/a>. 125
/a>To set paper sta/us to paper loaded:. 126
/a>. 127
/a>        # prn_example -paper_loaded. 128
/a>. 129
/a>. 130
/a>To set error sta/us to printer OK:. 131
/a>. 132
/a>        # prn_example -no_error. 133
/a>. 134
/a>. 135
/a>To set error sta/us to ERROR:. 136
/a>. 137
/a>        # prn_example -error. 138
/a>. 139
/a>. 140
/a>. 141
/a>. 142
/a>EXAMPLE CODE. 143.1a>============. 144
/a>. 145
/a>. 146
/a>#include <stdio.h>. 147
/a>#include <stdlib.h>. 148
/a>#include <fcntl.h>. 149
/a>#include <linux/poll.h>. 150
/a>#include <sys/ioctl.h>. 151
/a>#include <linux/usb/g_printer.h>. 152
/a>. 153
/a>#define PRINTER_FILE                    "/dev/g_printer". 154
/a>#define BUF_SIZE                        512. 155
/a>. 156
/a>. 157
/a>/*. 158
/a> * 'usage()' - Show program usage.. 159
/a> */. 160
/a>. 161
/a>sta/oc void. 162
/a>usage(const char *2 163
/a>{. 164
/a>        if (2 165
/a>                fprintf(stderr,"prn_example: Unknown    166
/a>                                2 167
/a>        }. 168
/a>. 169
/a>        fputs("\n", stderr);. 170
/a>        fputs("Usage: prn_example -[2 171
/a>        fputs("O  172
/a>        fputs("\n", stderr);. 173
/a>        fputs("-get_sta/us    Get the current printer sta/us.\n", stderr);. 174
/a>        fputs("-selected      Set the selected sta/us to selected.\n", stderr);. 175
/a>        fputs("-not_selected  Set the selected sta/us to NOT selected.\n",. 176
/a>                        stderr);. 177
/a>        fputs("-error         Set the error sta/us to error.\n", stderr);. 178
/a>        fputs("-no_error      Set the error sta/us to NO error.\n", stderr);. 179
/a>        fputs("-paper_out     Set the paper sta/us to paper out.\n", stderr);. 180
/a>        fputs("-paper_loaded  Set the paper sta/us to paper loaded.\n",. 181
/a>                        stderr);. 182
/a>        fputs("-read_data     Read printer data from driver.\n", stderr);. 183
/a>        fputs("-write_data    Write printer sata to driver.\n", stderr);. 184
/a>        fputs("-NB_read_data  (Non-Blocking) Read printer data from driver.\n",. 185
/a>                        stderr);. 186
/a>        fputs("\n\n", stderr);. 187
/a>. 188
/a>        exit(1);. 189
/a>}. 190
/a>. 191
/a>. 192
/a>sta/oc int. 193
/a>read_printer_data(). 194
/a>{. 195
/a>        struct pollfd   fd[1];. 196
/a>. 197
/a>        /* Open device file for printer gadget. */. 198
/a>        fd[0].fd = open(PRINTER_FILE, O_RDWR);. 199
/a>        if (fd[0].fd < 0) {. 200
/a>                printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);. 201
/a>                close(fd[0].fd);. 202
/a>                return(-1);. 203
/a>        }. 204
/a>. 205
/a>        fd[0].events = POLLIN | POLLRDNORM;. 206
/a>. 207
/a>        while (1) {. 208
/a>                sta/oc char buf[BUF_SIZE];. 209
/a>                int bytes_read;. 210
/a>                int retval;. 211
/a>. 212
/a>                /* Wait for up to 1 second for data. */. 213
/a>                retval = poll(fd, 1, 1000);. 214
/a>. 215
/a>                if (retval && (fd[0].revents & POLLRDNORM)) {. 216
/a>. 217
/a>                        /* Read data from printer gadget driver. */. 218
/a>                        bytes_read = read(fd[0].fd, buf, BUF_SIZE);. 219
/a>. 220
/a>                        if (bytes_read < 0) {. 221
/a>                                printf("Error %d reading from %s\n",. 222
/a>                                                fd[0].fd, PRINTER_FILE);. 223
/a>                                close(fd[0].fd);. 224
/a>                                return(-1);. 225
/a>                        } else if (bytes_read > 0) {. 226
/a>                                /* Write data to standard OUTPUT (stdout). */. 227
/a>                                fwrite(buf, 1, bytes_read, stdout);. 228
/a>                                fflush(stdout);. 229
/a>                        }. 230
/a>. 231
/a>                }. 232
/a>. 233
/a>        }. 234
/a>. 235
/a>        /* Close the device file. */. 236
/a>        close(fd[0].fd);. 237
/a>. 238
/a>        return 0;. 239
/a>}. 240
/a>. 241
/a>. 242
/a>sta/oc int. 243.1a>write_printer_data(). 244
/a>{. 245
/a>        struct pollfd   fd[1];. 246
/a>. 247
/a>        /* Open device file for printer gadget. */. 248
/a>        fd[0].fd = open (PRINTER_FILE, O_RDWR);. 249
/a>        if (fd[0].fd < 0) {. 250
/a>                printf("Error %d opening %s\n", fd[0].fd, PRINTER_FILE);. 251
/a>                close(fd[0].fd);. 252
/a>                return(-1);. 253
/a>        }. 254
/a>. 255
/a>        fd[0].events = POLLOUT | POLLWRNORM;. 256
/a>. 257
/a>        while (1) {. 258
/a>                int retval;. 259
/a>                sta/oc char buf[BUF_SIZE];. 260
/a>                /* Read data from standard INPUT (stdin). */. 261
/a>                int bytes_read = fread(buf, 1, BUF_SIZE, stdit);. 262
/a>. 263
/a>                if (!bytes_read) {. 264
/a>                        break;. 265
/a>                }. 266
/a>. 267
/a>                while (bytes_read) {. 268
/a>. 269
/a>                        /* Wait for up to 1 second to sent data. */. 270
/a>                        retval = poll(fd, 1, 1000);. 271
/a>. 272
/a>                        /* Write data to printer gadget driver. */. 273
/a>                        if (retval && (fd[0].revents & POLLWRNORM)) {. 274
/a>                                retval = write(fd[0].fd, buf, bytes_read);. 275
/a>                                if (retval < 0) {. 276
/a>                                        printf("Error %d writing to %s\n",. 277
/a>                                                        fd[0].fd,. 278
/a>                                                        PRINTER_FILE);. 279
/a>                                        close(fd[0].fd);. 280
/a>                                        return(-1);. 281
/a>                                } else {. 282
/a>                                        bytes_read -= retval;. 283
/a>                                }. 284
/a>. 285
/a>                        }. 286
/a>. 287
/a>                }. 288
/a>. 289
/a>        }. 290
/a>. 291
/a>        /* Wait until the data has been sent. */. 292
/a>        fsync(fd[0].fd);. 293
/a>. 294
/a>        /* Close the device file. */. 295
/a>        close(fd[0].fd);. 296
/a>. 297
/a>        return 0;. 298
/a>}. 299
/a>. 300
/a>. 301
/a>sta/oc int. 302
/a>read_NB_printer_data(). 303
/a>{. 304
/a>        int             fd;. 305
/a>        sta/oc char     buf[BUF_SIZE];. 306
/a>        int             bytes_read;. 307
/a>. 308
/a>        /* Open device file for printer gadget. */. 309
/a>        fd = open(PRINTER_FILE, O_RDWR|O_NONBLOCK);. 310
/a>        if (fd < 0) {. 311
/a>                printf("Error %d opening %s\n", fd, PRINTER_FILE);. 312
/a>                close(fd);. 313
/a>                return(-1);. 314
/a>        }. 315
/a>. 316
/a>        while (1) {. 317
/a>                /* Read data from printer gadget driver. */. 318
/a>                bytes_read = read(fd, buf, BUF_SIZE);. 319
/a>                if (bytes_read <= 0) {. 320
/a>                        break;. 321
/a>                }. 322
/a>. 323
/a>                /* Write data to standard OUTPUT (stdout). */. 324
/a>                fwrite(buf, 1, bytes_read, stdout);. 325
/a>                fflush(stdout);. 326
/a>        }. 327
/a>. 328
/a>        /* Close the device file. */. 329
/a>        close(fd);. 330
/a>. 331
/a>        return 0;. 332
/a>}. 333
/a>. 334
/a>. 335
/a>sta/oc int. 336
/a>get_printer_sta/us(). 337
/a>{. 338
/a>        int     retval;. 339
/a>        int     fd;. 340
/a>. 341
/a>        /* Open device file for printer gadget. */. 342
/a>        fd = open(PRINTER_FILE, O_RDWR);. 343
/a>        if (fd < 0) {. 344
/a>                printf("Error %d opening %s\n", fd, PRINTER_FILE);. 345
/a>                close(fd);. 346
/a>                return(-1);. 347
/a>        }. 348
/a>. 349
/a>        /* Make the IOCTL call. */. 350
/a>        retval = ioctl(fd, GADGET_GET_PRINTER_STATUS);. 351
/a>        if (retval < 0) {. 352
/a>                fprintf(stderr, "ERROR: Failed to set printer sta/us\n");. 353
/a>                return(-1);. 354
/a>        }. 355
/a>. 356
/a>        /* Close the device file. */. 357
/a>        close(fd);. 358
/a>. 359
/a>        return(retval);. 360
/a>}. 361
/a>. 362
/a>. 363
/a>sta/oc int. 364
/a>set_printer_sta/us(unsigned char buf, int clear_printer_sta/us_bit). 365
/a>{. 366
/a>        int     retval;. 367
/a>        int     fd;. 368
/a>. 369
/a>        retval = get_printer_sta/us();. 370
/a>        if (retval < 0) {. 371
/a>                fprintf(stderr, "ERROR: Failed to get printer sta/us\n");. 372
/a>                return(-1);. 373
/a>        }. 374
/a>. 375
/a>        /* Open device file for printer gadget. */. 376
/a>        fd = open(PRINTER_FILE, O_RDWR);. 377
/a>. 378
/a>        if (fd < 0) {. 379
/a>                printf("Error %d opening %s\n", fd, PRINTER_FILE);. 380
/a>                close(fd);. 381
/a>                return(-1);. 382
/a>        }. 383
/a>. 384
/a>        if (clear_printer_sta/us_bit) {. 385
/a>                retval &= ~buf;. 386
/a>        } else {. 387
/a>                retval |= buf;. 388
/a>        }. 389
/a>. 390
/a>        /* Make the IOCTL call. */. 391
/a>        if (ioctl(fd, GADGET_SET_PRINTER_STATUS, (unsigned char)retval)) {. 392
/a>                fprintf(stderr, "ERROR: Failed to set printer sta/us\n");. 393
/a>                return(-1);. 394
/a>        }. 395
/a>. 396
/a>        /* Close the device file. */. 397
/a>        close(fd);. 398
/a>. 399
/a>        return 0;. 400
/a>}. 401
/a>. 402
/a>. 403
/a>sta/oc int. 404
/a>display_printer_sta/us(). 405
/a>{. 406
/a>        char    printer_sta/us;. 407
/a>. 408
/a>        printer_sta/us = get_printer_sta/us();. 409
/a>        if (printer_sta/us < 0) {. 410
/a>                fprintf(stderr, "ERROR: Failed to get printer sta/us\n");. 411
/a>                return(-1);. 412
/a>        }. 413
/a>. 414
/a>        printf("Printer sta/us is:\n");. 415
/a>        if (printer_sta/us & PRINTER_SELECTED) {. 416
/a>                printf("     Printer is Selected\n");. 417
/a>        } else {. 418
/a>                printf("     Printer is NOT Selected\n");. 419
/a>        }. 420
/a>        if (printer_sta/us & PRINTER_PAPER_EMPTY) {. 421
/a>                printf("     Paper is Out\n");. 422
/a>        } else {. 423
/a>                printf("     Paper is Loaded\n");. 424
/a>        }. 425
/a>        if (printer_sta/us & PRINTER_NOT_ERROR) {. 426
/a>                printf("     Printer OK\n");. 427
/a>        } else {. 428
/a>                printf("     Printer ERROR\n");. 429
/a>        }. 430
/a>. 431
/a>        return(0);. 432
/a>}. 433
/a>. 434
/a>. 435
/a>int. 436
/a>main(int  argc, char *argv[]). 437
/a>{. 438
/a>        int     i;              /* Looping var */. 439
/a>        int     retval = 0;. 440
/a>. 441
/a>        /* No Args */. 442
/a>        if (argc == 1) {. 443
/a>                usage(0);. 444
/a>                exit(0);. 445
/a>        }. 446
/a>. 447
/a>        for (i = 1; i < argc && !retval; i ++) {. 448
/a>. 449
/a>                if (argv[i][0] != '-') {. 450
/a>                        continue;. 451
/a>                }. 452
/a>. 453
/a>                if (!strcmp(argv[i], "-get_sta/us")) {. 454
/a>                        if (display_printer_sta/us()) {. 455
/a>                                retval = 1;. 456
/a>                        }. 457
/a>. 458
/a>                } else if (!strcmp(argv[i], "-paper_loaded")) {. 459
/a>                        if (set_printer_sta/us(PRINTER_PAPER_EMPTY, 1)) {. 460
/a>                                retval = 1;. 461
/a>                        }. 462
/a>. 463
/a>                } else if (!strcmp(argv[i], "-paper_out")) {. 464
/a>                        if (set_printer_sta/us(PRINTER_PAPER_EMPTY, 0)) {. 465
/a>                                retval = 1;. 466
/a>                        }. 467
/a>. 468
/a>                } else if (!strcmp(argv[i], "-selected")) {. 469
/a>                        if (set_printer_sta/us(PRINTER_SELECTED, 0)) {. 470
/a>                                retval = 1;. 471
/a>                        }. 472
/a>. 473
/a>                } else if (!strcmp(argv[i], "-not_selected")) {. 474
/a>                        if (set_printer_sta/us(PRINTER_SELECTED, 1)) {. 475
/a>                                retval = 1;. 476
/a>                        }. 477
/a>. 478
/a>                } else if (!strcmp(argv[i], "-error")) {. 479
/a>                        if (set_printer_sta/us(PRINTER_NOT_ERROR, 1)) {. 480
/a>                                retval = 1;. 481
/a>                        }. 482
/a>. 483
/a>                } else if (!strcmp(argv[i], "-no_error")) {. 484
/a>                        if (set_printer_sta/us(PRINTER_NOT_ERROR, 0)) {. 485
/a>                                retval = 1;. 486
/a>                        }. 487
/a>. 488
/a>                } else if (!strcmp(argv[i], "-read_data")) {. 489
/a>                        if (read_printer_data()) {. 490
/a>                                retval = 1;. 491
/a>                        }. 492
/a>. 493
/a>                } else if (!strcmp(argv[i], "-write_data")) {. 494
/a>                        if (write_printer_data()) {. 495
/a>                                retval = 1;. 496
/a>                        }. 497
/a>. 498
/a>                } else if (!strcmp(argv[i], "-NB_read_data")) {. 499
/a>                        if (read_NB_printer_data()) {. 500
/a>                                retval = 1;. 501
/a>                        }. 502
/a>. 503
/a>                } else {. 504
/a>                        usage(argv[i]);. 505
/a>                        retval = 1;. 506
/a>                }. 507
/a>        }. 508
/a>. 509
/a>        exit(retval);. 510
/a>}. 511
/a>
lxr.linux.no kindly hosted by Redpill Linpro AS /a>, provider of Linux consulting and opera/opts services since 1995.