2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi-linux-pg.c 1.43 04/01/15 Copyright 1997 J. Schilling */
15 * Interface for the Linux PARIDE implementation.
17 * We emulate the functionality of the usal driver, via the pg driver.
19 * Warning: you may change this source, but if you do that
20 * you need to change the _usal_version and _usal_auth* string below.
21 * You may not return "schily" for an SCG_AUTHOR request anymore.
22 * Choose your name instead of "schily" and make clear that the version
23 * string is related to a modified source.
25 * Copyright (c) 1997 J. Schilling
26 * Copyright (c) 1998 Grant R. Guenther <grant@torque.net>
27 * Under the terms of the GNU public license.
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License version 2
32 * as published by the Free Software Foundation.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License along with
40 * this program; see the file COPYING. If not, write to the Free Software
41 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 #ifdef HAVE_LINUX_PG_H
48 #include "pg.h" /* Use local version as Linux sometimes doesn't have */
49 #endif /* installed. Now libusal always supports PP SCSI */
52 * Warning: you may change this source, but if you do that
53 * you need to change the _usal_version and _usal_auth* string below.
54 * You may not return "schily" for an SCG_AUTHOR request anymore.
55 * Choose your name instead of "schily" and make clear that the version
56 * string is related to a modified source.
58 static char _usal_trans_version_pg[] = "scsi-linux-pg.c-1.43"; /* The version for this transport*/
62 #define MAX_SCG 1 /* Max # of SCSI controllers */
67 short usalfiles[MAX_SCG][MAX_TGT][MAX_LUN];
68 short buscookies[MAX_SCG];
72 #define usallocal(p) ((struct usal_local *)((p)->local))
76 #define usalo_version pg_version
77 #define usalo_help pg_help
78 #define usalo_open pg_open
79 #define usalo_close pg_close
80 #define usalo_send pg_send
81 #define usalo_maxdma pg_maxdma
82 #define usalo_initiator_id pg_initiator_id
83 #define usalo_isatapi pg_isatapi
84 #define usalo_reset pg_reset
86 static char *pg_version(SCSI *usalp, int what);
87 static int pg_help(SCSI *usalp, FILE *f);
88 static int pg_open(SCSI *usalp, char *device);
89 static int pg_close(SCSI *usalp);
90 static long pg_maxdma(SCSI *usalp, long amt);
91 static int pg_initiator_id(SCSI *usalp);
92 static int pg_isatapi(SCSI *usalp);
93 static int pg_reset(SCSI *usalp, int what);
94 static int pg_send(SCSI *usalp);
98 static int do_usal_cmd(SCSI *usalp, struct usal_cmd *sp);
99 static int do_usal_sense(SCSI *usalp, struct usal_cmd *sp);
103 * Return version information for the low level SCSI transport code.
104 * This has been introduced to make it easier to trace down problems
108 usalo_version(SCSI *usalp, int what)
110 if (usalp != (SCSI *)0) {
114 return (_usal_trans_version_pg);
116 * If you changed this source, you are not allowed to
117 * return "schily" for the SCG_AUTHOR request.
120 return (_usal_auth_cdrkit);
129 usalo_help(SCSI *usalp, FILE *f)
131 __usal_help(f, "pg", "SCSI transport for ATAPI over Parallel Port",
132 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
137 usalo_open(SCSI *usalp, char *device)
139 int busno = usal_scsibus(usalp);
140 int tgt = usal_target(usalp);
141 int tlun = usal_lun(usalp);
148 register int nopen = 0;
151 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
154 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
155 "Illegal value for busno, target or lun '%d,%d,%d'",
162 * We need to find a fake bus number for the parallel port interface.
163 * Unfortunatly, the buscookie array may contain holes if
164 * SCSI_IOCTL_GET_BUS_NUMBER works, so we are searching backwards
165 * for some place for us.
166 * XXX Should add extra space in buscookies for a "PP bus".
169 if (usallocal(usalp)->buscookies[MAX_SCG-1] != (short)-1)
170 return (0); /* No space for pgbus */
172 for (b = MAX_SCG-1; b >= 0; b--) {
173 if (usallocal(usalp)->buscookies[b] != (short)-1) {
174 usallocal(usalp)->pgbus = ++b;
178 if (usalp->debug > 0) {
179 fprintf((FILE *)usalp->errfile,
180 "PP Bus: %d\n", usallocal(usalp)->pgbus);
183 if (usalp->local == NULL) {
184 usalp->local = malloc(sizeof (struct usal_local));
185 if (usalp->local == NULL)
188 usallocal(usalp)->pgbus = -2;
189 usallocal(usalp)->SCSIbuf = (char *)-1;
191 for (b = 0; b < MAX_SCG; b++) {
192 for (t = 0; t < MAX_TGT; t++) {
193 for (l = 0; l < MAX_LUN; l++)
194 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
199 if (usallocal(usalp)->pgbus < 0)
200 usallocal(usalp)->pgbus = 0;
202 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2))
205 if (busno >= 0 && tgt >= 0 && tlun >= 0) {
207 if (usallocal(usalp)->pgbus != busno)
210 snprintf(devname, sizeof (devname), "/dev/pg%d", tgt);
211 f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
214 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
215 "Cannot open '%s'", devname);
218 usallocal(usalp)->usalfiles[busno][tgt][tlun] = f;
222 for (tgt = 0; tgt < MAX_TGT; tgt++) {
223 snprintf(devname, sizeof (devname), "/dev/pg%d", tgt);
224 f = sg_open_excl(devname, O_RDWR | O_NONBLOCK);
227 * Set up error string but let us clear it later
228 * if at least one open succeeded.
231 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
232 "Cannot open '/dev/pg*'");
233 if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
235 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
236 "Cannot open '%s'", devname);
240 usallocal(usalp)->usalfiles[usallocal(usalp)->pgbus][tgt][tlun] = f;
245 if (nopen > 0 && usalp->errstr)
246 usalp->errstr[0] = '\0';
249 if (device != NULL && *device != '\0') {
254 f = open(device, O_RDWR | O_NONBLOCK);
255 /* if (f < 0 && errno == ENOENT) {*/
258 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
264 p = device + strlen(device) -1;
266 if (tgt < 0 || tgt > 9)
268 usallocal(usalp)->usalfiles[usallocal(usalp)->pgbus][tgt][tlun] = f;
269 usal_settarget(usalp, usallocal(usalp)->pgbus, tgt, tlun);
277 usalo_close(SCSI *usalp)
284 if (usalp->local == NULL)
286 if (usallocal(usalp)->pgbus < 0)
288 b = usallocal(usalp)->pgbus;
289 usallocal(usalp)->buscookies[b] = (short)-1;
291 for (t = 0; t < MAX_TGT; t++) {
292 for (l = 0; l < MAX_LUN; l++) {
293 f = usallocal(usalp)->usalfiles[b][t][l];
296 usallocal(usalp)->usalfiles[b][t][l] = (short)-1;
303 usalo_maxdma(SCSI *usalp, long amt)
305 return (PG_MAX_DATA);
311 usalo_getbuf(SCSI *usalp, long amt)
315 if (usalp->debug > 0) {
316 fprintf((FILE *)usalp->errfile,
317 "usalo_getbuf: %ld bytes\n", amt);
319 ret = valloc((size_t)(amt+getpagesize()));
322 usalp->bufbase = ret;
323 ret += getpagesize();
324 usallocal(usalp)->SCSIbuf = ret;
325 return ((void *)ret);
330 usalo_freebuf(SCSI *usalp)
333 free(usalp->bufbase);
334 usalp->bufbase = NULL;
338 usalo_havebus(SCSI *usalp, int busno)
343 if (busno < 0 || busno >= MAX_SCG)
346 if (usalp->local == NULL)
349 for (t = 0; t < MAX_TGT; t++) {
350 for (l = 0; l < MAX_LUN; l++)
351 if (usallocal(usalp)->usalfiles[busno][t][l] >= 0)
358 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
360 if (busno < 0 || busno >= MAX_SCG ||
361 tgt < 0 || tgt >= MAX_TGT ||
362 tlun < 0 || tlun >= MAX_LUN)
365 if (usalp->local == NULL)
368 return ((int)usallocal(usalp)->usalfiles[busno][tgt][tlun]);
370 #endif /* USE_PG_ONLY */
373 usalo_initiator_id(SCSI *usalp)
379 usalo_isatapi(SCSI *usalp)
385 usalo_reset(SCSI *usalp, int what)
387 struct pg_write_hdr hdr = {PG_MAGIC, PG_RESET, 0};
389 if (what == SCG_RESET_NOP)
391 if (what != SCG_RESET_BUS) {
396 * XXX Does this reset TGT or BUS ???
398 return (write(usalp->fd, (char *)&hdr, sizeof (hdr)));
403 #define MAX(a, b) ((a) > (b) ? (a):(b))
406 #define RHSIZE sizeof (struct pg_read_hdr)
407 #define WHSIZE sizeof (struct pg_write_hdr)
408 #define LEAD MAX(RHSIZE, WHSIZE)
411 do_usal_cmd(SCSI *usalp, struct usal_cmd *sp)
414 char local[LEAD+PG_MAX_DATA];
416 int inward = (sp->flags & SCG_RECV_DATA);
418 struct pg_write_hdr *whp;
419 struct pg_read_hdr *rhp;
422 if (sp->cdb_len > 12)
423 comerrno(EX_BAD, "Can't do %d byte command.\n", sp->cdb_len);
425 if (sp->addr == usallocal(usalp)->SCSIbuf) {
432 movebytes(sp->addr, dbp, sp->size);
435 whp = (struct pg_write_hdr *)(dbp - WHSIZE);
436 rhp = (struct pg_read_hdr *)(dbp - RHSIZE);
438 whp->magic = PG_MAGIC;
439 whp->func = PG_COMMAND;
440 whp->dlen = sp->size;
441 whp->timeout = sp->timeout;
443 for (i = 0; i < 12; i++) {
445 whp->packet[i] = sp->cdb.cmd_cdb[i];
454 r = write(usalp->fd, (char *)whp, i);
456 if (r < 0) { /* command was not sent */
457 sp->ux_errno = geterrno();
458 if (sp->ux_errno == ETIME) {
460 * If the previous command timed out, we cannot send
461 * any further command until the command in the drive
462 * is ready. So we behave as if the drive did not
463 * respond to the command.
465 sp->error = SCG_FATAL;
472 errmsg("usalo_send(%s) wrote %d bytes (expected %d).\n",
473 usalp->cmdname, r, i);
478 r = read(usalp->fd, (char *)rhp, RHSIZE+sp->size);
481 sp->ux_errno = geterrno();
482 if (sp->ux_errno == ETIME) {
483 sp->error = SCG_TIMEOUT;
486 sp->error = SCG_FATAL;
493 * "DMA overrun" should be handled in the kernel.
494 * However this may happen with flaky PP adapters.
497 "DMA (read) overrun by %d bytes (requested %d bytes).\n",
498 i - sp->size, sp->size);
499 sp->resid = sp->size - i;
500 sp->error = SCG_RETRYABLE;
503 sp->resid = sp->size - i;
506 if (use_local && inward)
507 movebytes(dbp, sp->addr, i);
509 fillbytes(&sp->scb, sizeof (sp->scb), '\0');
510 fillbytes(&sp->u_sense.cmd_sense, sizeof (sp->u_sense.cmd_sense), '\0');
512 sp->error = SCG_NO_ERROR;
515 sp->u_scb.cmd_scb[0] = i;
517 if (sp->ux_errno == 0)
520 * If there is no DMA overrun and there is a
521 * SCSI Status byte != 0 then the SCSI cdb transport was OK
522 * and sp->error must be SCG_NO_ERROR.
524 /* sp->error = SCG_RETRYABLE;*/
532 do_usal_sense(SCSI *usalp, struct usal_cmd *sp)
535 struct usal_cmd s_cmd;
537 fillbytes((caddr_t)&s_cmd, sizeof (s_cmd), '\0');
538 s_cmd.addr = (caddr_t)sp->u_sense.cmd_sense;
539 s_cmd.size = sp->sense_len;
540 s_cmd.flags = SCG_RECV_DATA|SCG_DISRE_ENA;
541 s_cmd.cdb_len = SC_G0_CDBLEN;
542 s_cmd.sense_len = CCS_SENSE_LEN;
543 s_cmd.cdb.g0_cdb.cmd = SC_REQUEST_SENSE;
544 s_cmd.cdb.g0_cdb.lun = sp->cdb.g0_cdb.lun;
545 s_cmd.cdb.g0_cdb.count = sp->sense_len;
546 ret = do_usal_cmd(usalp, &s_cmd);
551 sp->sense_count = sp->sense_len - s_cmd.resid;
556 usalo_send(SCSI *usalp)
558 struct usal_cmd *sp = usalp->scmd;
562 sp->error = SCG_FATAL;
565 ret = do_usal_cmd(usalp, sp);
568 if (sp->u_scb.cmd_scb[0] & 2)
569 ret = do_usal_sense(usalp, sp);
573 /* end of scsi-linux-pg.c */
583 #undef usalo_initiator_id