summaryrefslogtreecommitdiff
path: root/backend/kvs1025_usb.c
blob: 90ce4d040b1f75273e82e3755d8aa0474aba0abd (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
   Copyright (C) 2008, Panasonic Russia Ltd.
*/
/* sane - Scanner Access Now Easy.
   Panasonic KV-S1020C / KV-S1025C USB scanners.
*/

#define DEBUG_DECLARE_ONLY

#include "../include/sane/config.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include "../include/sane/sane.h"
#include "../include/sane/saneopts.h"
#include "../include/sane/sanei.h"
#include "../include/sane/sanei_usb.h"
#include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_config.h"
#include "../include/lassert.h"

#include "kvs1025.h"
#include "kvs1025_low.h"
#include "kvs1025_usb.h"
#include "kvs1025_cmds.h"

#include "../include/sane/sanei_debug.h"

extern PKV_DEV g_devices;	/* Chain of devices */
extern const SANE_Device **g_devlist;

/* static functions */

/* Attach USB scanner */
static SANE_Status
attach_scanner_usb (const char *device_name)
{
  PKV_DEV dev;
  SANE_Word vendor, product;

  DBG (DBG_error, "attaching USB scanner %s\n", device_name);

  sanei_usb_get_vendor_product_byname(device_name,&vendor,&product);

  dev = (PKV_DEV) malloc (sizeof (KV_DEV));

  if (dev == NULL)
    return SANE_STATUS_NO_MEM;

  memset (dev, 0, sizeof (KV_DEV));

  dev->bus_mode = KV_USB_BUS;
  dev->usb_fd = -1;
  dev->scsi_fd = -1;
  strcpy (dev->device_name, device_name);

  dev->buffer0 = (unsigned char *) malloc (SCSI_BUFFER_SIZE + 12);
  dev->buffer = dev->buffer0 + 12;

  if (dev->buffer0 == NULL)
    {
      free (dev);
      return SANE_STATUS_NO_MEM;
    }

  dev->scsi_type = 6;
  strcpy (dev->scsi_type_str, "ADF Scanner");
  strcpy (dev->scsi_vendor, "Panasonic");
  strcpy (dev->scsi_product,
    product == (int) KV_S1020C ? "KV-S1020C" :
    product == (int) KV_S1025C ? "KV-S1025C" :
    product == (int) KV_S1045C ? "KV-S1045C" :
    "KV-S10xxC");
  strcpy (dev->scsi_version, "1.00");

  /* Set SANE_Device */
  dev->sane.name = dev->device_name;
  dev->sane.vendor = dev->scsi_vendor;
  dev->sane.model = dev->scsi_product;
  dev->sane.type = dev->scsi_type_str;

  /* Add into g_devices chain */
  dev->next = g_devices;
  g_devices = dev;

  return SANE_STATUS_GOOD;
}

/* Get all supported scanners, and store into g_devlist */
SANE_Status
kv_usb_enum_devices (void)
{
  int cnt = 0;
  int i;
  PKV_DEV pd;
  char usb_str[18];

  DBG (DBG_proc, "kv_usb_enum_devices: enter\n");

  sanei_usb_init();

  sprintf(usb_str,"usb %#04x %#04x",VENDOR_ID,KV_S1020C);
  sanei_usb_attach_matching_devices(usb_str, attach_scanner_usb);

  sprintf(usb_str,"usb %#04x %#04x",VENDOR_ID,KV_S1025C);
  sanei_usb_attach_matching_devices(usb_str, attach_scanner_usb);

  sprintf(usb_str,"usb %#04x %#04x",VENDOR_ID,KV_S1045C);
  sanei_usb_attach_matching_devices(usb_str, attach_scanner_usb);

  for (pd = g_devices; pd; pd=pd->next) {
    cnt++;
  }

  g_devlist =
    (const SANE_Device **) malloc (sizeof (SANE_Device *) * (cnt + 1));
  if (g_devlist == NULL)
    {
      DBG (DBG_proc,
	   "kv_usb_enum_devices: leave on error " " --out of memory\n");
      return SANE_STATUS_NO_MEM;
    }

  pd = g_devices;
  for (i = 0; i < cnt; i++)
    {
      g_devlist[i] = (const SANE_Device *) &pd->sane;
      pd = pd->next;
    }
  g_devlist[cnt] = 0;

  DBG (DBG_proc, "kv_usb_enum_devices: leave with %d devices.\n", cnt);

  return SANE_STATUS_GOOD;
}

/* Check if device is already open */
SANE_Bool
kv_usb_already_open (PKV_DEV dev)
{
  return (dev->usb_fd > -1);
}

/* Open an USB device */
SANE_Status
kv_usb_open (PKV_DEV dev)
{
  SANE_Status ret;

  DBG (DBG_proc, "kv_usb_open: enter\n");
  if (kv_usb_already_open(dev))
    {
      DBG (DBG_proc, "kv_usb_open: leave -- already open\n");
      return SANE_STATUS_GOOD;
    }

  ret = sanei_usb_open (dev->device_name, &(dev->usb_fd));
  if (ret)
    {
      DBG (DBG_error, "kv_usb_open: leave -- cannot open device\n");
      return SANE_STATUS_IO_ERROR;
    }

  sanei_usb_clear_halt (dev->usb_fd);

  DBG (DBG_proc, "kv_usb_open: leave\n");
  return SANE_STATUS_GOOD;
}

/* Close an USB device */
void
kv_usb_close (PKV_DEV dev)
{
  DBG (DBG_proc, "kv_usb_close: enter\n");
  if (kv_usb_already_open(dev))
    {
      sanei_usb_close(dev->usb_fd);
      dev->usb_fd = -1;
    }
  DBG (DBG_proc, "kv_usb_close: leave\n");
}

/* Clean up the USB bus and release all resources allocated to devices */
void
kv_usb_cleanup (void)
{
}

/* Send command via USB, and get response data */
SANE_Status
kv_usb_escape (PKV_DEV dev,
	       PKV_CMD_HEADER header, unsigned char *status_byte)
{
  int got_response = 0;
  size_t len;
  unsigned char cmd_buff[24];
  memset (cmd_buff, 0, 24);
  cmd_buff[3] = 0x18;		/* container length */
  cmd_buff[5] = 1;		/* container type: command block */
  cmd_buff[6] = 0x90;		/* code */

  if (!kv_usb_already_open(dev))
    {
      DBG (DBG_error, "kv_usb_escape: error, device not open.\n");
      return SANE_STATUS_IO_ERROR;
    }
  memcpy (cmd_buff + 12, header->cdb, header->cdb_size);

  /* change timeout */
  sanei_usb_set_timeout(KV_CMD_TIMEOUT);

  /* Send command */
  len = 24;
  if (sanei_usb_write_bulk (dev->usb_fd, (SANE_Byte *) cmd_buff, &len))
    {
      DBG (DBG_error, "usb_bulk_write: Error writing command.\n");
      hexdump (DBG_error, "cmd block", cmd_buff, 24);
      return SANE_STATUS_IO_ERROR;
    }

  /* Send / Read data */
  if (header->direction == KV_CMD_IN)
    {
      size_t size = header->data_size + 12;
      size_t size_read = size;
      unsigned char *data = ((unsigned char *) header->data) - 12;
      SANE_Status ret;

      ret  = sanei_usb_read_bulk (dev->usb_fd, (SANE_Byte *) data, &size_read);

      /*empty read is ok?*/
      if (ret == SANE_STATUS_EOF){
	sanei_usb_clear_halt (dev->usb_fd);
        ret = SANE_STATUS_GOOD;
      }

      if (ret) {
	sanei_usb_clear_halt (dev->usb_fd);
	DBG (DBG_error, "usb_bulk_read: Error reading data.\n");
	return SANE_STATUS_IO_ERROR;
      }

      if (size_read != size)
	{
	  DBG (DBG_shortread, "usb_bulk_read: Warning - short read\n");
	  DBG (DBG_shortread, "usb_bulk_read: bytes to read = %lu\n", (unsigned long)size);
	  DBG (DBG_shortread,
	       "usb_bulk_read: bytes actual read = %lu\n", (unsigned long)size_read);
	  /*hexdump (DBG_shortread, "data", data, size_read); */
	}
    }

  if (header->direction == KV_CMD_OUT)
    {
      size_t size = header->data_size + 12;
      size_t size_written = size;
      unsigned char *data = ((unsigned char *) header->data) - 12;
      SANE_Status ret;

      memset (data, 0, 12);
      Ito32 (size, data);
      data[5] = 0x02;		/* container type: data block */
      data[6] = 0xb0;		/* code */

      ret = sanei_usb_write_bulk (dev->usb_fd, (SANE_Byte *) data, &size_written);

      /*empty write is ok?*/
      if (ret == SANE_STATUS_EOF){
	sanei_usb_clear_halt (dev->usb_fd);
        ret = SANE_STATUS_GOOD;
      }

      if (ret) {
	sanei_usb_clear_halt (dev->usb_fd);
	DBG (DBG_error, "usb_bulk_write: Error writing data.\n");
	return SANE_STATUS_IO_ERROR;
      }

      if (size_written != size)
	{
	  DBG (DBG_shortread, "usb_bulk_write: Warning - short written\n");
	  DBG (DBG_shortread, "usb_bulk_write: bytes to write = %lu\n", (unsigned long)size);
	  DBG (DBG_shortread,
	       "usb_bulk_write: bytes actual written = %lu\n", (unsigned long)size_written);
	  hexdump (DBG_shortread, "data", data, size_written);
	}
    }

  /* Get response */
  if (!got_response)
    {
      SANE_Status ret;
      size_t len = 16;

      ret = sanei_usb_read_bulk (dev->usb_fd, (SANE_Byte *) cmd_buff, &len);
      
      if (ret || len != 16)
	{
	  DBG (DBG_error, "usb_bulk_read: Error reading response."
	       " read %lu bytes\n", (unsigned long)len);
	  sanei_usb_clear_halt (dev->usb_fd);
	  return SANE_STATUS_IO_ERROR;
	}
    }

  if (cmd_buff[5] != 3)
    {
      DBG (DBG_error, "usb_bulk_read: Invalid response block.\n");
      hexdump (DBG_error, "response", cmd_buff, 16);
      return SANE_STATUS_IO_ERROR;
    }

  *status_byte = cmd_buff[15] & 0x3E;

  return SANE_STATUS_GOOD;
}

/* Send command via USB, and request sense on CHECK CONDITION status */
SANE_Status
kv_usb_send_command (PKV_DEV dev,
		     PKV_CMD_HEADER header, PKV_CMD_RESPONSE response)
{
  unsigned char status = 0;
  SANE_Status s;
  memset (response, 0, sizeof (KV_CMD_RESPONSE));
  response->status = KV_FAILED;

  s = kv_usb_escape (dev, header, &status);

  if (s)
    {
      status = 0x02;
    }

  if (status == 0x02)
    {				/* check condition */
      /* request sense */
      KV_CMD_HEADER hdr;
      memset (&hdr, 0, sizeof (hdr));
      hdr.direction = KV_CMD_IN;
      hdr.cdb[0] = SCSI_REQUEST_SENSE;
      hdr.cdb[4] = 0x12;
      hdr.cdb_size = 6;
      hdr.data_size = 0x12;
      hdr.data = &response->sense;

      if (kv_usb_escape (dev, &hdr, &status) != 0)
	return SANE_STATUS_IO_ERROR;

      hexdump (DBG_error, "sense data", (unsigned char *) &response->sense,
	       0x12);

      response->status = KV_CHK_CONDITION;
    }
  else
    {
      response->status = KV_SUCCESS;
    }
  return SANE_STATUS_GOOD;
}