linux_hdio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* $OpenBSD: linux_hdio.c,v 1.9 2014/03/26 05:23:42 guenther Exp $ */
  2. /* $NetBSD: linux_hdio.c,v 1.1 2000/12/10 14:12:17 fvdl Exp $ */
  3. /*
  4. * Copyright (c) 2000 Wasabi Systems, Inc.
  5. * All rights reserved.
  6. *
  7. * Written by Frank van der Linden for Wasabi Systems, Inc.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed for the NetBSD Project by
  20. * Wasabi Systems, Inc.
  21. * 4. The name of Wasabi Systems, Inc. may not be used to endorse
  22. * or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  27. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
  29. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/file.h>
  41. #include <sys/filedesc.h>
  42. #include <sys/mount.h>
  43. #include <sys/proc.h>
  44. #include <sys/disklabel.h>
  45. #include <sys/dkio.h>
  46. #include <dev/ata/atareg.h>
  47. #include <dev/ic/wdcreg.h>
  48. #include <sys/ataio.h>
  49. #include <sys/syscallargs.h>
  50. #include <compat/linux/linux_types.h>
  51. #include <compat/linux/linux_ioctl.h>
  52. #include <compat/linux/linux_signal.h>
  53. #include <compat/linux/linux_util.h>
  54. #include <compat/linux/linux_hdio.h>
  55. #include <compat/linux/linux_syscallargs.h>
  56. int
  57. linux_ioctl_hdio(struct proc *p, struct linux_sys_ioctl_args *uap,
  58. register_t *retval)
  59. {
  60. u_long com;
  61. int error, error1;
  62. caddr_t sg;
  63. struct filedesc *fdp;
  64. struct file *fp;
  65. int (*ioctlf)(struct file *, u_long, caddr_t, struct proc *);
  66. struct ataparams *atap, ata;
  67. struct atareq req;
  68. struct disklabel label, *labp;
  69. struct partinfo partp;
  70. struct linux_hd_geometry hdg;
  71. struct linux_hd_big_geometry hdg_big;
  72. fdp = p->p_fd;
  73. if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
  74. return (EBADF);
  75. FREF(fp);
  76. com = SCARG(uap, com);
  77. ioctlf = fp->f_ops->fo_ioctl;
  78. retval[0] = error = 0;
  79. com = SCARG(uap, com);
  80. switch (com) {
  81. case LINUX_HDIO_OBSOLETE_IDENTITY:
  82. case LINUX_HDIO_GET_IDENTITY:
  83. sg = stackgap_init(p);
  84. atap = stackgap_alloc(&sg, DEV_BSIZE);
  85. if (atap == NULL) {
  86. error = ENOMEM;
  87. break;
  88. }
  89. req.flags = ATACMD_READ;
  90. req.command = WDCC_IDENTIFY;
  91. req.databuf = (caddr_t)atap;
  92. req.datalen = DEV_BSIZE;
  93. req.timeout = 1000;
  94. error = ioctlf(fp, ATAIOCCOMMAND, (caddr_t)&req, p);
  95. if (error != 0)
  96. break;
  97. if (req.retsts != ATACMD_OK) {
  98. error = EIO;
  99. break;
  100. }
  101. error = copyin(atap, &ata, sizeof ata);
  102. if (error != 0)
  103. break;
  104. /*
  105. * 142 is the size of the old structure used by Linux,
  106. * which doesn't seem to be defined anywhere anymore.
  107. */
  108. error = copyout(&ata, SCARG(uap, data),
  109. com == LINUX_HDIO_GET_IDENTITY ? sizeof ata : 142);
  110. break;
  111. case LINUX_HDIO_GETGEO:
  112. error = linux_machdepioctl(p, uap, retval);
  113. if (error == 0)
  114. break;
  115. error = ioctlf(fp, DIOCGDINFO, (caddr_t)&label, p);
  116. error1 = ioctlf(fp, DIOCGPART, (caddr_t)&partp, p);
  117. if (error != 0 && error1 != 0) {
  118. error = error1;
  119. break;
  120. }
  121. labp = error != 0 ? &label : partp.disklab;
  122. hdg.start = error1 != 0 ? DL_GETPOFFSET(partp.part) & 0x7fffffff: 0;
  123. hdg.heads = labp->d_ntracks;
  124. hdg.cylinders = labp->d_ncylinders;
  125. hdg.sectors = labp->d_nsectors;
  126. error = copyout(&hdg, SCARG(uap, data), sizeof hdg);
  127. break;
  128. case LINUX_HDIO_GETGEO_BIG:
  129. error = linux_machdepioctl(p, uap, retval);
  130. if (error == 0)
  131. break;
  132. case LINUX_HDIO_GETGEO_BIG_RAW:
  133. error = ioctlf(fp, DIOCGDINFO, (caddr_t)&label, p);
  134. error1 = ioctlf(fp, DIOCGPART, (caddr_t)&partp, p);
  135. if (error != 0 && error1 != 0) {
  136. error = error1;
  137. break;
  138. }
  139. labp = error != 0 ? &label : partp.disklab;
  140. hdg_big.start = error1 != 0 ? DL_GETPOFFSET(partp.part) & 0x7fffffff : 0;
  141. hdg_big.heads = labp->d_ntracks;
  142. hdg_big.cylinders = labp->d_ncylinders;
  143. hdg_big.sectors = labp->d_nsectors;
  144. error = copyout(&hdg_big, SCARG(uap, data), sizeof hdg_big);
  145. break;
  146. case LINUX_HDIO_GET_UNMASKINTR:
  147. case LINUX_HDIO_GET_MULTCOUNT:
  148. case LINUX_HDIO_GET_KEEPSETTINGS:
  149. case LINUX_HDIO_GET_32BIT:
  150. case LINUX_HDIO_GET_NOWERR:
  151. case LINUX_HDIO_GET_DMA:
  152. case LINUX_HDIO_GET_NICE:
  153. case LINUX_HDIO_DRIVE_RESET:
  154. case LINUX_HDIO_TRISTATE_HWIF:
  155. case LINUX_HDIO_DRIVE_TASK:
  156. case LINUX_HDIO_DRIVE_CMD:
  157. case LINUX_HDIO_SET_MULTCOUNT:
  158. case LINUX_HDIO_SET_UNMASKINTR:
  159. case LINUX_HDIO_SET_KEEPSETTINGS:
  160. case LINUX_HDIO_SET_32BIT:
  161. case LINUX_HDIO_SET_NOWERR:
  162. case LINUX_HDIO_SET_DMA:
  163. case LINUX_HDIO_SET_PIO_MODE:
  164. case LINUX_HDIO_SCAN_HWIF:
  165. case LINUX_HDIO_SET_NICE:
  166. case LINUX_HDIO_UNREGISTER_HWIF:
  167. error = EINVAL;
  168. }
  169. FRELE(fp, p);
  170. return error;
  171. }