1
/***************************************************************************
4
* Wed Oct 18 14:39:28 2008
5
* Copyright 2008 Lin Ma
7
****************************************************************************/
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program 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
18
* GNU Library General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
32
#include <sys/types.h>
37
#include <sys/scsi/scsi.h>
38
#include <sys/scsi/impl/uscsi.h>
40
#include "scsi-command.h"
41
#include "burn-debug.h"
42
#include "scsi-utils.h"
43
#include "scsi-error.h"
44
#include "scsi-sense-data.h"
46
struct _BraseroDeviceHandle {
50
struct _BraseroScsiCmd {
51
uchar cmd [BRASERO_SCSI_CMD_MAX_LEN];
52
BraseroDeviceHandle *handle;
54
const BraseroScsiCmdInfo *info;
56
typedef struct _BraseroScsiCmd BraseroScsiCmd;
58
#define BRASERO_SCSI_CMD_OPCODE_OFF 0
59
#define BRASERO_SCSI_CMD_SET_OPCODE(command) (command->cmd [BRASERO_SCSI_CMD_OPCODE_OFF] = command->info->opcode)
61
#define OPEN_FLAGS O_RDONLY /*|O_EXCL */|O_NONBLOCK
64
* This is to send a command
67
brasero_scsi_command_issue_sync (gpointer command,
70
BraseroScsiErrCode *error)
72
uchar sense_buffer [BRASERO_SENSE_DATA_SIZE];
73
struct uscsi_cmd transport;
74
BraseroScsiResult res;
78
memset (&sense_buffer, 0, BRASERO_SENSE_DATA_SIZE);
79
memset (&transport, 0, sizeof (struct uscsi_cmd));
83
if (cmd->info->direction & BRASERO_SCSI_READ)
84
transport.uscsi_flags = USCSI_READ;
85
else if (cmd->info->direction & BRASERO_SCSI_WRITE)
86
transport.uscsi_flags = USCSI_WRITE;
88
transport.uscsi_cdb = (caddr_t) cmd->cmd;
89
g_debug("cmd: %s\n", transport.uscsi_cdb);
90
transport.uscsi_cdblen = (uchar_t) cmd->info->size;
91
transport.uscsi_bufaddr = (caddr_t) buffer;
92
transport.uscsi_buflen = (size_t) size;
93
transport.uscsi_timeout = timeout;
95
/* where to output the scsi sense buffer */
96
transport.uscsi_flags |= USCSI_RQENABLE;
97
transport.uscsi_rqbuf = sense_buffer;
98
transport.uscsi_rqlen = BRASERO_SENSE_DATA_SIZE;
100
/* NOTE only for TEST UNIT READY, REQUEST/MODE SENSE, INQUIRY,
101
* READ CAPACITY, READ BUFFER, READ and LOG SENSE are allowed with it */
102
res = ioctl (cmd->handle->fd, USCSICMD, &transport);
104
BRASERO_SCSI_SET_ERRCODE (error, BRASERO_SCSI_ERRNO);
105
g_debug("ioctl ERR: %s\n", g_strerror(errno));
106
return BRASERO_SCSI_FAILURE;
109
if ((transport.uscsi_status & STATUS_MASK) == STATUS_GOOD)
110
return BRASERO_SCSI_OK;
112
if ((transport.uscsi_rqstatus & STATUS_MASK == STATUS_CHECK)
113
&& transport.uscsi_rqlen)
114
return brasero_sense_data_process (sense_buffer, error);
116
return BRASERO_SCSI_FAILURE;
120
brasero_scsi_command_new (const BraseroScsiCmdInfo *info,
121
BraseroDeviceHandle *handle)
125
/* make sure we can set the flags of the descriptor */
127
/* allocate the command */
128
cmd = g_new0 (BraseroScsiCmd, 1);
130
cmd->handle = handle;
132
BRASERO_SCSI_CMD_SET_OPCODE (cmd);
137
brasero_scsi_command_free (gpointer cmd)
140
return BRASERO_SCSI_OK;
144
* This is to open a device
147
BraseroDeviceHandle *
148
brasero_device_handle_open (const gchar *path,
149
BraseroScsiErrCode *code)
152
BraseroDeviceHandle *handle;
153
const gchar *blockdisk = "/dev/dsk/";
154
gchar *rawdisk = NULL;
156
fd = open (path, OPEN_FLAGS);
159
|| errno == EWOULDBLOCK
161
*code = BRASERO_SCSI_NOT_READY;
163
*code = BRASERO_SCSI_ERRNO;
165
g_debug("open ERR: %s\n", g_strerror(errno));
169
handle = g_new (BraseroDeviceHandle, 1);
176
brasero_device_handle_close (BraseroDeviceHandle *handle)