mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-08 21:22:09 -04:00
copy mass_storage driver from Kernel as initial version
Copy the mass_storage driver from Kernel source as initial version of Motorola mass storage base. Change-Id: I0d95b47de5c1463164843d9e8cebce06e9492e52 Signed-off-by: yanyh2 <yanyh2@motorola.com> Reviewed-on: https://gerrit.mot.com/2053805 SME-Granted: SME Approvals Granted SLTApproved: Slta Waiver Tested-by: Jira Key Reviewed-by: Xiaojun Ji <jixj@motorola.com> Reviewed-by: Jianqi Yang <yangj@motorola.com> Reviewed-by: Yuecai Liu <liuyc20@motorola.com> Submit-Approved: Jira Key
This commit is contained in:
parent
b3de4df72d
commit
1ae623bb38
7 changed files with 4393 additions and 0 deletions
8
drivers/moto_f_mass_storage/Android.mk
Normal file
8
drivers/moto_f_mass_storage/Android.mk
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
DLKM_DIR := motorola/kernel/modules
|
||||
LOCAL_PATH := $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
LOCAL_MODULE := moto_f_mass_storage.ko
|
||||
LOCAL_MODULE_TAGS := optional
|
||||
LOCAL_MODULE_PATH := $(KERNEL_MODULES_OUT)
|
||||
include $(DLKM_DIR)/AndroidKernelModule.mk
|
||||
6
drivers/moto_f_mass_storage/Kbuild
Normal file
6
drivers/moto_f_mass_storage/Kbuild
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# add -Wall to try to catch everything we can.
|
||||
EXTRA_CFLAGS += -Wall
|
||||
EXTRA_CFLAGS += -I$(ANDROID_BUILD_TOP)/motorola/kernel/modules/include
|
||||
|
||||
moto_f_mass_storage-objs += f_mass_storage.o storage_common.o
|
||||
obj-m += moto_f_mass_storage.o
|
||||
14
drivers/moto_f_mass_storage/Makefile
Normal file
14
drivers/moto_f_mass_storage/Makefile
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
all: modules
|
||||
|
||||
modules:
|
||||
$(MAKE) -C $(KERNEL_SRC) M=$(M) modules $(KBUILD_OPTIONS)
|
||||
|
||||
modules_install:
|
||||
$(MAKE) INSTALL_MOD_STRIP=1 -C $(KERNEL_SRC) M=$(M) modules_install
|
||||
|
||||
%:
|
||||
$(MAKE) -C $(KERNEL_SRC) M=$(M) $@ $(KBUILD_OPTIONS)
|
||||
|
||||
clean:
|
||||
rm -f *.o *.ko *.mod.c *.mod.o *~ .*.cmd Module.symvers
|
||||
rm -rf .tmp_versions
|
||||
3476
drivers/moto_f_mass_storage/f_mass_storage.c
Normal file
3476
drivers/moto_f_mass_storage/f_mass_storage.c
Normal file
File diff suppressed because it is too large
Load diff
144
drivers/moto_f_mass_storage/f_mass_storage.h
Normal file
144
drivers/moto_f_mass_storage/f_mass_storage.h
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef USB_F_MASS_STORAGE_H
|
||||
#define USB_F_MASS_STORAGE_H
|
||||
|
||||
#include <linux/usb/composite.h>
|
||||
#include "storage_common.h"
|
||||
|
||||
struct fsg_module_parameters {
|
||||
char *file[FSG_MAX_LUNS];
|
||||
bool ro[FSG_MAX_LUNS];
|
||||
bool removable[FSG_MAX_LUNS];
|
||||
bool cdrom[FSG_MAX_LUNS];
|
||||
bool nofua[FSG_MAX_LUNS];
|
||||
|
||||
unsigned int file_count, ro_count, removable_count, cdrom_count;
|
||||
unsigned int nofua_count;
|
||||
unsigned int luns; /* nluns */
|
||||
bool stall; /* can_stall */
|
||||
};
|
||||
|
||||
#define _FSG_MODULE_PARAM_ARRAY(prefix, params, name, type, desc) \
|
||||
module_param_array_named(prefix ## name, params.name, type, \
|
||||
&prefix ## params.name ## _count, \
|
||||
S_IRUGO); \
|
||||
MODULE_PARM_DESC(prefix ## name, desc)
|
||||
|
||||
#define _FSG_MODULE_PARAM(prefix, params, name, type, desc) \
|
||||
module_param_named(prefix ## name, params.name, type, \
|
||||
S_IRUGO); \
|
||||
MODULE_PARM_DESC(prefix ## name, desc)
|
||||
|
||||
#define __FSG_MODULE_PARAMETERS(prefix, params) \
|
||||
_FSG_MODULE_PARAM_ARRAY(prefix, params, file, charp, \
|
||||
"names of backing files or devices"); \
|
||||
_FSG_MODULE_PARAM_ARRAY(prefix, params, ro, bool, \
|
||||
"true to force read-only"); \
|
||||
_FSG_MODULE_PARAM_ARRAY(prefix, params, removable, bool, \
|
||||
"true to simulate removable media"); \
|
||||
_FSG_MODULE_PARAM_ARRAY(prefix, params, cdrom, bool, \
|
||||
"true to simulate CD-ROM instead of disk"); \
|
||||
_FSG_MODULE_PARAM_ARRAY(prefix, params, nofua, bool, \
|
||||
"true to ignore SCSI WRITE(10,12) FUA bit"); \
|
||||
_FSG_MODULE_PARAM(prefix, params, luns, uint, \
|
||||
"number of LUNs"); \
|
||||
_FSG_MODULE_PARAM(prefix, params, stall, bool, \
|
||||
"false to prevent bulk stalls")
|
||||
|
||||
#ifdef CONFIG_USB_GADGET_DEBUG_FILES
|
||||
|
||||
#define FSG_MODULE_PARAMETERS(prefix, params) \
|
||||
__FSG_MODULE_PARAMETERS(prefix, params); \
|
||||
module_param_named(num_buffers, fsg_num_buffers, uint, S_IRUGO);\
|
||||
MODULE_PARM_DESC(num_buffers, "Number of pipeline buffers")
|
||||
#else
|
||||
|
||||
#define FSG_MODULE_PARAMETERS(prefix, params) \
|
||||
__FSG_MODULE_PARAMETERS(prefix, params)
|
||||
|
||||
#endif
|
||||
|
||||
struct fsg_common;
|
||||
|
||||
/* FSF callback functions */
|
||||
struct fsg_lun_opts {
|
||||
struct config_group group;
|
||||
struct fsg_lun *lun;
|
||||
int lun_id;
|
||||
};
|
||||
|
||||
struct fsg_opts {
|
||||
struct fsg_common *common;
|
||||
struct usb_function_instance func_inst;
|
||||
struct fsg_lun_opts lun0;
|
||||
struct config_group *default_groups[2];
|
||||
bool no_configfs; /* for legacy gadgets */
|
||||
|
||||
/*
|
||||
* Read/write access to configfs attributes is handled by configfs.
|
||||
*
|
||||
* This is to protect the data from concurrent access by read/write
|
||||
* and create symlink/remove symlink.
|
||||
*/
|
||||
struct mutex lock;
|
||||
int refcnt;
|
||||
};
|
||||
|
||||
struct fsg_lun_config {
|
||||
const char *filename;
|
||||
char ro;
|
||||
char removable;
|
||||
char cdrom;
|
||||
char nofua;
|
||||
char inquiry_string[INQUIRY_STRING_LEN];
|
||||
};
|
||||
|
||||
struct fsg_config {
|
||||
unsigned nluns;
|
||||
struct fsg_lun_config luns[FSG_MAX_LUNS];
|
||||
|
||||
/* Callback functions. */
|
||||
const struct fsg_operations *ops;
|
||||
/* Gadget's private data. */
|
||||
void *private_data;
|
||||
|
||||
const char *vendor_name; /* 8 characters or less */
|
||||
const char *product_name; /* 16 characters or less */
|
||||
|
||||
char can_stall;
|
||||
unsigned int fsg_num_buffers;
|
||||
};
|
||||
|
||||
static inline struct fsg_opts *
|
||||
fsg_opts_from_func_inst(const struct usb_function_instance *fi)
|
||||
{
|
||||
return container_of(fi, struct fsg_opts, func_inst);
|
||||
}
|
||||
|
||||
void fsg_common_set_sysfs(struct fsg_common *common, bool sysfs);
|
||||
|
||||
int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n);
|
||||
|
||||
void fsg_common_free_buffers(struct fsg_common *common);
|
||||
|
||||
int fsg_common_set_cdev(struct fsg_common *common,
|
||||
struct usb_composite_dev *cdev, bool can_stall);
|
||||
|
||||
void fsg_common_remove_lun(struct fsg_lun *lun);
|
||||
|
||||
void fsg_common_remove_luns(struct fsg_common *common);
|
||||
|
||||
int fsg_common_create_lun(struct fsg_common *common, struct fsg_lun_config *cfg,
|
||||
unsigned int id, const char *name,
|
||||
const char **name_pfx);
|
||||
|
||||
int fsg_common_create_luns(struct fsg_common *common, struct fsg_config *cfg);
|
||||
|
||||
void fsg_common_set_inquiry_string(struct fsg_common *common, const char *vn,
|
||||
const char *pn);
|
||||
|
||||
void fsg_config_from_params(struct fsg_config *cfg,
|
||||
const struct fsg_module_parameters *params,
|
||||
unsigned int fsg_num_buffers);
|
||||
|
||||
#endif /* USB_F_MASS_STORAGE_H */
|
||||
522
drivers/moto_f_mass_storage/storage_common.c
Normal file
522
drivers/moto_f_mass_storage/storage_common.c
Normal file
|
|
@ -0,0 +1,522 @@
|
|||
// SPDX-License-Identifier: GPL-2.0+
|
||||
/*
|
||||
* storage_common.c -- Common definitions for mass storage functionality
|
||||
*
|
||||
* Copyright (C) 2003-2008 Alan Stern
|
||||
* Copyeight (C) 2009 Samsung Electronics
|
||||
* Author: Michal Nazarewicz (mina86@mina86.com)
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file requires the following identifiers used in USB strings to
|
||||
* be defined (each of type pointer to char):
|
||||
* - fsg_string_interface -- name of the interface
|
||||
*/
|
||||
|
||||
/*
|
||||
* When USB_GADGET_DEBUG_FILES is defined the module param num_buffers
|
||||
* sets the number of pipeline buffers (length of the fsg_buffhd array).
|
||||
* The valid range of num_buffers is: num >= 2 && num <= 4.
|
||||
*/
|
||||
|
||||
#include <linux/module.h>
|
||||
#include <linux/blkdev.h>
|
||||
#include <linux/file.h>
|
||||
#include <linux/fs.h>
|
||||
#include <linux/usb/composite.h>
|
||||
|
||||
#include "storage_common.h"
|
||||
|
||||
/* There is only one interface. */
|
||||
|
||||
struct usb_interface_descriptor fsg_intf_desc = {
|
||||
.bLength = sizeof fsg_intf_desc,
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
|
||||
.bNumEndpoints = 2, /* Adjusted during fsg_bind() */
|
||||
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
|
||||
.bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
|
||||
.bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
|
||||
.iInterface = FSG_STRING_INTERFACE,
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_intf_desc);
|
||||
|
||||
/*
|
||||
* Three full-speed endpoint descriptors: bulk-in, bulk-out, and
|
||||
* interrupt-in.
|
||||
*/
|
||||
|
||||
struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
.bEndpointAddress = USB_DIR_IN,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
/* wMaxPacketSize set by autoconfiguration */
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_fs_bulk_in_desc);
|
||||
|
||||
struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
.bEndpointAddress = USB_DIR_OUT,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
/* wMaxPacketSize set by autoconfiguration */
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_fs_bulk_out_desc);
|
||||
|
||||
struct usb_descriptor_header *fsg_fs_function[] = {
|
||||
(struct usb_descriptor_header *) &fsg_intf_desc,
|
||||
(struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
|
||||
(struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
|
||||
NULL,
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_fs_function);
|
||||
|
||||
|
||||
/*
|
||||
* USB 2.0 devices need to expose both high speed and full speed
|
||||
* descriptors, unless they only run at full speed.
|
||||
*
|
||||
* That means alternate endpoint descriptors (bigger packets).
|
||||
*/
|
||||
struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
/* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = cpu_to_le16(512),
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_hs_bulk_in_desc);
|
||||
|
||||
struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
/* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = cpu_to_le16(512),
|
||||
.bInterval = 1, /* NAK every 1 uframe */
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_hs_bulk_out_desc);
|
||||
|
||||
|
||||
struct usb_descriptor_header *fsg_hs_function[] = {
|
||||
(struct usb_descriptor_header *) &fsg_intf_desc,
|
||||
(struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
|
||||
(struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
|
||||
NULL,
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_hs_function);
|
||||
|
||||
struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
/* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = cpu_to_le16(1024),
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_desc);
|
||||
|
||||
struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = {
|
||||
.bLength = sizeof(fsg_ss_bulk_in_comp_desc),
|
||||
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
||||
|
||||
/*.bMaxBurst = DYNAMIC, */
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_ss_bulk_in_comp_desc);
|
||||
|
||||
struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = {
|
||||
.bLength = USB_DT_ENDPOINT_SIZE,
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
|
||||
/* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = cpu_to_le16(1024),
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_desc);
|
||||
|
||||
struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = {
|
||||
.bLength = sizeof(fsg_ss_bulk_in_comp_desc),
|
||||
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
||||
|
||||
/*.bMaxBurst = DYNAMIC, */
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_ss_bulk_out_comp_desc);
|
||||
|
||||
struct usb_descriptor_header *fsg_ss_function[] = {
|
||||
(struct usb_descriptor_header *) &fsg_intf_desc,
|
||||
(struct usb_descriptor_header *) &fsg_ss_bulk_in_desc,
|
||||
(struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc,
|
||||
(struct usb_descriptor_header *) &fsg_ss_bulk_out_desc,
|
||||
(struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc,
|
||||
NULL,
|
||||
};
|
||||
EXPORT_SYMBOL_GPL(fsg_ss_function);
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
* If the next two routines are called while the gadget is registered,
|
||||
* the caller must own fsg->filesem for writing.
|
||||
*/
|
||||
|
||||
void fsg_lun_close(struct fsg_lun *curlun)
|
||||
{
|
||||
if (curlun->filp) {
|
||||
LDBG(curlun, "close backing file\n");
|
||||
fput(curlun->filp);
|
||||
curlun->filp = NULL;
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_lun_close);
|
||||
|
||||
int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
|
||||
{
|
||||
int ro;
|
||||
struct file *filp = NULL;
|
||||
int rc = -EINVAL;
|
||||
struct inode *inode = NULL;
|
||||
loff_t size;
|
||||
loff_t num_sectors;
|
||||
loff_t min_sectors;
|
||||
unsigned int blkbits;
|
||||
unsigned int blksize;
|
||||
|
||||
/* R/W if we can, R/O if we must */
|
||||
ro = curlun->initially_ro;
|
||||
if (!ro) {
|
||||
filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
|
||||
if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
|
||||
ro = 1;
|
||||
}
|
||||
if (ro)
|
||||
filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
|
||||
if (IS_ERR(filp)) {
|
||||
LINFO(curlun, "unable to open backing file: %s\n", filename);
|
||||
return PTR_ERR(filp);
|
||||
}
|
||||
|
||||
if (!(filp->f_mode & FMODE_WRITE))
|
||||
ro = 1;
|
||||
|
||||
inode = file_inode(filp);
|
||||
if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
|
||||
LINFO(curlun, "invalid file type: %s\n", filename);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we can't read the file, it's no good.
|
||||
* If we can't write the file, use it read-only.
|
||||
*/
|
||||
if (!(filp->f_mode & FMODE_CAN_READ)) {
|
||||
LINFO(curlun, "file not readable: %s\n", filename);
|
||||
goto out;
|
||||
}
|
||||
if (!(filp->f_mode & FMODE_CAN_WRITE))
|
||||
ro = 1;
|
||||
|
||||
size = i_size_read(inode->i_mapping->host);
|
||||
if (size < 0) {
|
||||
LINFO(curlun, "unable to find file size: %s\n", filename);
|
||||
rc = (int) size;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (curlun->cdrom) {
|
||||
blksize = 2048;
|
||||
blkbits = 11;
|
||||
} else if (inode->i_bdev) {
|
||||
blksize = bdev_logical_block_size(inode->i_bdev);
|
||||
blkbits = blksize_bits(blksize);
|
||||
} else {
|
||||
blksize = 512;
|
||||
blkbits = 9;
|
||||
}
|
||||
|
||||
num_sectors = size >> blkbits; /* File size in logic-block-size blocks */
|
||||
min_sectors = 1;
|
||||
if (curlun->cdrom) {
|
||||
min_sectors = 300; /* Smallest track is 300 frames */
|
||||
if (num_sectors >= 256*60*75) {
|
||||
num_sectors = 256*60*75 - 1;
|
||||
LINFO(curlun, "file too big: %s\n", filename);
|
||||
LINFO(curlun, "using only first %d blocks\n",
|
||||
(int) num_sectors);
|
||||
}
|
||||
}
|
||||
if (num_sectors < min_sectors) {
|
||||
LINFO(curlun, "file too small: %s\n", filename);
|
||||
rc = -ETOOSMALL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (fsg_lun_is_open(curlun))
|
||||
fsg_lun_close(curlun);
|
||||
|
||||
curlun->blksize = blksize;
|
||||
curlun->blkbits = blkbits;
|
||||
curlun->ro = ro;
|
||||
curlun->filp = filp;
|
||||
curlun->file_length = size;
|
||||
curlun->num_sectors = num_sectors;
|
||||
LDBG(curlun, "open backing file: %s\n", filename);
|
||||
return 0;
|
||||
|
||||
out:
|
||||
fput(filp);
|
||||
return rc;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_lun_open);
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
* Sync the file data, don't bother with the metadata.
|
||||
* This code was copied from fs/buffer.c:sys_fdatasync().
|
||||
*/
|
||||
int fsg_lun_fsync_sub(struct fsg_lun *curlun)
|
||||
{
|
||||
struct file *filp = curlun->filp;
|
||||
|
||||
if (curlun->ro || !filp)
|
||||
return 0;
|
||||
return vfs_fsync(filp, 1);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_lun_fsync_sub);
|
||||
|
||||
void store_cdrom_address(u8 *dest, int msf, u32 addr)
|
||||
{
|
||||
if (msf) {
|
||||
/* Convert to Minutes-Seconds-Frames */
|
||||
addr >>= 2; /* Convert to 2048-byte frames */
|
||||
addr += 2*75; /* Lead-in occupies 2 seconds */
|
||||
dest[3] = addr % 75; /* Frames */
|
||||
addr /= 75;
|
||||
dest[2] = addr % 60; /* Seconds */
|
||||
addr /= 60;
|
||||
dest[1] = addr; /* Minutes */
|
||||
dest[0] = 0; /* Reserved */
|
||||
} else {
|
||||
/* Absolute sector */
|
||||
put_unaligned_be32(addr, dest);
|
||||
}
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(store_cdrom_address);
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf)
|
||||
{
|
||||
return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
|
||||
? curlun->ro
|
||||
: curlun->initially_ro);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_ro);
|
||||
|
||||
ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf)
|
||||
{
|
||||
return sprintf(buf, "%u\n", curlun->nofua);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_nofua);
|
||||
|
||||
ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
char *buf)
|
||||
{
|
||||
char *p;
|
||||
ssize_t rc;
|
||||
|
||||
down_read(filesem);
|
||||
if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
|
||||
p = file_path(curlun->filp, buf, PAGE_SIZE - 1);
|
||||
if (IS_ERR(p))
|
||||
rc = PTR_ERR(p);
|
||||
else {
|
||||
rc = strlen(p);
|
||||
memmove(buf, p, rc);
|
||||
buf[rc] = '\n'; /* Add a newline */
|
||||
buf[++rc] = 0;
|
||||
}
|
||||
} else { /* No file, return 0 bytes */
|
||||
*buf = 0;
|
||||
rc = 0;
|
||||
}
|
||||
up_read(filesem);
|
||||
return rc;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_file);
|
||||
|
||||
ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf)
|
||||
{
|
||||
return sprintf(buf, "%u\n", curlun->cdrom);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_cdrom);
|
||||
|
||||
ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf)
|
||||
{
|
||||
return sprintf(buf, "%u\n", curlun->removable);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_removable);
|
||||
|
||||
ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf)
|
||||
{
|
||||
return sprintf(buf, "%s\n", curlun->inquiry_string);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_show_inquiry_string);
|
||||
|
||||
/*
|
||||
* The caller must hold fsg->filesem for reading when calling this function.
|
||||
*/
|
||||
static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro)
|
||||
{
|
||||
if (fsg_lun_is_open(curlun)) {
|
||||
LDBG(curlun, "read-only status change prevented\n");
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
curlun->ro = ro;
|
||||
curlun->initially_ro = ro;
|
||||
LDBG(curlun, "read-only status set to %d\n", curlun->ro);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
ssize_t rc;
|
||||
bool ro;
|
||||
|
||||
rc = strtobool(buf, &ro);
|
||||
if (rc)
|
||||
return rc;
|
||||
|
||||
/*
|
||||
* Allow the write-enable status to change only while the
|
||||
* backing file is closed.
|
||||
*/
|
||||
down_read(filesem);
|
||||
rc = _fsg_store_ro(curlun, ro);
|
||||
if (!rc)
|
||||
rc = count;
|
||||
up_read(filesem);
|
||||
|
||||
return rc;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_ro);
|
||||
|
||||
ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count)
|
||||
{
|
||||
bool nofua;
|
||||
int ret;
|
||||
|
||||
ret = strtobool(buf, &nofua);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/* Sync data when switching from async mode to sync */
|
||||
if (!nofua && curlun->nofua)
|
||||
fsg_lun_fsync_sub(curlun);
|
||||
|
||||
curlun->nofua = nofua;
|
||||
|
||||
return count;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_nofua);
|
||||
|
||||
ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
|
||||
LDBG(curlun, "eject attempt prevented\n");
|
||||
return -EBUSY; /* "Door is locked" */
|
||||
}
|
||||
|
||||
/* Remove a trailing newline */
|
||||
if (count > 0 && buf[count-1] == '\n')
|
||||
((char *) buf)[count-1] = 0; /* Ugh! */
|
||||
|
||||
/* Load new medium */
|
||||
down_write(filesem);
|
||||
if (count > 0 && buf[0]) {
|
||||
/* fsg_lun_open() will close existing file if any. */
|
||||
rc = fsg_lun_open(curlun, buf);
|
||||
if (rc == 0)
|
||||
curlun->unit_attention_data =
|
||||
SS_NOT_READY_TO_READY_TRANSITION;
|
||||
} else if (fsg_lun_is_open(curlun)) {
|
||||
fsg_lun_close(curlun);
|
||||
curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
|
||||
}
|
||||
up_write(filesem);
|
||||
return (rc < 0 ? rc : count);
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_file);
|
||||
|
||||
ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count)
|
||||
{
|
||||
bool cdrom;
|
||||
int ret;
|
||||
|
||||
ret = strtobool(buf, &cdrom);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
down_read(filesem);
|
||||
ret = cdrom ? _fsg_store_ro(curlun, true) : 0;
|
||||
|
||||
if (!ret) {
|
||||
curlun->cdrom = cdrom;
|
||||
ret = count;
|
||||
}
|
||||
up_read(filesem);
|
||||
|
||||
return ret;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_cdrom);
|
||||
|
||||
ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
|
||||
size_t count)
|
||||
{
|
||||
bool removable;
|
||||
int ret;
|
||||
|
||||
ret = strtobool(buf, &removable);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
curlun->removable = removable;
|
||||
|
||||
return count;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_removable);
|
||||
|
||||
ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
|
||||
size_t count)
|
||||
{
|
||||
const size_t len = min(count, sizeof(curlun->inquiry_string));
|
||||
|
||||
if (len == 0 || buf[0] == '\n') {
|
||||
curlun->inquiry_string[0] = 0;
|
||||
} else {
|
||||
snprintf(curlun->inquiry_string,
|
||||
sizeof(curlun->inquiry_string), "%-28s", buf);
|
||||
if (curlun->inquiry_string[len-1] == '\n')
|
||||
curlun->inquiry_string[len-1] = ' ';
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(fsg_store_inquiry_string);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
223
drivers/moto_f_mass_storage/storage_common.h
Normal file
223
drivers/moto_f_mass_storage/storage_common.h
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef USB_STORAGE_COMMON_H
|
||||
#define USB_STORAGE_COMMON_H
|
||||
|
||||
#include <linux/device.h>
|
||||
#include <linux/usb/storage.h>
|
||||
#include <scsi/scsi.h>
|
||||
#include <asm/unaligned.h>
|
||||
|
||||
#ifndef DEBUG
|
||||
#undef VERBOSE_DEBUG
|
||||
#undef DUMP_MSGS
|
||||
#endif /* !DEBUG */
|
||||
|
||||
#ifdef VERBOSE_DEBUG
|
||||
#define VLDBG LDBG
|
||||
#else
|
||||
#define VLDBG(lun, fmt, args...) do { } while (0)
|
||||
#endif /* VERBOSE_DEBUG */
|
||||
|
||||
#define _LMSG(func, lun, fmt, args...) \
|
||||
do { \
|
||||
if ((lun)->name_pfx && *(lun)->name_pfx) \
|
||||
func("%s/%s: " fmt, *(lun)->name_pfx, \
|
||||
(lun)->name, ## args); \
|
||||
else \
|
||||
func("%s: " fmt, (lun)->name, ## args); \
|
||||
} while (0)
|
||||
|
||||
#define LDBG(lun, fmt, args...) _LMSG(pr_debug, lun, fmt, ## args)
|
||||
#define LERROR(lun, fmt, args...) _LMSG(pr_err, lun, fmt, ## args)
|
||||
#define LWARN(lun, fmt, args...) _LMSG(pr_warn, lun, fmt, ## args)
|
||||
#define LINFO(lun, fmt, args...) _LMSG(pr_info, lun, fmt, ## args)
|
||||
|
||||
|
||||
#ifdef DUMP_MSGS
|
||||
|
||||
# define dump_msg(fsg, /* const char * */ label, \
|
||||
/* const u8 * */ buf, /* unsigned */ length) \
|
||||
do { \
|
||||
if (length < 512) { \
|
||||
DBG(fsg, "%s, length %u:\n", label, length); \
|
||||
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
|
||||
16, 1, buf, length, 0); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
# define dump_cdb(fsg) do { } while (0)
|
||||
|
||||
#else
|
||||
|
||||
# define dump_msg(fsg, /* const char * */ label, \
|
||||
/* const u8 * */ buf, /* unsigned */ length) do { } while (0)
|
||||
|
||||
# ifdef VERBOSE_DEBUG
|
||||
|
||||
# define dump_cdb(fsg) \
|
||||
print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
|
||||
16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
|
||||
|
||||
# else
|
||||
|
||||
# define dump_cdb(fsg) do { } while (0)
|
||||
|
||||
# endif /* VERBOSE_DEBUG */
|
||||
|
||||
#endif /* DUMP_MSGS */
|
||||
|
||||
/* Length of a SCSI Command Data Block */
|
||||
#define MAX_COMMAND_SIZE 16
|
||||
|
||||
/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
|
||||
#define SS_NO_SENSE 0
|
||||
#define SS_COMMUNICATION_FAILURE 0x040800
|
||||
#define SS_INVALID_COMMAND 0x052000
|
||||
#define SS_INVALID_FIELD_IN_CDB 0x052400
|
||||
#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
|
||||
#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
|
||||
#define SS_MEDIUM_NOT_PRESENT 0x023a00
|
||||
#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
|
||||
#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
|
||||
#define SS_RESET_OCCURRED 0x062900
|
||||
#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
|
||||
#define SS_UNRECOVERED_READ_ERROR 0x031100
|
||||
#define SS_WRITE_ERROR 0x030c02
|
||||
#define SS_WRITE_PROTECTED 0x072700
|
||||
|
||||
#define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
|
||||
#define ASC(x) ((u8) ((x) >> 8))
|
||||
#define ASCQ(x) ((u8) (x))
|
||||
|
||||
/*
|
||||
* Vendor (8 chars), product (16 chars), release (4 hexadecimal digits) and NUL
|
||||
* byte
|
||||
*/
|
||||
#define INQUIRY_STRING_LEN ((size_t) (8 + 16 + 4 + 1))
|
||||
|
||||
struct fsg_lun {
|
||||
struct file *filp;
|
||||
loff_t file_length;
|
||||
loff_t num_sectors;
|
||||
|
||||
unsigned int initially_ro:1;
|
||||
unsigned int ro:1;
|
||||
unsigned int removable:1;
|
||||
unsigned int cdrom:1;
|
||||
unsigned int prevent_medium_removal:1;
|
||||
unsigned int registered:1;
|
||||
unsigned int info_valid:1;
|
||||
unsigned int nofua:1;
|
||||
|
||||
u32 sense_data;
|
||||
u32 sense_data_info;
|
||||
u32 unit_attention_data;
|
||||
|
||||
unsigned int blkbits; /* Bits of logical block size
|
||||
of bound block device */
|
||||
unsigned int blksize; /* logical block size of bound block device */
|
||||
struct device dev;
|
||||
const char *name; /* "lun.name" */
|
||||
const char **name_pfx; /* "function.name" */
|
||||
char inquiry_string[INQUIRY_STRING_LEN];
|
||||
};
|
||||
|
||||
static inline bool fsg_lun_is_open(struct fsg_lun *curlun)
|
||||
{
|
||||
return curlun->filp != NULL;
|
||||
}
|
||||
|
||||
/* Default size of buffer length. */
|
||||
#define FSG_BUFLEN ((u32)16384)
|
||||
|
||||
/* Maximal number of LUNs supported in mass storage function */
|
||||
#define FSG_MAX_LUNS 16
|
||||
|
||||
enum fsg_buffer_state {
|
||||
BUF_STATE_SENDING = -2,
|
||||
BUF_STATE_RECEIVING,
|
||||
BUF_STATE_EMPTY = 0,
|
||||
BUF_STATE_FULL
|
||||
};
|
||||
|
||||
struct fsg_buffhd {
|
||||
void *buf;
|
||||
enum fsg_buffer_state state;
|
||||
struct fsg_buffhd *next;
|
||||
|
||||
/*
|
||||
* The NetChip 2280 is faster, and handles some protocol faults
|
||||
* better, if we don't submit any short bulk-out read requests.
|
||||
* So we will record the intended request length here.
|
||||
*/
|
||||
unsigned int bulk_out_intended_length;
|
||||
|
||||
struct usb_request *inreq;
|
||||
struct usb_request *outreq;
|
||||
};
|
||||
|
||||
enum fsg_state {
|
||||
FSG_STATE_NORMAL,
|
||||
FSG_STATE_ABORT_BULK_OUT,
|
||||
FSG_STATE_PROTOCOL_RESET,
|
||||
FSG_STATE_CONFIG_CHANGE,
|
||||
FSG_STATE_EXIT,
|
||||
FSG_STATE_TERMINATED
|
||||
};
|
||||
|
||||
enum data_direction {
|
||||
DATA_DIR_UNKNOWN = 0,
|
||||
DATA_DIR_FROM_HOST,
|
||||
DATA_DIR_TO_HOST,
|
||||
DATA_DIR_NONE
|
||||
};
|
||||
|
||||
static inline struct fsg_lun *fsg_lun_from_dev(struct device *dev)
|
||||
{
|
||||
return container_of(dev, struct fsg_lun, dev);
|
||||
}
|
||||
|
||||
enum {
|
||||
FSG_STRING_INTERFACE
|
||||
};
|
||||
|
||||
extern struct usb_interface_descriptor fsg_intf_desc;
|
||||
|
||||
extern struct usb_endpoint_descriptor fsg_fs_bulk_in_desc;
|
||||
extern struct usb_endpoint_descriptor fsg_fs_bulk_out_desc;
|
||||
extern struct usb_descriptor_header *fsg_fs_function[];
|
||||
|
||||
extern struct usb_endpoint_descriptor fsg_hs_bulk_in_desc;
|
||||
extern struct usb_endpoint_descriptor fsg_hs_bulk_out_desc;
|
||||
extern struct usb_descriptor_header *fsg_hs_function[];
|
||||
|
||||
extern struct usb_endpoint_descriptor fsg_ss_bulk_in_desc;
|
||||
extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc;
|
||||
extern struct usb_endpoint_descriptor fsg_ss_bulk_out_desc;
|
||||
extern struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc;
|
||||
extern struct usb_descriptor_header *fsg_ss_function[];
|
||||
|
||||
void fsg_lun_close(struct fsg_lun *curlun);
|
||||
int fsg_lun_open(struct fsg_lun *curlun, const char *filename);
|
||||
int fsg_lun_fsync_sub(struct fsg_lun *curlun);
|
||||
void store_cdrom_address(u8 *dest, int msf, u32 addr);
|
||||
ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf);
|
||||
ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf);
|
||||
ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
char *buf);
|
||||
ssize_t fsg_show_inquiry_string(struct fsg_lun *curlun, char *buf);
|
||||
ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf);
|
||||
ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf);
|
||||
ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count);
|
||||
ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count);
|
||||
ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count);
|
||||
ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem,
|
||||
const char *buf, size_t count);
|
||||
ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
|
||||
size_t count);
|
||||
ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
|
||||
size_t count);
|
||||
|
||||
#endif /* USB_STORAGE_COMMON_H */
|
||||
Loading…
Reference in a new issue