Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rts51x_scsi.c
Go to the documentation of this file.
1 /* Driver for Realtek RTS51xx USB card reader
2  *
3  * Copyright(c) 2009 Realtek Semiconductor Corp. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2, or (at your option) any
8  * later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  * wwang ([email protected])
20  * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
21  * Maintainer:
22  * Edwin Rong ([email protected])
23  * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
24  */
25 
26 #include <linux/blkdev.h>
27 #include <linux/kthread.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/vmalloc.h>
31 #include <linux/export.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_device.h>
36 
37 #include "debug.h"
38 #include "rts51x.h"
39 #include "rts51x_chip.h"
40 #include "rts51x_scsi.h"
41 #include "rts51x_card.h"
42 #include "rts51x_transport.h"
43 #include "sd_cprm.h"
44 #include "ms_mg.h"
45 #include "trace.h"
46 
48 {
49  char *what = NULL;
50  int i, unknown_cmd = 0;
51 
52  switch (srb->cmnd[0]) {
53  case TEST_UNIT_READY:
54  what = (char *)"TEST_UNIT_READY";
55  break;
56  case REZERO_UNIT:
57  what = (char *)"REZERO_UNIT";
58  break;
59  case REQUEST_SENSE:
60  what = (char *)"REQUEST_SENSE";
61  break;
62  case FORMAT_UNIT:
63  what = (char *)"FORMAT_UNIT";
64  break;
65  case READ_BLOCK_LIMITS:
66  what = (char *)"READ_BLOCK_LIMITS";
67  break;
68  case 0x07:
69  what = (char *)"REASSIGN_BLOCKS";
70  break;
71  case READ_6:
72  what = (char *)"READ_6";
73  break;
74  case WRITE_6:
75  what = (char *)"WRITE_6";
76  break;
77  case SEEK_6:
78  what = (char *)"SEEK_6";
79  break;
80  case READ_REVERSE:
81  what = (char *)"READ_REVERSE";
82  break;
83  case WRITE_FILEMARKS:
84  what = (char *)"WRITE_FILEMARKS";
85  break;
86  case SPACE:
87  what = (char *)"SPACE";
88  break;
89  case INQUIRY:
90  what = (char *)"INQUIRY";
91  break;
93  what = (char *)"RECOVER_BUFFERED_DATA";
94  break;
95  case MODE_SELECT:
96  what = (char *)"MODE_SELECT";
97  break;
98  case RESERVE:
99  what = (char *)"RESERVE";
100  break;
101  case RELEASE:
102  what = (char *)"RELEASE";
103  break;
104  case COPY:
105  what = (char *)"COPY";
106  break;
107  case ERASE:
108  what = (char *)"ERASE";
109  break;
110  case MODE_SENSE:
111  what = (char *)"MODE_SENSE";
112  break;
113  case START_STOP:
114  what = (char *)"START_STOP";
115  break;
116  case RECEIVE_DIAGNOSTIC:
117  what = (char *)"RECEIVE_DIAGNOSTIC";
118  break;
119  case SEND_DIAGNOSTIC:
120  what = (char *)"SEND_DIAGNOSTIC";
121  break;
123  what = (char *)"ALLOW_MEDIUM_REMOVAL";
124  break;
125  case SET_WINDOW:
126  what = (char *)"SET_WINDOW";
127  break;
128  case READ_CAPACITY:
129  what = (char *)"READ_CAPACITY";
130  break;
131  case READ_10:
132  what = (char *)"READ_10";
133  break;
134  case WRITE_10:
135  what = (char *)"WRITE_10";
136  break;
137  case SEEK_10:
138  what = (char *)"SEEK_10";
139  break;
140  case WRITE_VERIFY:
141  what = (char *)"WRITE_VERIFY";
142  break;
143  case VERIFY:
144  what = (char *)"VERIFY";
145  break;
146  case SEARCH_HIGH:
147  what = (char *)"SEARCH_HIGH";
148  break;
149  case SEARCH_EQUAL:
150  what = (char *)"SEARCH_EQUAL";
151  break;
152  case SEARCH_LOW:
153  what = (char *)"SEARCH_LOW";
154  break;
155  case SET_LIMITS:
156  what = (char *)"SET_LIMITS";
157  break;
158  case READ_POSITION:
159  what = (char *)"READ_POSITION";
160  break;
161  case SYNCHRONIZE_CACHE:
162  what = (char *)"SYNCHRONIZE_CACHE";
163  break;
164  case LOCK_UNLOCK_CACHE:
165  what = (char *)"LOCK_UNLOCK_CACHE";
166  break;
167  case READ_DEFECT_DATA:
168  what = (char *)"READ_DEFECT_DATA";
169  break;
170  case MEDIUM_SCAN:
171  what = (char *)"MEDIUM_SCAN";
172  break;
173  case COMPARE:
174  what = (char *)"COMPARE";
175  break;
176  case COPY_VERIFY:
177  what = (char *)"COPY_VERIFY";
178  break;
179  case WRITE_BUFFER:
180  what = (char *)"WRITE_BUFFER";
181  break;
182  case READ_BUFFER:
183  what = (char *)"READ_BUFFER";
184  break;
185  case UPDATE_BLOCK:
186  what = (char *)"UPDATE_BLOCK";
187  break;
188  case READ_LONG:
189  what = (char *)"READ_LONG";
190  break;
191  case WRITE_LONG:
192  what = (char *)"WRITE_LONG";
193  break;
194  case CHANGE_DEFINITION:
195  what = (char *)"CHANGE_DEFINITION";
196  break;
197  case WRITE_SAME:
198  what = (char *)"WRITE_SAME";
199  break;
201  what = (char *)"READ SUBCHANNEL";
202  break;
203  case READ_TOC:
204  what = (char *)"READ_TOC";
205  break;
206  case GPCMD_READ_HEADER:
207  what = (char *)"READ HEADER";
208  break;
209  case GPCMD_PLAY_AUDIO_10:
210  what = (char *)"PLAY AUDIO (10)";
211  break;
213  what = (char *)"PLAY AUDIO MSF";
214  break;
216  what = (char *)"GET EVENT/STATUS NOTIFICATION";
217  break;
218  case GPCMD_PAUSE_RESUME:
219  what = (char *)"PAUSE/RESUME";
220  break;
221  case LOG_SELECT:
222  what = (char *)"LOG_SELECT";
223  break;
224  case LOG_SENSE:
225  what = (char *)"LOG_SENSE";
226  break;
228  what = (char *)"STOP PLAY/SCAN";
229  break;
231  what = (char *)"READ DISC INFORMATION";
232  break;
234  what = (char *)"READ TRACK INFORMATION";
235  break;
237  what = (char *)"RESERVE TRACK";
238  break;
239  case GPCMD_SEND_OPC:
240  what = (char *)"SEND OPC";
241  break;
242  case MODE_SELECT_10:
243  what = (char *)"MODE_SELECT_10";
244  break;
246  what = (char *)"REPAIR TRACK";
247  break;
248  case 0x59:
249  what = (char *)"READ MASTER CUE";
250  break;
251  case MODE_SENSE_10:
252  what = (char *)"MODE_SENSE_10";
253  break;
254  case GPCMD_CLOSE_TRACK:
255  what = (char *)"CLOSE TRACK/SESSION";
256  break;
257  case 0x5C:
258  what = (char *)"READ BUFFER CAPACITY";
259  break;
260  case 0x5D:
261  what = (char *)"SEND CUE SHEET";
262  break;
263  case GPCMD_BLANK:
264  what = (char *)"BLANK";
265  break;
266  case REPORT_LUNS:
267  what = (char *)"REPORT LUNS";
268  break;
269  case MOVE_MEDIUM:
270  what = (char *)"MOVE_MEDIUM or PLAY AUDIO (12)";
271  break;
272  case READ_12:
273  what = (char *)"READ_12";
274  break;
275  case WRITE_12:
276  what = (char *)"WRITE_12";
277  break;
278  case WRITE_VERIFY_12:
279  what = (char *)"WRITE_VERIFY_12";
280  break;
281  case SEARCH_HIGH_12:
282  what = (char *)"SEARCH_HIGH_12";
283  break;
284  case SEARCH_EQUAL_12:
285  what = (char *)"SEARCH_EQUAL_12";
286  break;
287  case SEARCH_LOW_12:
288  what = (char *)"SEARCH_LOW_12";
289  break;
290  case SEND_VOLUME_TAG:
291  what = (char *)"SEND_VOLUME_TAG";
292  break;
293  case READ_ELEMENT_STATUS:
294  what = (char *)"READ_ELEMENT_STATUS";
295  break;
296  case GPCMD_READ_CD_MSF:
297  what = (char *)"READ CD MSF";
298  break;
299  case GPCMD_SCAN:
300  what = (char *)"SCAN";
301  break;
302  case GPCMD_SET_SPEED:
303  what = (char *)"SET CD SPEED";
304  break;
306  what = (char *)"MECHANISM STATUS";
307  break;
308  case GPCMD_READ_CD:
309  what = (char *)"READ CD";
310  break;
311  case 0xE1:
312  what = (char *)"WRITE CONTINUE";
313  break;
314  case WRITE_LONG_2:
315  what = (char *)"WRITE_LONG_2";
316  break;
317  case VENDOR_CMND:
318  what = (char *)"Realtek's vendor command";
319  break;
320  default:
321  what = (char *)"(unknown command)";
322  unknown_cmd = 1;
323  break;
324  }
325 
326  if (srb->cmnd[0] != TEST_UNIT_READY)
327  RTS51X_DEBUGP("Command %s (%d bytes)\n", what, srb->cmd_len);
328  if (unknown_cmd) {
329  RTS51X_DEBUGP("");
330  for (i = 0; i < srb->cmd_len && i < 16; i++)
331  RTS51X_DEBUGPN(" %02x", srb->cmnd[i]);
332  RTS51X_DEBUGPN("\n");
333  }
334 }
335 
336 void set_sense_type(struct rts51x_chip *chip, unsigned int lun, int sense_type)
337 {
338  switch (sense_type) {
340  set_sense_data(chip, lun, CUR_ERR, 0x06, 0, 0x28, 0, 0, 0);
341  break;
342 
344  set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x3A, 0, 0, 0);
345  break;
346 
348  set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x21, 0, 0, 0);
349  break;
350 
352  set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x25, 0, 0, 0);
353  break;
354 
356  set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x27, 0, 0, 0);
357  break;
358 
360  set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x11, 0, 0, 0);
361  break;
362 
364  set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x02, 0, 0);
365  break;
366 
368  set_sense_data(chip, lun, CUR_ERR, ILGAL_REQ, 0,
370  break;
371 
373  set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x31, 0x01, 0, 0);
374  break;
375 
376 #ifdef SUPPORT_MAGIC_GATE
378  set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x02, 0, 0);
379  break;
380 
382  set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x00, 0, 0);
383  break;
384 
386  set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x30, 0x00, 0, 0);
387  break;
388 
390  set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x00, 0, 0);
391  break;
392 #endif
393 
394  case SENSE_TYPE_NO_SENSE:
395  default:
396  set_sense_data(chip, lun, CUR_ERR, 0, 0, 0, 0, 0, 0);
397  break;
398  }
399 }
400 
401 void set_sense_data(struct rts51x_chip *chip, unsigned int lun, u8 err_code,
402  u8 sense_key, u32 info, u8 asc, u8 ascq, u8 sns_key_info0,
403  u16 sns_key_info1)
404 {
405  struct sense_data_t *sense = &(chip->sense_buffer[lun]);
406 
407  sense->err_code = err_code;
408  sense->sense_key = sense_key;
409  sense->info[0] = (u8) (info >> 24);
410  sense->info[1] = (u8) (info >> 16);
411  sense->info[2] = (u8) (info >> 8);
412  sense->info[3] = (u8) info;
413 
414  sense->ad_sense_len = sizeof(struct sense_data_t) - 8;
415  sense->asc = asc;
416  sense->ascq = ascq;
417  if (sns_key_info0 != 0) {
418  sense->sns_key_info[0] = SKSV | sns_key_info0;
419  sense->sns_key_info[1] = (sns_key_info1 & 0xf0) >> 8;
420  sense->sns_key_info[2] = sns_key_info1 & 0x0f;
421  }
422 }
423 
424 static int test_unit_ready(struct scsi_cmnd *srb, struct rts51x_chip *chip)
425 {
426  unsigned int lun = SCSI_LUN(srb);
427 
428  rts51x_init_cards(chip);
429 
430  if (!check_card_ready(chip, lun)) {
432  return TRANSPORT_FAILED;
433  }
434 
435  if (!check_lun_mc(chip, lun)) {
436  set_lun_mc(chip, lun);
438  return TRANSPORT_FAILED;
439  }
440 
441  return TRANSPORT_GOOD;
442 }
443 
444 unsigned char formatter_inquiry_str[20] = {
445  'M', 'E', 'M', 'O', 'R', 'Y', 'S', 'T', 'I', 'C', 'K',
446  '-', 'M', 'G', /* Byte[47:49] */
447  0x0B, /* Byte[50]: MG, MS, MSPro, MSXC */
448  0x00, /* Byte[51]: Category Specific Commands */
449  0x00, /* Byte[52]: Access Control and feature */
450  0x20, 0x20, 0x20, /* Byte[53:55] */
451 };
452 
453 static int inquiry(struct scsi_cmnd *srb, struct rts51x_chip *chip)
454 {
455  unsigned int lun = SCSI_LUN(srb);
456  char *inquiry_default = (char *)"Generic-xD/SD/M.S. 1.00 ";
457  char *inquiry_string;
458  unsigned char sendbytes;
459  unsigned char *buf;
460  u8 card = get_lun_card(chip, lun);
461  int pro_formatter_flag = 0;
462  unsigned char inquiry_buf[] = {
464  RMB_DISC | 0x0D,
465  0x00,
466  0x01,
467  0x1f,
468  0x02,
469  0,
471  };
472 
473  inquiry_string = inquiry_default;
474 
475  buf = vmalloc(scsi_bufflen(srb));
476  if (buf == NULL)
477  TRACE_RET(chip, TRANSPORT_ERROR);
478 
479  if (MS_FORMATTER_ENABLED(chip) && (get_lun2card(chip, lun) & MS_CARD)) {
480  if (!card || (card == MS_CARD))
481  pro_formatter_flag = 1;
482  }
483 
484  if (pro_formatter_flag) {
485  if (scsi_bufflen(srb) < 56)
486  sendbytes = (unsigned char)(scsi_bufflen(srb));
487  else
488  sendbytes = 56;
489  } else {
490  if (scsi_bufflen(srb) < 36)
491  sendbytes = (unsigned char)(scsi_bufflen(srb));
492  else
493  sendbytes = 36;
494  }
495 
496  if (sendbytes > 8) {
497  memcpy(buf, inquiry_buf, 8);
498  memcpy(buf + 8, inquiry_string, sendbytes - 8);
499  if (pro_formatter_flag)
500  buf[4] = 0x33; /* Additional Length */
501  } else {
502  memcpy(buf, inquiry_buf, sendbytes);
503  }
504 
505  if (pro_formatter_flag) {
506  if (sendbytes > 36)
507  memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
508  }
509 
510  scsi_set_resid(srb, 0);
511 
512  rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
513  vfree(buf);
514 
515  return TRANSPORT_GOOD;
516 }
517 
518 static int start_stop_unit(struct scsi_cmnd *srb, struct rts51x_chip *chip)
519 {
520  unsigned int lun = SCSI_LUN(srb);
521 
522  scsi_set_resid(srb, scsi_bufflen(srb));
523 
524  if (srb->cmnd[1] == 1)
525  return TRANSPORT_GOOD;
526 
527  switch (srb->cmnd[0x4]) {
528  case STOP_MEDIUM:
529  /* Media disabled */
530  return TRANSPORT_GOOD;
531 
532  case UNLOAD_MEDIUM:
533  /* Media shall be unload */
534  if (check_card_ready(chip, lun))
535  eject_card(chip, lun);
536  return TRANSPORT_GOOD;
537 
538  case MAKE_MEDIUM_READY:
539  case LOAD_MEDIUM:
540  if (check_card_ready(chip, lun)) {
541  return TRANSPORT_GOOD;
542  } else {
545  }
546 
547  break;
548  }
549 
550  TRACE_RET(chip, TRANSPORT_ERROR);
551 }
552 
553 static int allow_medium_removal(struct scsi_cmnd *srb, struct rts51x_chip *chip)
554 {
555  int prevent;
556 
557  prevent = srb->cmnd[4] & 0x1;
558 
559  scsi_set_resid(srb, 0);
560 
561  if (prevent) {
562  set_sense_type(chip, SCSI_LUN(srb),
565  }
566 
567  return TRANSPORT_GOOD;
568 }
569 
570 static void ms_mode_sense(struct rts51x_chip *chip, u8 cmd,
571  int lun, u8 *buf, int buf_len)
572 {
573  struct ms_info *ms_card = &(chip->ms_card);
574  int sys_info_offset;
575  int data_size = buf_len;
576  int support_format = 0;
577  int i = 0;
578 
579  if (cmd == MODE_SENSE) {
580  sys_info_offset = 8;
581  if (data_size > 0x68)
582  data_size = 0x68;
583  buf[i++] = 0x67; /* Mode Data Length */
584  } else {
585  sys_info_offset = 12;
586  if (data_size > 0x6C)
587  data_size = 0x6C;
588  buf[i++] = 0x00; /* Mode Data Length (MSB) */
589  buf[i++] = 0x6A; /* Mode Data Length (LSB) */
590  }
591 
592  /* Medium Type Code */
593  if (check_card_ready(chip, lun)) {
594  if (CHK_MSXC(ms_card)) {
595  support_format = 1;
596  buf[i++] = 0x40;
597  } else if (CHK_MSPRO(ms_card)) {
598  support_format = 1;
599  buf[i++] = 0x20;
600  } else {
601  buf[i++] = 0x10;
602  }
603 
604  /* WP */
605  if (check_card_wp(chip, lun))
606  buf[i++] = 0x80;
607  else
608  buf[i++] = 0x00;
609  } else {
610  buf[i++] = 0x00; /* MediaType */
611  buf[i++] = 0x00; /* WP */
612  }
613 
614  buf[i++] = 0x00; /* Reserved */
615 
616  if (cmd == MODE_SENSE_10) {
617  buf[i++] = 0x00; /* Reserved */
618  buf[i++] = 0x00; /* Block descriptor length(MSB) */
619  buf[i++] = 0x00; /* Block descriptor length(LSB) */
620 
621  /* The Following Data is the content of "Page 0x20" */
622  if (data_size >= 9)
623  buf[i++] = 0x20; /* Page Code */
624  if (data_size >= 10)
625  buf[i++] = 0x62; /* Page Length */
626  if (data_size >= 11)
627  buf[i++] = 0x00; /* No Access Control */
628  if (data_size >= 12) {
629  if (support_format)
630  buf[i++] = 0xC0; /* SF, SGM */
631  else
632  buf[i++] = 0x00;
633  }
634  } else {
635  /* The Following Data is the content of "Page 0x20" */
636  if (data_size >= 5)
637  buf[i++] = 0x20; /* Page Code */
638  if (data_size >= 6)
639  buf[i++] = 0x62; /* Page Length */
640  if (data_size >= 7)
641  buf[i++] = 0x00; /* No Access Control */
642  if (data_size >= 8) {
643  if (support_format)
644  buf[i++] = 0xC0; /* SF, SGM */
645  else
646  buf[i++] = 0x00;
647  }
648  }
649 
650  if (data_size > sys_info_offset) {
651  /* 96 Bytes Attribute Data */
652  int len = data_size - sys_info_offset;
653  len = (len < 96) ? len : 96;
654 
655  memcpy(buf + sys_info_offset, ms_card->raw_sys_info, len);
656  }
657 }
658 
659 static int mode_sense(struct scsi_cmnd *srb, struct rts51x_chip *chip)
660 {
661  unsigned int lun = SCSI_LUN(srb);
662  unsigned int dataSize;
663  int status;
664  int pro_formatter_flag;
665  unsigned char pageCode, *buf;
666  u8 card = get_lun_card(chip, lun);
667 
668  if (!check_card_ready(chip, lun)) {
670  scsi_set_resid(srb, scsi_bufflen(srb));
672  }
673 
674  pro_formatter_flag = 0;
675  dataSize = 8;
676  /* In Combo mode, device responses ModeSense command as a MS LUN
677  * when no card is inserted */
678  if ((get_lun2card(chip, lun) & MS_CARD)) {
679  if (!card || (card == MS_CARD)) {
680  dataSize = 108;
681  if (chip->option.mspro_formatter_enable)
682  pro_formatter_flag = 1;
683  }
684  }
685 
686  buf = kmalloc(dataSize, GFP_KERNEL);
687  if (buf == NULL)
688  TRACE_RET(chip, TRANSPORT_ERROR);
689 
690  pageCode = srb->cmnd[2] & 0x3f;
691 
692  if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
693  (pageCode == 0x00) || (pro_formatter_flag && (pageCode == 0x20))) {
694  if (srb->cmnd[0] == MODE_SENSE) {
695  if ((pageCode == 0x3F) || (pageCode == 0x20)) {
696  ms_mode_sense(chip, srb->cmnd[0], lun, buf,
697  dataSize);
698  } else {
699  dataSize = 4;
700  buf[0] = 0x03;
701  buf[1] = 0x00;
702  if (check_card_wp(chip, lun))
703  buf[2] = 0x80;
704  else
705  buf[3] = 0x00;
706  }
707  } else {
708  if ((pageCode == 0x3F) || (pageCode == 0x20)) {
709  ms_mode_sense(chip, srb->cmnd[0], lun, buf,
710  dataSize);
711  } else {
712  dataSize = 8;
713  buf[0] = 0x00;
714  buf[1] = 0x06;
715  buf[2] = 0x00;
716  if (check_card_wp(chip, lun))
717  buf[3] = 0x80;
718  else
719  buf[3] = 0x00;
720  buf[4] = 0x00;
721  buf[5] = 0x00;
722  buf[6] = 0x00;
723  buf[7] = 0x00;
724  }
725  }
726  status = TRANSPORT_GOOD;
727  } else {
729  scsi_set_resid(srb, scsi_bufflen(srb));
730  status = TRANSPORT_FAILED;
731  }
732 
733  if (status == TRANSPORT_GOOD) {
734  unsigned int len = min(scsi_bufflen(srb), dataSize);
735  rts51x_set_xfer_buf(buf, len, srb);
736  scsi_set_resid(srb, scsi_bufflen(srb) - len);
737  }
738  kfree(buf);
739 
740  return status;
741 }
742 
743 static int request_sense(struct scsi_cmnd *srb, struct rts51x_chip *chip)
744 {
745  struct sense_data_t *sense;
746  unsigned int lun = SCSI_LUN(srb);
747  struct ms_info *ms_card = &(chip->ms_card);
748  unsigned char *tmp, *buf;
749 
750  sense = &(chip->sense_buffer[lun]);
751 
752  if ((get_lun_card(chip, lun) == MS_CARD)
753  && PRO_UNDER_FORMATTING(ms_card)) {
754  mspro_format_sense(chip, lun);
755  }
756 
757  buf = vmalloc(scsi_bufflen(srb));
758  if (buf == NULL)
759  TRACE_RET(chip, TRANSPORT_ERROR);
760 
761  tmp = (unsigned char *)sense;
762  memcpy(buf, tmp, scsi_bufflen(srb));
763 
764  rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
765  vfree(buf);
766 
767  scsi_set_resid(srb, 0);
768  /* Reset Sense Data */
770  return TRANSPORT_GOOD;
771 }
772 
773 static int read_write(struct scsi_cmnd *srb, struct rts51x_chip *chip)
774 {
775  unsigned int lun = SCSI_LUN(srb);
776  int retval;
777  u32 start_sec;
778  u16 sec_cnt;
779 
780  if (!check_card_ready(chip, lun) || (chip->capacity[lun] == 0)) {
783  }
784 
785  if (!check_lun_mc(chip, lun)) {
786  set_lun_mc(chip, lun);
788  return TRANSPORT_FAILED;
789  }
790 
791  rts51x_prepare_run(chip);
792  RTS51X_SET_STAT(chip, STAT_RUN);
793 
794  if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10)) {
795  start_sec =
796  ((u32) srb->cmnd[2] << 24) |
797  ((u32) srb->cmnd[3] << 16) |
798  ((u32) srb->cmnd[4] << 8) |
799  ((u32) srb->cmnd[5]);
800  sec_cnt = ((u16) (srb->cmnd[7]) << 8) | srb->cmnd[8];
801  } else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6)) {
802  start_sec = ((u32) (srb->cmnd[1] & 0x1F) << 16) |
803  ((u32) srb->cmnd[2] << 8) | ((u32) srb->cmnd[3]);
804  sec_cnt = srb->cmnd[4];
805  } else if ((srb->cmnd[0] == VENDOR_CMND) &&
806  (srb->cmnd[1] == SCSI_APP_CMD) &&
807  ((srb->cmnd[2] == PP_READ10) ||
808  (srb->cmnd[2] == PP_WRITE10))) {
809  start_sec = ((u32) srb->cmnd[4] << 24) |
810  ((u32) srb->cmnd[5] << 16) |
811  ((u32) srb->cmnd[6] << 8) |
812  ((u32) srb->cmnd[7]);
813  sec_cnt = ((u16) (srb->cmnd[9]) << 8) | srb->cmnd[10];
814  } else {
817  }
818 
819  if ((start_sec > chip->capacity[lun]) ||
820  ((start_sec + sec_cnt) > chip->capacity[lun])) {
823  }
824 
825  if (sec_cnt == 0) {
826  scsi_set_resid(srb, 0);
827  return TRANSPORT_GOOD;
828  }
829 
830  if ((srb->sc_data_direction == DMA_TO_DEVICE)
831  && check_card_wp(chip, lun)) {
832  RTS51X_DEBUGP("Write protected card!\n");
835  }
836 
837  retval = card_rw(srb, chip, start_sec, sec_cnt);
838  if (retval != STATUS_SUCCESS) {
839  if (srb->sc_data_direction == DMA_FROM_DEVICE) {
840  set_sense_type(chip, lun,
842  } else {
844  }
846  }
847 
848  scsi_set_resid(srb, 0);
849 
850  return TRANSPORT_GOOD;
851 }
852 
853 static int read_format_capacity(struct scsi_cmnd *srb, struct rts51x_chip *chip)
854 {
855  unsigned char *buf;
856  unsigned int lun = SCSI_LUN(srb);
857  unsigned int buf_len;
858  u8 card = get_lun_card(chip, lun);
859  int desc_cnt;
860  int i = 0;
861 
862  if (!check_card_ready(chip, lun)) {
863  if (!chip->option.mspro_formatter_enable) {
866  }
867  }
868 
869  buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
870 
871  buf = kmalloc(buf_len, GFP_KERNEL);
872  if (buf == NULL)
873  TRACE_RET(chip, TRANSPORT_ERROR);
874 
875  buf[i++] = 0;
876  buf[i++] = 0;
877  buf[i++] = 0;
878 
879  /* Capacity List Length */
880  if ((buf_len > 12) && chip->option.mspro_formatter_enable &&
881  (chip->lun2card[lun] & MS_CARD) && (!card || (card == MS_CARD))) {
882  buf[i++] = 0x10;
883  desc_cnt = 2;
884  } else {
885  buf[i++] = 0x08;
886  desc_cnt = 1;
887  }
888 
889  while (desc_cnt) {
890  if (check_card_ready(chip, lun)) {
891  buf[i++] = (unsigned char)((chip->capacity[lun]) >> 24);
892  buf[i++] = (unsigned char)((chip->capacity[lun]) >> 16);
893  buf[i++] = (unsigned char)((chip->capacity[lun]) >> 8);
894  buf[i++] = (unsigned char)(chip->capacity[lun]);
895 
896  if (desc_cnt == 2)
897  /* Byte[8]: Descriptor Type: Formatted medium */
898  buf[i++] = 2;
899  else
900  buf[i++] = 0; /* Byte[16] */
901  } else {
902  buf[i++] = 0xFF;
903  buf[i++] = 0xFF;
904  buf[i++] = 0xFF;
905  buf[i++] = 0xFF;
906 
907  if (desc_cnt == 2)
908  /* Byte[8]: Descriptor Type: No medium */
909  buf[i++] = 3;
910  else
911  buf[i++] = 0; /*Byte[16] */
912  }
913 
914  buf[i++] = 0x00;
915  buf[i++] = 0x02;
916  buf[i++] = 0x00;
917 
918  desc_cnt--;
919  }
920 
921  buf_len = min(scsi_bufflen(srb), buf_len);
922  rts51x_set_xfer_buf(buf, buf_len, srb);
923  kfree(buf);
924 
925  scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
926 
927  return TRANSPORT_GOOD;
928 }
929 
930 static int read_capacity(struct scsi_cmnd *srb, struct rts51x_chip *chip)
931 {
932  unsigned char *buf;
933  unsigned int lun = SCSI_LUN(srb);
934 
935  if (!check_card_ready(chip, lun)) {
938  }
939 
940  if (!check_lun_mc(chip, lun)) {
941  set_lun_mc(chip, lun);
943  return TRANSPORT_FAILED;
944  }
945 
946  buf = kmalloc(8, GFP_KERNEL);
947  if (buf == NULL)
948  TRACE_RET(chip, TRANSPORT_ERROR);
949 
950  buf[0] = (unsigned char)((chip->capacity[lun] - 1) >> 24);
951  buf[1] = (unsigned char)((chip->capacity[lun] - 1) >> 16);
952  buf[2] = (unsigned char)((chip->capacity[lun] - 1) >> 8);
953  buf[3] = (unsigned char)(chip->capacity[lun] - 1);
954 
955  buf[4] = 0x00;
956  buf[5] = 0x00;
957  buf[6] = 0x02;
958  buf[7] = 0x00;
959 
960  rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
961  kfree(buf);
962 
963  scsi_set_resid(srb, 0);
964 
965  return TRANSPORT_GOOD;
966 }
967 
968 static int get_dev_status(struct scsi_cmnd *srb, struct rts51x_chip *chip)
969 {
970  unsigned int lun = SCSI_LUN(srb);
971  unsigned int buf_len;
972  u8 status[32] = { 0 };
973 
974  rts51x_pp_status(chip, lun, status, 32);
975 
976  buf_len = min(scsi_bufflen(srb), (unsigned int)sizeof(status));
977  rts51x_set_xfer_buf(status, buf_len, srb);
978  scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
979 
980  return TRANSPORT_GOOD;
981 }
982 
983 static int read_status(struct scsi_cmnd *srb, struct rts51x_chip *chip)
984 {
985  u8 rts51x_status[16];
986  unsigned int buf_len;
987  unsigned int lun = SCSI_LUN(srb);
988 
989  rts51x_read_status(chip, lun, rts51x_status, 16);
990 
991  buf_len = min(scsi_bufflen(srb), (unsigned int)sizeof(rts51x_status));
992  rts51x_set_xfer_buf(rts51x_status, buf_len, srb);
993  scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
994 
995  return TRANSPORT_GOOD;
996 }
997 
998 static int read_mem(struct scsi_cmnd *srb, struct rts51x_chip *chip)
999 {
1000  unsigned int lun = SCSI_LUN(srb);
1001  unsigned short addr, len, i;
1002  int retval;
1003  u8 *buf;
1004 
1005  rts51x_prepare_run(chip);
1006  RTS51X_SET_STAT(chip, STAT_RUN);
1007 
1008  addr = ((u16) srb->cmnd[2] << 8) | srb->cmnd[3];
1009  len = ((u16) srb->cmnd[4] << 8) | srb->cmnd[5];
1010 
1011  if (addr < 0xe000) {
1012  RTS51X_DEBUGP("filter!addr=0x%x\n", addr);
1013  return TRANSPORT_GOOD;
1014  }
1015 
1016  buf = vmalloc(len);
1017  if (!buf)
1018  TRACE_RET(chip, TRANSPORT_ERROR);
1019 
1020  for (i = 0; i < len; i++) {
1021  retval = rts51x_ep0_read_register(chip, addr + i, buf + i);
1022  if (retval != STATUS_SUCCESS) {
1023  vfree(buf);
1024  set_sense_type(chip, lun,
1026  TRACE_RET(chip, TRANSPORT_FAILED);
1027  }
1028  }
1029 
1030  len = (unsigned short)min(scsi_bufflen(srb), (unsigned int)len);
1031  rts51x_set_xfer_buf(buf, len, srb);
1032  scsi_set_resid(srb, scsi_bufflen(srb) - len);
1033 
1034  vfree(buf);
1035 
1036  return TRANSPORT_GOOD;
1037 }
1038 
1039 static int write_mem(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1040 {
1041  unsigned int lun = SCSI_LUN(srb);
1042  unsigned short addr, len, i;
1043  int retval;
1044  u8 *buf;
1045 
1046  rts51x_prepare_run(chip);
1047  RTS51X_SET_STAT(chip, STAT_RUN);
1048 
1049  addr = ((u16) srb->cmnd[2] << 8) | srb->cmnd[3];
1050  len = ((u16) srb->cmnd[4] << 8) | srb->cmnd[5];
1051 
1052  if (addr < 0xe000) {
1053  RTS51X_DEBUGP("filter!addr=0x%x\n", addr);
1054  return TRANSPORT_GOOD;
1055  }
1056 
1057  len = (unsigned short)min(scsi_bufflen(srb), (unsigned int)len);
1058  buf = vmalloc(len);
1059  if (!buf)
1060  TRACE_RET(chip, TRANSPORT_ERROR);
1061 
1062  rts51x_get_xfer_buf(buf, len, srb);
1063 
1064  for (i = 0; i < len; i++) {
1065  retval =
1066  rts51x_ep0_write_register(chip, addr + i, 0xFF, buf[i]);
1067  if (retval != STATUS_SUCCESS) {
1068  vfree(buf);
1070  TRACE_RET(chip, TRANSPORT_FAILED);
1071  }
1072  }
1073 
1074  vfree(buf);
1075  scsi_set_resid(srb, scsi_bufflen(srb) - len);
1076 
1077  return TRANSPORT_GOOD;
1078 }
1079 
1080 static int get_sd_csd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1081 {
1082  struct sd_info *sd_card = &(chip->sd_card);
1083  unsigned int lun = SCSI_LUN(srb);
1084 
1085  if (!check_card_ready(chip, lun)) {
1087  TRACE_RET(chip, TRANSPORT_FAILED);
1088  }
1089 
1090  if (get_lun_card(chip, lun) != SD_CARD) {
1092  TRACE_RET(chip, TRANSPORT_FAILED);
1093  }
1094 
1095  scsi_set_resid(srb, 0);
1096  rts51x_set_xfer_buf(sd_card->raw_csd, scsi_bufflen(srb), srb);
1097 
1098  return TRANSPORT_GOOD;
1099 }
1100 
1101 static int read_phy_register(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1102 {
1103  int retval;
1104  u8 addr, len, i;
1105  u8 *buf;
1106 
1107  rts51x_prepare_run(chip);
1108  RTS51X_SET_STAT(chip, STAT_RUN);
1109 
1110  addr = srb->cmnd[5];
1111  len = srb->cmnd[7];
1112 
1113  if (len) {
1114  buf = vmalloc(len);
1115  if (!buf)
1116  TRACE_RET(chip, TRANSPORT_ERROR);
1117 
1118  for (i = 0; i < len; i++) {
1119  retval =
1120  rts51x_read_phy_register(chip, addr + i, buf + i);
1121  if (retval != STATUS_SUCCESS) {
1122  vfree(buf);
1123  set_sense_type(chip, SCSI_LUN(srb),
1125  TRACE_RET(chip, TRANSPORT_FAILED);
1126  }
1127  }
1128 
1129  len = min(scsi_bufflen(srb), (unsigned int)len);
1130  rts51x_set_xfer_buf(buf, len, srb);
1131  scsi_set_resid(srb, scsi_bufflen(srb) - len);
1132 
1133  vfree(buf);
1134  }
1135 
1136  return TRANSPORT_GOOD;
1137 }
1138 
1139 static int write_phy_register(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1140 {
1141  int retval;
1142  u8 addr, len, i;
1143  u8 *buf;
1144 
1145  rts51x_prepare_run(chip);
1146  RTS51X_SET_STAT(chip, STAT_RUN);
1147 
1148  addr = srb->cmnd[5];
1149  len = srb->cmnd[7];
1150 
1151  if (len) {
1152  len = min(scsi_bufflen(srb), (unsigned int)len);
1153 
1154  buf = vmalloc(len);
1155  if (buf == NULL)
1156  TRACE_RET(chip, TRANSPORT_ERROR);
1157 
1158  rts51x_get_xfer_buf(buf, len, srb);
1159  scsi_set_resid(srb, scsi_bufflen(srb) - len);
1160 
1161  for (i = 0; i < len; i++) {
1162  retval =
1163  rts51x_write_phy_register(chip, addr + i, buf[i]);
1164  if (retval != STATUS_SUCCESS) {
1165  vfree(buf);
1166  set_sense_type(chip, SCSI_LUN(srb),
1168  TRACE_RET(chip, TRANSPORT_FAILED);
1169  }
1170  }
1171 
1172  vfree(buf);
1173  }
1174 
1175  return TRANSPORT_GOOD;
1176 }
1177 
1178 static int get_card_bus_width(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1179 {
1180  unsigned int lun = SCSI_LUN(srb);
1181  u8 card, bus_width;
1182 
1183  if (!check_card_ready(chip, lun)) {
1185  TRACE_RET(chip, TRANSPORT_FAILED);
1186  }
1187 
1188  card = get_lun_card(chip, lun);
1189  if ((card == SD_CARD) || (card == MS_CARD)) {
1190  bus_width = chip->card_bus_width[lun];
1191  } else {
1193  TRACE_RET(chip, TRANSPORT_FAILED);
1194  }
1195 
1196  scsi_set_resid(srb, 0);
1197  rts51x_set_xfer_buf(&bus_width, scsi_bufflen(srb), srb);
1198 
1199  return TRANSPORT_GOOD;
1200 }
1201 
1202 #ifdef _MSG_TRACE
1203 static int trace_msg_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1204 {
1205  unsigned char *buf = NULL;
1206  u8 clear;
1207  unsigned int buf_len;
1208 
1209  buf_len =
1210  4 +
1212 
1213  if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) == NULL)) {
1214  set_sense_type(chip, SCSI_LUN(srb),
1216  TRACE_RET(chip, TRANSPORT_FAILED);
1217  }
1218 
1219  clear = srb->cmnd[2];
1220 
1221  buf = vmalloc(scsi_bufflen(srb));
1222  if (buf == NULL)
1223  TRACE_RET(chip, TRANSPORT_ERROR);
1224 
1225  rts51x_trace_msg(chip, buf, clear);
1226 
1227  rts51x_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1228  vfree(buf);
1229 
1230  scsi_set_resid(srb, 0);
1231  return TRANSPORT_GOOD;
1232 }
1233 #endif
1234 
1235 static int rw_mem_cmd_buf(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1236 {
1237  int retval = STATUS_SUCCESS;
1238  unsigned int lun = SCSI_LUN(srb);
1239  u8 cmd_type, mask, value, idx, mode, len;
1240  u16 addr;
1241  u32 timeout;
1242 
1243  rts51x_prepare_run(chip);
1244  RTS51X_SET_STAT(chip, STAT_RUN);
1245 
1246  switch (srb->cmnd[3]) {
1247  case INIT_BATCHCMD:
1248  rts51x_init_cmd(chip);
1249  break;
1250 
1251  case ADD_BATCHCMD:
1252  cmd_type = srb->cmnd[4];
1253  if (cmd_type > 2) {
1254  set_sense_type(chip, lun,
1256  TRACE_RET(chip, TRANSPORT_FAILED);
1257  }
1258  addr = (srb->cmnd[5] << 8) | srb->cmnd[6];
1259  mask = srb->cmnd[7];
1260  value = srb->cmnd[8];
1261  rts51x_add_cmd(chip, cmd_type, addr, mask, value);
1262  break;
1263 
1264  case SEND_BATCHCMD:
1265  mode = srb->cmnd[4];
1266  len = srb->cmnd[5];
1267  timeout =
1268  ((u32) srb->cmnd[6] << 24) | ((u32) srb->
1269  cmnd[7] << 16) | ((u32) srb->
1270  cmnd[8] <<
1271  8) | ((u32)
1272  srb->
1273  cmnd
1274  [9]);
1275  retval = rts51x_send_cmd(chip, mode, 1000);
1276  if (retval != STATUS_SUCCESS) {
1278  TRACE_RET(chip, TRANSPORT_FAILED);
1279  }
1280  if (mode & STAGE_R) {
1281  retval = rts51x_get_rsp(chip, len, timeout);
1282  if (retval != STATUS_SUCCESS) {
1283  set_sense_type(chip, lun,
1285  TRACE_RET(chip, TRANSPORT_FAILED);
1286  }
1287  }
1288  break;
1289 
1290  case GET_BATCHRSP:
1291  idx = srb->cmnd[4];
1292  value = chip->rsp_buf[idx];
1293  if (scsi_bufflen(srb) < 1) {
1294  set_sense_type(chip, lun,
1296  TRACE_RET(chip, TRANSPORT_FAILED);
1297  }
1298  rts51x_set_xfer_buf(&value, 1, srb);
1299  scsi_set_resid(srb, 0);
1300  break;
1301 
1302  default:
1304  TRACE_RET(chip, TRANSPORT_FAILED);
1305  }
1306 
1307  if (retval != STATUS_SUCCESS) {
1309  TRACE_RET(chip, TRANSPORT_FAILED);
1310  }
1311 
1312  return TRANSPORT_GOOD;
1313 }
1314 
1315 static int suit_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1316 {
1317  int result;
1318 
1319  switch (srb->cmnd[3]) {
1320  case INIT_BATCHCMD:
1321  case ADD_BATCHCMD:
1322  case SEND_BATCHCMD:
1323  case GET_BATCHRSP:
1324  result = rw_mem_cmd_buf(srb, chip);
1325  break;
1326  default:
1327  result = TRANSPORT_ERROR;
1328  }
1329 
1330  return result;
1331 }
1332 
1333 static int app_cmd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1334 {
1335  int result;
1336 
1337  switch (srb->cmnd[2]) {
1338  case PP_READ10:
1339  case PP_WRITE10:
1340  result = read_write(srb, chip);
1341  break;
1342 
1343  case SUIT_CMD:
1344  result = suit_cmd(srb, chip);
1345  break;
1346 
1347  case READ_PHY:
1348  result = read_phy_register(srb, chip);
1349  break;
1350 
1351  case WRITE_PHY:
1352  result = write_phy_register(srb, chip);
1353  break;
1354 
1355  case GET_DEV_STATUS:
1356  result = get_dev_status(srb, chip);
1357  break;
1358 
1359  default:
1360  set_sense_type(chip, SCSI_LUN(srb),
1362  TRACE_RET(chip, TRANSPORT_FAILED);
1363  }
1364 
1365  return result;
1366 }
1367 
1368 static int vendor_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1369 {
1370  int result = TRANSPORT_GOOD;
1371 
1372  switch (srb->cmnd[1]) {
1373  case READ_STATUS:
1374  result = read_status(srb, chip);
1375  break;
1376 
1377  case READ_MEM:
1378  result = read_mem(srb, chip);
1379  break;
1380 
1381  case WRITE_MEM:
1382  result = write_mem(srb, chip);
1383  break;
1384 
1385  case GET_BUS_WIDTH:
1386  result = get_card_bus_width(srb, chip);
1387  break;
1388 
1389  case GET_SD_CSD:
1390  result = get_sd_csd(srb, chip);
1391  break;
1392 
1393 #ifdef _MSG_TRACE
1394  case TRACE_MSG:
1395  result = trace_msg_cmd(srb, chip);
1396  break;
1397 #endif
1398 
1399  case SCSI_APP_CMD:
1400  result = app_cmd(srb, chip);
1401  break;
1402 
1403  default:
1404  set_sense_type(chip, SCSI_LUN(srb),
1406  TRACE_RET(chip, TRANSPORT_FAILED);
1407  }
1408 
1409  return result;
1410 }
1411 
1412 static int ms_format_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1413 {
1414  struct ms_info *ms_card = &(chip->ms_card);
1415  unsigned int lun = SCSI_LUN(srb);
1416  int retval, quick_format;
1417 
1418  if (get_lun_card(chip, lun) != MS_CARD) {
1420  TRACE_RET(chip, TRANSPORT_FAILED);
1421  }
1422 
1423  if ((srb->cmnd[3] != 0x4D) || (srb->cmnd[4] != 0x47)
1424  || (srb->cmnd[5] != 0x66) || (srb->cmnd[6] != 0x6D)
1425  || (srb->cmnd[7] != 0x74)) {
1427  TRACE_RET(chip, TRANSPORT_FAILED);
1428  }
1429 
1430  if (srb->cmnd[8] & 0x01)
1431  quick_format = 0;
1432  else
1433  quick_format = 1;
1434 
1435  if (!(chip->card_ready & MS_CARD)) {
1437  TRACE_RET(chip, TRANSPORT_FAILED);
1438  }
1439 
1440  if (chip->card_wp & MS_CARD) {
1442  TRACE_RET(chip, TRANSPORT_FAILED);
1443  }
1444 
1445  if (!CHK_MSPRO(ms_card)) {
1447  TRACE_RET(chip, TRANSPORT_FAILED);
1448  }
1449 
1450  rts51x_prepare_run(chip);
1451  RTS51X_SET_STAT(chip, STAT_RUN);
1452 
1453  retval = mspro_format(srb, chip, MS_SHORT_DATA_LEN, quick_format);
1454  if (retval != STATUS_SUCCESS) {
1456  TRACE_RET(chip, TRANSPORT_FAILED);
1457  }
1458 
1459  scsi_set_resid(srb, 0);
1460  return TRANSPORT_GOOD;
1461 }
1462 
1463 #ifdef SUPPORT_PCGL_1P18
1464 static int get_ms_information(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1465 {
1466  struct ms_info *ms_card = &(chip->ms_card);
1467  unsigned int lun = SCSI_LUN(srb);
1468  u8 dev_info_id, data_len;
1469  u8 *buf;
1470  unsigned int buf_len;
1471  int i;
1472 
1473  if (!check_card_ready(chip, lun)) {
1475  TRACE_RET(chip, TRANSPORT_FAILED);
1476  }
1477  if ((get_lun_card(chip, lun) != MS_CARD)) {
1479  TRACE_RET(chip, TRANSPORT_FAILED);
1480  }
1481 
1482  if ((srb->cmnd[2] != 0xB0) || (srb->cmnd[4] != 0x4D) ||
1483  (srb->cmnd[5] != 0x53) || (srb->cmnd[6] != 0x49) ||
1484  (srb->cmnd[7] != 0x44)) {
1486  TRACE_RET(chip, TRANSPORT_FAILED);
1487  }
1488 
1489  dev_info_id = srb->cmnd[3];
1490  if ((CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
1491  (!CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
1492  !CHK_MSPRO(ms_card)) {
1494  TRACE_RET(chip, TRANSPORT_FAILED);
1495  }
1496 
1497  if (dev_info_id == 0x15)
1498  buf_len = data_len = 0x3A;
1499  else
1500  buf_len = data_len = 0x6A;
1501 
1502  buf = kmalloc(buf_len, GFP_KERNEL);
1503  if (!buf)
1504  TRACE_RET(chip, TRANSPORT_ERROR);
1505 
1506  i = 0;
1507  /* GET Memory Stick Media Information Response Header */
1508  buf[i++] = 0x00; /* Data length MSB */
1509  buf[i++] = data_len; /* Data length LSB */
1510  /* Device Information Type Code */
1511  if (CHK_MSXC(ms_card))
1512  buf[i++] = 0x03;
1513  else
1514  buf[i++] = 0x02;
1515  /* SGM bit */
1516  buf[i++] = 0x01;
1517  /* Reserved */
1518  buf[i++] = 0x00;
1519  buf[i++] = 0x00;
1520  buf[i++] = 0x00;
1521  /* Number of Device Information */
1522  buf[i++] = 0x01;
1523 
1524  /* Device Information Body
1525  * Device Information ID Number */
1526  buf[i++] = dev_info_id;
1527  /* Device Information Length */
1528  if (dev_info_id == 0x15)
1529  data_len = 0x31;
1530  else
1531  data_len = 0x61;
1532  buf[i++] = 0x00; /* Data length MSB */
1533  buf[i++] = data_len; /* Data length LSB */
1534  /* Valid Bit */
1535  buf[i++] = 0x80;
1536  if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
1537  /* System Information */
1538  memcpy(buf + i, ms_card->raw_sys_info, 96);
1539  } else {
1540  /* Model Name */
1541  memcpy(buf + i, ms_card->raw_model_name, 48);
1542  }
1543 
1544  rts51x_set_xfer_buf(buf, buf_len, srb);
1545 
1546  if (dev_info_id == 0x15)
1547  scsi_set_resid(srb, scsi_bufflen(srb) - 0x3C);
1548  else
1549  scsi_set_resid(srb, scsi_bufflen(srb) - 0x6C);
1550 
1551  kfree(buf);
1552  return STATUS_SUCCESS;
1553 }
1554 #endif
1555 
1556 static int ms_sp_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1557 {
1558  int retval = TRANSPORT_ERROR;
1559 
1560  if (srb->cmnd[2] == MS_FORMAT)
1561  retval = ms_format_cmnd(srb, chip);
1562 #ifdef SUPPORT_PCGL_1P18
1563  else if (srb->cmnd[2] == GET_MS_INFORMATION)
1564  retval = get_ms_information(srb, chip);
1565 #endif
1566 
1567  return retval;
1568 }
1569 
1570 #ifdef SUPPORT_CPRM
1571 static int sd_extention_cmnd(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1572 {
1573  unsigned int lun = SCSI_LUN(srb);
1574  int result;
1575 
1576  rts51x_prepare_run(chip);
1577  RTS51X_SET_STAT(chip, STAT_RUN);
1578 
1579  sd_cleanup_work(chip);
1580 
1581  if (!check_card_ready(chip, lun)) {
1583  TRACE_RET(chip, TRANSPORT_FAILED);
1584  }
1585  if ((get_lun_card(chip, lun) != SD_CARD)) {
1587  TRACE_RET(chip, TRANSPORT_FAILED);
1588  }
1589 
1590  switch (srb->cmnd[0]) {
1591  case SD_PASS_THRU_MODE:
1592  result = sd_pass_thru_mode(srb, chip);
1593  break;
1594 
1595  case SD_EXECUTE_NO_DATA:
1596  result = sd_execute_no_data(srb, chip);
1597  break;
1598 
1599  case SD_EXECUTE_READ:
1600  result = sd_execute_read_data(srb, chip);
1601  break;
1602 
1603  case SD_EXECUTE_WRITE:
1604  result = sd_execute_write_data(srb, chip);
1605  break;
1606 
1607  case SD_GET_RSP:
1608  result = sd_get_cmd_rsp(srb, chip);
1609  break;
1610 
1611  case SD_HW_RST:
1612  result = sd_hw_rst(srb, chip);
1613  break;
1614 
1615  default:
1617  TRACE_RET(chip, TRANSPORT_FAILED);
1618  }
1619 
1620  return result;
1621 }
1622 #endif
1623 
1624 #ifdef SUPPORT_MAGIC_GATE
1625 static int mg_report_key(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1626 {
1627  struct ms_info *ms_card = &(chip->ms_card);
1628  unsigned int lun = SCSI_LUN(srb);
1629  int retval;
1630  u8 key_format;
1631 
1632  rts51x_prepare_run(chip);
1633  RTS51X_SET_STAT(chip, STAT_RUN);
1634 
1635  ms_cleanup_work(chip);
1636 
1637  if (!check_card_ready(chip, lun)) {
1639  TRACE_RET(chip, TRANSPORT_FAILED);
1640  }
1641  if ((get_lun_card(chip, lun) != MS_CARD)) {
1643  TRACE_RET(chip, TRANSPORT_FAILED);
1644  }
1645 
1646  if (srb->cmnd[7] != KC_MG_R_PRO) {
1648  TRACE_RET(chip, TRANSPORT_FAILED);
1649  }
1650 
1651  if (!CHK_MSPRO(ms_card)) {
1653  TRACE_RET(chip, TRANSPORT_FAILED);
1654  }
1655 
1656  key_format = srb->cmnd[10] & 0x3F;
1657 
1658  switch (key_format) {
1659  case KF_GET_LOC_EKB:
1660  if ((scsi_bufflen(srb) == 0x41C) &&
1661  (srb->cmnd[8] == 0x04) && (srb->cmnd[9] == 0x1C)) {
1662  retval = mg_get_local_EKB(srb, chip);
1663  if (retval != STATUS_SUCCESS)
1664  TRACE_RET(chip, TRANSPORT_FAILED);
1665  } else {
1666  set_sense_type(chip, lun,
1668  TRACE_RET(chip, TRANSPORT_FAILED);
1669  }
1670  break;
1671 
1672  case KF_RSP_CHG:
1673  if ((scsi_bufflen(srb) == 0x24) &&
1674  (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x24)) {
1675  retval = mg_get_rsp_chg(srb, chip);
1676  if (retval != STATUS_SUCCESS)
1677  TRACE_RET(chip, TRANSPORT_FAILED);
1678  } else {
1679  set_sense_type(chip, lun,
1681  TRACE_RET(chip, TRANSPORT_FAILED);
1682  }
1683  break;
1684 
1685  case KF_GET_ICV:
1686  ms_card->mg_entry_num = srb->cmnd[5];
1687  if ((scsi_bufflen(srb) == 0x404) &&
1688  (srb->cmnd[8] == 0x04) &&
1689  (srb->cmnd[9] == 0x04) &&
1690  (srb->cmnd[2] == 0x00) &&
1691  (srb->cmnd[3] == 0x00) &&
1692  (srb->cmnd[4] == 0x00) && (srb->cmnd[5] < 32)) {
1693  retval = mg_get_ICV(srb, chip);
1694  if (retval != STATUS_SUCCESS)
1695  TRACE_RET(chip, TRANSPORT_FAILED);
1696  } else {
1697  set_sense_type(chip, lun,
1699  TRACE_RET(chip, TRANSPORT_FAILED);
1700  }
1701  break;
1702 
1703  default:
1705  TRACE_RET(chip, TRANSPORT_FAILED);
1706  }
1707 
1708  scsi_set_resid(srb, 0);
1709  return TRANSPORT_GOOD;
1710 }
1711 
1712 static int mg_send_key(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1713 {
1714  struct ms_info *ms_card = &(chip->ms_card);
1715  unsigned int lun = SCSI_LUN(srb);
1716  int retval;
1717  u8 key_format;
1718 
1719  rts51x_prepare_run(chip);
1720  RTS51X_SET_STAT(chip, STAT_RUN);
1721 
1722  ms_cleanup_work(chip);
1723 
1724  if (!check_card_ready(chip, lun)) {
1726  TRACE_RET(chip, TRANSPORT_FAILED);
1727  }
1728  if (check_card_wp(chip, lun)) {
1730  TRACE_RET(chip, TRANSPORT_FAILED);
1731  }
1732  if ((get_lun_card(chip, lun) != MS_CARD)) {
1734  TRACE_RET(chip, TRANSPORT_FAILED);
1735  }
1736 
1737  if (srb->cmnd[7] != KC_MG_R_PRO) {
1739  TRACE_RET(chip, TRANSPORT_FAILED);
1740  }
1741 
1742  if (!CHK_MSPRO(ms_card)) {
1744  TRACE_RET(chip, TRANSPORT_FAILED);
1745  }
1746 
1747  key_format = srb->cmnd[10] & 0x3F;
1748 
1749  switch (key_format) {
1750  case KF_SET_LEAF_ID:
1751  if ((scsi_bufflen(srb) == 0x0C) &&
1752  (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1753  retval = mg_set_leaf_id(srb, chip);
1754  if (retval != STATUS_SUCCESS)
1755  TRACE_RET(chip, TRANSPORT_FAILED);
1756  } else {
1757  set_sense_type(chip, lun,
1759  TRACE_RET(chip, TRANSPORT_FAILED);
1760  }
1761  break;
1762 
1763  case KF_CHG_HOST:
1764  if ((scsi_bufflen(srb) == 0x0C) &&
1765  (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1766  retval = mg_chg(srb, chip);
1767  if (retval != STATUS_SUCCESS)
1768  TRACE_RET(chip, TRANSPORT_FAILED);
1769  } else {
1770  set_sense_type(chip, lun,
1772  TRACE_RET(chip, TRANSPORT_FAILED);
1773  }
1774  break;
1775 
1776  case KF_RSP_HOST:
1777  if ((scsi_bufflen(srb) == 0x0C) &&
1778  (srb->cmnd[8] == 0x00) && (srb->cmnd[9] == 0x0C)) {
1779  retval = mg_rsp(srb, chip);
1780  if (retval != STATUS_SUCCESS)
1781  TRACE_RET(chip, TRANSPORT_FAILED);
1782  } else {
1783  set_sense_type(chip, lun,
1785  TRACE_RET(chip, TRANSPORT_FAILED);
1786  }
1787  break;
1788 
1789  case KF_SET_ICV:
1790  ms_card->mg_entry_num = srb->cmnd[5];
1791  if ((scsi_bufflen(srb) == 0x404) &&
1792  (srb->cmnd[8] == 0x04) &&
1793  (srb->cmnd[9] == 0x04) &&
1794  (srb->cmnd[2] == 0x00) &&
1795  (srb->cmnd[3] == 0x00) &&
1796  (srb->cmnd[4] == 0x00) && (srb->cmnd[5] < 32)) {
1797  retval = mg_set_ICV(srb, chip);
1798  if (retval != STATUS_SUCCESS)
1799  TRACE_RET(chip, TRANSPORT_FAILED);
1800  } else {
1801  set_sense_type(chip, lun,
1803  TRACE_RET(chip, TRANSPORT_FAILED);
1804  }
1805  break;
1806 
1807  default:
1809  TRACE_RET(chip, TRANSPORT_FAILED);
1810  }
1811 
1812  scsi_set_resid(srb, 0);
1813  return TRANSPORT_GOOD;
1814 }
1815 #endif
1816 
1817 int rts51x_scsi_handler(struct scsi_cmnd *srb, struct rts51x_chip *chip)
1818 {
1819  struct ms_info *ms_card = &(chip->ms_card);
1820  unsigned int lun = SCSI_LUN(srb);
1821  int result = TRANSPORT_GOOD;
1822 
1823  if ((get_lun_card(chip, lun) == MS_CARD) &&
1824  (ms_card->format_status == FORMAT_IN_PROGRESS)) {
1825  if ((srb->cmnd[0] != REQUEST_SENSE)
1826  && (srb->cmnd[0] != INQUIRY)) {
1827  /* Logical Unit Not Ready Format in Progress */
1828  set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
1829  0, (u16) (ms_card->progress));
1830  TRACE_RET(chip, TRANSPORT_FAILED);
1831  }
1832  }
1833 
1834  switch (srb->cmnd[0]) {
1835  case READ_10:
1836  case WRITE_10:
1837  case READ_6:
1838  case WRITE_6:
1839  result = read_write(srb, chip);
1840  break;
1841 
1842  case TEST_UNIT_READY:
1843  result = test_unit_ready(srb, chip);
1844  break;
1845 
1846  case INQUIRY:
1847  result = inquiry(srb, chip);
1848  break;
1849 
1850  case READ_CAPACITY:
1851  result = read_capacity(srb, chip);
1852  break;
1853 
1854  case START_STOP:
1855  result = start_stop_unit(srb, chip);
1856  break;
1857 
1858  case ALLOW_MEDIUM_REMOVAL:
1859  result = allow_medium_removal(srb, chip);
1860  break;
1861 
1862  case REQUEST_SENSE:
1863  result = request_sense(srb, chip);
1864  break;
1865 
1866  case MODE_SENSE:
1867  case MODE_SENSE_10:
1868  result = mode_sense(srb, chip);
1869  break;
1870 
1871  case 0x23:
1872  result = read_format_capacity(srb, chip);
1873  break;
1874 
1875  case VENDOR_CMND:
1876  result = vendor_cmnd(srb, chip);
1877  break;
1878 
1879  case MS_SP_CMND:
1880  result = ms_sp_cmnd(srb, chip);
1881  break;
1882 
1883 #ifdef SUPPORT_CPRM
1884  case SD_PASS_THRU_MODE:
1885  case SD_EXECUTE_NO_DATA:
1886  case SD_EXECUTE_READ:
1887  case SD_EXECUTE_WRITE:
1888  case SD_GET_RSP:
1889  case SD_HW_RST:
1890  result = sd_extention_cmnd(srb, chip);
1891  break;
1892 #endif
1893 
1894 #ifdef SUPPORT_MAGIC_GATE
1895  case CMD_MSPRO_MG_RKEY:
1896  result = mg_report_key(srb, chip);
1897  break;
1898 
1899  case CMD_MSPRO_MG_SKEY:
1900  result = mg_send_key(srb, chip);
1901  break;
1902 #endif
1903 
1904  case FORMAT_UNIT:
1905  case MODE_SELECT:
1906  case VERIFY:
1907  result = TRANSPORT_GOOD;
1908  break;
1909 
1910  default:
1912  result = TRANSPORT_FAILED;
1913  }
1914 
1915  return result;
1916 }
1917 
1918 /***********************************************************************
1919  * Host functions
1920  ***********************************************************************/
1921 
1922 int slave_alloc(struct scsi_device *sdev)
1923 {
1924  /*
1925  * Set the INQUIRY transfer length to 36. We don't use any of
1926  * the extra data and many devices choke if asked for more or
1927  * less than 36 bytes.
1928  */
1929  sdev->inquiry_len = 36;
1930  return 0;
1931 }
1932 
1933 int slave_configure(struct scsi_device *sdev)
1934 {
1935  /* Scatter-gather buffers (all but the last) must have a length
1936  * divisible by the bulk maxpacket size. Otherwise a data packet
1937  * would end up being short, causing a premature end to the data
1938  * transfer. Since high-speed bulk pipes have a maxpacket size
1939  * of 512, we'll use that as the scsi device queue's DMA alignment
1940  * mask. Guaranteeing proper alignment of the first buffer will
1941  * have the desired effect because, except at the beginning and
1942  * the end, scatter-gather buffers follow page boundaries. */
1943  blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
1944 
1945  /* Set the SCSI level to at least 2. We'll leave it at 3 if that's
1946  * what is originally reported. We need this to avoid confusing
1947  * the SCSI layer with devices that report 0 or 1, but need 10-byte
1948  * commands (ala ATAPI devices behind certain bridges, or devices
1949  * which simply have broken INQUIRY data).
1950  *
1951  * NOTE: This means /dev/sg programs (ala cdrecord) will get the
1952  * actual information. This seems to be the preference for
1953  * programs like that.
1954  *
1955  * NOTE: This also means that /proc/scsi/scsi and sysfs may report
1956  * the actual value or the modified one, depending on where the
1957  * data comes from.
1958  */
1959  if (sdev->scsi_level < SCSI_2)
1960  sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
1961 
1962  return 0;
1963 }
1964 
1965 /***********************************************************************
1966  * /proc/scsi/ functions
1967  ***********************************************************************/
1968 
1969 /* we use this macro to help us write into the buffer */
1970 #undef SPRINTF
1971 #define SPRINTF(args...) \
1972  do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
1973 
1974 int proc_info(struct Scsi_Host *host, char *buffer,
1975  char **start, off_t offset, int length, int inout)
1976 {
1977  char *pos = buffer;
1978 
1979  /* if someone is sending us data, just throw it away */
1980  if (inout)
1981  return length;
1982 
1983  /* print the controller name */
1984  SPRINTF(" Host scsi%d: %s\n", host->host_no, RTS51X_NAME);
1985 
1986  /* print product, vendor, and driver version strings */
1987  SPRINTF(" Vendor: Realtek Corp.\n");
1988  SPRINTF(" Product: RTS51xx USB Card Reader\n");
1989  SPRINTF(" Version: %s\n", DRIVER_VERSION);
1990  SPRINTF(" Build: %s\n", __TIME__);
1991 
1992  /*
1993  * Calculate start of next buffer, and return value.
1994  */
1995  *start = buffer + offset;
1996 
1997  if ((pos - buffer) < offset)
1998  return 0;
1999  else if ((pos - buffer - offset) < length)
2000  return pos - buffer - offset;
2001  else
2002  return length;
2003 }
2004 
2005 /* queue a command */
2006 /* This is always called with scsi_lock(host) held */
2007 int queuecommand_lck(struct scsi_cmnd *srb, void (*done) (struct scsi_cmnd *))
2008 {
2009  struct rts51x_chip *chip = host_to_rts51x(srb->device->host);
2010 
2011  /* check for state-transition errors */
2012  if (chip->srb != NULL) {
2013  RTS51X_DEBUGP("Error in %s: chip->srb = %p\n",
2014  __func__, chip->srb);
2015  return SCSI_MLQUEUE_HOST_BUSY;
2016  }
2017 
2018  /* fail the command if we are disconnecting */
2019  if (test_bit(FLIDX_DISCONNECTING, &chip->usb->dflags)) {
2020  RTS51X_DEBUGP("Fail command during disconnect\n");
2021  srb->result = DID_NO_CONNECT << 16;
2022  done(srb);
2023  return 0;
2024  }
2025 
2026  /* enqueue the command and wake up the control thread */
2027  srb->scsi_done = done;
2028  chip->srb = srb;
2029  complete(&chip->usb->cmnd_ready);
2030 
2031  return 0;
2032 }
2033 
2035 /***********************************************************************
2036  * Error handling functions
2037  ***********************************************************************/
2038 /* Command timeout and abort */
2040 {
2041  struct rts51x_chip *chip = host_to_rts51x(srb->device->host);
2042 
2043  RTS51X_DEBUGP("%s called\n", __func__);
2044 
2045  /* us->srb together with the TIMED_OUT, RESETTING, and ABORTING
2046  * bits are protected by the host lock. */
2047  scsi_lock(rts51x_to_host(chip));
2048 
2049  /* Is this command still active? */
2050  if (chip->srb != srb) {
2051  scsi_unlock(rts51x_to_host(chip));
2052  RTS51X_DEBUGP("-- nothing to abort\n");
2053  return FAILED;
2054  }
2055 
2056  /* Set the TIMED_OUT bit. Also set the ABORTING bit, but only if
2057  * a device reset isn't already in progress (to avoid interfering
2058  * with the reset). Note that we must retain the host lock while
2059  * calling usb_stor_stop_transport(); otherwise it might interfere
2060  * with an auto-reset that begins as soon as we release the lock. */
2061  set_bit(FLIDX_TIMED_OUT, &chip->usb->dflags);
2062  if (!test_bit(FLIDX_RESETTING, &chip->usb->dflags)) {
2063  set_bit(FLIDX_ABORTING, &chip->usb->dflags);
2064  /* rts51x_stop_transport(us); */
2065  }
2066  scsi_unlock(rts51x_to_host(chip));
2067 
2068  /* Wait for the aborted command to finish */
2069  wait_for_completion(&chip->usb->notify);
2070  return SUCCESS;
2071 }
2072 
2073 /* This invokes the transport reset mechanism to reset the state of the
2074  * device */
2075 int device_reset(struct scsi_cmnd *srb)
2076 {
2077  int result = 0;
2078 
2079  RTS51X_DEBUGP("%s called\n", __func__);
2080 
2081  return result < 0 ? FAILED : SUCCESS;
2082 }
2083 
2084 /* Simulate a SCSI bus reset by resetting the device's USB port. */
2085 int bus_reset(struct scsi_cmnd *srb)
2086 {
2087  int result = 0;
2088 
2089  RTS51X_DEBUGP("%s called\n", __func__);
2090 
2091  return result < 0 ? FAILED : SUCCESS;
2092 }
2093 
2094 static const char *rts5139_info(struct Scsi_Host *host)
2095 {
2096  return "SCSI emulation for RTS5139 USB card reader";
2097 }
2098 
2100  /* basic userland interface stuff */
2101  .name = RTS51X_NAME,
2102  .proc_name = RTS51X_NAME,
2103  .proc_info = proc_info,
2104  .info = rts5139_info,
2105 
2106  /* command interface -- queued only */
2107  .queuecommand = queuecommand,
2108 
2109  /* error and abort handlers */
2110  .eh_abort_handler = command_abort,
2111  .eh_device_reset_handler = device_reset,
2112  .eh_bus_reset_handler = bus_reset,
2113 
2114  /* queue commands only, only one command per LUN */
2115  .can_queue = 1,
2116  .cmd_per_lun = 1,
2117 
2118  /* unknown initiator id */
2119  .this_id = -1,
2120 
2121  .slave_alloc = slave_alloc,
2122  .slave_configure = slave_configure,
2123 
2124  /* lots of sg segments can be handled */
2125  .sg_tablesize = SG_ALL,
2126 
2127  /* limit the total size of a transfer to 120 KB */
2128  .max_sectors = 240,
2129 
2130  /* merge commands... this seems to help performance, but
2131  * periodically someone should test to see which setting is more
2132  * optimal.
2133  */
2134  .use_clustering = 1,
2135 
2136  /* emulated HBA */
2137  .emulated = 1,
2138 
2139  /* we do our own delay after a device or bus reset */
2140  .skip_settle_delay = 1,
2141 
2142  /* sysfs device attributes */
2143  /* .sdev_attrs = sysfs_device_attr_list, */
2144 
2145  /* module management */
2146  .module = THIS_MODULE
2147 };
2148