This source file includes following definitions.
- freecom_readdata
- freecom_writedata
- freecom_transport
- init_freecom
- usb_stor_freecom_reset
- pdump
- freecom_probe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <linux/module.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_cmnd.h>
20
21 #include "usb.h"
22 #include "transport.h"
23 #include "protocol.h"
24 #include "debug.h"
25 #include "scsiglue.h"
26
27 #define DRV_NAME "ums-freecom"
28
29 MODULE_DESCRIPTION("Driver for Freecom USB/IDE adaptor");
30 MODULE_AUTHOR("David Brown <usb-storage@davidb.org>");
31 MODULE_LICENSE("GPL");
32 MODULE_IMPORT_NS(USB_STORAGE);
33
34 #ifdef CONFIG_USB_STORAGE_DEBUG
35 static void pdump(struct us_data *us, void *ibuffer, int length);
36 #endif
37
38
39 #define ERR_STAT 0x01
40 #define DRQ_STAT 0x08
41
42
43 struct freecom_cb_wrap {
44 u8 Type;
45 u8 Timeout;
46 u8 Atapi[12];
47 u8 Filler[50];
48 };
49
50 struct freecom_xfer_wrap {
51 u8 Type;
52 u8 Timeout;
53 __le32 Count;
54 u8 Pad[58];
55 } __attribute__ ((packed));
56
57 struct freecom_ide_out {
58 u8 Type;
59 u8 Pad;
60 __le16 Value;
61 u8 Pad2[60];
62 };
63
64 struct freecom_ide_in {
65 u8 Type;
66 u8 Pad[63];
67 };
68
69 struct freecom_status {
70 u8 Status;
71 u8 Reason;
72 __le16 Count;
73 u8 Pad[60];
74 };
75
76
77
78
79
80 #define FCM_INT_STATUS 0x02
81 #define FCM_STATUS_BUSY 0x80
82
83
84
85
86
87 #define FCM_PACKET_ATAPI 0x21
88 #define FCM_PACKET_STATUS 0x20
89
90
91
92
93
94 #define FCM_PACKET_INPUT 0x81
95
96
97 #define FCM_PACKET_OUTPUT 0x01
98
99
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
110 static 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
122 static struct usb_device_id freecom_usb_ids[] = {
123 # include "unusual_freecom.h"
124 { }
125 };
126 MODULE_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
144 static struct us_unusual_dev freecom_unusual_dev_list[] = {
145 # include "unusual_freecom.h"
146 { }
147 };
148
149 #undef UNUSUAL_DEV
150
151 static int
152 freecom_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
184 static int
185 freecom_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
221 static 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
255
256 if (result != USB_STOR_XFER_GOOD) {
257 usb_stor_dbg(us, "freecom transport error\n");
258 return USB_STOR_TRANSPORT_ERROR;
259 }
260
261
262
263
264
265 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
266 FCM_STATUS_PACKET_LENGTH, &partial);
267 usb_stor_dbg(us, "foo Status result %d %u\n", result, partial);
268 if (result != USB_STOR_XFER_GOOD)
269 return USB_STOR_TRANSPORT_ERROR;
270
271 US_DEBUG(pdump(us, (void *)fst, partial));
272
273
274
275
276
277
278
279
280
281
282 while (fst->Status & FCM_STATUS_BUSY) {
283 usb_stor_dbg(us, "20 second USB/ATAPI bridge TIMEOUT occurred!\n");
284 usb_stor_dbg(us, "fst->Status is %x\n", fst->Status);
285
286
287 fcb->Type = FCM_PACKET_STATUS;
288 fcb->Timeout = 0;
289 memset (fcb->Atapi, 0, sizeof(fcb->Atapi));
290 memset (fcb->Filler, 0, sizeof (fcb->Filler));
291
292
293 result = usb_stor_bulk_transfer_buf (us, opipe, fcb,
294 FCM_PACKET_LENGTH, NULL);
295
296
297
298
299
300
301 if (result != USB_STOR_XFER_GOOD) {
302 usb_stor_dbg(us, "freecom transport error\n");
303 return USB_STOR_TRANSPORT_ERROR;
304 }
305
306
307 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
308 FCM_STATUS_PACKET_LENGTH, &partial);
309
310 usb_stor_dbg(us, "bar Status result %d %u\n", result, partial);
311 if (result != USB_STOR_XFER_GOOD)
312 return USB_STOR_TRANSPORT_ERROR;
313
314 US_DEBUG(pdump(us, (void *)fst, partial));
315 }
316
317 if (partial != 4)
318 return USB_STOR_TRANSPORT_ERROR;
319 if ((fst->Status & 1) != 0) {
320 usb_stor_dbg(us, "operation failed\n");
321 return USB_STOR_TRANSPORT_FAILED;
322 }
323
324
325
326
327
328
329 usb_stor_dbg(us, "Device indicates that it has %d bytes available\n",
330 le16_to_cpu(fst->Count));
331 usb_stor_dbg(us, "SCSI requested %d\n", scsi_bufflen(srb));
332
333
334 switch (srb->cmnd[0]) {
335 case INQUIRY:
336 case REQUEST_SENSE:
337 case MODE_SENSE:
338 case MODE_SENSE_10:
339 length = le16_to_cpu(fst->Count);
340 break;
341 default:
342 length = scsi_bufflen(srb);
343 }
344
345
346 if (length > scsi_bufflen(srb)) {
347 length = scsi_bufflen(srb);
348 usb_stor_dbg(us, "Truncating request to match buffer length: %d\n",
349 length);
350 }
351
352
353
354
355
356
357 switch (us->srb->sc_data_direction) {
358 case DMA_FROM_DEVICE:
359
360 if (!length)
361 break;
362
363
364
365
366 if ((fst->Status & DRQ_STAT) == 0 || (fst->Reason & 3) != 2) {
367 usb_stor_dbg(us, "SCSI wants data, drive doesn't have any\n");
368 return USB_STOR_TRANSPORT_FAILED;
369 }
370 result = freecom_readdata (srb, us, ipipe, opipe, length);
371 if (result != USB_STOR_TRANSPORT_GOOD)
372 return result;
373
374 usb_stor_dbg(us, "Waiting for status\n");
375 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
376 FCM_PACKET_LENGTH, &partial);
377 US_DEBUG(pdump(us, (void *)fst, partial));
378
379 if (partial != 4 || result > USB_STOR_XFER_SHORT)
380 return USB_STOR_TRANSPORT_ERROR;
381 if ((fst->Status & ERR_STAT) != 0) {
382 usb_stor_dbg(us, "operation failed\n");
383 return USB_STOR_TRANSPORT_FAILED;
384 }
385 if ((fst->Reason & 3) != 3) {
386 usb_stor_dbg(us, "Drive seems still hungry\n");
387 return USB_STOR_TRANSPORT_FAILED;
388 }
389 usb_stor_dbg(us, "Transfer happy\n");
390 break;
391
392 case DMA_TO_DEVICE:
393
394 if (!length)
395 break;
396
397
398
399
400
401 result = freecom_writedata (srb, us, ipipe, opipe, length);
402 if (result != USB_STOR_TRANSPORT_GOOD)
403 return result;
404
405 usb_stor_dbg(us, "Waiting for status\n");
406 result = usb_stor_bulk_transfer_buf (us, ipipe, fst,
407 FCM_PACKET_LENGTH, &partial);
408
409 if (partial != 4 || result > USB_STOR_XFER_SHORT)
410 return USB_STOR_TRANSPORT_ERROR;
411 if ((fst->Status & ERR_STAT) != 0) {
412 usb_stor_dbg(us, "operation failed\n");
413 return USB_STOR_TRANSPORT_FAILED;
414 }
415 if ((fst->Reason & 3) != 3) {
416 usb_stor_dbg(us, "Drive seems still hungry\n");
417 return USB_STOR_TRANSPORT_FAILED;
418 }
419
420 usb_stor_dbg(us, "Transfer happy\n");
421 break;
422
423
424 case DMA_NONE:
425
426 break;
427
428 default:
429
430 usb_stor_dbg(us, "freecom unimplemented direction: %d\n",
431 us->srb->sc_data_direction);
432
433 return USB_STOR_TRANSPORT_FAILED;
434 break;
435 }
436
437 return USB_STOR_TRANSPORT_GOOD;
438 }
439
440 static int init_freecom(struct us_data *us)
441 {
442 int result;
443 char *buffer = us->iobuf;
444
445
446
447
448
449
450 result = usb_stor_control_msg(us, us->recv_ctrl_pipe,
451 0x4c, 0xc0, 0x4346, 0x0, buffer, 0x20, 3*HZ);
452 buffer[32] = '\0';
453 usb_stor_dbg(us, "String returned from FC init is: %s\n", buffer);
454
455
456
457
458
459
460
461
462
463 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
464 0x4d, 0x40, 0x24d8, 0x0, NULL, 0x0, 3*HZ);
465 usb_stor_dbg(us, "result from activate reset is %d\n", result);
466
467
468 msleep(250);
469
470
471 result = usb_stor_control_msg(us, us->send_ctrl_pipe,
472 0x4d, 0x40, 0x24f8, 0x0, NULL, 0x0, 3*HZ);
473 usb_stor_dbg(us, "result from clear reset is %d\n", result);
474
475
476 msleep(3 * 1000);
477
478 return USB_STOR_TRANSPORT_GOOD;
479 }
480
481 static int usb_stor_freecom_reset(struct us_data *us)
482 {
483 printk (KERN_CRIT "freecom reset called\n");
484
485
486 return FAILED;
487 }
488
489 #ifdef CONFIG_USB_STORAGE_DEBUG
490 static void pdump(struct us_data *us, void *ibuffer, int length)
491 {
492 static char line[80];
493 int offset = 0;
494 unsigned char *buffer = (unsigned char *) ibuffer;
495 int i, j;
496 int from, base;
497
498 offset = 0;
499 for (i = 0; i < length; i++) {
500 if ((i & 15) == 0) {
501 if (i > 0) {
502 offset += sprintf (line+offset, " - ");
503 for (j = i - 16; j < i; j++) {
504 if (buffer[j] >= 32 && buffer[j] <= 126)
505 line[offset++] = buffer[j];
506 else
507 line[offset++] = '.';
508 }
509 line[offset] = 0;
510 usb_stor_dbg(us, "%s\n", line);
511 offset = 0;
512 }
513 offset += sprintf (line+offset, "%08x:", i);
514 } else if ((i & 7) == 0) {
515 offset += sprintf (line+offset, " -");
516 }
517 offset += sprintf (line+offset, " %02x", buffer[i] & 0xff);
518 }
519
520
521 from = (length - 1) % 16;
522 base = ((length - 1) / 16) * 16;
523
524 for (i = from + 1; i < 16; i++)
525 offset += sprintf (line+offset, " ");
526 if (from < 8)
527 offset += sprintf (line+offset, " ");
528 offset += sprintf (line+offset, " - ");
529
530 for (i = 0; i <= from; i++) {
531 if (buffer[base+i] >= 32 && buffer[base+i] <= 126)
532 line[offset++] = buffer[base+i];
533 else
534 line[offset++] = '.';
535 }
536 line[offset] = 0;
537 usb_stor_dbg(us, "%s\n", line);
538 offset = 0;
539 }
540 #endif
541
542 static struct scsi_host_template freecom_host_template;
543
544 static int freecom_probe(struct usb_interface *intf,
545 const struct usb_device_id *id)
546 {
547 struct us_data *us;
548 int result;
549
550 result = usb_stor_probe1(&us, intf, id,
551 (id - freecom_usb_ids) + freecom_unusual_dev_list,
552 &freecom_host_template);
553 if (result)
554 return result;
555
556 us->transport_name = "Freecom";
557 us->transport = freecom_transport;
558 us->transport_reset = usb_stor_freecom_reset;
559 us->max_lun = 0;
560
561 result = usb_stor_probe2(us);
562 return result;
563 }
564
565 static struct usb_driver freecom_driver = {
566 .name = DRV_NAME,
567 .probe = freecom_probe,
568 .disconnect = usb_stor_disconnect,
569 .suspend = usb_stor_suspend,
570 .resume = usb_stor_resume,
571 .reset_resume = usb_stor_reset_resume,
572 .pre_reset = usb_stor_pre_reset,
573 .post_reset = usb_stor_post_reset,
574 .id_table = freecom_usb_ids,
575 .soft_unbind = 1,
576 .no_dynamic_id = 1,
577 };
578
579 module_usb_stor_driver(freecom_driver, freecom_host_template, DRV_NAME);