print.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * usb/print - usb printer file server
  3. * BUG: Assumes the printer will be always connected and
  4. * not hot-plugged. (Otherwise should stay running and
  5. * listen to errors to keep the device there as long as it has
  6. * not failed). Also, this is untested and done ad-hoc to
  7. * replace the print script.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <thread.h>
  12. #include "usb.h"
  13. enum
  14. {
  15. Qdir = 0,
  16. Qctl,
  17. Qraw,
  18. Qdata,
  19. Qmax,
  20. };
  21. int
  22. findendpoints(Dev *dev, int devid)
  23. {
  24. Ep *ep;
  25. Dev *d;
  26. Usbdev *ud;
  27. int i, epout;
  28. epout = -1;
  29. ud = dev->usb;
  30. for(i = 0; i < nelem(ud->ep); i++){
  31. if((ep = ud->ep[i]) == nil)
  32. break;
  33. if(ep->iface->csp != 0x020107)
  34. continue;
  35. if(ep->type == Ebulk && (ep->dir == Eboth || ep->dir == Eout))
  36. if(epout == -1)
  37. epout = ep->id;
  38. }
  39. dprint(2, "print: ep ids: out %d\n", epout);
  40. if(epout == -1)
  41. return -1;
  42. d = openep(dev, epout);
  43. if(d == nil){
  44. fprint(2, "print: openep %d: %r\n", epout);
  45. return -1;
  46. }
  47. opendevdata(d, OWRITE);
  48. if(d->dfd < 0){
  49. fprint(2, "print: open i/o ep data: %r\n");
  50. closedev(d);
  51. return -1;
  52. }
  53. dprint(2, "print: ep out %s\n", d->dir);
  54. if(usbdebug > 1)
  55. devctl(d, "debug 1");
  56. devctl(d, "name lp%d", devid);
  57. return 0;
  58. }
  59. static int
  60. usage(void)
  61. {
  62. werrstr("usage: usb/print [-N id]");
  63. return -1;
  64. }
  65. int
  66. printmain(Dev *dev, int argc, char **argv)
  67. {
  68. int devid;
  69. devid = dev->id;
  70. ARGBEGIN{
  71. case 'N':
  72. devid = atoi(EARGF(usage()));
  73. break;
  74. default:
  75. return usage();
  76. }ARGEND
  77. if(argc != 0)
  78. return usage();
  79. if(findendpoints(dev, devid) < 0){
  80. werrstr("print: endpoints not found");
  81. return -1;
  82. }
  83. return 0;
  84. }