1 /* $OpenBSD: dev_hppa.c,v 1.9 2003/06/01 17:00:34 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1998 Michael Shalayeff 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Michael Shalayeff. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 */ 34 35 #include "libsa.h" 36 #include <sys/param.h> 37 #include <sys/disklabel.h> 38 #include <sys/reboot.h> 39 #include <dev/cons.h> 40 41 #include <machine/iomod.h> 42 43 #include "dev_hppa.h" 44 45 extern int debug; 46 47 const char cdevs[][4] = { 48 "ite", "", "", "", "", "", "", "", 49 "", "", "", "", "" 50 }; 51 const int ncdevs = NENTS(cdevs); 52 53 const struct pdc_devs { 54 char name[3]; 55 int dev_type; 56 } pdc_devs[] = { 57 { "dk", 0 }, 58 { "ct", 1 }, 59 { "lf", 2 }, 60 { "", -1 }, 61 { "rd", -1 }, 62 { "sw", -1 }, 63 { "fl", -1 }, 64 }; 65 66 /* pass dev_t to the open routines */ 67 int 68 devopen(f, fname, file) 69 struct open_file *f; 70 const char *fname; 71 char **file; 72 { 73 struct hppa_dev *hpd; 74 const struct pdc_devs *dp = pdc_devs; 75 int rc = 1; 76 77 if (!(*file = strchr(fname, ':'))) 78 return ENODEV; 79 else 80 (*file)++; 81 82 #ifdef DEBUGBUG 83 if (debug) 84 printf("devopen: "); 85 #endif 86 87 for (dp = pdc_devs; dp < &pdc_devs[NENTS(pdc_devs)]; dp++) 88 if (!strncmp(fname, dp->name, sizeof(dp->name)-1)) 89 break; 90 91 if (dp >= &pdc_devs[NENTS(pdc_devs)] || dp->dev_type < 0) 92 return ENODEV; 93 #ifdef DEBUGBUG 94 if (debug) 95 printf("%s\n", dp->name); 96 #endif 97 98 if (!(hpd = alloc(sizeof *hpd))) { 99 #ifdef DEBUG 100 printf ("devopen: no mem\n"); 101 #endif 102 } else { 103 bzero(hpd, sizeof *hpd); 104 hpd->bootdev = bootdev; 105 hpd->buf = (char *)(((u_int)hpd->ua_buf + IODC_MINIOSIZ-1) & 106 ~(IODC_MINIOSIZ-1)); 107 f->f_devdata = hpd; 108 if ((rc = (*devsw[dp->dev_type].dv_open)(f, file)) == 0) { 109 f->f_dev = &devsw[dp->dev_type]; 110 return 0; 111 } 112 free (hpd, 0); 113 f->f_devdata = NULL; 114 } 115 116 if (!(f->f_flags & F_NODEV)) 117 f->f_dev = &devsw[dp->dev_type]; 118 119 if (!f->f_devdata) 120 *file = NULL; 121 122 return rc; 123 } 124 125 void 126 devboot(dev, p) 127 dev_t dev; 128 char *p; 129 { 130 const char *q; 131 if (!dev) { 132 int type, unit; 133 134 switch (PAGE0->mem_boot.pz_class) { 135 case PCL_RANDOM: 136 type = 0; 137 unit = PAGE0->mem_boot.pz_layers[0]; 138 break; 139 case PCL_SEQU: 140 type = 1; 141 unit = PAGE0->mem_boot.pz_layers[0]; 142 break; 143 case PCL_NET_MASK|PCL_SEQU: 144 type = 2; 145 unit = 0; 146 break; 147 default: 148 type = 0; 149 unit = 0; 150 break; 151 } 152 dev = bootdev = MAKEBOOTDEV(type, 0, 0, unit, B_PARTITION(dev)); 153 } 154 #ifdef _TEST 155 *p++ = '/'; 156 *p++ = 'd'; 157 *p++ = 'e'; 158 *p++ = 'v'; 159 *p++ = '/'; 160 *p++ = 'r'; 161 #endif 162 /* quick copy device name */ 163 for (q = pdc_devs[B_TYPE(dev)].name; (*p++ = *q++);); 164 p[-1] = '0' + B_UNIT(dev); 165 *p++ = 'a' + B_PARTITION(dev); 166 *p = '\0'; 167 } 168 169 int pch_pos; 170 171 void 172 putchar(c) 173 int c; 174 { 175 switch(c) { 176 case '\177': /* DEL erases */ 177 cnputc('\b'); 178 cnputc(' '); 179 case '\b': 180 cnputc('\b'); 181 if (pch_pos) 182 pch_pos--; 183 break; 184 case '\t': 185 do 186 cnputc(' '); 187 while(++pch_pos % 8); 188 break; 189 case '\n': 190 case '\r': 191 cnputc(c); 192 pch_pos=0; 193 break; 194 default: 195 cnputc(c); 196 pch_pos++; 197 break; 198 } 199 } 200 201 int 202 getchar() 203 { 204 int c = cngetc(); 205 206 if (c == '\r') 207 c = '\n'; 208 209 if ((c < ' ' && c != '\n') || c == '\177') 210 return(c); 211 212 putchar(c); 213 214 return(c); 215 } 216 217 char ttyname_buf[8]; 218 char * 219 ttyname(fd) 220 int fd; 221 { 222 snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d", 223 cdevs[major(cn_tab->cn_dev)], 224 minor(cn_tab->cn_dev)); 225 return (ttyname_buf); 226 } 227 228 dev_t 229 ttydev(name) 230 char *name; 231 { 232 int i, unit = -1; 233 char *no = name + strlen(name) - 1; 234 235 while (no >= name && *no >= '0' && *no <= '9') 236 unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0'; 237 if (no < name || unit < 0) 238 return (NODEV); 239 for (i = 0; i < ncdevs; i++) 240 if (strncmp(name, cdevs[i], no - name + 1) == 0) 241 return (makedev(i, unit)); 242 return (NODEV); 243 } 244