Add missing libraries to examples' Makefiles
[dockapps.git] / ascd / libworkman / plat_svr4.c
blob374cf2887ff0c90cbf55e7c8a66815962415b90b
1 /*
2 * $Id: plat_svr4.c,v 1.7 1999/03/07 08:36:41 dirk Exp $
4 * This file is part of WorkMan, the civilized CD player library
5 * (c) 1991-1997 by Steven Grimm (original author)
6 * (c) by Dirk Försterling (current 'author' = maintainer)
7 * The maintainer can be contacted by his e-mail address:
8 * milliByte@DeathsDoor.com
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * SVR4 specific. Much of this is similar to plat_hpux.c.
28 #if defined(SVR4) && !defined(sun) && !defined(__sun__) && !defined(__sony_news)
30 static char plat_svr4_id[] = "$Id: plat_svr4.c,v 1.7 1999/03/07 08:36:41 dirk Exp $";
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <sys/mkdev.h>
41 #include <sys/stat.h>
42 #include <sys/sdi.h>
43 #include <sys/sdi_edt.h>
44 #include <sys/scsi.h>
45 #include <errno.h>
47 #include "include/wm_config.h"
48 #include "include/wm_struct.h"
50 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
53 void *malloc();
54 char *strchr();
56 int min_volume = 0;
57 int max_volume = 255;
59 extern char *cd_device;
62 * Initialize the drive. A no-op for the generic driver.
64 int
65 gen_init(d)
66 struct wm_drive *d;
68 return (0);
72 * Get the number of tracks on the CD.
74 int
75 gen_get_trackcount(d, tracks)
76 struct wm_drive *d;
77 int *tracks;
79 return (wm_scsi2_get_trackcount(d, tracks));
83 * Get the start time and mode (data or audio) of a track.
85 int
86 gen_get_trackinfo(d, track, data, startframe)
87 struct wm_drive *d;
88 int track, *data, *startframe;
90 return (wm_scsi2_get_trackinfo(d, track, data, startframe));
94 * Get the number of frames on the CD.
96 int
97 gen_get_cdlen(d, frames)
98 struct wm_drive *d;
99 int *frames;
101 int tmp;
103 return (wm_scsi2_get_cdlen(d, frames));
107 * Get the current status of the drive: the current play mode, the absolute
108 * position from start of disc (in frames), and the current track and index
109 * numbers if the CD is playing or paused.
112 gen_get_drive_status(d, oldmode, mode, pos, track, index)
113 struct wm_drive *d;
114 enum wm_cd_modes oldmode, *mode;
115 int *pos, *track, *index;
117 return (wm_scsi2_get_drive_status(d, oldmode, mode, pos, track, index));
121 * Set the volume level for the left and right channels. Their values
122 * range from 0 to 100.
125 gen_set_volume(d, left, right)
126 struct wm_drive *d;
127 int left, right;
129 return (wm_scsi2_set_volume(d, left, right));
133 * Read the initial volume from the drive, if available. Each channel
134 * ranges from 0 to 100, with -1 indicating data not available.
137 gen_get_volume(d, left, right)
138 struct wm_drive *d;
139 int *left, *right;
141 return (wm_scsi2_get_volume(d, left, right));
145 * Pause the CD.
148 gen_pause(d)
149 struct wm_drive *d;
151 return (wm_scsi2_pause(d));
155 * Resume playing the CD (assuming it was paused.)
158 gen_resume(d)
159 struct wm_drive *d;
161 return (wm_scsi2_resume(d));
165 * Stop the CD.
168 gen_stop(d)
169 struct wm_drive *d;
171 return (wm_scsi2_stop(d));
175 * Play the CD from one position to another (both in frames.)
178 gen_play(d, start, end)
179 struct wm_drive *d;
180 int start, end;
182 return (wm_scsi2_play(d, start, end));
186 * Eject the current CD, if there is one.
189 gen_eject(struct wm_drive *d)
191 return (wm_scsi2_eject(d));
192 } /* gen_eject() */
195 * Close the tray.
196 * please review scsi.c / wm_scsi2_closetray()
197 * and send changes to milliByte@DeathsDoor.com
200 gen_closetray( struct wm_drive *d )
202 return(wm_scsi2_closetray(d));
203 } /* gen_closetray() */
205 static int
206 create_cdrom_node(char *dev_name)
208 char pass_through[100];
209 int file_des;
210 dev_t pass_thru_device;
211 int err;
212 int ccode;
215 strcpy(pass_through, dev_name);
216 strcat(pass_through, "p" );
218 if (setreuid(-1,0) < 0)
220 perror("setregid/setreuid/access");
221 exit(1);
224 ccode = access(pass_through, F_OK);
226 if (ccode < 0)
228 if ((file_des = open(dev_name, O_RDONLY)) < 0)
230 perror("open cdrom devices failed");
231 return -1;
234 if (ioctl(file_des, B_GETDEV, &pass_thru_device) < 0)
236 perror("Call to get pass-through device number failed");
237 return -1;
240 (void)close(file_des);
242 if (mknod(pass_through, (S_IFCHR | S_IREAD | S_IWRITE),
243 pass_thru_device) < 0)
245 perror("Unable to make pass-through node");
246 return -1;
249 if (chown(pass_through, 0 , 0) < 0)
251 perror("chown");
252 return -1;
255 if (chmod(pass_through, 0660 ) < 0)
257 perror("chmod");
258 return -1;
262 file_des = open( pass_through, O_RDWR);
263 err = errno;
265 if ( (setreuid(-1,getuid()) < 0) || (setregid(-1,getgid()) < 0) )
267 perror("setreuid/setregid");
268 exit(1);
270 errno = err;
271 return file_des;
275 * Open the CD and figure out which kind of drive is attached.
278 wmcd_open(d)
279 struct wm_drive *d;
281 int fd, flag = 1;
282 static int warned = 0;
283 char vendor[32] = WM_STR_GENVENDOR;
284 char model[32] = WM_STR_GENMODEL;
285 char rev[32] = WM_STR_GENREV;
287 if (d->fd >= 0) /* Device already open? */
288 return (0);
290 if (cd_device == NULL)
291 cd_device = DEFAULT_CD_DEVICE;
293 d->fd = create_cdrom_node(cd_device); /* this will do open */
295 if (d->fd < 0)
297 if (errno == EACCES)
299 if (! warned)
301 fprintf(stderr,"Cannot access %s\n",cd_device);
302 warned++;
305 else if (errno != EINTR)
307 perror(cd_device);
308 exit(1);
311 /* No CD in drive. (Is this true also for svr4 ? XXX ) */
312 return (1);
315 if (warned)
317 warned = 0;
318 fprintf(stderr, "Thank you.\n");
321 /* Now fill in the relevant parts of the wm_drive structure. */
323 fd = d->fd;
325 if (wm_scsi_get_drive_type(d, vendor, model, rev) < 0)
327 perror("Cannot inquiry drive for it's type");
328 exit(1);
330 *d = *(find_drive_struct(vendor, model, rev));
331 wm_drive_settype(vendor, model, rev);
333 d->fd = fd;
335 return (0);
336 } /* wmcd_open() */
339 * Re-Open the device if it is open.
342 wmcd_reopen( struct wm_drive *d )
344 int status;
346 do {
347 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen ");
348 if (d->fd >= 0) /* Device really open? */
350 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closes the device and ");
351 status = close( d->fd ); /* close it! */
352 /* we know, that the file is closed, do we? */
353 d->fd = -1;
355 wm_susleep( 1000 );
356 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
357 status = wmcd_open( d ); /* open it as usual */
358 wm_susleep( 1000 );
359 } while ( status != 0 );
360 return status;
361 } /* wmcd_reopen() */
367 * Send a SCSI command out the bus.
370 wm_scsi(d, xcdb, cdblen, retbuf, retbuflen, getreply)
371 struct wm_drive *d;
372 unsigned char *xcdb;
373 int cdblen;
374 int getreply;
375 char *retbuf;
376 int retbuflen;
378 int ccode;
379 int file_des = d->fd;
380 int i,j;
381 unsigned char sense_buffer[ SENSE_SZ ];
382 int errno_save;
384 /* getreply == 1 is read, == 0 is write */
386 struct sb sb;
387 struct scs scs;
389 sb.sb_type = ISCB_TYPE;
391 sb.SCB.sc_comp_code = SDI_PROGRES;
392 sb.SCB.sc_int = NULL;
393 sb.SCB.sc_wd = 0;
394 sb.SCB.sc_dev.sa_major = 0;
395 sb.SCB.sc_dev.sa_minor = 0;
396 sb.SCB.sc_dev.sa_lun = 0;
397 sb.SCB.sc_dev.sa_exlun = 0;
398 sb.SCB.sc_status = 0;
399 sb.SCB.sc_link = (struct sb *) NULL;
400 sb.SCB.sc_resid = 0;
402 sb.SCB.sc_cmdpt = (void *)xcdb;
403 sb.SCB.sc_cmdsz = cdblen;
405 sb.SCB.sc_datapt = retbuf ;
406 sb.SCB.sc_datasz = retbuflen ;
408 if (getreply == 1)
409 sb.SCB.sc_mode = SCB_READ;
410 else
411 sb.SCB.sc_mode = SCB_WRITE;
413 sb.SCB.sc_time = 500;
415 ccode = ioctl(file_des, SDI_SEND, &sb);
417 if ( (sb.SCB.sc_comp_code != 0xd000000e ) ||
418 ( sb.SCB.sc_status != 02) )
419 return ccode;
421 errno_save = errno;
423 sb.SCB.sc_comp_code = SDI_PROGRES;
424 sb.SCB.sc_int = NULL;
425 sb.SCB.sc_wd = 0;
426 sb.SCB.sc_dev.sa_major = 0;
427 sb.SCB.sc_dev.sa_minor = 0;
428 sb.SCB.sc_dev.sa_lun = 0;
429 sb.SCB.sc_dev.sa_exlun = 0;
430 sb.SCB.sc_status = 0;
431 sb.SCB.sc_link = (struct sb *) NULL;
432 sb.SCB.sc_resid = 0;
434 scs.ss_op = SS_REQSEN;
435 scs.ss_lun = 0;
436 scs.ss_addr1 = 0;
437 scs.ss_addr = 0;
438 scs.ss_len = SENSE_SZ;
439 scs.ss_cont = 0;
441 sb.SCB.sc_cmdpt = SCS_AD(&scs);
442 sb.SCB.sc_cmdsz = SCS_SZ;
443 sb.SCB.sc_datapt = sense_buffer;
444 sb.SCB.sc_datasz = 18;
445 sb.SCB.sc_mode = SCB_READ;
446 sb.SCB.sc_time = 5000;
448 if (ioctl(file_des, SDI_SEND, &sb) < 0)
450 fprintf(stderr,"Cannot read sense.\n");
451 exit(-1);
454 errno=errno_save;
455 return -1;
458 #endif