51 #include <linux/errno.h>
52 #include <linux/module.h>
53 #include <linux/slab.h>
55 #include <scsi/scsi.h>
78 static int datafab_determine_lun(
struct us_data *us,
85 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
86 vendorName, productName, useProtocol, useTransport, \
87 initFunction, flags) \
88 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
89 .driver_info = (flags) }
102 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
103 vendor_name, product_name, use_protocol, use_transport, \
104 init_function, Flags) \
106 .vendorName = vendor_name, \
107 .productName = product_name, \
108 .useProtocol = use_protocol, \
109 .useTransport = use_transport, \
110 .initFunction = init_function, \
122 datafab_bulk_read(
struct us_data *us,
unsigned char *
data,
unsigned int len) {
126 US_DEBUGP(
"datafab_bulk_read: len = %d\n", len);
133 datafab_bulk_write(
struct us_data *us,
unsigned char *data,
unsigned int len) {
137 US_DEBUGP(
"datafab_bulk_write: len = %d\n", len);
143 static int datafab_read_data(
struct us_data *us,
150 unsigned char thistime;
151 unsigned int totallen, alloclen;
153 unsigned int sg_offset = 0;
161 if (sectors > 0x0FFFFFFF)
164 if (info->
lun == -1) {
165 result = datafab_determine_lun(us, info);
170 totallen = sectors * info->
ssize;
176 alloclen =
min(totallen, 65536
u);
185 len =
min(totallen, alloclen);
186 thistime = (len / info->
ssize) & 0xff;
189 command[1] = thistime;
190 command[2] = sector & 0xFF;
191 command[3] = (sector >> 8) & 0xFF;
192 command[4] = (sector >> 16) & 0xFF;
194 command[5] = 0xE0 + (info->
lun << 4);
195 command[5] |= (sector >> 24) & 0x0F;
200 result = datafab_bulk_write(us, command, 8);
205 result = datafab_bulk_read(us, buffer, len);
215 }
while (totallen > 0);
226 static int datafab_write_data(
struct us_data *us,
231 unsigned char *command = us->
iobuf;
232 unsigned char *reply = us->
iobuf;
234 unsigned char thistime;
235 unsigned int totallen, alloclen;
237 unsigned int sg_offset = 0;
245 if (sectors > 0x0FFFFFFF)
248 if (info->
lun == -1) {
249 result = datafab_determine_lun(us, info);
254 totallen = sectors * info->
ssize;
260 alloclen =
min(totallen, 65536
u);
269 len =
min(totallen, alloclen);
270 thistime = (len / info->
ssize) & 0xff;
277 command[1] = thistime;
278 command[2] = sector & 0xFF;
279 command[3] = (sector >> 8) & 0xFF;
280 command[4] = (sector >> 16) & 0xFF;
282 command[5] = 0xE0 + (info->
lun << 4);
283 command[5] |= (sector >> 24) & 0x0F;
288 result = datafab_bulk_write(us, command, 8);
293 result = datafab_bulk_write(us, buffer, len);
298 result = datafab_bulk_read(us, reply, 2);
302 if (reply[0] != 0x50 && reply[1] != 0) {
304 "write return code: %02x %02x\n",
312 }
while (totallen > 0);
323 static int datafab_determine_lun(
struct us_data *us,
332 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
333 unsigned char *command = us->
iobuf;
340 memcpy(command, scommand, 8);
345 US_DEBUGP(
"datafab_determine_lun: locating...\n");
349 while (count++ < 3) {
352 rc = datafab_bulk_write(us, command, 8);
358 rc = datafab_bulk_read(us, buf, 512);
367 rc = datafab_bulk_write(us, command, 8);
373 rc = datafab_bulk_read(us, buf, 512);
390 static int datafab_id_device(
struct us_data *us,
397 static unsigned char scommand[8] = { 0, 1, 0, 0, 0, 0xa0, 0xec, 1 };
398 unsigned char *command = us->
iobuf;
399 unsigned char *reply;
405 if (info->
lun == -1) {
406 rc = datafab_determine_lun(us, info);
411 memcpy(command, scommand, 8);
416 command[5] += (info->
lun << 4);
418 rc = datafab_bulk_write(us, command, 8);
426 rc = datafab_bulk_read(us, reply, 512);
431 ((
u32)(reply[116]) << 16) |
432 ((
u32)(reply[115]) << 8) |
433 ((
u32)(reply[114]) );
446 static int datafab_handle_mode_sense(
struct us_data *us,
450 static unsigned char rw_err_page[12] = {
451 0x1, 0xA, 0x21, 1, 0, 0, 0, 0, 1, 0, 0, 0
453 static unsigned char cache_page[12] = {
454 0x8, 0xA, 0x1, 0, 0, 0, 0, 0, 0, 0, 0, 0
456 static unsigned char rbac_page[12] = {
457 0x1B, 0xA, 0, 0x81, 0, 0, 0, 0, 0, 0, 0, 0
459 static unsigned char timer_page[8] = {
460 0x1C, 0x6, 0, 0, 0, 0
472 pc = srb->
cmnd[2] >> 6;
473 page_code = srb->
cmnd[2] & 0x3F;
477 US_DEBUGP(
"datafab_handle_mode_sense: Current values\n");
480 US_DEBUGP(
"datafab_handle_mode_sense: Changeable values\n");
483 US_DEBUGP(
"datafab_handle_mode_sense: Default values\n");
486 US_DEBUGP(
"datafab_handle_mode_sense: Saves values\n");
508 memcpy(ptr + i, rw_err_page,
sizeof(rw_err_page));
509 i +=
sizeof(rw_err_page);
513 memcpy(ptr + i, cache_page,
sizeof(cache_page));
514 i +=
sizeof(cache_page);
518 memcpy(ptr + i, rbac_page,
sizeof(rbac_page));
519 i +=
sizeof(rbac_page);
523 memcpy(ptr + i, timer_page,
sizeof(timer_page));
524 i +=
sizeof(timer_page);
528 memcpy(ptr + i, timer_page,
sizeof(timer_page));
529 i +=
sizeof(timer_page);
530 memcpy(ptr + i, rbac_page,
sizeof(rbac_page));
531 i +=
sizeof(rbac_page);
532 memcpy(ptr + i, cache_page,
sizeof(cache_page));
533 i +=
sizeof(cache_page);
534 memcpy(ptr + i, rw_err_page,
sizeof(rw_err_page));
535 i +=
sizeof(rw_err_page);
548 static void datafab_info_destructor(
void *
extra)
561 unsigned long block, blocks;
562 unsigned char *ptr = us->
iobuf;
563 static unsigned char inquiry_reply[8] = {
564 0x00, 0x80, 0x00, 0x01, 0x1F, 0x00, 0x00, 0x00
571 "Can't allocate storage for Datafab info struct!\n");
581 US_DEBUGP(
"datafab_transport: INQUIRY. Returning bogus response");
582 memcpy(ptr, inquiry_reply,
sizeof(inquiry_reply));
589 rc = datafab_id_device(us, info);
593 US_DEBUGP(
"datafab_transport: READ_CAPACITY: %ld sectors, %ld bytes per sector\n",
606 US_DEBUGP(
"datafab_transport: Gah! MODE_SELECT_10.\n");
613 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
618 US_DEBUGP(
"datafab_transport: READ_10: read block 0x%04lx count %ld\n", block, blocks);
619 return datafab_read_data(us, info, block, blocks);
625 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
628 blocks = ((
u32)(srb->
cmnd[6]) << 24) | ((
u32)(srb->
cmnd[7]) << 16) |
631 US_DEBUGP(
"datafab_transport: READ_12: read block 0x%04lx count %ld\n", block, blocks);
632 return datafab_read_data(us, info, block, blocks);
636 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
641 US_DEBUGP(
"datafab_transport: WRITE_10: write block 0x%04lx count %ld\n", block, blocks);
642 return datafab_write_data(us, info, block, blocks);
648 block = ((
u32)(srb->
cmnd[2]) << 24) | ((
u32)(srb->
cmnd[3]) << 16) |
651 blocks = ((
u32)(srb->
cmnd[6]) << 24) | ((
u32)(srb->
cmnd[7]) << 16) |
654 US_DEBUGP(
"datafab_transport: WRITE_12: write block 0x%04lx count %ld\n", block, blocks);
655 return datafab_write_data(us, info, block, blocks);
659 US_DEBUGP(
"datafab_transport: TEST_UNIT_READY.\n");
660 return datafab_id_device(us, info);
664 US_DEBUGP(
"datafab_transport: REQUEST_SENSE. Returning faked response\n");
682 US_DEBUGP(
"datafab_transport: MODE_SENSE_6 detected\n");
683 return datafab_handle_mode_sense(us, srb, 1);
687 US_DEBUGP(
"datafab_transport: MODE_SENSE_10 detected\n");
688 return datafab_handle_mode_sense(us, srb, 0);
701 US_DEBUGP(
"datafab_transport: START_STOP.\n");
704 rc = datafab_id_device(us, info);
715 US_DEBUGP(
"datafab_transport: Gah! Unknown command: %d (0x%x)\n",
730 (
id - datafab_usb_ids) + datafab_unusual_dev_list);
743 static struct usb_driver datafab_driver = {
744 .name =
"ums-datafab",
745 .probe = datafab_probe,
752 .id_table = datafab_usb_ids,