18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for USB Mass Storage compliant devices
48c2ecf20Sopenharmony_ci * Debugging Functions Source Code File
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Current development and maintenance by:
78c2ecf20Sopenharmony_ci *   (c) 1999-2002 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Developed with the assistance of:
108c2ecf20Sopenharmony_ci *   (c) 2002 Alan Stern <stern@rowland.org>
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Initial work by:
138c2ecf20Sopenharmony_ci *   (c) 1999 Michael Gee (michael@linuxspecific.com)
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * This driver is based on the 'USB Mass Storage Class' document. This
168c2ecf20Sopenharmony_ci * describes in detail the protocol used to communicate with such
178c2ecf20Sopenharmony_ci * devices.  Clearly, the designers had SCSI and ATAPI commands in
188c2ecf20Sopenharmony_ci * mind when they created this document.  The commands are all very
198c2ecf20Sopenharmony_ci * similar to commands in the SCSI-II and ATAPI specifications.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * It is important to note that in a number of cases this class
228c2ecf20Sopenharmony_ci * exhibits class-specific exemptions from the USB specification.
238c2ecf20Sopenharmony_ci * Notably the usage of NAK, STALL and ACK differs from the norm, in
248c2ecf20Sopenharmony_ci * that they are used to communicate wait, failed and OK on commands.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Also, for certain devices, the interrupt endpoint is used to convey
278c2ecf20Sopenharmony_ci * status of a command.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/device.h>
318c2ecf20Sopenharmony_ci#include <linux/cdrom.h>
328c2ecf20Sopenharmony_ci#include <linux/export.h>
338c2ecf20Sopenharmony_ci#include <scsi/scsi.h>
348c2ecf20Sopenharmony_ci#include <scsi/scsi_cmnd.h>
358c2ecf20Sopenharmony_ci#include <scsi/scsi_dbg.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include "usb.h"
388c2ecf20Sopenharmony_ci#include "debug.h"
398c2ecf20Sopenharmony_ci#include "scsi.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_civoid usb_stor_show_command(const struct us_data *us, struct scsi_cmnd *srb)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	char *what = NULL;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	switch (srb->cmnd[0]) {
478c2ecf20Sopenharmony_ci	case TEST_UNIT_READY: what = "TEST_UNIT_READY"; break;
488c2ecf20Sopenharmony_ci	case REZERO_UNIT: what = "REZERO_UNIT"; break;
498c2ecf20Sopenharmony_ci	case REQUEST_SENSE: what = "REQUEST_SENSE"; break;
508c2ecf20Sopenharmony_ci	case FORMAT_UNIT: what = "FORMAT_UNIT"; break;
518c2ecf20Sopenharmony_ci	case READ_BLOCK_LIMITS: what = "READ_BLOCK_LIMITS"; break;
528c2ecf20Sopenharmony_ci	case REASSIGN_BLOCKS: what = "REASSIGN_BLOCKS"; break;
538c2ecf20Sopenharmony_ci	case READ_6: what = "READ_6"; break;
548c2ecf20Sopenharmony_ci	case WRITE_6: what = "WRITE_6"; break;
558c2ecf20Sopenharmony_ci	case SEEK_6: what = "SEEK_6"; break;
568c2ecf20Sopenharmony_ci	case READ_REVERSE: what = "READ_REVERSE"; break;
578c2ecf20Sopenharmony_ci	case WRITE_FILEMARKS: what = "WRITE_FILEMARKS"; break;
588c2ecf20Sopenharmony_ci	case SPACE: what = "SPACE"; break;
598c2ecf20Sopenharmony_ci	case INQUIRY: what = "INQUIRY"; break;
608c2ecf20Sopenharmony_ci	case RECOVER_BUFFERED_DATA: what = "RECOVER_BUFFERED_DATA"; break;
618c2ecf20Sopenharmony_ci	case MODE_SELECT: what = "MODE_SELECT"; break;
628c2ecf20Sopenharmony_ci	case RESERVE: what = "RESERVE"; break;
638c2ecf20Sopenharmony_ci	case RELEASE: what = "RELEASE"; break;
648c2ecf20Sopenharmony_ci	case COPY: what = "COPY"; break;
658c2ecf20Sopenharmony_ci	case ERASE: what = "ERASE"; break;
668c2ecf20Sopenharmony_ci	case MODE_SENSE: what = "MODE_SENSE"; break;
678c2ecf20Sopenharmony_ci	case START_STOP: what = "START_STOP"; break;
688c2ecf20Sopenharmony_ci	case RECEIVE_DIAGNOSTIC: what = "RECEIVE_DIAGNOSTIC"; break;
698c2ecf20Sopenharmony_ci	case SEND_DIAGNOSTIC: what = "SEND_DIAGNOSTIC"; break;
708c2ecf20Sopenharmony_ci	case ALLOW_MEDIUM_REMOVAL: what = "ALLOW_MEDIUM_REMOVAL"; break;
718c2ecf20Sopenharmony_ci	case SET_WINDOW: what = "SET_WINDOW"; break;
728c2ecf20Sopenharmony_ci	case READ_CAPACITY: what = "READ_CAPACITY"; break;
738c2ecf20Sopenharmony_ci	case READ_10: what = "READ_10"; break;
748c2ecf20Sopenharmony_ci	case WRITE_10: what = "WRITE_10"; break;
758c2ecf20Sopenharmony_ci	case SEEK_10: what = "SEEK_10"; break;
768c2ecf20Sopenharmony_ci	case WRITE_VERIFY: what = "WRITE_VERIFY"; break;
778c2ecf20Sopenharmony_ci	case VERIFY: what = "VERIFY"; break;
788c2ecf20Sopenharmony_ci	case SEARCH_HIGH: what = "SEARCH_HIGH"; break;
798c2ecf20Sopenharmony_ci	case SEARCH_EQUAL: what = "SEARCH_EQUAL"; break;
808c2ecf20Sopenharmony_ci	case SEARCH_LOW: what = "SEARCH_LOW"; break;
818c2ecf20Sopenharmony_ci	case SET_LIMITS: what = "SET_LIMITS"; break;
828c2ecf20Sopenharmony_ci	case READ_POSITION: what = "READ_POSITION"; break;
838c2ecf20Sopenharmony_ci	case SYNCHRONIZE_CACHE: what = "SYNCHRONIZE_CACHE"; break;
848c2ecf20Sopenharmony_ci	case LOCK_UNLOCK_CACHE: what = "LOCK_UNLOCK_CACHE"; break;
858c2ecf20Sopenharmony_ci	case READ_DEFECT_DATA: what = "READ_DEFECT_DATA"; break;
868c2ecf20Sopenharmony_ci	case MEDIUM_SCAN: what = "MEDIUM_SCAN"; break;
878c2ecf20Sopenharmony_ci	case COMPARE: what = "COMPARE"; break;
888c2ecf20Sopenharmony_ci	case COPY_VERIFY: what = "COPY_VERIFY"; break;
898c2ecf20Sopenharmony_ci	case WRITE_BUFFER: what = "WRITE_BUFFER"; break;
908c2ecf20Sopenharmony_ci	case READ_BUFFER: what = "READ_BUFFER"; break;
918c2ecf20Sopenharmony_ci	case UPDATE_BLOCK: what = "UPDATE_BLOCK"; break;
928c2ecf20Sopenharmony_ci	case READ_LONG: what = "READ_LONG"; break;
938c2ecf20Sopenharmony_ci	case WRITE_LONG: what = "WRITE_LONG"; break;
948c2ecf20Sopenharmony_ci	case CHANGE_DEFINITION: what = "CHANGE_DEFINITION"; break;
958c2ecf20Sopenharmony_ci	case WRITE_SAME: what = "WRITE_SAME"; break;
968c2ecf20Sopenharmony_ci	case GPCMD_READ_SUBCHANNEL: what = "READ SUBCHANNEL"; break;
978c2ecf20Sopenharmony_ci	case READ_TOC: what = "READ_TOC"; break;
988c2ecf20Sopenharmony_ci	case GPCMD_READ_HEADER: what = "READ HEADER"; break;
998c2ecf20Sopenharmony_ci	case GPCMD_PLAY_AUDIO_10: what = "PLAY AUDIO (10)"; break;
1008c2ecf20Sopenharmony_ci	case GPCMD_PLAY_AUDIO_MSF: what = "PLAY AUDIO MSF"; break;
1018c2ecf20Sopenharmony_ci	case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
1028c2ecf20Sopenharmony_ci		what = "GET EVENT/STATUS NOTIFICATION"; break;
1038c2ecf20Sopenharmony_ci	case GPCMD_PAUSE_RESUME: what = "PAUSE/RESUME"; break;
1048c2ecf20Sopenharmony_ci	case LOG_SELECT: what = "LOG_SELECT"; break;
1058c2ecf20Sopenharmony_ci	case LOG_SENSE: what = "LOG_SENSE"; break;
1068c2ecf20Sopenharmony_ci	case GPCMD_STOP_PLAY_SCAN: what = "STOP PLAY/SCAN"; break;
1078c2ecf20Sopenharmony_ci	case GPCMD_READ_DISC_INFO: what = "READ DISC INFORMATION"; break;
1088c2ecf20Sopenharmony_ci	case GPCMD_READ_TRACK_RZONE_INFO:
1098c2ecf20Sopenharmony_ci		what = "READ TRACK INFORMATION"; break;
1108c2ecf20Sopenharmony_ci	case GPCMD_RESERVE_RZONE_TRACK: what = "RESERVE TRACK"; break;
1118c2ecf20Sopenharmony_ci	case GPCMD_SEND_OPC: what = "SEND OPC"; break;
1128c2ecf20Sopenharmony_ci	case MODE_SELECT_10: what = "MODE_SELECT_10"; break;
1138c2ecf20Sopenharmony_ci	case GPCMD_REPAIR_RZONE_TRACK: what = "REPAIR TRACK"; break;
1148c2ecf20Sopenharmony_ci	case 0x59: what = "READ MASTER CUE"; break;
1158c2ecf20Sopenharmony_ci	case MODE_SENSE_10: what = "MODE_SENSE_10"; break;
1168c2ecf20Sopenharmony_ci	case GPCMD_CLOSE_TRACK: what = "CLOSE TRACK/SESSION"; break;
1178c2ecf20Sopenharmony_ci	case 0x5C: what = "READ BUFFER CAPACITY"; break;
1188c2ecf20Sopenharmony_ci	case 0x5D: what = "SEND CUE SHEET"; break;
1198c2ecf20Sopenharmony_ci	case GPCMD_BLANK: what = "BLANK"; break;
1208c2ecf20Sopenharmony_ci	case REPORT_LUNS: what = "REPORT LUNS"; break;
1218c2ecf20Sopenharmony_ci	case MOVE_MEDIUM: what = "MOVE_MEDIUM or PLAY AUDIO (12)"; break;
1228c2ecf20Sopenharmony_ci	case READ_12: what = "READ_12"; break;
1238c2ecf20Sopenharmony_ci	case WRITE_12: what = "WRITE_12"; break;
1248c2ecf20Sopenharmony_ci	case WRITE_VERIFY_12: what = "WRITE_VERIFY_12"; break;
1258c2ecf20Sopenharmony_ci	case SEARCH_HIGH_12: what = "SEARCH_HIGH_12"; break;
1268c2ecf20Sopenharmony_ci	case SEARCH_EQUAL_12: what = "SEARCH_EQUAL_12"; break;
1278c2ecf20Sopenharmony_ci	case SEARCH_LOW_12: what = "SEARCH_LOW_12"; break;
1288c2ecf20Sopenharmony_ci	case SEND_VOLUME_TAG: what = "SEND_VOLUME_TAG"; break;
1298c2ecf20Sopenharmony_ci	case READ_ELEMENT_STATUS: what = "READ_ELEMENT_STATUS"; break;
1308c2ecf20Sopenharmony_ci	case GPCMD_READ_CD_MSF: what = "READ CD MSF"; break;
1318c2ecf20Sopenharmony_ci	case GPCMD_SCAN: what = "SCAN"; break;
1328c2ecf20Sopenharmony_ci	case GPCMD_SET_SPEED: what = "SET CD SPEED"; break;
1338c2ecf20Sopenharmony_ci	case GPCMD_MECHANISM_STATUS: what = "MECHANISM STATUS"; break;
1348c2ecf20Sopenharmony_ci	case GPCMD_READ_CD: what = "READ CD"; break;
1358c2ecf20Sopenharmony_ci	case 0xE1: what = "WRITE CONTINUE"; break;
1368c2ecf20Sopenharmony_ci	case WRITE_LONG_2: what = "WRITE_LONG_2"; break;
1378c2ecf20Sopenharmony_ci	default: what = "(unknown command)"; break;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci	usb_stor_dbg(us, "Command %s (%d bytes)\n", what, srb->cmd_len);
1408c2ecf20Sopenharmony_ci	usb_stor_dbg(us, "bytes: %*ph\n", min_t(int, srb->cmd_len, 16),
1418c2ecf20Sopenharmony_ci		     (const unsigned char *)srb->cmnd);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_civoid usb_stor_show_sense(const struct us_data *us,
1458c2ecf20Sopenharmony_ci			 unsigned char key,
1468c2ecf20Sopenharmony_ci			 unsigned char asc,
1478c2ecf20Sopenharmony_ci			 unsigned char ascq)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	const char *what, *keystr, *fmt;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	keystr = scsi_sense_key_string(key);
1528c2ecf20Sopenharmony_ci	what = scsi_extd_sense_format(asc, ascq, &fmt);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (keystr == NULL)
1558c2ecf20Sopenharmony_ci		keystr = "(Unknown Key)";
1568c2ecf20Sopenharmony_ci	if (what == NULL)
1578c2ecf20Sopenharmony_ci		what = "(unknown ASC/ASCQ)";
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (fmt)
1608c2ecf20Sopenharmony_ci		usb_stor_dbg(us, "%s: %s (%s%x)\n", keystr, what, fmt, ascq);
1618c2ecf20Sopenharmony_ci	else
1628c2ecf20Sopenharmony_ci		usb_stor_dbg(us, "%s: %s\n", keystr, what);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_civoid usb_stor_dbg(const struct us_data *us, const char *fmt, ...)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	va_list args;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	va_start(args, fmt);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	dev_vprintk_emit(LOGLEVEL_DEBUG, &us->pusb_dev->dev, fmt, args);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	va_end(args);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_stor_dbg);
176