File: scsi-freebsd.c

package info (click to toggle)
dvdisaster 0.72.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 20,088 kB
  • ctags: 6,526
  • sloc: ansic: 29,301; php: 3,324; sh: 370; xml: 296; makefile: 73; cs: 33
file content (215 lines) | stat: -rw-r--r-- 5,351 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*  dvdisaster: Additional error correction for optical media.
 *  Copyright (C) 2004-2012 Carsten Gnoerlich.
 *  Project home page: http://www.dvdisaster.com
 *  Email: carsten@dvdisaster.com  -or-  cgnoerlich@fsfe.org
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
 *  or direct your browser at http://www.gnu.org.
 */

#include "dvdisaster.h"

#include "scsi-layer.h"
#include "udf.h"

#ifdef SYS_FREEBSD

/* SCSI wrappers for FreeBSD are still work in progress. */

char* DefaultDevice()
{  DeviceHandle *dh;
   GDir *dir;
   const char* dev;
   int dev_type;

   /*** Look for suitable devices */

   dir = g_dir_open("/dev", 0, NULL);

   if(!dir)
   {  PrintLog(_("Can not access /dev for devices\n"
		  "No drives will be pre-selected.\n"));

      return g_strdup("no_drives");
   }

   dh = g_malloc(sizeof(DeviceHandle));

   /* iterate through /dev/pass<n> */

   while((dev = g_dir_read_name(dir)))
   { 
     if((strlen(dev) == 5 && !strncmp(dev,"pass",4)))
     { char buf[80];

       sprintf(buf,"/dev/%s", dev); 

       memset(dh, 0, sizeof(DeviceHandle));

       /* see if we can open it */

       dh->camdev = cam_open_pass(buf, O_RDWR, NULL);
       dh->device = buf;

       if(!dh->camdev)   /* device not even present */
	 continue;
       
       dh->ccb = cam_getccb(dh->camdev);
       if(!dh->ccb)
	 continue;

       /* make sure its a CDROM type drive */

       dev_type = InquireDevice(dh, 1);

       cam_freeccb(dh->ccb);
       cam_close_device(dh->camdev);

       if(dev_type != 5)  /* not a CD/DVD ROM */
	 continue;

       /* remember it in our device list */

       g_ptr_array_add(Closure->deviceNodes, g_strdup(buf));
       sprintf(buf, "%s (/dev/%s)", dh->devinfo, dev);
       g_ptr_array_add(Closure->deviceNames, g_strdup(buf));
     }
   }

   g_dir_close(dir);
   g_free(dh);

   if(Closure->deviceNodes->len)
     return g_strdup(g_ptr_array_index(Closure->deviceNodes, 0));
   else
   {  PrintLog(_("No CD/DVD drives found in /dev.\n"
		  "No drives will be pre-selected.\n"));

      return g_strdup("no_drives");
   }
}

DeviceHandle* OpenDevice(char *device)
{  DeviceHandle *dh;

   dh = g_malloc0(sizeof(DeviceHandle));

   /* Make sure we do not overrun any memory due
      to differently sized sense structures */

   dh->senseSize = sizeof(Sense);
   if(dh->senseSize > sizeof(struct scsi_sense_data))
      dh->senseSize = sizeof(struct scsi_sense_data);

   dh->camdev = cam_open_pass(device, O_RDWR, NULL);

   if(!dh->camdev)
   {  g_free(dh);
      Stop(_("Could not open %s: %s"),device, strerror(errno));
      return NULL;
   }

   dh->ccb = cam_getccb(dh->camdev);

   if(!dh->ccb)
   {  cam_close_device(dh->camdev);
      g_free(dh);
      Stop("Could not allocate ccb for %s", device);
      return NULL;
   }

   dh->device = g_strdup(device);

   return dh;
}

void CloseDevice(DeviceHandle *dh)
{ 
  if(dh->canReadDefective)
    SetRawMode(dh, MODE_PAGE_UNSET);

  if(dh->rawBuffer)
     FreeRawBuffer(dh->rawBuffer);

  if(dh->ccb)
    cam_freeccb(dh->ccb);
  if(dh->camdev)
    cam_close_device(dh->camdev);
  if(dh->device)
    g_free(dh->device);
  if(dh->rs02Header)
    g_free(dh->rs02Header);
  if(dh->typeDescr) 
    g_free(dh->typeDescr);
  if(dh->mediumDescr) 
    g_free(dh->mediumDescr);
  if(dh->isoInfo)
    FreeIsoInfo(dh->isoInfo);
  if(dh->defects)
    FreeBitmap(dh->defects);
  g_free(dh);
}

int SendPacket(DeviceHandle *dh, unsigned char *cmd, int cdb_size, unsigned char *buf, int size, Sense *sense, int data_mode)
{  union ccb *ccb = dh->ccb;
   u_int32_t flags = 0;
   u_int8_t status;

   bzero(&(&ccb->ccb_h)[1],
	 sizeof(struct ccb_scsiio) - sizeof(struct ccb_hdr));

   switch(data_mode)
   {  case DATA_READ:
        flags = CAM_DIR_IN;
	break;
      case DATA_WRITE:
	flags = CAM_DIR_OUT;
	break;
      case DATA_NONE:
	flags = CAM_DIR_NONE;
	break;
      default:
	Stop("illegal data_mode: %d", data_mode);
   }

   cam_fill_csio(&ccb->csio, 1, NULL, flags, CAM_TAG_ACTION_NONE,//MSG_SIMPLE_Q_TAG,
		 buf, size, sizeof(struct scsi_sense_data), cdb_size, 
		 120*1000);  /* 120 secs timeout */

   memcpy(ccb->csio.cdb_io.cdb_bytes, cmd, cdb_size);
		 
   /* Send ccb */

   if(cam_send_ccb(dh->camdev, ccb) < 0) 
   {  printf("cam_send failed\n");
      cam_error_print(dh->camdev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
      return -1;
   }

   /* Extract sense data */

   memcpy(sense, &(ccb->csio.sense_data), dh->senseSize);

   if((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
     return 0;

   /* See what went wrong (has still to be done; see scsi-win32.c) */

   status = ccb->csio.scsi_status;

   return -1;
}

#endif /* SYS_FREEBSD */