Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  *  Sony MemoryStick support
0004  *
0005  *  Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
0006  */
0007 
0008 #ifndef _MEMSTICK_H
0009 #define _MEMSTICK_H
0010 
0011 #include <linux/workqueue.h>
0012 #include <linux/scatterlist.h>
0013 #include <linux/device.h>
0014 
0015 /*** Hardware based structures ***/
0016 
0017 struct ms_status_register {
0018     unsigned char reserved;
0019     unsigned char interrupt;
0020 #define MEMSTICK_INT_CMDNAK 0x01
0021 #define MEMSTICK_INT_IOREQ  0x08
0022 #define MEMSTICK_INT_IOBREQ 0x10
0023 #define MEMSTICK_INT_BREQ   0x20
0024 #define MEMSTICK_INT_ERR    0x40
0025 #define MEMSTICK_INT_CED    0x80
0026 
0027     unsigned char status0;
0028 #define MEMSTICK_STATUS0_WP  0x01
0029 #define MEMSTICK_STATUS0_SL  0x02
0030 #define MEMSTICK_STATUS0_BF  0x10
0031 #define MEMSTICK_STATUS0_BE  0x20
0032 #define MEMSTICK_STATUS0_FB0 0x40
0033 #define MEMSTICK_STATUS0_MB  0x80
0034 
0035     unsigned char status1;
0036 #define MEMSTICK_STATUS1_UCFG 0x01
0037 #define MEMSTICK_STATUS1_FGER 0x02
0038 #define MEMSTICK_STATUS1_UCEX 0x04
0039 #define MEMSTICK_STATUS1_EXER 0x08
0040 #define MEMSTICK_STATUS1_UCDT 0x10
0041 #define MEMSTICK_STATUS1_DTER 0x20
0042 #define MEMSTICK_STATUS1_FB1  0x40
0043 #define MEMSTICK_STATUS1_MB   0x80
0044 } __attribute__((packed));
0045 
0046 struct ms_id_register {
0047     unsigned char type;
0048     unsigned char if_mode;
0049     unsigned char category;
0050     unsigned char class;
0051 } __attribute__((packed));
0052 
0053 struct ms_param_register {
0054     unsigned char system;
0055 #define MEMSTICK_SYS_PAM  0x08
0056 #define MEMSTICK_SYS_BAMD 0x80
0057 
0058     unsigned char block_address_msb;
0059     unsigned short block_address;
0060     unsigned char cp;
0061 #define MEMSTICK_CP_BLOCK     0x00
0062 #define MEMSTICK_CP_PAGE      0x20
0063 #define MEMSTICK_CP_EXTRA     0x40
0064 #define MEMSTICK_CP_OVERWRITE 0x80
0065 
0066     unsigned char page_address;
0067 } __attribute__((packed));
0068 
0069 struct ms_extra_data_register {
0070     unsigned char  overwrite_flag;
0071 #define MEMSTICK_OVERWRITE_UDST  0x10
0072 #define MEMSTICK_OVERWRITE_PGST1 0x20
0073 #define MEMSTICK_OVERWRITE_PGST0 0x40
0074 #define MEMSTICK_OVERWRITE_BKST  0x80
0075 
0076     unsigned char  management_flag;
0077 #define MEMSTICK_MANAGEMENT_SYSFLG 0x04
0078 #define MEMSTICK_MANAGEMENT_ATFLG  0x08
0079 #define MEMSTICK_MANAGEMENT_SCMS1  0x10
0080 #define MEMSTICK_MANAGEMENT_SCMS0  0x20
0081 
0082     unsigned short logical_address;
0083 } __attribute__((packed));
0084 
0085 struct ms_register {
0086     struct ms_status_register     status;
0087     struct ms_id_register         id;
0088     unsigned char                 reserved[8];
0089     struct ms_param_register      param;
0090     struct ms_extra_data_register extra_data;
0091 } __attribute__((packed));
0092 
0093 struct mspro_param_register {
0094     unsigned char  system;
0095 #define MEMSTICK_SYS_PAR4   0x00
0096 #define MEMSTICK_SYS_PAR8   0x40
0097 #define MEMSTICK_SYS_SERIAL 0x80
0098 
0099     __be16 data_count;
0100     __be32 data_address;
0101     unsigned char  tpc_param;
0102 } __attribute__((packed));
0103 
0104 struct mspro_io_info_register {
0105     unsigned char version;
0106     unsigned char io_category;
0107     unsigned char current_req;
0108     unsigned char card_opt_info;
0109     unsigned char rdy_wait_time;
0110 } __attribute__((packed));
0111 
0112 struct mspro_io_func_register {
0113     unsigned char func_enable;
0114     unsigned char func_select;
0115     unsigned char func_intmask;
0116     unsigned char transfer_mode;
0117 } __attribute__((packed));
0118 
0119 struct mspro_io_cmd_register {
0120     unsigned short tpc_param;
0121     unsigned short data_count;
0122     unsigned int   data_address;
0123 } __attribute__((packed));
0124 
0125 struct mspro_register {
0126     struct ms_status_register     status;
0127     struct ms_id_register         id;
0128     unsigned char                 reserved0[8];
0129     struct mspro_param_register   param;
0130     unsigned char                 reserved1[8];
0131     struct mspro_io_info_register io_info;
0132     struct mspro_io_func_register io_func;
0133     unsigned char                 reserved2[7];
0134     struct mspro_io_cmd_register  io_cmd;
0135     unsigned char                 io_int;
0136     unsigned char                 io_int_func;
0137 } __attribute__((packed));
0138 
0139 struct ms_register_addr {
0140     unsigned char r_offset;
0141     unsigned char r_length;
0142     unsigned char w_offset;
0143     unsigned char w_length;
0144 } __attribute__((packed));
0145 
0146 enum memstick_tpc {
0147     MS_TPC_READ_MG_STATUS   = 0x01,
0148     MS_TPC_READ_LONG_DATA   = 0x02,
0149     MS_TPC_READ_SHORT_DATA  = 0x03,
0150     MS_TPC_READ_MG_DATA     = 0x03,
0151     MS_TPC_READ_REG         = 0x04,
0152     MS_TPC_READ_QUAD_DATA   = 0x05,
0153     MS_TPC_READ_IO_DATA     = 0x05,
0154     MS_TPC_GET_INT          = 0x07,
0155     MS_TPC_SET_RW_REG_ADRS  = 0x08,
0156     MS_TPC_EX_SET_CMD       = 0x09,
0157     MS_TPC_WRITE_QUAD_DATA  = 0x0a,
0158     MS_TPC_WRITE_IO_DATA    = 0x0a,
0159     MS_TPC_WRITE_REG        = 0x0b,
0160     MS_TPC_WRITE_SHORT_DATA = 0x0c,
0161     MS_TPC_WRITE_MG_DATA    = 0x0c,
0162     MS_TPC_WRITE_LONG_DATA  = 0x0d,
0163     MS_TPC_SET_CMD          = 0x0e
0164 };
0165 
0166 enum memstick_command {
0167     MS_CMD_BLOCK_END       = 0x33,
0168     MS_CMD_RESET           = 0x3c,
0169     MS_CMD_BLOCK_WRITE     = 0x55,
0170     MS_CMD_SLEEP           = 0x5a,
0171     MS_CMD_BLOCK_ERASE     = 0x99,
0172     MS_CMD_BLOCK_READ      = 0xaa,
0173     MS_CMD_CLEAR_BUF       = 0xc3,
0174     MS_CMD_FLASH_STOP      = 0xcc,
0175     MS_CMD_LOAD_ID         = 0x60,
0176     MS_CMD_CMP_ICV         = 0x7f,
0177     MSPRO_CMD_FORMAT       = 0x10,
0178     MSPRO_CMD_SLEEP        = 0x11,
0179     MSPRO_CMD_WAKEUP       = 0x12,
0180     MSPRO_CMD_READ_DATA    = 0x20,
0181     MSPRO_CMD_WRITE_DATA   = 0x21,
0182     MSPRO_CMD_READ_ATRB    = 0x24,
0183     MSPRO_CMD_STOP         = 0x25,
0184     MSPRO_CMD_ERASE        = 0x26,
0185     MSPRO_CMD_READ_QUAD    = 0x27,
0186     MSPRO_CMD_WRITE_QUAD   = 0x28,
0187     MSPRO_CMD_SET_IBD      = 0x46,
0188     MSPRO_CMD_GET_IBD      = 0x47,
0189     MSPRO_CMD_IN_IO_DATA   = 0xb0,
0190     MSPRO_CMD_OUT_IO_DATA  = 0xb1,
0191     MSPRO_CMD_READ_IO_ATRB = 0xb2,
0192     MSPRO_CMD_IN_IO_FIFO   = 0xb3,
0193     MSPRO_CMD_OUT_IO_FIFO  = 0xb4,
0194     MSPRO_CMD_IN_IOM       = 0xb5,
0195     MSPRO_CMD_OUT_IOM      = 0xb6,
0196 };
0197 
0198 /*** Driver structures and functions ***/
0199 
0200 enum memstick_param { MEMSTICK_POWER = 1, MEMSTICK_INTERFACE };
0201 
0202 #define MEMSTICK_POWER_OFF 0
0203 #define MEMSTICK_POWER_ON  1
0204 
0205 #define MEMSTICK_SERIAL   0
0206 #define MEMSTICK_PAR4     1
0207 #define MEMSTICK_PAR8     2
0208 
0209 struct memstick_host;
0210 struct memstick_driver;
0211 
0212 struct memstick_device_id {
0213     unsigned char match_flags;
0214 #define MEMSTICK_MATCH_ALL            0x01
0215 
0216     unsigned char type;
0217 #define MEMSTICK_TYPE_LEGACY          0xff
0218 #define MEMSTICK_TYPE_DUO             0x00
0219 #define MEMSTICK_TYPE_PRO             0x01
0220 
0221     unsigned char category;
0222 #define MEMSTICK_CATEGORY_STORAGE     0xff
0223 #define MEMSTICK_CATEGORY_STORAGE_DUO 0x00
0224 #define MEMSTICK_CATEGORY_IO          0x01
0225 #define MEMSTICK_CATEGORY_IO_PRO      0x10
0226 
0227     unsigned char class;
0228 #define MEMSTICK_CLASS_FLASH          0xff
0229 #define MEMSTICK_CLASS_DUO            0x00
0230 #define MEMSTICK_CLASS_ROM            0x01
0231 #define MEMSTICK_CLASS_RO             0x02
0232 #define MEMSTICK_CLASS_WP             0x03
0233 };
0234 
0235 struct memstick_request {
0236     unsigned char tpc;
0237     unsigned char data_dir:1,
0238               need_card_int:1,
0239               long_data:1;
0240     unsigned char int_reg;
0241     int           error;
0242     union {
0243         struct scatterlist sg;
0244         struct {
0245             unsigned char data_len;
0246             unsigned char data[15];
0247         };
0248     };
0249 };
0250 
0251 struct memstick_dev {
0252     struct memstick_device_id id;
0253     struct memstick_host     *host;
0254     struct ms_register_addr  reg_addr;
0255     struct completion        mrq_complete;
0256     struct memstick_request  current_mrq;
0257 
0258     /* Check that media driver is still willing to operate the device. */
0259     int                      (*check)(struct memstick_dev *card);
0260     /* Get next request from the media driver.                         */
0261     int                      (*next_request)(struct memstick_dev *card,
0262                          struct memstick_request **mrq);
0263     /* Tell the media driver to stop doing things                      */
0264     void                     (*stop)(struct memstick_dev *card);
0265     /* Allow the media driver to continue                              */
0266     void                     (*start)(struct memstick_dev *card);
0267 
0268     struct device            dev;
0269 };
0270 
0271 struct memstick_host {
0272     struct mutex        lock;
0273     unsigned int        id;
0274     unsigned int        caps;
0275 #define MEMSTICK_CAP_AUTO_GET_INT  1
0276 #define MEMSTICK_CAP_PAR4          2
0277 #define MEMSTICK_CAP_PAR8          4
0278 
0279     struct work_struct  media_checker;
0280     struct device       dev;
0281 
0282     struct memstick_dev *card;
0283     unsigned int        retries;
0284     bool removing;
0285 
0286     /* Notify the host that some requests are pending. */
0287     void                (*request)(struct memstick_host *host);
0288     /* Set host IO parameters (power, clock, etc).     */
0289     int                 (*set_param)(struct memstick_host *host,
0290                      enum memstick_param param,
0291                      int value);
0292     unsigned long       private[] ____cacheline_aligned;
0293 };
0294 
0295 struct memstick_driver {
0296     struct memstick_device_id *id_table;
0297     int                       (*probe)(struct memstick_dev *card);
0298     void                      (*remove)(struct memstick_dev *card);
0299     int                       (*suspend)(struct memstick_dev *card,
0300                          pm_message_t state);
0301     int                       (*resume)(struct memstick_dev *card);
0302 
0303     struct device_driver      driver;
0304 };
0305 
0306 int memstick_register_driver(struct memstick_driver *drv);
0307 void memstick_unregister_driver(struct memstick_driver *drv);
0308 
0309 struct memstick_host *memstick_alloc_host(unsigned int extra,
0310                       struct device *dev);
0311 
0312 int memstick_add_host(struct memstick_host *host);
0313 void memstick_remove_host(struct memstick_host *host);
0314 void memstick_free_host(struct memstick_host *host);
0315 void memstick_detect_change(struct memstick_host *host);
0316 void memstick_suspend_host(struct memstick_host *host);
0317 void memstick_resume_host(struct memstick_host *host);
0318 
0319 void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
0320               const struct scatterlist *sg);
0321 void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
0322                const void *buf, size_t length);
0323 int memstick_next_req(struct memstick_host *host,
0324               struct memstick_request **mrq);
0325 void memstick_new_req(struct memstick_host *host);
0326 
0327 int memstick_set_rw_addr(struct memstick_dev *card);
0328 
0329 static inline void *memstick_priv(struct memstick_host *host)
0330 {
0331     return (void *)host->private;
0332 }
0333 
0334 static inline void *memstick_get_drvdata(struct memstick_dev *card)
0335 {
0336     return dev_get_drvdata(&card->dev);
0337 }
0338 
0339 static inline void memstick_set_drvdata(struct memstick_dev *card, void *data)
0340 {
0341     dev_set_drvdata(&card->dev, data);
0342 }
0343 
0344 #endif