uk.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* $OpenBSD: uk.c,v 1.17 2011/06/01 17:47:31 matthew Exp $ */
  2. /* $NetBSD: uk.c,v 1.15 1996/03/17 00:59:57 thorpej Exp $ */
  3. /*
  4. * Copyright (c) 1994 Charles Hannum. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Charles Hannum.
  17. * 4. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * Dummy driver for a device we can't identify.
  33. * Originally by Julian Elischer (julian@tfs.com)
  34. */
  35. #include <sys/types.h>
  36. #include <sys/param.h>
  37. #include <sys/systm.h>
  38. #include <sys/errno.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/conf.h>
  41. #include <sys/device.h>
  42. #include <sys/vnode.h>
  43. #include <scsi/scsi_all.h>
  44. #include <scsi/scsiconf.h>
  45. #define UKUNIT(z) (minor(z))
  46. struct uk_softc {
  47. struct device sc_dev;
  48. struct scsi_link *sc_link; /* all the inter level info */
  49. };
  50. int ukmatch(struct device *, void *, void *);
  51. void ukattach(struct device *, struct device *, void *);
  52. int ukdetach(struct device *, int);
  53. struct cfattach uk_ca = {
  54. sizeof(struct uk_softc), ukmatch, ukattach, ukdetach
  55. };
  56. struct cfdriver uk_cd = {
  57. NULL, "uk", DV_DULL
  58. };
  59. #define uklookup(unit) (struct uk_softc *)device_lookup(&uk_cd, (unit))
  60. int
  61. ukmatch(struct device *parent, void *match, void *aux)
  62. {
  63. return (1);
  64. }
  65. /*
  66. * The routine called by the low level scsi routine when it discovers
  67. * a device suitable for this driver.
  68. */
  69. void
  70. ukattach(struct device *parent, struct device *self, void *aux)
  71. {
  72. struct uk_softc *sc = (void *)self;
  73. struct scsi_attach_args *sa = aux;
  74. struct scsi_link *sc_link = sa->sa_sc_link;
  75. SC_DEBUG(sc_link, SDEV_DB2, ("ukattach: "));
  76. /* Store information needed to contact our base driver */
  77. sc->sc_link = sc_link;
  78. sc_link->device_softc = sc;
  79. sc_link->openings = 1;
  80. printf("\n");
  81. }
  82. int
  83. ukdetach(struct device *self, int flags)
  84. {
  85. int bmaj, cmaj, mn;
  86. mn = self->dv_unit;
  87. for (bmaj = 0; bmaj < nblkdev; bmaj++)
  88. if (bdevsw[bmaj].d_open == ukopen)
  89. vdevgone(bmaj, mn, mn, VBLK);
  90. for (cmaj = 0; cmaj < nchrdev; cmaj++)
  91. if (cdevsw[cmaj].d_open == ukopen)
  92. vdevgone(cmaj, mn, mn, VCHR);
  93. return (0);
  94. }
  95. /*
  96. * open the device.
  97. */
  98. int
  99. ukopen(dev_t dev, int flag, int fmt, struct proc *p)
  100. {
  101. int unit;
  102. struct uk_softc *sc;
  103. struct scsi_link *sc_link;
  104. unit = UKUNIT(dev);
  105. sc = uklookup(unit);
  106. if (sc == NULL)
  107. return (ENXIO);
  108. sc_link = sc->sc_link;
  109. SC_DEBUG(sc_link, SDEV_DB1, ("ukopen: dev=0x%x (unit %d (of %d))\n",
  110. dev, unit, uk_cd.cd_ndevs));
  111. /* Only allow one at a time */
  112. if (sc_link->flags & SDEV_OPEN) {
  113. device_unref(&sc->sc_dev);
  114. return (EBUSY);
  115. }
  116. sc_link->flags |= SDEV_OPEN;
  117. SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
  118. device_unref(&sc->sc_dev);
  119. return (0);
  120. }
  121. /*
  122. * close the device.. only called if we are the LAST
  123. * occurrence of an open device
  124. */
  125. int
  126. ukclose(dev_t dev, int flag, int fmt, struct proc *p)
  127. {
  128. struct uk_softc *sc;
  129. sc = uklookup(UKUNIT(dev));
  130. if (sc == NULL)
  131. return (ENXIO);
  132. SC_DEBUG(sc->sc_link, SDEV_DB1, ("closing\n"));
  133. sc->sc_link->flags &= ~SDEV_OPEN;
  134. device_unref(&sc->sc_dev);
  135. return (0);
  136. }
  137. /*
  138. * Perform special action on behalf of the user
  139. * Only does generic scsi ioctls.
  140. */
  141. int
  142. ukioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
  143. {
  144. int rv;
  145. struct uk_softc *sc;
  146. sc = uklookup(UKUNIT(dev));
  147. if (sc == NULL)
  148. return (ENXIO);
  149. rv = scsi_do_ioctl(sc->sc_link, cmd, addr, flag);
  150. device_unref(&sc->sc_dev);
  151. return (rv);
  152. }