1 /*
2 * $Id: plat_news.c,v 1.7 1999/03/07 08:36:40 dirk Exp $
3 *
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
9 *
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.
14 *
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.
19 *
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
23 *
24 *
25 * Sony NEWS-specific drive control routines.
26 */
27
28 #if defined( __sony_news) || defined(sony_news)
29
30 static char plat_news_id[] = "$Id: plat_news.c,v 1.7 1999/03/07 08:36:40 dirk Exp $";
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <ustat.h>
36 #include <CD.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41
42 #include "include/wm_config.h"
43 #include "include/wm_struct.h"
44
45 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
46
47 void *malloc();
48 char *strchr();
49
50 extern char *cd_device;
51 extern int intermittent_dev;
52
53 int min_volume = 128;
54 int max_volume = 255;
55
56 /*
57 * Initialize the drive. A no-op for the generic driver.
58 */
59 int
gen_init(d)60 gen_init(d)
61 struct wm_drive *d;
62 {
63 return (0);
64 }
65
66 /*
67 * Get the number of tracks on the CD.
68 */
69 int
gen_get_trackcount(d,tracks)70 gen_get_trackcount(d, tracks)
71 struct wm_drive *d;
72 int *tracks;
73 {
74 struct CD_Capacity cc;
75
76 if (CD_GetCapacity(d->fd, &cc))
77 return (-1);
78
79 *tracks = cc.etrack - 1;
80 return (0);
81 }
82
83 /*
84 * Get the start time and mode (data or audio) of a track.
85 */
86 int
gen_get_trackinfo(d,track,data,startframe)87 gen_get_trackinfo(d, track, data, startframe)
88 struct wm_drive *d;
89 int track, *data, *startframe;
90 {
91 struct CD_TOCinfo hdr;
92 struct CD_TOCdata ent;
93
94 hdr.strack = track;
95 hdr.ntrack = 1;
96 hdr.data = &ent;
97 if (CD_ReadTOC(d->fd, &hdr))
98 return (-1);
99
100 *data = (ent.control & 4) ? 1 : 0;
101 *startframe = ent.baddr;
102
103 return (0);
104 }
105
106 /*
107 * Get the number of frames on the CD.
108 */
109 int
gen_get_cdlen(d,frames)110 gen_get_cdlen(d, frames)
111 struct wm_drive *d;
112 int *frames;
113 {
114 int tmp;
115
116 if ((d->get_trackcount)(d, &tmp))
117 return (-1);
118
119 return (gen_get_trackinfo(d, tmp + 1, &tmp, frames));
120 }
121
122 /*
123 * Get the current status of the drive: the current play mode, the absolute
124 * position from start of disc (in frames), and the current track and index
125 * numbers if the CD is playing or paused.
126 */
127 int
gen_get_drive_status(d,oldmode,mode,pos,track,index)128 gen_get_drive_status(d, oldmode, mode, pos, track, index)
129 struct wm_drive *d;
130 enum wm_cd_modes oldmode, *mode;
131 int *pos, *track, *index;
132 {
133 struct CD_Status sc;
134
135 /* If we can't get status, the CD is ejected, so default to that. */
136 *mode = WM_CDM_EJECTED;
137
138 /* Is the device open? */
139 if (d->fd < 0)
140 {
141 switch (wmcd_open(d)) {
142 case -1: /* error */
143 return (-1);
144
145 case 1: /* retry */
146 return (0);
147 }
148 }
149
150 /* Disc is ejected. Close the device. */
151 if (CD_GetStatus(d->fd, &sc))
152 {
153 WMclose(d->fd);
154 d->fd = -1;
155 return (0);
156 }
157
158 switch (sc.status) {
159 case CDSTAT_PLAY:
160 *mode = PLAYING;
161 *track = sc.tno;
162 *index = sc.index;
163 *pos = sc.baddr;
164 break;
165
166 case CDSTAT_PAUSE:
167 if (oldmode == WM_CDM_PLAYING || oldmode == WM_CDM_PAUSED)
168 {
169 *mode = WM_CDM_PAUSED;
170 *track = sc.tno;
171 *index = sc.index;
172 *pos = sc.baddr;
173 }
174 else
175 *mode = WM_CDM_STOPPED;
176 break;
177
178 case CDSTAT_STOP:
179 if (oldmode == WM_CDM_PLAYING)
180 {
181 *mode = WM_CDM_TRACK_DONE; /* waiting for next track. */
182 break;
183 }
184 /* fall through */
185
186 default:
187 *mode = WM_CDM_STOPPED;
188 break;
189 }
190
191 return (0);
192 }
193
194 /*
195 * Set the volume level for the left and right channels. Their values
196 * range from 0 to 100.
197 */
198 int
gen_set_volume(d,left,right)199 gen_set_volume(d, left, right)
200 struct wm_drive *d;
201 int left, right;
202 {
203 /* NEWS can't adjust volume! */
204 return (0);
205 }
206
207 /*
208 * Pause the CD.
209 */
210 int
gen_pause(d)211 gen_pause(d)
212 struct wm_drive *d;
213 {
214 CD_Pause(d->fd);
215
216 return (0);
217 }
218
219 /*
220 * Resume playing the CD (assuming it was paused.)
221 */
222 int
gen_resume(d)223 gen_resume(d)
224 struct wm_drive *d;
225 {
226 CD_Restart(d->fd);
227
228 return (0);
229 }
230
231 /*
232 * Stop the CD.
233 */
234 int
gen_stop(d)235 gen_stop(d)
236 struct wm_drive *d;
237 {
238 CD_Stop(d->fd);
239
240 return (0);
241 }
242
243 /*
244 * Play the CD from one position to another (both in frames.)
245 */
246 int
gen_play(d,start,end)247 gen_play(d, start, end)
248 struct wm_drive *d;
249 int start, end;
250 {
251 struct CD_PlayAddr msf;
252
253 msf.addrmode = CD_MSF;
254 msf.addr.msf.startmsf.min = start / (60*75);
255 msf.addr.msf.startmsf.sec = (start % (60*75)) / 75;
256 msf.addr.msf.startmsf.frame = start % 75;
257 msf.addr.msf.endmsf.min = end / (60*75);
258 msf.addr.msf.endmsf.sec = (end % (60*75)) / 75;
259 msf.addr.msf.endmsf.frame = end % 75;
260
261 if (CD_Play(d->fd, &msf))
262 {
263 printf("wm_cd_play_chunk(%d,%d)\n",start,end);
264 printf("msf = %d:%d:%d %d:%d:%d\n",
265 msf.addr.msf.startmsf.min,
266 msf.addr.msf.startmsf.sec,
267 msf.addr.msf.startmsf.frame,
268 msf.addr.msf.endmsf.min,
269 msf.addr.msf.endmsf.sec,
270 msf.addr.msf.endmsf.frame);
271 perror("CD_Play");
272 return (-1);
273 }
274
275 return (0);
276 }
277
278 /*
279 * Eject the current CD, if there is one.
280 */
281 int
gen_eject(d)282 gen_eject(d)
283 struct wm_drive *d;
284 {
285 struct stat stbuf;
286 struct ustat ust;
287
288 if (fstat(d->fd, &stbuf) != 0)
289 return (-2);
290
291 /* Is this a mounted filesystem? */
292 if (ustat(stbuf.st_rdev, &ust) == 0)
293 return (-3);
294
295 if (CD_AutoEject(d->fd))
296 return (-1);
297
298 /* Close the device if it needs to vanish. */
299 if (intermittent_dev)
300 {
301 WMclose(d->fd);
302 d->fd = -1;
303 }
304
305 return (0);
306 }
307
308 /*----------------------------------------*
309 * Close the CD tray
310 *
311 * Please edit and send changes to
312 * milliByte@DeathsDoor.com
313 *----------------------------------------*/
314
gen_closetray(struct wm_drive * d)315 int gen_closetray(struct wm_drive *d)
316 {
317 #ifdef CAN_CLOSE
318 if(!close(d->fd))
319 {
320 d->fd=-1;
321 return(wmcd_reopen(d));
322 } else {
323 return(-1);
324 }
325 #else
326 /* Always succeed if the drive can't close */
327 return(0);
328 #endif /* CAN_CLOSE */
329 } /* gen_closetray() */
330
331
332 /*
333 * Close the CD device.
334 */
335 int
WMclose(fd)336 WMclose(fd)
337 int fd;
338 {
339 int ret;
340
341 ret = CD_Close(fd);
342 wm_susleep(3000000);
343 return (ret);
344 }
345
346 /*
347 * Read the initial volume from the drive, if available. Each channel
348 * ranges from 0 to 100, with -1 indicating data not available.
349 */
350 int
gen_get_volume(d,left,right)351 gen_get_volume(d, left, right)
352 struct wm_drive *d;
353 int *left, *right;
354 {
355 /* Suns, HPs, Linux, NEWS can't read the volume; oh well */
356 *left = *right = -1;
357
358 return (0);
359 }
360
361 /*
362 * Pass SCSI commands to the device.
363 */
364 int
wm_scsi(d,cdb,cdblen,buf,buflen,getreply)365 wm_scsi(d, cdb, cdblen, buf, buflen, getreply)
366 struct wm_drive *d;
367 unsigned char *cdb, *buf;
368 int cdblen, buflen, getreply;
369 {
370 /* NEWS can't do SCSI passthrough... or can it? */
371 return (-1);
372 }
373
374 /*
375 * Open the CD device and figure out what kind of drive is attached.
376 */
377 int
wmcd_open(d)378 wmcd_open(d)
379 struct wm_drive *d;
380 {
381 int fd;
382 static int warned = 0;
383 char vendor[32] = WM_STR_GENVENDOR;
384 char model[32] = WM_STR_GENMODEL;
385 char rev[32] = WM_STR_GENREV;
386
387 if (d->fd >= 0) /* Device already open? */
388 return (0);
389
390 intermittent_dev = 1;
391 if (cd_device == NULL)
392 cd_device = DEFAULT_CD_DEVICE;
393
394 if ((d->fd = CD_Open(cd_device, 0)) < 0)
395 {
396 /* Solaris 2.2 volume manager moves links around */
397 if (errno == ENOENT && intermittent_dev)
398 return (0);
399
400 if (errno == EACCES)
401 {
402 if (!warned)
403 {
404 char realname[MAXPATHLEN];
405
406 if (realpath(cd_device, realname) == NULL)
407 {
408 perror("realpath");
409 return (0);
410 }
411
412 fprintf(stderr,
413 "As root, please run\n\nchmod 666 %s\n\n%s\n", realname,
414 "to give yourself permission to access the CD-ROM device.");
415 warned++;
416 }
417 }
418 else if (errno != EIO) /* defined at top */
419 {
420 perror(cd_device);
421 exit(1);
422 }
423
424 /* No CD in drive. */
425 return (1);
426 }
427
428 if (warned)
429 {
430 warned = 0;
431 fprintf(stderr, "Thank you.\n");
432 }
433
434 /* Now fill in the relevant parts of the wm_drive structure. */
435 fd = d->fd;
436
437 /* Figure out the drive type, if possible */
438 wm_scsi_get_drive_type(d, vendor, model, rev);
439 *d = *(find_drive_struct(vendor, model, rev));
440 wm_drive_settype(vendor, model, rev);
441
442 d->fd = fd;
443
444 (d->init)(d);
445
446 return (0);
447 } /* wmcd_open() */
448
449 /*
450 * Re-Open the device if it is open.
451 */
452 int
wmcd_reopen(struct wm_drive * d)453 wmcd_reopen( struct wm_drive *d )
454 {
455 int status;
456
457 do {
458 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "wmcd_reopen ");
459 if (d->fd >= 0) /* Device really open? */
460 {
461 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closes the device and ");
462 status = close( d->fd ); /* close it! */
463 /* we know, that the file is closed, do we? */
464 d->fd = -1;
465 }
466 wm_susleep( 1000 );
467 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "calls wmcd_open()\n");
468 status = wmcd_open( d ); /* open it as usual */
469 wm_susleep( 1000 );
470 } while ( status != 0 );
471 return status;
472 } /* wmcd_reopen() */
473
474 #endif
475