[Spice-devel] [PATCH spice-gtk 20/44] fixup! usb-redir: add files for SCSI and USB MSC implementation

Frediano Ziglio fziglio at redhat.com
Tue Jul 30 12:03:08 UTC 2019


Use proper type for CdScsiTarget and UsbCdBulkMsdDevice using
forward declarations, no reasons to hide.
Avoids useless casts and improve type safety.
---
 src/cd-scsi.c         | 46 ++++++++++++++-----------------------------
 src/cd-scsi.h         | 30 ++++++++++++++++------------
 src/cd-usb-bulk-msd.c | 44 +++++++++++++----------------------------
 src/cd-usb-bulk-msd.h | 29 ++++++++++++++-------------
 4 files changed, 61 insertions(+), 88 deletions(-)

diff --git a/src/cd-scsi.c b/src/cd-scsi.c
index 715fe1c6..39692576 100644
--- a/src/cd-scsi.c
+++ b/src/cd-scsi.c
@@ -37,8 +37,6 @@
 
 #define MAX_LUNS   32
 
-typedef struct CdScsiTarget CdScsiTarget;
-
 typedef enum CdScsiPowerCondition {
     CD_SCSI_POWER_STOPPED,
     CD_SCSI_POWER_ACTIVE,
@@ -412,7 +410,7 @@ static void cd_scsi_cmd_names_init(void)
     cmd_names_initialized = TRUE;
 }
 
-void *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns)
+CdScsiTarget *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns)
 {
     CdScsiTarget *st;
 
@@ -431,10 +429,10 @@ void *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns)
 
     cd_scsi_cmd_names_init();
 
-    return (void *)st;
+    return st;
 }
 
-void cd_scsi_target_free(void *scsi_target)
+void cd_scsi_target_free(CdScsiTarget *scsi_target)
 {
     cd_scsi_target_reset(scsi_target);
     g_free(scsi_target);
@@ -452,10 +450,9 @@ static inline gboolean cd_scsi_target_lun_realized(CdScsiTarget *st, uint32_t lu
     return (st->num_luns == 0 || !st->units[lun].realized) ? FALSE : TRUE;
 }
 
-int cd_scsi_dev_realize(void *scsi_target, uint32_t lun,
+int cd_scsi_dev_realize(CdScsiTarget *st, uint32_t lun,
                         const CdScsiDeviceParameters *dev_params)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -509,9 +506,8 @@ static void cd_scsi_lu_media_reset(CdScsiLU *dev)
     dev->num_blocks = 0;
 }
 
-int cd_scsi_dev_lock(void *scsi_target, uint32_t lun, gboolean lock)
+int cd_scsi_dev_lock(CdScsiTarget *st, uint32_t lun, gboolean lock)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -551,10 +547,9 @@ static void cd_scsi_lu_unload(CdScsiLU *dev)
     dev->loaded = FALSE;
 }
 
-int cd_scsi_dev_load(void *scsi_target, uint32_t lun,
+int cd_scsi_dev_load(CdScsiTarget *st, uint32_t lun,
                      const CdScsiMediaParameters *media_params)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -579,9 +574,8 @@ int cd_scsi_dev_load(void *scsi_target, uint32_t lun,
     return 0;
 }
 
-int cd_scsi_dev_get_info(void *scsi_target, uint32_t lun, CdScsiDeviceInfo *lun_info)
+int cd_scsi_dev_get_info(CdScsiTarget *st, uint32_t lun, CdScsiDeviceInfo *lun_info)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -606,9 +600,8 @@ int cd_scsi_dev_get_info(void *scsi_target, uint32_t lun, CdScsiDeviceInfo *lun_
     return 0;
 }
 
-int cd_scsi_dev_unload(void *scsi_target, uint32_t lun)
+int cd_scsi_dev_unload(CdScsiTarget *st, uint32_t lun)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -639,9 +632,8 @@ int cd_scsi_dev_unload(void *scsi_target, uint32_t lun)
     return 0;
 }
 
-int cd_scsi_dev_unrealize(void *scsi_target, uint32_t lun)
+int cd_scsi_dev_unrealize(CdScsiTarget *st, uint32_t lun)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -681,9 +673,8 @@ int cd_scsi_dev_unrealize(void *scsi_target, uint32_t lun)
     return 0;
 }
 
-int cd_scsi_dev_reset(void *scsi_target, uint32_t lun)
+int cd_scsi_dev_reset(CdScsiTarget *st, uint32_t lun)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     CdScsiLU *dev;
 
     if (!cd_scsi_target_lun_legal(st, lun)) {
@@ -724,10 +715,8 @@ static void cd_scsi_target_do_reset(CdScsiTarget *st)
     cd_scsi_target_reset_complete(st->user_data);
 }
 
-int cd_scsi_target_reset(void *scsi_target)
+int cd_scsi_target_reset(CdScsiTarget *st)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
-
     if (st->state == CD_SCSI_TGT_STATE_RESET) {
         SPICE_DEBUG("Target already in reset");
         return -1;
@@ -736,7 +725,7 @@ int cd_scsi_target_reset(void *scsi_target)
     st->state = CD_SCSI_TGT_STATE_RESET;
 
     if (st->cur_req != NULL) {
-        cd_scsi_dev_request_cancel(scsi_target, st->cur_req);
+        cd_scsi_dev_request_cancel(st, st->cur_req);
         if (st->cur_req != NULL) {
             SPICE_DEBUG("Target reset in progress...");
             return 0;
@@ -2576,9 +2565,8 @@ static void cd_scsi_cmd_read(CdScsiLU *dev, CdScsiRequest *req)
     cd_scsi_read_async_start(dev, req);
 }
 
-void cd_scsi_dev_request_submit(void *scsi_target, CdScsiRequest *req)
+void cd_scsi_dev_request_submit(CdScsiTarget *st, CdScsiRequest *req)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
     uint32_t lun = req->lun;
     uint32_t opcode = (uint32_t)req->cdb[0];
     const char *cmd_name = scsi_cmd_name[opcode];
@@ -2734,10 +2722,8 @@ done:
     }
 }
 
-void cd_scsi_dev_request_cancel(void *scsi_target, CdScsiRequest *req)
+void cd_scsi_dev_request_cancel(CdScsiTarget *st, CdScsiRequest *req)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
-
     if (st->cur_req == req) {
         if (req->req_state == SCSI_REQ_RUNNING) {
             SPICE_DEBUG("request_cancel: lun: %" G_GUINT32_FORMAT
@@ -2752,10 +2738,8 @@ void cd_scsi_dev_request_cancel(void *scsi_target, CdScsiRequest *req)
     }
 }
 
-void cd_scsi_dev_request_release(void *scsi_target, CdScsiRequest *req)
+void cd_scsi_dev_request_release(CdScsiTarget *st, CdScsiRequest *req)
 {
-    CdScsiTarget *st = (CdScsiTarget *)scsi_target;
-
     st->cur_req = NULL;
     cd_scsi_req_init(req);
 
diff --git a/src/cd-scsi.h b/src/cd-scsi.h
index 0fd7b90b..0b6ab9c1 100644
--- a/src/cd-scsi.h
+++ b/src/cd-scsi.h
@@ -85,25 +85,29 @@ typedef struct CdScsiRequest {
 CdScsiReqState cd_scsi_get_req_state(CdScsiRequest *req);
 
 /* SCSI target/device API */
+typedef struct CdScsiTarget CdScsiTarget;
 
-void *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns); /* to be used in callbacks */
-void cd_scsi_target_free(void *scsi_target);
+/* to be used in callbacks */
+CdScsiTarget *cd_scsi_target_alloc(void *target_user_data, uint32_t max_luns);
+void cd_scsi_target_free(CdScsiTarget *scsi_target);
 
-int cd_scsi_dev_realize(void *scsi_target, uint32_t lun, const CdScsiDeviceParameters *dev_params);
-int cd_scsi_dev_unrealize(void *scsi_target, uint32_t lun);
+int cd_scsi_dev_realize(CdScsiTarget *scsi_target, uint32_t lun,
+                        const CdScsiDeviceParameters *dev_params);
+int cd_scsi_dev_unrealize(CdScsiTarget *scsi_target, uint32_t lun);
 
-int cd_scsi_dev_lock(void *scsi_target, uint32_t lun, gboolean lock);
-int cd_scsi_dev_load(void *scsi_target, uint32_t lun, const CdScsiMediaParameters *media_params);
-int cd_scsi_dev_get_info(void *scsi_target, uint32_t lun, CdScsiDeviceInfo *lun_info);
-int cd_scsi_dev_unload(void *scsi_target, uint32_t lun);
+int cd_scsi_dev_lock(CdScsiTarget *scsi_target, uint32_t lun, gboolean lock);
+int cd_scsi_dev_load(CdScsiTarget *scsi_target, uint32_t lun,
+                     const CdScsiMediaParameters *media_params);
+int cd_scsi_dev_get_info(CdScsiTarget *scsi_target, uint32_t lun, CdScsiDeviceInfo *lun_info);
+int cd_scsi_dev_unload(CdScsiTarget *scsi_target, uint32_t lun);
 
-void cd_scsi_dev_request_submit(void *scsi_target, CdScsiRequest *request);
-void cd_scsi_dev_request_cancel(void *scsi_target, CdScsiRequest *request);
-void cd_scsi_dev_request_release(void *scsi_target, CdScsiRequest *request);
+void cd_scsi_dev_request_submit(CdScsiTarget *scsi_target, CdScsiRequest *request);
+void cd_scsi_dev_request_cancel(CdScsiTarget *scsi_target, CdScsiRequest *request);
+void cd_scsi_dev_request_release(CdScsiTarget *scsi_target, CdScsiRequest *request);
 
-int cd_scsi_dev_reset(void *scsi_target, uint32_t lun);
+int cd_scsi_dev_reset(CdScsiTarget *scsi_target, uint32_t lun);
 
-int cd_scsi_target_reset(void *scsi_target);
+int cd_scsi_target_reset(CdScsiTarget *scsi_target);
 
 /* Callbacks */
 
diff --git a/src/cd-usb-bulk-msd.c b/src/cd-usb-bulk-msd.c
index c1a3a8c3..89e94f8e 100644
--- a/src/cd-usb-bulk-msd.c
+++ b/src/cd-usb-bulk-msd.c
@@ -83,7 +83,7 @@ typedef struct UsbCdBulkMsdRequest {
 
 typedef struct UsbCdBulkMsdDevice {
     UsbCdState state;
-    void *scsi_target; /* scsi handle */
+    CdScsiTarget *scsi_target; /* scsi handle */
     void *usb_user_data; /* used in callbacks to usb */
     UsbCdBulkMsdRequest usb_req; /* now supporting a single cmd */
     uint8_t *data_buf;
@@ -120,11 +120,9 @@ static void cd_usb_bulk_msd_set_state(UsbCdBulkMsdDevice *cd, UsbCdState state)
     cd->state = state;
 }
 
-void *cd_usb_bulk_msd_alloc(void *usb_user_data, uint32_t max_luns)
+UsbCdBulkMsdDevice *cd_usb_bulk_msd_alloc(void *usb_user_data, uint32_t max_luns)
 {
-    UsbCdBulkMsdDevice *cd;
-
-    cd = g_malloc0(sizeof(*cd));
+    UsbCdBulkMsdDevice *cd = g_new0(UsbCdBulkMsdDevice, 1);
 
     cd->data_buf_len = 256 * 1024;
     cd->data_buf = g_malloc(cd->data_buf_len);
@@ -141,10 +139,9 @@ void *cd_usb_bulk_msd_alloc(void *usb_user_data, uint32_t max_luns)
     return cd;
 }
 
-int cd_usb_bulk_msd_realize(void *device, uint32_t lun,
+int cd_usb_bulk_msd_realize(UsbCdBulkMsdDevice *cd, uint32_t lun,
                             const CdScsiDeviceParameters *dev_params)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     CdScsiDeviceParameters scsi_dev_params;
     int rc;
 
@@ -168,9 +165,8 @@ int cd_usb_bulk_msd_realize(void *device, uint32_t lun,
     return 0;
 }
 
-int cd_usb_bulk_msd_lock(void *device, uint32_t lun, gboolean lock)
+int cd_usb_bulk_msd_lock(UsbCdBulkMsdDevice *cd, uint32_t lun, gboolean lock)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     int rc;
 
     rc = cd_scsi_dev_lock(cd->scsi_target, lun, lock);
@@ -183,10 +179,9 @@ int cd_usb_bulk_msd_lock(void *device, uint32_t lun, gboolean lock)
     return 0;
 }
 
-int cd_usb_bulk_msd_load(void *device, uint32_t lun,
+int cd_usb_bulk_msd_load(UsbCdBulkMsdDevice *cd, uint32_t lun,
                          const CdScsiMediaParameters *media_params)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     int rc;
 
     rc = cd_scsi_dev_load(cd->scsi_target, lun, media_params);
@@ -199,9 +194,8 @@ int cd_usb_bulk_msd_load(void *device, uint32_t lun,
     return 0;
 }
 
-int cd_usb_bulk_msd_get_info(void *device, uint32_t lun, CdScsiDeviceInfo *lun_info)
+int cd_usb_bulk_msd_get_info(UsbCdBulkMsdDevice *cd, uint32_t lun, CdScsiDeviceInfo *lun_info)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     int rc;
 
     rc = cd_scsi_dev_get_info(cd->scsi_target, lun, lun_info);
@@ -213,9 +207,8 @@ int cd_usb_bulk_msd_get_info(void *device, uint32_t lun, CdScsiDeviceInfo *lun_i
     return 0;
 }
 
-int cd_usb_bulk_msd_unload(void *device, uint32_t lun)
+int cd_usb_bulk_msd_unload(UsbCdBulkMsdDevice *cd, uint32_t lun)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     int rc;
 
     rc = cd_scsi_dev_unload(cd->scsi_target, lun);
@@ -228,9 +221,8 @@ int cd_usb_bulk_msd_unload(void *device, uint32_t lun)
     return 0;
 }
 
-int cd_usb_bulk_msd_unrealize(void *device, uint32_t lun)
+int cd_usb_bulk_msd_unrealize(UsbCdBulkMsdDevice *cd, uint32_t lun)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     int rc;
 
     rc = cd_scsi_dev_unrealize(cd->scsi_target, lun);
@@ -243,20 +235,16 @@ int cd_usb_bulk_msd_unrealize(void *device, uint32_t lun)
     return 0;
 }
 
-void cd_usb_bulk_msd_free(void *device)
+void cd_usb_bulk_msd_free(UsbCdBulkMsdDevice *cd)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
-
     cd_scsi_target_free(cd->scsi_target);
     g_free(cd);
 
     SPICE_DEBUG("Free");
 }
 
-int cd_usb_bulk_msd_reset(void *device)
+int cd_usb_bulk_msd_reset(UsbCdBulkMsdDevice *cd)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
-
     cd_scsi_target_reset(cd->scsi_target);
     cd_usb_bulk_msd_set_state(cd, USB_CD_STATE_CBW);
 
@@ -395,9 +383,8 @@ static void usb_cd_send_data_in(UsbCdBulkMsdDevice *cd, uint32_t max_len)
     }
 }
 
-int cd_usb_bulk_msd_read(void *device, uint32_t max_len)
+int cd_usb_bulk_msd_read(UsbCdBulkMsdDevice *cd, uint32_t max_len)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
     CdScsiRequest *scsi_req = &usb_req->scsi_req;
 
@@ -487,9 +474,8 @@ void cd_scsi_dev_request_complete(void *target_user_data, CdScsiRequest *scsi_re
     }
 }
 
-int cd_usb_bulk_msd_cancel_read(void *device)
+int cd_usb_bulk_msd_cancel_read(UsbCdBulkMsdDevice *cd)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
     UsbCdBulkMsdRequest *usb_req = &cd->usb_req;
     CdScsiRequest *scsi_req = &usb_req->scsi_req;
 
@@ -497,10 +483,8 @@ int cd_usb_bulk_msd_cancel_read(void *device)
     return 0;
 }
 
-int cd_usb_bulk_msd_write(void *device, uint8_t *buf_out, uint32_t buf_out_len)
+int cd_usb_bulk_msd_write(UsbCdBulkMsdDevice *cd, uint8_t *buf_out, uint32_t buf_out_len)
 {
-    UsbCdBulkMsdDevice *cd = (UsbCdBulkMsdDevice *)device;
-
     switch (cd->state) {
     case USB_CD_STATE_CBW: /* Command Block */
         parse_usb_msd_cmd(cd, buf_out, buf_out_len);
diff --git a/src/cd-usb-bulk-msd.h b/src/cd-usb-bulk-msd.h
index 976b422a..492a49b8 100644
--- a/src/cd-usb-bulk-msd.h
+++ b/src/cd-usb-bulk-msd.h
@@ -35,6 +35,8 @@ typedef enum CdUsbBulkStatus {
     BULK_STATUS_STALL,
 } CdUsbBulkStatus;
 
+typedef struct UsbCdBulkMsdDevice UsbCdBulkMsdDevice;
+
 /* USB backend callbacks */
 
 /* called on completed read data bulk transfer
@@ -60,56 +62,55 @@ void cd_usb_bulk_msd_reset_complete(void *user_data,
 /* MSD backend api */
 
 /* allocate new device descriptor */
-void *cd_usb_bulk_msd_alloc(void *user_data, uint32_t max_lun);
+UsbCdBulkMsdDevice *cd_usb_bulk_msd_alloc(void *user_data, uint32_t max_lun);
 
 /* free device descriptor */
-void cd_usb_bulk_msd_free(void *device);
+void cd_usb_bulk_msd_free(UsbCdBulkMsdDevice *device);
 
-/* configure a new Logical Unit to be represented by the device
+/* configure a new Logical Unit to be represen ted by the device
  *  returns: error code
  */
-int cd_usb_bulk_msd_realize(void *device, uint32_t lun,
+int cd_usb_bulk_msd_realize(UsbCdBulkMsdDevice *device, uint32_t lun,
                             const CdScsiDeviceParameters *dev_params);
 
 /* lock the device, prevent unloading
  * returns: error code
  */
-int cd_usb_bulk_msd_lock(void *device, uint32_t lun, gboolean lock);
+int cd_usb_bulk_msd_lock(UsbCdBulkMsdDevice *device, uint32_t lun, gboolean lock);
 
 /* load new media, if already loaded, simulate media change
  * returns: error code
  */
-int cd_usb_bulk_msd_load(void *device, uint32_t lun,
+int cd_usb_bulk_msd_load(UsbCdBulkMsdDevice *device, uint32_t lun,
                          const CdScsiMediaParameters *media_params);
 
 /* query unit parameters and status
  * returns: error code
  */
-int cd_usb_bulk_msd_get_info(void *device, uint32_t lun,
+int cd_usb_bulk_msd_get_info(UsbCdBulkMsdDevice *device, uint32_t lun,
                              CdScsiDeviceInfo *lun_info);
 
 /* unload the media
  * returns: error code
  */
-int cd_usb_bulk_msd_unload(void *device, uint32_t lun);
+int cd_usb_bulk_msd_unload(UsbCdBulkMsdDevice *device, uint32_t lun);
 
 /* detach a Logical Unit
  * returns: error code
  */
-int cd_usb_bulk_msd_unrealize(void *device, uint32_t lun);
+int cd_usb_bulk_msd_unrealize(UsbCdBulkMsdDevice *device, uint32_t lun);
 
 /* reset the device instance; cancel all IO ops, reset state
  * returns: error code
  */
-int cd_usb_bulk_msd_reset(void *device);
+int cd_usb_bulk_msd_reset(UsbCdBulkMsdDevice *device);
 
 
 /* perform a write data bulk transfer
  * data_len - length of available data to write
  * returns: error code
  */
-int cd_usb_bulk_msd_write(void *device,
-                          uint8_t *buf, uint32_t data_len);
+int cd_usb_bulk_msd_write(UsbCdBulkMsdDevice*device, uint8_t *buf, uint32_t data_len);
 
 
 /* perform a read data bulk transfer
@@ -121,12 +122,12 @@ int cd_usb_bulk_msd_write(void *device,
  *
  * returns: 0 - success, -1 - error
  */
-int cd_usb_bulk_msd_read(void *device, uint32_t max_len);
+int cd_usb_bulk_msd_read(UsbCdBulkMsdDevice *device, uint32_t max_len);
 
 /* cancels pending read data bulk transfer
  * returns: error code
  */
-int cd_usb_bulk_msd_cancel_read(void *device);
+int cd_usb_bulk_msd_cancel_read(UsbCdBulkMsdDevice *device);
 
 G_END_DECLS
 
-- 
2.20.1



More information about the Spice-devel mailing list