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#include <linux/module.h>
30#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h>
32
33#include "usb.h"
34#include "transport.h"
35#include "protocol.h"
36#include "debug.h"
37
38MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
39MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
40MODULE_LICENSE("GPL");
41
42#ifdef CONFIG_USB_STORAGE_DEBUG
43static void pdump(struct us_data *us, void *ibuffer, int length);
44#endif
45
46
47#define ERR_STAT 0x01
48#define DRQ_STAT 0x08
49
50
51struct freecom_cb_wrap {
52 u8 Type;
53 u8 Timeout;
54 u8 Atapi[12];
55 u8 Filler[50];
56};
57
58struct freecom_xfer_wrap {
59 u8 Type;
60 u8 Timeout;
61 __le32 Count;
62 u8 Pad[58];
63} __attribute__ ((packed));
64
65struct freecom_ide_out {
66 u8 Type;
67 u8 Pad;
68 __le16 Value;
69 u8 Pad2[60];
70};
71
72struct freecom_ide_in {
73 u8 Type;
74 u8 Pad[63];
75};
76
77struct freecom_status {
78 u8 Status;
79 u8 Reason;
80 __le16 Count;
81 u8 Pad[60];
82};
83
84
85
86#define FCM_INT_STATUS 0x02
87#define FCM_STATUS_BUSY 0x80
88
89
90
91#define FCM_PACKET_ATAPI 0x21
92#define FCM_PACKET_STATUS 0x20
93
94
95
96#define FCM_PACKET_INPUT 0x81
97
98
99#define FCM_PACKET_OUTPUT 0x01
100
101
102
103#define FCM_PACKET_IDE_WRITE 0x40
104#define FCM_PACKET_IDE_READ 0xC0
105
106
107#define FCM_PACKET_LENGTH 64
108#define FCM_STATUS_PACKET_LENGTH 4
109
110static int init_freecom(struct us_data *us);
111
112
113
114
115
116#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
117 vendorName, productName, useProtocol, useTransport, \
118 initFunction, flags) \
119{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
120 .driver_info = (flags) }
121
122static struct usb_device_id freecom_usb_ids[] = {
123# include "unusual_freecom.h"
124 { }
125};
126MODULE_DEVICE_TABLE(usb, freecom_usb_ids);
127
128#undef UNUSUAL_DEV
129
130
131
132
133#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
134 vendor_name, product_name, use_protocol, use_transport, \
135 init_function, Flags) \
136{ \
137 .vendorName = vendor_name, \
138 .productName = product_name, \
139 .useProtocol = use_protocol, \
140 .useTransport = use_transport, \
141 .initFunction = init_function, \
142}
143
144static struct us_unusual_dev freecom_unusual_dev_list[] = {
145# include "unusual_freecom.h"
146 { }
147};
148
149#undef UNUSUAL_DEV
150
151static int
152freecom_readdata (struct scsi_cmnd *srb, struct us_data *us,
153 unsigned int ipipe, unsigned int opipe, int count)
154{
155 struct freecom_xfer_wrap *fxfr =
156 (struct freecom_xfer_wrap *) us->iobuf;
157 int result;
158
159 fxfr->Type = FCM_PACKET_INPUT | 0x00;
160 fxfr->Timeout = 0;
161 fxfr->Count = cpu_to_le32 (count);
162 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
163
164 usb_stor_dbg(us, "Read data Freecom! (c=%d)\n", count);
165
166
167 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
168 FCM_PACKET_LENGTH, NULL);
169 if (result != USB_STOR_XFER_GOOD) {
170 usb_stor_dbg(us, "Freecom readdata transport error\n");
171 return USB_STOR_TRANSPORT_ERROR;
172 }
173
174
175 usb_stor_dbg(us, "Start of read\n");
176 result = usb_stor_bulk_srb(us, ipipe, srb);
177 usb_stor_dbg(us, "freecom_readdata done!\n");
178
179 if (result > USB_STOR_XFER_SHORT)
180 return USB_STOR_TRANSPORT_ERROR;
181 return USB_STOR_TRANSPORT_GOOD;
182}
183
184static int
185freecom_writedata (struct scsi_cmnd *srb, struct us_data *us,
186 int unsigned ipipe, unsigned int opipe, int count)
187{
188 struct freecom_xfer_wrap *fxfr =
189 (struct freecom_xfer_wrap *) us->iobuf;
190 int result;
191
192 fxfr->Type = FCM_PACKET_OUTPUT | 0x00;
193 fxfr->Timeout = 0;
194 fxfr->Count = cpu_to_le32 (count);
195 memset (fxfr->Pad, 0, sizeof (fxfr->Pad));
196
197 usb_stor_dbg(us, "Write data Freecom! (c=%d)\n", count);
198
199
200 result = usb_stor_bulk_transfer_buf (us, opipe, fxfr,
201 FCM_PACKET_LENGTH, NULL);
202 if (result != USB_STOR_XFER_GOOD) {
203 usb_stor_dbg(us, "Freecom writedata transport error\n");
204 return USB_STOR_TRANSPORT_ERROR;
205 }
206
207
208 usb_stor_dbg(us, "Start of write\n");
209 result = usb_stor_bulk_srb(us, opipe, srb);
210
211 usb_stor_dbg(us, "freecom_writedata done!\n");
212 if (result > USB_STOR_XFER_SHORT)
213 return USB_STOR_TRANSPORT_ERROR;
214 return USB_STOR_TRANSPORT_GOOD;
215}
216
217
218
219
220
221static int freecom_transport(struct scsi_cmnd *srb, struct us_data *us)
222{
223 struct freecom_cb_wrap *fcb;
224 struct freecom_status *fst;
225 unsigned int ipipe, opipe;
226 int result;
227 unsigned int partial;
228 int length;
229
230 fcb = (struct freecom_cb_wrap *) us->iobuf;
231 fst = (struct freecom_status *) us->iobuf;
232
233 usb_stor_dbg(us, "Freecom TRANSPORT STARTED\n");
234
235
236 opipe = us->send_bulk_pipe;
237 ipipe = us->recv_bulk_pipe;
238
239
240 fcb->Type = FCM_PACKET_ATAPI | 0x00;
241 fcb->Timeout = 0;
242 memcpy (fcb->Atapi, srb->cmnd, 12);
243 memset (fcb->Filler, 0, sizeof (fcb->Filler));
244
245 US_DEBUG(pdump(us, srb->cmnd, 12));
246
247
248 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
249 FCM_PACKET_LENGTH, NULL);
250
251
252
253
254 if (result != USB_STOR_XFER_GOOD) {
255 usb_stor_dbg(us, "freecom transport error\n");
256 return USB_STOR_TRANSPORT_ERROR;
257 }
258
259
260
261 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
262 FCM_STATUS_PACKET_LENGTH, &partial);
263 usb_stor_dbg(us, "foo Status result %d %u\n", result, partial);
264 if (result != USB_STOR_XFER_GOOD)
265 return USB_STOR_TRANSPORT_ERROR;
266
267 US_DEBUG(pdump(us, (void *)fst, partial));
268
269
270
271
272
273
274
275
276
277 while (fst->Status & FCM_STATUS_BUSY) {
278 usb_stor_dbg(us, "20 second USB/ATAPI bridge TIMEOUT occurred!\n");
279 usb_stor_dbg(us, "fst->Status is %x\n", fst->Status);
280
281
282 fcb->Type = FCM_PACKET_STATUS;
283 fcb->Timeout = 0;
284 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
285 memset (fcb->Filler, 0, sizeof (fcb->Filler));
286
287
288 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
289 FCM_PACKET_LENGTH, NULL);
290
291
292
293
294
295 if (result != USB_STOR_XFER_GOOD) {
296 usb_stor_dbg(us, "freecom transport error\n");
297 return USB_STOR_TRANSPORT_ERROR;
298 }
299
300
301 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
302 FCM_STATUS_PACKET_LENGTH, &partial);
303
304 usb_stor_dbg(us, "bar Status result %d %u\n", result, partial);
305 if (result != USB_STOR_XFER_GOOD)
306 return USB_STOR_TRANSPORT_ERROR;
307
308 US_DEBUG(pdump(us, (void *)fst, partial));
309 }
310
311 if (partial != 4)
312 return USB_STOR_TRANSPORT_ERROR;
313 if ((fst->Status & 1) != 0) {
314 usb_stor_dbg(us, "operation failed\n");
315 return USB_STOR_TRANSPORT_FAILED;
316 }
317
318
319
320
321 usb_stor_dbg(us, "Device indicates that it has %d bytes available\n",
322 le16_to_cpu(fst->Count));
323 usb_stor_dbg(us, "SCSI requested %d\n", scsi_bufflen(srb));
324
325
326 switch (srb->cmnd[0]) {
327 case INQUIRY:
328 case REQUEST_SENSE:
329 case MODE_SENSE:
330 case MODE_SENSE_10:
331 length = le16_to_cpu(fst->Count);
332 break;
333 default:
334 length = scsi_bufflen(srb);
335 }
336
337
338 if (length > scsi_bufflen(srb)) {
339 length = scsi_bufflen(srb);
340 usb_stor_dbg(us, "Truncating request to match buffer length: %d\n",
341 length);
342 }
343
344
345
346
347 switch (us->srb->sc_data_direction) {
348 case DMA_FROM_DEVICE:
349
350 if (!length)
351 break;
352
353
354 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
355 usb_stor_dbg(us, "SCSI wants data, drive doesn't have any\n");
356 return USB_STOR_TRANSPORT_FAILED;
357 }
358 result = freecom_readdata (srb, us, ipipe, opipe, length);
359 if (result != USB_STOR_TRANSPORT_GOOD)
360 return result;
361
362 usb_stor_dbg(us, "Waiting for status\n");
363 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
364 FCM_PACKET_LENGTH, &partial);
365 US_DEBUG(pdump(us, (void *)fst, partial));
366
367 if (partial != 4 || result > USB_STOR_XFER_SHORT)
368 return USB_STOR_TRANSPORT_ERROR;
369 if ((fst->Status & ERR_STAT) != 0) {
370 usb_stor_dbg(us, "operation failed\n");
371 return USB_STOR_TRANSPORT_FAILED;
372 }
373 if ((fst->Reason & 3) != 3) {
374 usb_stor_dbg(us, "Drive seems still hungry\n");
375 return USB_STOR_TRANSPORT_FAILED;
376 }
377 usb_stor_dbg(us, "Transfer happy\n");
378 break;
379
380 case DMA_TO_DEVICE:
381
382 if (!length)
383 break;
384
385
386
387 result = freecom_writedata (srb, us, ipipe, opipe, length);
388 if (result != USB_STOR_TRANSPORT_GOOD)
389 return result;
390
391 usb_stor_dbg(us, "Waiting for status\n");
392 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
393 FCM_PACKET_LENGTH, &partial);
394
395 if (partial != 4 || result > USB_STOR_XFER_SHORT)
396 return USB_STOR_TRANSPORT_ERROR;
397 if ((fst->Status & ERR_STAT) != 0) {
398 usb_stor_dbg(us, "operation failed\n");
399 return USB_STOR_TRANSPORT_FAILED;
400 }
401 if ((fst->Reason & 3) != 3) {
402 usb_stor_dbg(us, "Drive seems still hungry\n");
403 return USB_STOR_TRANSPORT_FAILED;
404 }
405
406 usb_stor_dbg(us, "Transfer happy\n");
407 break;
408
409
410 case DMA_NONE:
411
412 break;
413
414 default:
415
416 usb_stor_dbg(us, "freecom unimplemented direction: %d\n",
417 us->srb->sc_data_direction);
418
419 return USB_STOR_TRANSPORT_FAILED;
420 break;
421 }
422
423 return USB_STOR_TRANSPORT_GOOD;
424}
425
426static int init_freecom(struct us_data *us)
427{
428 int result;
429 char *buffer = us->iobuf;
430
431
432
433
434
435 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
436 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
437 buffer[32] = '\0';
438 usb_stor_dbg(us, "String returned from FC init is: %s\n", buffer);
439
440
441
442
443
444
445
446
447 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
448 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
449 usb_stor_dbg(us, "result from activate reset is %d\n", result);
450
451
452 mdelay(250);
453
454
455 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
456 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
457 usb_stor_dbg(us, "result from clear reset is %d\n", result);
458
459
460 mdelay(3 * 1000);
461
462 return USB_STOR_TRANSPORT_GOOD;
463}
464
465static int usb_stor_freecom_reset(struct us_data *us)
466{
467 printk (KERN_CRIT "freecom reset called\n");
468
469
470 return FAILED;
471}
472
473#ifdef CONFIG_USB_STORAGE_DEBUG
474static void pdump(struct us_data *us, void *ibuffer, int length)
475{
476 static char line[80];
477 int offset = 0;
478 unsigned char *buffer = (unsigned char *) ibuffer;
479 int i, j;
480 int from, base;
481
482 offset = 0;
483 for (i = 0; i < length; i++) {
484 if ((i & 15) == 0) {
485 if (i > 0) {
486 offset += sprintf (line+offset, " - ");
487 for (j = i - 16; j < i; j++) {
488 if (buffer[j] >= 32 && buffer[j] <= 126)
489 line[offset++] = buffer[j];
490 else
491 line[offset++] = '.';
492 }
493 line[offset] = 0;
494 usb_stor_dbg(us, "%s\n", line);
495 offset = 0;
496 }
497 offset += sprintf (line+offset, "%08x:", i);
498 } else if ((i & 7) == 0) {
499 offset += sprintf (line+offset, " -");
500 }
501 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
502 }
503
504
505 from = (length - 1) % 16;
506 base = ((length - 1) / 16) * 16;
507
508 for (i = from + 1; i < 16; i++)
509 offset += sprintf (line+offset, " ");
510 if (from < 8)
511 offset += sprintf (line+offset, " ");
512 offset += sprintf (line+offset, " - ");
513
514 for (i = 0; i <= from; i++) {
515 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
516 line[offset++] = buffer[base+i];
517 else
518 line[offset++] = '.';
519 }
520 line[offset] = 0;
521 usb_stor_dbg(us, "%s\n", line);
522 offset = 0;
523}
524#endif
525
526static int freecom_probe(struct usb_interface *intf,
527 const struct usb_device_id *id)
528{
529 struct us_data *us;
530 int result;
531
532 result = usb_stor_probe1(&us, intf, id,
533 (id - freecom_usb_ids) + freecom_unusual_dev_list);
534 if (result)
535 return result;
536
537 us->transport_name = "Freecom";
538 us->transport = freecom_transport;
539 us->transport_reset = usb_stor_freecom_reset;
540 us->max_lun = 0;
541
542 result = usb_stor_probe2(us);
543 return result;
544}
545
546static struct usb_driver freecom_driver = {
547 .name = "ums-freecom",
548 .probe = freecom_probe,
549 .disconnect = usb_stor_disconnect,
550 .suspend = usb_stor_suspend,
551 .resume = usb_stor_resume,
552 .reset_resume = usb_stor_reset_resume,
553 .pre_reset = usb_stor_pre_reset,
554 .post_reset = usb_stor_post_reset,
555 .id_table = freecom_usb_ids,
556 .soft_unbind = 1,
557 .no_dynamic_id = 1,
558};
559
560module_usb_driver(freecom_driver);
561