summaryrefslogtreecommitdiff
path: root/backend/niash_xfer.c
blob: a3d0ea2571e96adeeec8b6b738e62eafd8c47ced (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
  Copyright (C) 2001 Bertrik Sikken (bertrik@zonnet.nl)

  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.

  $Id$
*/

/*
        Provides a simple interface to read and write data from the scanner,
        without any knowledge whether it's a parallel or USB scanner
*/

#include <stdio.h>              /* printf */
#include <errno.h>              /* better error reports */
#include <string.h>             /* better error reports */

#include "niash_xfer.h"

#include "../include/sane/sanei_usb.h"

/* list of supported models */
STATIC TScannerModel ScannerModels[] = {
  {"Hewlett-Packard", "ScanJet 3300C", 0x3F0, 0x205, eHp3300c}
  ,
  {"Hewlett-Packard", "ScanJet 3400C", 0x3F0, 0x405, eHp3400c}
  ,
  {"Hewlett-Packard", "ScanJet 4300C", 0x3F0, 0x305, eHp4300c}
  ,
  {"Silitek Corp.", "HP ScanJet 4300c", 0x47b, 0x1002, eHp3400c}
  ,
  {"Agfa", "Snapscan Touch", 0x6BD, 0x100, eAgfaTouch}
  ,
  {"Trust", "Office Scanner USB 19200", 0x47b, 0x1000, eAgfaTouch}
  ,
/* last entry all zeros */
  {0, 0, 0, 0, 0}
};

static TFnReportDevice *_pfnReportDevice;
static TScannerModel *_pModel;

/*
  MatchUsbDevice
  ==============
        Matches a given USB vendor and product id against a list of
        supported scanners.

  IN  iVendor   USB vendor ID
          iProduct  USB product ID
  OUT *ppModel  Pointer to TScannerModel structure

  Returns TRUE if a matching USB scanner was found
*/
STATIC SANE_Bool
MatchUsbDevice (int iVendor, int iProduct, TScannerModel ** ppModel)
{
  TScannerModel *pModels = ScannerModels;

  DBG (DBG_MSG, "Matching USB device 0x%04X-0x%04X ... ", iVendor, iProduct);
  while (pModels->pszName != NULL)
    {
      if ((pModels->iVendor == iVendor) && (pModels->iProduct == iProduct))
        {
          DBG (DBG_MSG, "found %s %s\n", pModels->pszVendor,
               pModels->pszName);
          *ppModel = pModels;
          return SANE_TRUE;
        }
      /* next model to match */
      pModels++;
    }
  DBG (DBG_MSG, "nothing found\n");
  return SANE_FALSE;
}

/************************************************************************
  Public functions for the SANE compilation
************************************************************************/


/* callback for sanei_usb_attach_matching_devices */
static SANE_Status
_AttachUsb (SANE_String_Const devname)
{
  DBG (DBG_MSG, "_AttachUsb: found %s\n", devname);

  _pfnReportDevice (_pModel, (const char *) devname);

  return SANE_STATUS_GOOD;
}


/*
  NiashXferInit
  ===============
        Initialises all registered data transfer modules, which causes
        them to report any devices found through the pfnReport callback.

  IN  pfnReport Function to call to report a transfer device
*/
static void
NiashXferInit (TFnReportDevice * pfnReport)
{
  TScannerModel *pModels = ScannerModels;

  sanei_usb_init ();
  _pfnReportDevice = pfnReport;

  /* loop over all scanner models */
  while (pModels->pszName != NULL)
    {
      DBG (DBG_MSG, "Looking for %s...\n", pModels->pszName);
      _pModel = pModels;
      if (sanei_usb_find_devices ((SANE_Int) pModels->iVendor,
                                  (SANE_Int) pModels->iProduct,
                                  _AttachUsb) != SANE_STATUS_GOOD)
        {

          DBG (DBG_ERR, "Error invoking sanei_usb_find_devices");
          break;
        }
      pModels++;
    }
}


static int
NiashXferOpen (const char *pszName, EScannerModel * peModel)
{
  SANE_Status status;
  SANE_Word vendor, product;
  int fd;
  TScannerModel *pModel = 0;

  DBG (DBG_MSG, "Trying to open %s...\n", pszName);

  status = sanei_usb_open (pszName, &fd);
  if (status != SANE_STATUS_GOOD)
    {
      return -1;
    }

  status = sanei_usb_get_vendor_product (fd, &vendor, &product);
  if (status == SANE_STATUS_GOOD)
    {
      MatchUsbDevice (vendor, product, &pModel);
      *peModel = pModel->eModel;
    }

  DBG (DBG_MSG, "handle = %d\n", (int) fd);
  return fd;
}


static void
NiashXferClose (int iHandle)
{
  /* close usb device */
  if (iHandle != -1)
    {
      sanei_usb_close (iHandle);
    }
}


static void
parusb_write_reg (int fd, unsigned char bReg, unsigned char bValue)
{
  sanei_usb_control_msg (fd,
                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
                         0x0C, bReg, 0, 1, &bValue);
}


static void
parusb_read_reg (int fd, unsigned char bReg, unsigned char *pbValue)
{
  sanei_usb_control_msg (fd,
                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
                         0x0C, bReg, 0, 1, pbValue);
}


static void
NiashWriteReg (int iHandle, unsigned char bReg, unsigned char bData)
{
  if (iHandle < 0)
    {
      DBG (DBG_MSG, "Invalid handle %d\n", iHandle);
      return;
    }

  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, EPP_ADDR, bReg);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, EPP_DATA_WRITE, bData);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}


static void
NiashReadReg (int iHandle, unsigned char bReg, unsigned char *pbData)
{
  if (iHandle < 0)
    {
      return;
    }

  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, EPP_ADDR, bReg);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x34);
  parusb_read_reg (iHandle, EPP_DATA_READ, pbData);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}


static void
NiashWriteBulk (int iHandle, unsigned char *pabBuf, int iSize)
{
  /*  byte  abSetup[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     HP3400 probably needs 0x01, 0x01 */
  SANE_Byte abSetup[8] = { 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  size_t size;

  if (iHandle < 0)
    {
      return;
    }

  /* select scanner register 0x24 */
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, EPP_ADDR, 0x24);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);

  /* tell scanner that a bulk transfer follows */
  abSetup[4] = (iSize) & 0xFF;
  abSetup[5] = (iSize >> 8) & 0xFF;
  sanei_usb_control_msg (iHandle,
                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
                         0x04, USB_SETUP, 0, 8, abSetup);

  /* do the bulk write */
  size = iSize;
  if (sanei_usb_write_bulk (iHandle, pabBuf, &size) != SANE_STATUS_GOOD)
    {
      DBG (DBG_ERR, "ERROR: Bulk write failed\n");
    }
}


static void
NiashReadBulk (int iHandle, unsigned char *pabBuf, int iSize)
{
  SANE_Byte abSetup[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  size_t size;

  if (iHandle < 0)
    {
      return;
    }

  /* select scanner register 0x24 */
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, EPP_ADDR, 0x24);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);

  /* tell scanner that a bulk transfer follows */
  abSetup[4] = (iSize) & 0xFF;
  abSetup[5] = (iSize >> 8) & 0xFF;
  sanei_usb_control_msg (iHandle,
                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
                         0x04, USB_SETUP, 0, 8, abSetup);

  /* do the bulk read */
  size = iSize;
  if (sanei_usb_read_bulk (iHandle, pabBuf, &size) != SANE_STATUS_GOOD)
    {
      DBG (DBG_ERR, "ERROR: Bulk read failed\n");
    }
}


static void
NiashWakeup (int iHandle)
{
  unsigned char abMagic[] = { 0xA0, 0xA8, 0x50, 0x58, 0x90, 0x98, 0xC0, 0xC8,
    0x90, 0x98, 0xE0, 0xE8
  };
  int i;

  if (iHandle < 0)
    {
      return;
    }

  /* write magic startup sequence */
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  for (i = 0; i < (int) sizeof (abMagic); i++)
    {
      parusb_write_reg (iHandle, SPP_DATA, abMagic[i]);
    }

  /* write 0x04 to scanner register 0x00 the hard way */
  parusb_write_reg (iHandle, SPP_DATA, 0x00);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x1D);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);

  parusb_write_reg (iHandle, SPP_DATA, 0x04);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x17);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x15);
  parusb_write_reg (iHandle, SPP_CONTROL, 0x14);
}