2006-08-15 20:37:04 +00:00
|
|
|
/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
|
|
|
|
|
2006-10-10 17:55:34 +00:00
|
|
|
/* ts A61010 */
|
|
|
|
/* #include <a ssert.h> */
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2006-10-13 10:22:21 +00:00
|
|
|
|
|
|
|
/* #include <m alloc.h> ts A61013 : not in Linux man 3 malloc */
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <linux/hdreg.h>
|
2006-10-07 17:05:45 +00:00
|
|
|
#include <stdlib.h>
|
2006-10-15 13:07:43 +00:00
|
|
|
#include <scsi/sg.h>
|
|
|
|
#include <scsi/scsi.h>
|
2006-08-15 20:37:04 +00:00
|
|
|
|
|
|
|
#include "transport.h"
|
|
|
|
#include "drive.h"
|
|
|
|
#include "sg.h"
|
|
|
|
#include "spc.h"
|
|
|
|
#include "mmc.h"
|
|
|
|
#include "sbc.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "toc.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2006-10-15 13:07:43 +00:00
|
|
|
/* kludge! glibc headers don't define all the SCSI stuff that we use! */
|
|
|
|
#ifndef SG_GET_ACCESS_COUNT
|
|
|
|
# define SG_GET_ACCESS_COUNT 0x2289
|
|
|
|
#endif
|
|
|
|
|
2006-09-24 18:07:10 +00:00
|
|
|
#include "libdax_msgs.h"
|
|
|
|
extern struct libdax_msgs *libdax_messenger;
|
|
|
|
|
2006-10-05 14:21:34 +00:00
|
|
|
static void enumerate_common(char *fname, int bus_no, int host_no,
|
|
|
|
int channel_no, int target_no, int lun_no);
|
2006-08-15 20:37:04 +00:00
|
|
|
|
2006-08-15 20:40:30 +00:00
|
|
|
/* ts A51221 */
|
|
|
|
int burn_drive_is_banned(char *device_address);
|
|
|
|
|
2006-08-20 19:03:21 +00:00
|
|
|
/* ts A60813 : storage objects are in libburn/init.c
|
|
|
|
wether to use O_EXCL
|
|
|
|
wether to use O_NOBLOCK with open(2) on devices
|
|
|
|
wether to take O_EXCL rejection as fatal error */
|
|
|
|
extern int burn_sg_open_o_excl;
|
|
|
|
extern int burn_sg_open_o_nonblock;
|
|
|
|
extern int burn_sg_open_abort_busy;
|
|
|
|
|
|
|
|
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
int mmc_function_spy(char * text);
|
|
|
|
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
static int sgio_test(int fd)
|
|
|
|
{
|
|
|
|
unsigned char test_ops[] = { 0, 0, 0, 0, 0, 0 };
|
|
|
|
sg_io_hdr_t s;
|
|
|
|
|
|
|
|
memset(&s, 0, sizeof(sg_io_hdr_t));
|
|
|
|
s.interface_id = 'S';
|
|
|
|
s.dxfer_direction = SG_DXFER_NONE;
|
|
|
|
s.cmd_len = 6;
|
|
|
|
s.cmdp = test_ops;
|
|
|
|
s.timeout = 12345;
|
|
|
|
return ioctl(fd, SG_IO, &s);
|
|
|
|
}
|
|
|
|
|
2006-09-22 13:30:32 +00:00
|
|
|
|
2006-09-25 14:09:14 +00:00
|
|
|
/* ts A60925 : ticket 74 */
|
|
|
|
int sg_close_drive_fd(char *fname, int driveno, int *fd, int sorry)
|
|
|
|
{
|
|
|
|
int ret, os_errno, sevno= LIBDAX_MSGS_SEV_DEBUG;
|
|
|
|
char msg[4096+100];
|
|
|
|
|
|
|
|
if(*fd < 0)
|
|
|
|
return(0);
|
|
|
|
ret = close(*fd);
|
|
|
|
*fd = -1337;
|
|
|
|
if(ret != -1)
|
|
|
|
return 1;
|
|
|
|
os_errno= errno;
|
|
|
|
|
|
|
|
if (fname != NULL)
|
|
|
|
sprintf(msg, "Encountered error when closing drive '%s'",
|
|
|
|
fname);
|
|
|
|
else
|
|
|
|
sprintf(msg, "Encountered error when closing drive");
|
|
|
|
|
|
|
|
if (sorry)
|
|
|
|
sevno = LIBDAX_MSGS_SEV_SORRY;
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno, 0x00020002,
|
|
|
|
sevno, LIBDAX_MSGS_PRIO_HIGH, msg, os_errno, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
/* ts A60924 */
|
|
|
|
int sg_handle_busy_device(char *fname, int os_errno)
|
|
|
|
{
|
|
|
|
char msg[4096];
|
|
|
|
|
|
|
|
/* ts A60814 : i saw no way to do this more nicely */
|
|
|
|
if (burn_sg_open_abort_busy) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn: FATAL : Application triggered abort on busy device '%s'\n",
|
|
|
|
fname);
|
2006-10-07 17:05:45 +00:00
|
|
|
|
|
|
|
/* ts A61007 */
|
|
|
|
abort();
|
|
|
|
/* a ssert("drive busy" == "non fatal"); */
|
2006-09-27 11:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ts A60924 : now reporting to libdax_msgs */
|
|
|
|
sprintf(msg, "Cannot open busy device '%s'", fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020001,
|
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_LOW,
|
|
|
|
msg, os_errno, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-22 13:30:32 +00:00
|
|
|
/* ts A60922 ticket 33 */
|
2006-09-22 17:01:26 +00:00
|
|
|
/** Returns the next index number and the next enumerated drive address.
|
2006-10-14 12:24:03 +00:00
|
|
|
@param idx An opaque handle. Make no own theories about it.
|
2006-09-22 17:01:26 +00:00
|
|
|
@param adr Takes the reply
|
|
|
|
@param adr_size Gives maximum size of reply including final 0
|
2006-10-14 12:24:03 +00:00
|
|
|
@param initialize 1 = start new,
|
|
|
|
0 = continue, use no other values for now
|
|
|
|
-1 = finish
|
2006-09-22 17:01:26 +00:00
|
|
|
@return 1 = reply is a valid address , 0 = no further address available
|
|
|
|
-1 = severe error (e.g. adr_size too small)
|
|
|
|
*/
|
2006-10-14 12:24:03 +00:00
|
|
|
int sg_give_next_adr(burn_drive_enumerator_t *idx,
|
|
|
|
char adr[], int adr_size, int initialize)
|
2006-09-22 13:30:32 +00:00
|
|
|
{
|
2006-10-14 12:24:03 +00:00
|
|
|
/* sg.h : typedef int burn_drive_enumerator_t; */
|
2006-09-22 17:01:26 +00:00
|
|
|
static int sg_limit = 32, ata_limit = 26;
|
|
|
|
int baseno = 0;
|
|
|
|
|
2006-10-14 12:24:03 +00:00
|
|
|
if (initialize == -1)
|
|
|
|
return 0;
|
|
|
|
|
2006-09-22 17:01:26 +00:00
|
|
|
if (initialize == 1)
|
|
|
|
*idx = -1;
|
|
|
|
(*idx)++;
|
|
|
|
if (*idx >= sg_limit)
|
|
|
|
goto next_ata;
|
|
|
|
if (adr_size < 10)
|
|
|
|
return -1;
|
|
|
|
sprintf(adr, "/dev/sg%d", *idx);
|
|
|
|
return 1;
|
|
|
|
next_ata:;
|
|
|
|
baseno += sg_limit;
|
|
|
|
if (*idx - baseno >= ata_limit)
|
|
|
|
goto next_nothing;
|
|
|
|
if (adr_size < 9)
|
|
|
|
return -1;
|
|
|
|
sprintf(adr, "/dev/hd%c", 'a' + (*idx - baseno));
|
|
|
|
return 1;
|
|
|
|
next_nothing:;
|
|
|
|
baseno += ata_limit;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-09-22 13:30:32 +00:00
|
|
|
|
2006-09-22 17:01:26 +00:00
|
|
|
int sg_is_enumerable_adr(char *adr)
|
|
|
|
{
|
|
|
|
char fname[4096];
|
|
|
|
int i, ret = 0, first = 1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ret= sg_give_next_adr(&i, fname, sizeof(fname), first);
|
|
|
|
if(ret <= 0)
|
|
|
|
break;
|
|
|
|
first = 0;
|
2006-09-22 13:30:32 +00:00
|
|
|
if (strcmp(adr, fname) == 0)
|
|
|
|
return 1;
|
2006-09-22 17:01:26 +00:00
|
|
|
|
2006-09-22 13:30:32 +00:00
|
|
|
}
|
2006-09-22 17:01:26 +00:00
|
|
|
return(0);
|
2006-09-22 13:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
/* ts A60926 */
|
|
|
|
int sg_release_siblings(int sibling_fds[], int *sibling_count)
|
2006-09-24 18:07:10 +00:00
|
|
|
{
|
2006-09-27 11:57:28 +00:00
|
|
|
int i;
|
|
|
|
char msg[81];
|
|
|
|
|
|
|
|
for(i= 0; i < *sibling_count; i++)
|
|
|
|
sg_close_drive_fd(NULL, -1, &(sibling_fds[i]), 0);
|
|
|
|
if(*sibling_count > 0) {
|
|
|
|
sprintf(msg, "Closed %d O_EXCL scsi siblings", *sibling_count);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020007,
|
|
|
|
LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH, msg, 0,0);
|
|
|
|
}
|
|
|
|
*sibling_count = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-09-24 18:07:10 +00:00
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
int sg_open_drive_fd(char *fname, int scan_mode)
|
|
|
|
{
|
|
|
|
int open_mode = O_RDWR, fd;
|
|
|
|
char msg[81];
|
|
|
|
|
2006-09-27 14:40:21 +00:00
|
|
|
/* ts A60813 - A60927
|
|
|
|
O_EXCL with devices is a non-POSIX feature
|
2006-09-27 11:57:28 +00:00
|
|
|
of Linux kernels. Possibly introduced 2002.
|
|
|
|
Mentioned in "The Linux SCSI Generic (sg) HOWTO" */
|
|
|
|
if(burn_sg_open_o_excl)
|
|
|
|
open_mode |= O_EXCL;
|
|
|
|
/* ts A60813
|
|
|
|
O_NONBLOCK was already hardcoded in ata_ but not in sg_.
|
|
|
|
There must be some reason for this. So O_NONBLOCK is
|
|
|
|
default mode for both now. Disable on own risk. */
|
|
|
|
if(burn_sg_open_o_nonblock)
|
|
|
|
open_mode |= O_NONBLOCK;
|
|
|
|
|
|
|
|
/* <<< debugging
|
|
|
|
fprintf(stderr,
|
|
|
|
"\nlibburn: experimental: o_excl= %d , o_nonblock= %d, abort_on_busy= %d\n",
|
|
|
|
burn_sg_open_o_excl,burn_sg_open_o_nonblock,burn_sg_open_abort_busy);
|
|
|
|
fprintf(stderr,
|
|
|
|
"libburn: experimental: O_EXCL= %d , O_NONBLOCK= %d\n",
|
|
|
|
!!(open_mode&O_EXCL),!!(open_mode&O_NONBLOCK));
|
|
|
|
*/
|
|
|
|
|
|
|
|
fd = open(fname, open_mode);
|
|
|
|
if (fd == -1) {
|
|
|
|
/* <<< debugging
|
2006-09-24 18:07:10 +00:00
|
|
|
fprintf(stderr,
|
2006-09-27 11:57:28 +00:00
|
|
|
"\nlibburn: experimental: fname= %s , errno= %d\n",
|
|
|
|
fname,errno);
|
|
|
|
*/
|
|
|
|
if (errno == EBUSY) {
|
|
|
|
sg_handle_busy_device(fname, errno);
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (scan_mode)
|
|
|
|
return -1;
|
|
|
|
sprintf(msg, "Failed to open device '%s'",fname);
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020005,
|
|
|
|
LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
msg, errno, 0);
|
|
|
|
return -1;
|
2006-09-24 18:07:10 +00:00
|
|
|
}
|
2006-09-27 11:57:28 +00:00
|
|
|
return fd;
|
|
|
|
}
|
2006-09-24 18:07:10 +00:00
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
int sg_open_scsi_siblings(char *path, int driveno,
|
|
|
|
int sibling_fds[], int *sibling_count,
|
|
|
|
int host_no, int channel_no, int id_no, int lun_no)
|
|
|
|
{
|
2006-10-05 14:21:34 +00:00
|
|
|
int tld, i, ret, fd, i_bus_no = -1;
|
2006-09-27 11:57:28 +00:00
|
|
|
int i_host_no = -1, i_channel_no = -1, i_target_no = -1, i_lun_no = -1;
|
|
|
|
char msg[161], fname[81];
|
|
|
|
|
|
|
|
static char tldev[][81]= {"/dev/sr%d", "/dev/scd%d", "/dev/st%d", ""};
|
|
|
|
|
|
|
|
if(host_no < 0 || id_no < 0 || channel_no < 0 || lun_no < 0)
|
|
|
|
return(2);
|
|
|
|
if(*sibling_count > 0)
|
|
|
|
sg_release_siblings(sibling_fds, sibling_count);
|
|
|
|
|
|
|
|
for (tld = 0; tldev[tld][0] != 0; tld++) {
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
sprintf(fname, tldev[tld], i);
|
2006-10-05 14:21:34 +00:00
|
|
|
ret = sg_obtain_scsi_adr(fname, &i_bus_no, &i_host_no,
|
2006-09-27 11:57:28 +00:00
|
|
|
&i_channel_no, &i_target_no, &i_lun_no);
|
|
|
|
if (ret <= 0)
|
|
|
|
continue;
|
|
|
|
if (i_host_no != host_no || i_channel_no != channel_no)
|
|
|
|
continue;
|
|
|
|
if (i_target_no != id_no || i_lun_no != lun_no)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fd = sg_open_drive_fd(fname, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (*sibling_count>=LIBBURN_SG_MAX_SIBLINGS) {
|
|
|
|
sprintf(msg, "Too many scsi siblings of '%s'",
|
|
|
|
path);
|
|
|
|
libdax_msgs_submit(libdax_messenger,
|
|
|
|
driveno, 0x00020006,
|
|
|
|
LIBDAX_MSGS_SEV_FATAL,
|
|
|
|
LIBDAX_MSGS_PRIO_HIGH, msg, 0, 0);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
sprintf(msg, "Opened O_EXCL scsi sibling '%s' of '%s'",
|
|
|
|
fname, path);
|
|
|
|
libdax_msgs_submit(libdax_messenger, driveno,
|
|
|
|
0x00020004,
|
|
|
|
LIBDAX_MSGS_SEV_NOTE, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
msg, 0, 0);
|
|
|
|
sibling_fds[*sibling_count] = fd;
|
|
|
|
(*sibling_count)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(1);
|
|
|
|
failed:;
|
|
|
|
sg_release_siblings(sibling_fds, sibling_count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ts A60926 */
|
|
|
|
int sg_close_drive(struct burn_drive *d)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!burn_drive_is_open(d))
|
|
|
|
return 0;
|
|
|
|
sg_release_siblings(d->sibling_fds, &(d->sibling_count));
|
|
|
|
ret = sg_close_drive_fd(d->devname, d->global_index, &(d->fd), 0);
|
|
|
|
return ret;
|
2006-09-24 18:07:10 +00:00
|
|
|
}
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
void ata_enumerate(void)
|
|
|
|
{
|
|
|
|
struct hd_driveid tm;
|
|
|
|
int i, fd;
|
|
|
|
char fname[10];
|
|
|
|
|
|
|
|
for (i = 0; i < 26; i++) {
|
|
|
|
sprintf(fname, "/dev/hd%c", 'a' + i);
|
2006-08-15 20:40:30 +00:00
|
|
|
/* ts A51221 */
|
|
|
|
if (burn_drive_is_banned(fname))
|
2006-09-27 14:40:21 +00:00
|
|
|
continue;
|
2006-09-27 11:57:28 +00:00
|
|
|
fd = sg_open_drive_fd(fname, 1);
|
|
|
|
if (fd == -1)
|
2006-09-27 14:40:21 +00:00
|
|
|
continue;
|
2006-09-27 11:57:28 +00:00
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
/* found a drive */
|
|
|
|
ioctl(fd, HDIO_GET_IDENTITY, &tm);
|
|
|
|
|
|
|
|
/* not atapi */
|
|
|
|
if (!(tm.config & 0x8000) || (tm.config & 0x4000)) {
|
2006-09-25 14:09:14 +00:00
|
|
|
sg_close_drive_fd(fname, -1, &fd, 0);
|
2006-09-27 14:40:21 +00:00
|
|
|
continue;
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if SG_IO fails on an atapi device, we should stop trying to
|
|
|
|
use hd* devices */
|
|
|
|
if (sgio_test(fd) == -1) {
|
2006-09-25 14:09:14 +00:00
|
|
|
sg_close_drive_fd(fname, -1, &fd, 0);
|
2006-08-15 20:37:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-09-25 14:09:14 +00:00
|
|
|
if (sg_close_drive_fd(fname, -1, &fd, 1) <= 0)
|
2006-09-27 14:40:21 +00:00
|
|
|
continue;
|
2006-10-05 14:21:34 +00:00
|
|
|
enumerate_common(fname, -1, -1, -1, -1, -1);
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sg_enumerate(void)
|
|
|
|
{
|
|
|
|
struct sg_scsi_id sid;
|
2006-09-27 11:57:28 +00:00
|
|
|
int i, fd, sibling_fds[LIBBURN_SG_MAX_SIBLINGS], sibling_count= 0, ret;
|
2006-10-05 14:21:34 +00:00
|
|
|
int bus_no = -1;
|
2006-08-15 20:37:04 +00:00
|
|
|
char fname[10];
|
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
sprintf(fname, "/dev/sg%d", i);
|
2006-08-15 20:40:30 +00:00
|
|
|
/* ts A51221 */
|
|
|
|
if (burn_drive_is_banned(fname))
|
2006-09-27 11:57:28 +00:00
|
|
|
continue;
|
2006-09-27 14:40:21 +00:00
|
|
|
/* ts A60927 */
|
2006-09-27 11:57:28 +00:00
|
|
|
fd = sg_open_drive_fd(fname, 1);
|
|
|
|
if (fd == -1)
|
2006-09-27 14:40:21 +00:00
|
|
|
continue;
|
2006-09-27 11:57:28 +00:00
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
/* found a drive */
|
|
|
|
ioctl(fd, SG_GET_SCSI_ID, &sid);
|
2006-10-05 14:21:34 +00:00
|
|
|
|
|
|
|
#ifdef SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
/* Hearsay A61005 */
|
|
|
|
if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &bus_no) == -1)
|
|
|
|
bus_no = -1;
|
|
|
|
#endif
|
|
|
|
|
2006-09-25 14:09:14 +00:00
|
|
|
if (sg_close_drive_fd(fname, -1, &fd,
|
|
|
|
sid.scsi_type == TYPE_ROM ) <= 0)
|
2006-09-27 11:57:28 +00:00
|
|
|
continue;
|
2006-08-15 20:37:04 +00:00
|
|
|
if (sid.scsi_type != TYPE_ROM)
|
2006-09-27 11:57:28 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* ts A60927 : trying to do locking with growisofs */
|
|
|
|
if(burn_sg_open_o_excl>1) {
|
|
|
|
ret = sg_open_scsi_siblings(
|
|
|
|
fname, -1, sibling_fds, &sibling_count,
|
|
|
|
sid.host_no, sid.channel,
|
|
|
|
sid.scsi_id, sid.lun);
|
|
|
|
if (ret<=0) {
|
|
|
|
sg_handle_busy_device(fname, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* the final occupation will be done in sg_grab() */
|
|
|
|
sg_release_siblings(sibling_fds, &sibling_count);
|
|
|
|
}
|
2006-10-05 14:21:34 +00:00
|
|
|
#ifdef SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
if(bus_no == -1)
|
|
|
|
bus_no = 1000 * (sid.host_no + 1) + sid.channel;
|
|
|
|
#else
|
|
|
|
bus_no = sid.host_no;
|
|
|
|
#endif
|
|
|
|
enumerate_common(fname, bus_no, sid.host_no, sid.channel,
|
|
|
|
sid.scsi_id, sid.lun);
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-05 14:21:34 +00:00
|
|
|
|
|
|
|
/* ts A60923 - A61005 : introduced new SCSI parameters */
|
|
|
|
static void enumerate_common(char *fname, int bus_no, int host_no,
|
|
|
|
int channel_no, int target_no, int lun_no)
|
2006-08-15 20:37:04 +00:00
|
|
|
{
|
2006-09-27 11:57:28 +00:00
|
|
|
int i;
|
2006-08-15 20:37:04 +00:00
|
|
|
struct burn_drive *t;
|
|
|
|
struct burn_drive out;
|
|
|
|
|
2006-09-23 11:36:39 +00:00
|
|
|
/* ts A60923 */
|
2006-10-05 14:21:34 +00:00
|
|
|
out.bus_no = bus_no;
|
2006-09-23 11:36:39 +00:00
|
|
|
out.host = host_no;
|
|
|
|
out.id = target_no;
|
|
|
|
out.channel = channel_no;
|
|
|
|
out.lun = lun_no;
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
out.devname = burn_strdup(fname);
|
|
|
|
out.fd = -1337;
|
2006-09-27 11:57:28 +00:00
|
|
|
out.sibling_count = 0;
|
|
|
|
for(i= 0; i<LIBBURN_SG_MAX_SIBLINGS; i++)
|
|
|
|
out.sibling_fds[i] = -1337;
|
2006-08-15 20:37:04 +00:00
|
|
|
|
2006-10-20 11:34:52 +00:00
|
|
|
/* ts A61020 */
|
|
|
|
out.start_lba= -2000000000;
|
|
|
|
out.end_lba= -2000000000;
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
out.grab = sg_grab;
|
|
|
|
out.release = sg_release;
|
|
|
|
out.issue_command = sg_issue_command;
|
|
|
|
out.getcaps = spc_getcaps;
|
|
|
|
out.released = 1;
|
|
|
|
out.status = BURN_DISC_UNREADY;
|
|
|
|
|
|
|
|
out.eject = sbc_eject;
|
|
|
|
out.load = sbc_load;
|
|
|
|
out.lock = spc_prevent;
|
|
|
|
out.unlock = spc_allow;
|
|
|
|
out.read_disc_info = spc_sense_write_params;
|
|
|
|
out.get_erase_progress = spc_get_erase_progress;
|
|
|
|
out.test_unit_ready = spc_test_unit_ready;
|
|
|
|
out.probe_write_modes = spc_probe_write_modes;
|
|
|
|
out.read_toc = mmc_read_toc;
|
|
|
|
out.write = mmc_write;
|
|
|
|
out.erase = mmc_erase;
|
|
|
|
out.read_sectors = mmc_read_sectors;
|
|
|
|
out.perform_opc = mmc_perform_opc;
|
|
|
|
out.set_speed = mmc_set_speed;
|
|
|
|
out.send_parameters = spc_select_error_params;
|
|
|
|
out.send_write_parameters = spc_select_write_params;
|
|
|
|
out.send_cue_sheet = mmc_send_cue_sheet;
|
|
|
|
out.sync_cache = mmc_sync_cache;
|
|
|
|
out.get_nwa = mmc_get_nwa;
|
|
|
|
out.close_disc = mmc_close_disc;
|
|
|
|
out.close_session = mmc_close_session;
|
2006-10-13 10:22:21 +00:00
|
|
|
out.idata = malloc(sizeof(struct burn_scsi_inquiry_data));
|
2006-08-15 20:37:04 +00:00
|
|
|
out.idata->valid = 0;
|
|
|
|
out.mdata = malloc(sizeof(struct scsi_mode_data));
|
|
|
|
out.mdata->valid = 0;
|
2006-10-07 12:29:22 +00:00
|
|
|
|
2006-10-07 17:05:45 +00:00
|
|
|
/* ts A61007 : obsolete Assert in drive_getcaps() */
|
2006-10-07 12:29:22 +00:00
|
|
|
if(out.idata == NULL || out.mdata == NULL) {
|
|
|
|
libdax_msgs_submit(libdax_messenger, -1, 0x00020108,
|
|
|
|
LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
"Could not allocate new drive object", 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
memset(&out.params, 0, sizeof(struct params));
|
|
|
|
t = burn_drive_register(&out);
|
|
|
|
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("enumerate_common : -------- doing grab");
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
/* try to get the drive info */
|
|
|
|
if (sg_grab(t)) {
|
|
|
|
burn_print(2, "getting drive info\n");
|
|
|
|
t->getcaps(t);
|
|
|
|
t->unlock(t);
|
|
|
|
t->released = 1;
|
|
|
|
} else {
|
|
|
|
burn_print(2, "unable to grab new located drive\n");
|
2006-09-27 11:57:28 +00:00
|
|
|
burn_drive_unregister(t);
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("enumerate_common : ----- would release ");
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
we use the sg reference count to decide whether we can use the
|
|
|
|
drive or not.
|
|
|
|
if refcount is not one, drive is open somewhere else.
|
2006-08-20 19:03:21 +00:00
|
|
|
|
|
|
|
ts A60813: this test is too late. O_EXCL is the stronger solution.
|
2006-09-23 11:36:39 +00:00
|
|
|
After all the test was disabled already in icculus.org/burn CVS.
|
2006-08-15 20:37:04 +00:00
|
|
|
*/
|
|
|
|
int sg_grab(struct burn_drive *d)
|
|
|
|
{
|
2006-09-27 11:57:28 +00:00
|
|
|
int fd, count, os_errno= 0, ret;
|
2006-08-15 20:37:04 +00:00
|
|
|
|
2006-08-20 19:03:21 +00:00
|
|
|
/* ts A60813 */
|
|
|
|
int open_mode = O_RDWR;
|
|
|
|
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("sg_grab");
|
|
|
|
|
|
|
|
|
2006-09-27 14:40:21 +00:00
|
|
|
/* ts A60813 - A60927
|
|
|
|
O_EXCL with devices is a non-POSIX feature
|
2006-08-20 19:03:21 +00:00
|
|
|
of Linux kernels. Possibly introduced 2002.
|
2006-09-27 14:40:21 +00:00
|
|
|
Mentioned in "The Linux SCSI Generic (sg) HOWTO".
|
|
|
|
*/
|
2006-08-20 19:03:21 +00:00
|
|
|
if(burn_sg_open_o_excl)
|
|
|
|
open_mode |= O_EXCL;
|
|
|
|
|
|
|
|
/* ts A60813
|
|
|
|
O_NONBLOCK was hardcoded here. So it should stay default mode. */
|
|
|
|
if(burn_sg_open_o_nonblock)
|
|
|
|
open_mode |= O_NONBLOCK;
|
|
|
|
|
|
|
|
/* ts A60813
|
|
|
|
After enumeration the drive fd is probably still open.
|
|
|
|
-1337 is the initial value of burn_drive.fd and the value after
|
|
|
|
relase of drive. Unclear why not the official error return
|
|
|
|
value -1 of open(2) war used. */
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60822: was if(d->fd == -1337) { */
|
|
|
|
if(! burn_drive_is_open(d)) {
|
|
|
|
|
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("sg_grab ----------- opening");
|
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
/* ts A60926 */
|
|
|
|
if(burn_sg_open_o_excl>1) {
|
|
|
|
fd = -1;
|
|
|
|
ret = sg_open_scsi_siblings(d->devname,
|
|
|
|
d->global_index,d->sibling_fds,
|
|
|
|
&(d->sibling_count),
|
|
|
|
d->host, d->channel, d->id, d->lun);
|
|
|
|
if(ret <= 0)
|
|
|
|
goto drive_is_in_use;
|
|
|
|
}
|
|
|
|
|
2006-08-20 19:03:21 +00:00
|
|
|
fd = open(d->devname, open_mode);
|
2006-09-25 14:09:14 +00:00
|
|
|
os_errno = errno;
|
2006-08-27 15:16:33 +00:00
|
|
|
} else
|
2006-08-20 19:03:21 +00:00
|
|
|
fd= d->fd;
|
|
|
|
|
2006-10-07 17:05:45 +00:00
|
|
|
/* ts A61007 : this is redundant */
|
|
|
|
/* a ssert(fd != -1337); */
|
|
|
|
|
|
|
|
if (fd >= 0) {
|
2006-08-20 19:03:21 +00:00
|
|
|
|
|
|
|
/* ts A60814:
|
|
|
|
according to my experiments this test would work now ! */
|
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
/* ts A60926 : this was disabled */
|
|
|
|
/* Tests with growisofs on kernel 2.4.21 yielded that this
|
|
|
|
does not help against blocking on busy drives.
|
|
|
|
*/
|
|
|
|
/* <<< the old dummy */
|
|
|
|
/* er = ioctl(fd, SG_GET_ACCESS_COUNT, &count);*/
|
2006-08-15 20:37:04 +00:00
|
|
|
count = 1;
|
2006-09-27 11:57:28 +00:00
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
if (1 == count) {
|
|
|
|
d->fd = fd;
|
|
|
|
fcntl(fd, F_SETOWN, getpid());
|
|
|
|
d->released = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-09-27 11:57:28 +00:00
|
|
|
|
|
|
|
drive_is_in_use:;
|
2006-09-25 14:09:14 +00:00
|
|
|
libdax_msgs_submit(libdax_messenger, d->global_index,
|
|
|
|
0x00020003,
|
2006-10-13 11:46:07 +00:00
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
|
2006-09-25 14:09:14 +00:00
|
|
|
"Could not grab drive - already in use", 0, 0);
|
2006-09-27 11:57:28 +00:00
|
|
|
sg_close_drive(d);
|
2006-09-25 14:09:14 +00:00
|
|
|
d->fd = -1337;
|
2006-08-15 20:37:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-09-25 14:09:14 +00:00
|
|
|
libdax_msgs_submit(libdax_messenger, d->global_index, 0x00020003,
|
2006-10-13 11:46:07 +00:00
|
|
|
LIBDAX_MSGS_SEV_SORRY, LIBDAX_MSGS_PRIO_HIGH,
|
2006-09-25 14:09:14 +00:00
|
|
|
"Could not grab drive", os_errno, 0);
|
2006-08-15 20:37:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
non zero return means you still have the drive and it's not
|
|
|
|
in a state to be released? (is that even possible?)
|
|
|
|
*/
|
|
|
|
|
|
|
|
int sg_release(struct burn_drive *d)
|
|
|
|
{
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("sg_release");
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
if (d->fd < 1) {
|
|
|
|
burn_print(1, "release an ungrabbed drive. die\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-27 15:16:33 +00:00
|
|
|
|
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
mmc_function_spy("sg_release ----------- closing");
|
|
|
|
|
2006-09-27 11:57:28 +00:00
|
|
|
sg_close_drive(d);
|
2006-08-15 20:37:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sg_issue_command(struct burn_drive *d, struct command *c)
|
|
|
|
{
|
2006-10-07 17:05:45 +00:00
|
|
|
int done = 0, no_c_page = 0;
|
2006-08-15 20:37:04 +00:00
|
|
|
int err;
|
|
|
|
sg_io_hdr_t s;
|
|
|
|
|
2006-08-27 15:16:33 +00:00
|
|
|
/* ts A60821
|
|
|
|
<<< debug: for tracing calls which might use open drive fds */
|
|
|
|
char buf[161];
|
|
|
|
sprintf(buf,"sg_issue_command d->fd= %d d->released= %d\n",
|
|
|
|
d->fd,d->released);
|
|
|
|
mmc_function_spy(buf);
|
|
|
|
|
2006-10-10 17:55:34 +00:00
|
|
|
/* ts A61010 : with no fd there is no chance to send an ioctl */
|
|
|
|
if (d->fd < 0) {
|
|
|
|
c->error = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-27 15:16:33 +00:00
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
c->error = 0;
|
|
|
|
memset(&s, 0, sizeof(sg_io_hdr_t));
|
|
|
|
|
|
|
|
s.interface_id = 'S';
|
|
|
|
|
|
|
|
if (c->dir == TO_DRIVE)
|
|
|
|
s.dxfer_direction = SG_DXFER_TO_DEV;
|
|
|
|
else if (c->dir == FROM_DRIVE)
|
|
|
|
s.dxfer_direction = SG_DXFER_FROM_DEV;
|
|
|
|
else if (c->dir == NO_TRANSFER) {
|
|
|
|
s.dxfer_direction = SG_DXFER_NONE;
|
2006-10-07 17:05:45 +00:00
|
|
|
|
|
|
|
/* ts A61007 */
|
|
|
|
/* a ssert(!c->page); */
|
|
|
|
no_c_page = 1;
|
2006-08-15 20:37:04 +00:00
|
|
|
}
|
|
|
|
s.cmd_len = c->oplen;
|
|
|
|
s.cmdp = c->opcode;
|
|
|
|
s.mx_sb_len = 32;
|
|
|
|
s.sbp = c->sense;
|
|
|
|
memset(c->sense, 0, sizeof(c->sense));
|
|
|
|
s.timeout = 200000;
|
2006-10-07 17:05:45 +00:00
|
|
|
if (c->page && !no_c_page) {
|
2006-08-15 20:37:04 +00:00
|
|
|
s.dxferp = c->page->data;
|
|
|
|
if (c->dir == FROM_DRIVE) {
|
|
|
|
s.dxfer_len = BUFFER_SIZE;
|
|
|
|
/* touch page so we can use valgrind */
|
|
|
|
memset(c->page->data, 0, BUFFER_SIZE);
|
|
|
|
} else {
|
2006-10-10 17:55:34 +00:00
|
|
|
|
|
|
|
/* ts A61010 */
|
|
|
|
/* a ssert(c->page->bytes > 0); */
|
|
|
|
if (c->page->bytes <= 0) {
|
|
|
|
c->error = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
s.dxfer_len = c->page->bytes;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.dxferp = NULL;
|
|
|
|
s.dxfer_len = 0;
|
|
|
|
}
|
|
|
|
s.usr_ptr = c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
err = ioctl(d->fd, SG_IO, &s);
|
2006-10-10 17:55:34 +00:00
|
|
|
|
|
|
|
/* ts A61010 */
|
|
|
|
/* a ssert(err != -1); */
|
|
|
|
if (err == -1) {
|
|
|
|
libdax_msgs_submit(libdax_messenger,
|
|
|
|
d->global_index, 0x0002010c,
|
|
|
|
LIBDAX_MSGS_SEV_FATAL, LIBDAX_MSGS_PRIO_HIGH,
|
|
|
|
"Failed to transfer command to drive",
|
|
|
|
errno, 0);
|
|
|
|
sg_close_drive(d);
|
|
|
|
d->released = 1;
|
|
|
|
d->busy = BURN_DRIVE_IDLE;
|
|
|
|
c->error = 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-15 20:37:04 +00:00
|
|
|
if (s.sb_len_wr) {
|
|
|
|
if (!c->retry) {
|
|
|
|
c->error = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
switch (scsi_error(d, s.sbp, s.sb_len_wr)) {
|
|
|
|
case RETRY:
|
|
|
|
done = 0;
|
|
|
|
break;
|
|
|
|
case FAIL:
|
|
|
|
done = 1;
|
|
|
|
c->error = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
} while (!done);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum response scsi_error(struct burn_drive *d, unsigned char *sense,
|
|
|
|
int senselen)
|
|
|
|
{
|
|
|
|
int key, asc, ascq;
|
|
|
|
|
|
|
|
senselen = senselen;
|
|
|
|
key = sense[2];
|
|
|
|
asc = sense[12];
|
|
|
|
ascq = sense[13];
|
|
|
|
|
|
|
|
burn_print(12, "CONDITION: 0x%x 0x%x 0x%x on %s %s\n",
|
|
|
|
key, asc, ascq, d->idata->vendor, d->idata->product);
|
|
|
|
|
|
|
|
switch (asc) {
|
|
|
|
case 0:
|
|
|
|
burn_print(12, "NO ERROR!\n");
|
|
|
|
return RETRY;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
burn_print(1, "not ready\n");
|
|
|
|
return RETRY;
|
|
|
|
case 4:
|
|
|
|
burn_print(1,
|
|
|
|
"logical unit is in the process of becoming ready\n");
|
|
|
|
return RETRY;
|
|
|
|
case 0x20:
|
|
|
|
if (key == 5)
|
|
|
|
burn_print(1, "bad opcode\n");
|
|
|
|
return FAIL;
|
|
|
|
case 0x21:
|
|
|
|
burn_print(1, "invalid address or something\n");
|
|
|
|
return FAIL;
|
|
|
|
case 0x24:
|
|
|
|
if (key == 5)
|
|
|
|
burn_print(1, "invalid field in cdb\n");
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
return FAIL;
|
|
|
|
case 0x26:
|
|
|
|
if ( key == 5 )
|
|
|
|
burn_print( 1, "invalid field in parameter list\n" );
|
|
|
|
return FAIL;
|
|
|
|
case 0x28:
|
|
|
|
if (key == 6)
|
|
|
|
burn_print(1,
|
|
|
|
"Not ready to ready change, medium may have changed\n");
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
return RETRY;
|
|
|
|
case 0x3A:
|
|
|
|
burn_print(12, "Medium not present in %s %s\n",
|
|
|
|
d->idata->vendor, d->idata->product);
|
|
|
|
|
|
|
|
d->status = BURN_DISC_EMPTY;
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
burn_print(1, "unknown failure\n");
|
|
|
|
burn_print(1, "key:0x%x, asc:0x%x, ascq:0x%x\n", key, asc, ascq);
|
|
|
|
return FAIL;
|
|
|
|
}
|
2006-09-22 19:29:11 +00:00
|
|
|
|
|
|
|
/* ts A60922 */
|
|
|
|
/** Try to obtain SCSI address parameters.
|
|
|
|
@return 1 is success , 0 is failure
|
|
|
|
*/
|
2006-10-05 14:21:34 +00:00
|
|
|
int sg_obtain_scsi_adr(char *path, int *bus_no, int *host_no, int *channel_no,
|
2006-09-22 19:29:11 +00:00
|
|
|
int *target_no, int *lun_no)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
struct my_scsi_idlun {
|
|
|
|
int x;
|
|
|
|
int host_unique_id;
|
|
|
|
};
|
|
|
|
struct my_scsi_idlun idlun;
|
|
|
|
|
2006-09-27 13:41:19 +00:00
|
|
|
if (strncmp(path, "/dev/hd", 7) == 0
|
|
|
|
&& path[7] >= 'a' && path[7] <= 'z' && path[8] == 0)
|
|
|
|
return 0; /* on RIP 14 all hdx return SCSI adr 0,0,0,0 */
|
|
|
|
|
2006-09-22 19:29:11 +00:00
|
|
|
fd = open(path, O_RDONLY | O_NONBLOCK);
|
|
|
|
if(fd < 0)
|
|
|
|
return 0;
|
|
|
|
|
2006-10-05 14:21:34 +00:00
|
|
|
#ifdef SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
/* Hearsay A61005 */
|
|
|
|
if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, bus_no) == -1)
|
|
|
|
*bus_no = -1;
|
|
|
|
#endif
|
|
|
|
|
2006-09-22 19:29:11 +00:00
|
|
|
/* http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/scsi_g_idlun.html */
|
2006-10-05 14:21:34 +00:00
|
|
|
ret = ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun);
|
2006-09-22 19:29:11 +00:00
|
|
|
|
2006-09-25 14:09:14 +00:00
|
|
|
sg_close_drive_fd(path, -1, &fd, 0);
|
|
|
|
if (ret == -1)
|
2006-09-22 19:29:11 +00:00
|
|
|
return(0);
|
|
|
|
*host_no= (idlun.x>>24)&255;
|
|
|
|
*channel_no= (idlun.x>>16)&255;
|
|
|
|
*target_no= (idlun.x)&255;
|
|
|
|
*lun_no= (idlun.x>>8)&255;
|
2006-10-05 14:21:34 +00:00
|
|
|
#ifdef SCSI_IOCTL_GET_BUS_NUMBER
|
|
|
|
if(*bus_no == -1)
|
|
|
|
*bus_no = 1000 * (*host_no + 1) + *channel_no;
|
|
|
|
#else
|
|
|
|
*bus_no= *host_no;
|
|
|
|
#endif
|
2006-09-22 19:29:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|