blob: c87be72de818ec4b2433d13609b8330d686688d2 [file] [log] [blame]
Dave Chapman1eca02d2009-08-04 20:32:30 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Dominik Riebeling7e0fe362009-11-06 23:20:51 +00008 * $Id$
Dave Chapman1eca02d2009-08-04 20:32:30 +00009 *
10 * Copyright (C) 2009 Dave Chapman
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 *
21 * Based on the getCapsUsingSCSIPassThrough() function from "cddrv.cpp":
22 * - http://www.farmanager.com/svn/trunk/unicode_far/cddrv.cpp
23 *
24 * Copyright (c) 1996 Eugene Roshal
25 * Copyright (c) 2000 Far Group
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. The name of the authors may not be used to endorse or promote products
37 * derived from this software without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
40 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 *
50 ****************************************************************************/
51
52#include <windows.h>
53#include <stddef.h>
54#include <stdio.h>
55#include <ddk/ntddscsi.h>
56
57#include "ipodio.h"
58
59typedef struct _SCSI_PASS_THROUGH_WITH_BUFFERS {
60 SCSI_PASS_THROUGH Spt;
61 ULONG Filler; /* realign buffers to double word boundary */
62 UCHAR SenseBuf[32];
63 UCHAR DataBuf[512];
64} SCSI_PASS_THROUGH_WITH_BUFFERS, *PSCSI_PASS_THROUGH_WITH_BUFFERS;
65
66int ipod_scsi_inquiry(struct ipod_t* ipod, int page_code,
67 unsigned char* buf, int bufsize)
68{
69 SCSI_PASS_THROUGH_WITH_BUFFERS sptwb;
70 ULONG length;
71 DWORD returned;
72 BOOL status;
73
74 if (bufsize > 255) {
75 fprintf(stderr,"[ERR] Invalid bufsize in ipod_scsi_inquiry\n");
76 return -1;
77 }
78
79 memset(&sptwb, 0, sizeof(sptwb));
80
81 sptwb.Spt.Length = sizeof(SCSI_PASS_THROUGH);
82 sptwb.Spt.PathId = 0;
83 sptwb.Spt.TargetId = 1;
84 sptwb.Spt.Lun = 0;
85 sptwb.Spt.CdbLength = 6;
86 sptwb.Spt.SenseInfoLength = 32; /* sbuf size */;
87 sptwb.Spt.DataIn = SCSI_IOCTL_DATA_IN;
88 sptwb.Spt.DataTransferLength = bufsize;
89 sptwb.Spt.TimeOutValue = 2; /* 2 seconds */
90 sptwb.Spt.DataBufferOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, DataBuf);
91 sptwb.Spt.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, SenseBuf);
92 length = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, DataBuf) +
93 sptwb.Spt.DataTransferLength;
94
95 /* Set cdb info */
96 sptwb.Spt.Cdb[0] = 0x12; /* SCSI Inquiry */
97 sptwb.Spt.Cdb[1] = 1;
98 sptwb.Spt.Cdb[2] = page_code;
99 sptwb.Spt.Cdb[3] = 0;
100 sptwb.Spt.Cdb[4] = bufsize;
101 sptwb.Spt.Cdb[5] = 0;
102
103 status = DeviceIoControl(ipod->dh,
104 IOCTL_SCSI_PASS_THROUGH,
105 &sptwb,
106 sizeof(SCSI_PASS_THROUGH),
107 &sptwb,
108 length,
109 &returned,
110 FALSE);
111
112 if (status) {
Dominik Riebeling05925572009-08-07 22:58:20 +0000113 /* W32 sometimes returns more bytes with additional garbage.
114 * Make sure to not copy that garbage. */
115 memcpy(buf, sptwb.DataBuf,
116 (DWORD)bufsize >= returned ? returned : (DWORD)bufsize);
Dave Chapman1eca02d2009-08-04 20:32:30 +0000117 return 0;
118 } else {
119 return -1;
120 }
121}