aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/usb/gadget/f_mass_storage.c341
1 files changed, 173 insertions, 168 deletions
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index ed16a9538753..7750346b696c 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -320,14 +320,32 @@ MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
320/*-------------------------------------------------------------------------*/ 320/*-------------------------------------------------------------------------*/
321 321
322 322
323/* Data shared by all the FSG instances. */
324struct fsg_common {
325 /* filesem protects: backing files in use */
326 struct rw_semaphore filesem;
327
328 struct fsg_buffhd *next_buffhd_to_fill;
329 struct fsg_buffhd *next_buffhd_to_drain;
330 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
331
332 int cmnd_size;
333 u8 cmnd[MAX_COMMAND_SIZE];
334
335 unsigned int nluns;
336 unsigned int lun;
337 struct fsg_lun *luns;
338 struct fsg_lun *curlun;
339};
340
341
323struct fsg_dev { 342struct fsg_dev {
324 /* lock protects: state, all the req_busy's, and cbbuf_cmnd */ 343 struct fsg_common *common;
344
345 /* lock protects: state, all the req_busy's */
325 spinlock_t lock; 346 spinlock_t lock;
326 struct usb_gadget *gadget; 347 struct usb_gadget *gadget;
327 348
328 /* filesem protects: backing files in use */
329 struct rw_semaphore filesem;
330
331 /* reference counting: wait until all LUNs are released */ 349 /* reference counting: wait until all LUNs are released */
332 struct kref ref; 350 struct kref ref;
333 351
@@ -356,27 +374,16 @@ struct fsg_dev {
356 struct usb_ep *bulk_in; 374 struct usb_ep *bulk_in;
357 struct usb_ep *bulk_out; 375 struct usb_ep *bulk_out;
358 376
359 struct fsg_buffhd *next_buffhd_to_fill;
360 struct fsg_buffhd *next_buffhd_to_drain;
361 struct fsg_buffhd buffhds[FSG_NUM_BUFFERS];
362
363 int thread_wakeup_needed; 377 int thread_wakeup_needed;
364 struct completion thread_notifier; 378 struct completion thread_notifier;
365 struct task_struct *thread_task; 379 struct task_struct *thread_task;
366 380
367 int cmnd_size;
368 u8 cmnd[MAX_COMMAND_SIZE];
369 enum data_direction data_dir; 381 enum data_direction data_dir;
370 u32 data_size; 382 u32 data_size;
371 u32 data_size_from_cmnd; 383 u32 data_size_from_cmnd;
372 u32 tag; 384 u32 tag;
373 unsigned int lun;
374 u32 residue; 385 u32 residue;
375 u32 usb_amount_left; 386 u32 usb_amount_left;
376
377 unsigned int nluns;
378 struct fsg_lun *luns;
379 struct fsg_lun *curlun;
380}; 387};
381 388
382typedef void (*fsg_routine_t)(struct fsg_dev *); 389typedef void (*fsg_routine_t)(struct fsg_dev *);
@@ -674,7 +681,7 @@ static int class_setup_req(struct fsg_dev *fsg,
674 if (w_index != 0 || w_value != 0) 681 if (w_index != 0 || w_value != 0)
675 return -EDOM; 682 return -EDOM;
676 VDBG(fsg, "get max LUN\n"); 683 VDBG(fsg, "get max LUN\n");
677 *(u8 *) req->buf = fsg->nluns - 1; 684 *(u8 *) req->buf = fsg->common->nluns - 1;
678 return 1; 685 return 1;
679 } 686 }
680 687
@@ -903,7 +910,7 @@ static int sleep_thread(struct fsg_dev *fsg)
903 910
904static int do_read(struct fsg_dev *fsg) 911static int do_read(struct fsg_dev *fsg)
905{ 912{
906 struct fsg_lun *curlun = fsg->curlun; 913 struct fsg_lun *curlun = fsg->common->curlun;
907 u32 lba; 914 u32 lba;
908 struct fsg_buffhd *bh; 915 struct fsg_buffhd *bh;
909 int rc; 916 int rc;
@@ -915,15 +922,15 @@ static int do_read(struct fsg_dev *fsg)
915 922
916 /* Get the starting Logical Block Address and check that it's 923 /* Get the starting Logical Block Address and check that it's
917 * not too big */ 924 * not too big */
918 if (fsg->cmnd[0] == SC_READ_6) 925 if (fsg->common->cmnd[0] == SC_READ_6)
919 lba = get_unaligned_be24(&fsg->cmnd[1]); 926 lba = get_unaligned_be24(&fsg->common->cmnd[1]);
920 else { 927 else {
921 lba = get_unaligned_be32(&fsg->cmnd[2]); 928 lba = get_unaligned_be32(&fsg->common->cmnd[2]);
922 929
923 /* We allow DPO (Disable Page Out = don't save data in the 930 /* We allow DPO (Disable Page Out = don't save data in the
924 * cache) and FUA (Force Unit Access = don't read from the 931 * cache) and FUA (Force Unit Access = don't read from the
925 * cache), but we don't implement them. */ 932 * cache), but we don't implement them. */
926 if ((fsg->cmnd[1] & ~0x18) != 0) { 933 if ((fsg->common->cmnd[1] & ~0x18) != 0) {
927 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 934 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
928 return -EINVAL; 935 return -EINVAL;
929 } 936 }
@@ -958,7 +965,7 @@ static int do_read(struct fsg_dev *fsg)
958 partial_page); 965 partial_page);
959 966
960 /* Wait for the next buffer to become available */ 967 /* Wait for the next buffer to become available */
961 bh = fsg->next_buffhd_to_fill; 968 bh = fsg->common->next_buffhd_to_fill;
962 while (bh->state != BUF_STATE_EMPTY) { 969 while (bh->state != BUF_STATE_EMPTY) {
963 rc = sleep_thread(fsg); 970 rc = sleep_thread(fsg);
964 if (rc) 971 if (rc)
@@ -1018,7 +1025,7 @@ static int do_read(struct fsg_dev *fsg)
1018 bh->inreq->zero = 0; 1025 bh->inreq->zero = 0;
1019 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1026 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1020 &bh->inreq_busy, &bh->state); 1027 &bh->inreq_busy, &bh->state);
1021 fsg->next_buffhd_to_fill = bh->next; 1028 fsg->common->next_buffhd_to_fill = bh->next;
1022 } 1029 }
1023 1030
1024 return -EIO; // No default reply 1031 return -EIO; // No default reply
@@ -1029,7 +1036,7 @@ static int do_read(struct fsg_dev *fsg)
1029 1036
1030static int do_write(struct fsg_dev *fsg) 1037static int do_write(struct fsg_dev *fsg)
1031{ 1038{
1032 struct fsg_lun *curlun = fsg->curlun; 1039 struct fsg_lun *curlun = fsg->common->curlun;
1033 u32 lba; 1040 u32 lba;
1034 struct fsg_buffhd *bh; 1041 struct fsg_buffhd *bh;
1035 int get_some_more; 1042 int get_some_more;
@@ -1050,20 +1057,20 @@ static int do_write(struct fsg_dev *fsg)
1050 1057
1051 /* Get the starting Logical Block Address and check that it's 1058 /* Get the starting Logical Block Address and check that it's
1052 * not too big */ 1059 * not too big */
1053 if (fsg->cmnd[0] == SC_WRITE_6) 1060 if (fsg->common->cmnd[0] == SC_WRITE_6)
1054 lba = get_unaligned_be24(&fsg->cmnd[1]); 1061 lba = get_unaligned_be24(&fsg->common->cmnd[1]);
1055 else { 1062 else {
1056 lba = get_unaligned_be32(&fsg->cmnd[2]); 1063 lba = get_unaligned_be32(&fsg->common->cmnd[2]);
1057 1064
1058 /* We allow DPO (Disable Page Out = don't save data in the 1065 /* We allow DPO (Disable Page Out = don't save data in the
1059 * cache) and FUA (Force Unit Access = write directly to the 1066 * cache) and FUA (Force Unit Access = write directly to the
1060 * medium). We don't implement DPO; we implement FUA by 1067 * medium). We don't implement DPO; we implement FUA by
1061 * performing synchronous output. */ 1068 * performing synchronous output. */
1062 if ((fsg->cmnd[1] & ~0x18) != 0) { 1069 if ((fsg->common->cmnd[1] & ~0x18) != 0) {
1063 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1070 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1064 return -EINVAL; 1071 return -EINVAL;
1065 } 1072 }
1066 if (fsg->cmnd[1] & 0x08) { // FUA 1073 if (fsg->common->cmnd[1] & 0x08) { // FUA
1067 spin_lock(&curlun->filp->f_lock); 1074 spin_lock(&curlun->filp->f_lock);
1068 curlun->filp->f_flags |= O_SYNC; 1075 curlun->filp->f_flags |= O_SYNC;
1069 spin_unlock(&curlun->filp->f_lock); 1076 spin_unlock(&curlun->filp->f_lock);
@@ -1082,7 +1089,7 @@ static int do_write(struct fsg_dev *fsg)
1082 while (amount_left_to_write > 0) { 1089 while (amount_left_to_write > 0) {
1083 1090
1084 /* Queue a request for more data from the host */ 1091 /* Queue a request for more data from the host */
1085 bh = fsg->next_buffhd_to_fill; 1092 bh = fsg->common->next_buffhd_to_fill;
1086 if (bh->state == BUF_STATE_EMPTY && get_some_more) { 1093 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
1087 1094
1088 /* Figure out how much we want to get: 1095 /* Figure out how much we want to get:
@@ -1133,17 +1140,17 @@ static int do_write(struct fsg_dev *fsg)
1133 bh->outreq->short_not_ok = 1; 1140 bh->outreq->short_not_ok = 1;
1134 start_transfer(fsg, fsg->bulk_out, bh->outreq, 1141 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1135 &bh->outreq_busy, &bh->state); 1142 &bh->outreq_busy, &bh->state);
1136 fsg->next_buffhd_to_fill = bh->next; 1143 fsg->common->next_buffhd_to_fill = bh->next;
1137 continue; 1144 continue;
1138 } 1145 }
1139 1146
1140 /* Write the received data to the backing file */ 1147 /* Write the received data to the backing file */
1141 bh = fsg->next_buffhd_to_drain; 1148 bh = fsg->common->next_buffhd_to_drain;
1142 if (bh->state == BUF_STATE_EMPTY && !get_some_more) 1149 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
1143 break; // We stopped early 1150 break; // We stopped early
1144 if (bh->state == BUF_STATE_FULL) { 1151 if (bh->state == BUF_STATE_FULL) {
1145 smp_rmb(); 1152 smp_rmb();
1146 fsg->next_buffhd_to_drain = bh->next; 1153 fsg->common->next_buffhd_to_drain = bh->next;
1147 bh->state = BUF_STATE_EMPTY; 1154 bh->state = BUF_STATE_EMPTY;
1148 1155
1149 /* Did something go wrong with the transfer? */ 1156 /* Did something go wrong with the transfer? */
@@ -1218,7 +1225,7 @@ static int do_write(struct fsg_dev *fsg)
1218 1225
1219static int do_synchronize_cache(struct fsg_dev *fsg) 1226static int do_synchronize_cache(struct fsg_dev *fsg)
1220{ 1227{
1221 struct fsg_lun *curlun = fsg->curlun; 1228 struct fsg_lun *curlun = fsg->common->curlun;
1222 int rc; 1229 int rc;
1223 1230
1224 /* We ignore the requested LBA and write out all file's 1231 /* We ignore the requested LBA and write out all file's
@@ -1244,10 +1251,10 @@ static void invalidate_sub(struct fsg_lun *curlun)
1244 1251
1245static int do_verify(struct fsg_dev *fsg) 1252static int do_verify(struct fsg_dev *fsg)
1246{ 1253{
1247 struct fsg_lun *curlun = fsg->curlun; 1254 struct fsg_lun *curlun = fsg->common->curlun;
1248 u32 lba; 1255 u32 lba;
1249 u32 verification_length; 1256 u32 verification_length;
1250 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; 1257 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1251 loff_t file_offset, file_offset_tmp; 1258 loff_t file_offset, file_offset_tmp;
1252 u32 amount_left; 1259 u32 amount_left;
1253 unsigned int amount; 1260 unsigned int amount;
@@ -1255,7 +1262,7 @@ static int do_verify(struct fsg_dev *fsg)
1255 1262
1256 /* Get the starting Logical Block Address and check that it's 1263 /* Get the starting Logical Block Address and check that it's
1257 * not too big */ 1264 * not too big */
1258 lba = get_unaligned_be32(&fsg->cmnd[2]); 1265 lba = get_unaligned_be32(&fsg->common->cmnd[2]);
1259 if (lba >= curlun->num_sectors) { 1266 if (lba >= curlun->num_sectors) {
1260 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 1267 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
1261 return -EINVAL; 1268 return -EINVAL;
@@ -1263,12 +1270,12 @@ static int do_verify(struct fsg_dev *fsg)
1263 1270
1264 /* We allow DPO (Disable Page Out = don't save data in the 1271 /* We allow DPO (Disable Page Out = don't save data in the
1265 * cache) but we don't implement it. */ 1272 * cache) but we don't implement it. */
1266 if ((fsg->cmnd[1] & ~0x10) != 0) { 1273 if ((fsg->common->cmnd[1] & ~0x10) != 0) {
1267 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1274 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1268 return -EINVAL; 1275 return -EINVAL;
1269 } 1276 }
1270 1277
1271 verification_length = get_unaligned_be16(&fsg->cmnd[7]); 1278 verification_length = get_unaligned_be16(&fsg->common->cmnd[7]);
1272 if (unlikely(verification_length == 0)) 1279 if (unlikely(verification_length == 0))
1273 return -EIO; // No default reply 1280 return -EIO; // No default reply
1274 1281
@@ -1348,7 +1355,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1348 static char product_disk_id[] = "File-Stor Gadget"; 1355 static char product_disk_id[] = "File-Stor Gadget";
1349 static char product_cdrom_id[] = "File-CD Gadget "; 1356 static char product_cdrom_id[] = "File-CD Gadget ";
1350 1357
1351 if (!fsg->curlun) { // Unsupported LUNs are okay 1358 if (!fsg->common->curlun) { // Unsupported LUNs are okay
1352 fsg->bad_lun_okay = 1; 1359 fsg->bad_lun_okay = 1;
1353 memset(buf, 0, 36); 1360 memset(buf, 0, 36);
1354 buf[0] = 0x7f; // Unsupported, no device-type 1361 buf[0] = 0x7f; // Unsupported, no device-type
@@ -1374,7 +1381,7 @@ static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1374 1381
1375static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1382static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1376{ 1383{
1377 struct fsg_lun *curlun = fsg->curlun; 1384 struct fsg_lun *curlun = fsg->common->curlun;
1378 u8 *buf = (u8 *) bh->buf; 1385 u8 *buf = (u8 *) bh->buf;
1379 u32 sd, sdinfo; 1386 u32 sd, sdinfo;
1380 int valid; 1387 int valid;
@@ -1428,9 +1435,9 @@ static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1428 1435
1429static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1436static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1430{ 1437{
1431 struct fsg_lun *curlun = fsg->curlun; 1438 struct fsg_lun *curlun = fsg->common->curlun;
1432 u32 lba = get_unaligned_be32(&fsg->cmnd[2]); 1439 u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]);
1433 int pmi = fsg->cmnd[8]; 1440 int pmi = fsg->common->cmnd[8];
1434 u8 *buf = (u8 *) bh->buf; 1441 u8 *buf = (u8 *) bh->buf;
1435 1442
1436 /* Check the PMI and LBA fields */ 1443 /* Check the PMI and LBA fields */
@@ -1448,12 +1455,12 @@ static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1448 1455
1449static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1456static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1450{ 1457{
1451 struct fsg_lun *curlun = fsg->curlun; 1458 struct fsg_lun *curlun = fsg->common->curlun;
1452 int msf = fsg->cmnd[1] & 0x02; 1459 int msf = fsg->common->cmnd[1] & 0x02;
1453 u32 lba = get_unaligned_be32(&fsg->cmnd[2]); 1460 u32 lba = get_unaligned_be32(&fsg->common->cmnd[2]);
1454 u8 *buf = (u8 *) bh->buf; 1461 u8 *buf = (u8 *) bh->buf;
1455 1462
1456 if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */ 1463 if ((fsg->common->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
1457 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1464 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1458 return -EINVAL; 1465 return -EINVAL;
1459 } 1466 }
@@ -1471,12 +1478,12 @@ static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1471 1478
1472static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1479static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1473{ 1480{
1474 struct fsg_lun *curlun = fsg->curlun; 1481 struct fsg_lun *curlun = fsg->common->curlun;
1475 int msf = fsg->cmnd[1] & 0x02; 1482 int msf = fsg->common->cmnd[1] & 0x02;
1476 int start_track = fsg->cmnd[6]; 1483 int start_track = fsg->common->cmnd[6];
1477 u8 *buf = (u8 *) bh->buf; 1484 u8 *buf = (u8 *) bh->buf;
1478 1485
1479 if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ 1486 if ((fsg->common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
1480 start_track > 1) { 1487 start_track > 1) {
1481 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1488 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1482 return -EINVAL; 1489 return -EINVAL;
@@ -1499,8 +1506,8 @@ static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1499 1506
1500static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1507static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1501{ 1508{
1502 struct fsg_lun *curlun = fsg->curlun; 1509 struct fsg_lun *curlun = fsg->common->curlun;
1503 int mscmnd = fsg->cmnd[0]; 1510 int mscmnd = fsg->common->cmnd[0];
1504 u8 *buf = (u8 *) bh->buf; 1511 u8 *buf = (u8 *) bh->buf;
1505 u8 *buf0 = buf; 1512 u8 *buf0 = buf;
1506 int pc, page_code; 1513 int pc, page_code;
@@ -1508,12 +1515,12 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1508 int valid_page = 0; 1515 int valid_page = 0;
1509 int len, limit; 1516 int len, limit;
1510 1517
1511 if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD 1518 if ((fsg->common->cmnd[1] & ~0x08) != 0) { // Mask away DBD
1512 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1519 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1513 return -EINVAL; 1520 return -EINVAL;
1514 } 1521 }
1515 pc = fsg->cmnd[2] >> 6; 1522 pc = fsg->common->cmnd[2] >> 6;
1516 page_code = fsg->cmnd[2] & 0x3f; 1523 page_code = fsg->common->cmnd[2] & 0x3f;
1517 if (pc == 3) { 1524 if (pc == 3) {
1518 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED; 1525 curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
1519 return -EINVAL; 1526 return -EINVAL;
@@ -1581,7 +1588,7 @@ static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1581static int do_start_stop(struct fsg_dev *fsg) 1588static int do_start_stop(struct fsg_dev *fsg)
1582{ 1589{
1583 if (!mod_data.removable) { 1590 if (!mod_data.removable) {
1584 fsg->curlun->sense_data = SS_INVALID_COMMAND; 1591 fsg->common->curlun->sense_data = SS_INVALID_COMMAND;
1585 return -EINVAL; 1592 return -EINVAL;
1586 } 1593 }
1587 return 0; 1594 return 0;
@@ -1590,7 +1597,7 @@ static int do_start_stop(struct fsg_dev *fsg)
1590 1597
1591static int do_prevent_allow(struct fsg_dev *fsg) 1598static int do_prevent_allow(struct fsg_dev *fsg)
1592{ 1599{
1593 struct fsg_lun *curlun = fsg->curlun; 1600 struct fsg_lun *curlun = fsg->common->curlun;
1594 int prevent; 1601 int prevent;
1595 1602
1596 if (!mod_data.removable) { 1603 if (!mod_data.removable) {
@@ -1598,8 +1605,8 @@ static int do_prevent_allow(struct fsg_dev *fsg)
1598 return -EINVAL; 1605 return -EINVAL;
1599 } 1606 }
1600 1607
1601 prevent = fsg->cmnd[4] & 0x01; 1608 prevent = fsg->common->cmnd[4] & 0x01;
1602 if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent 1609 if ((fsg->common->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
1603 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 1610 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1604 return -EINVAL; 1611 return -EINVAL;
1605 } 1612 }
@@ -1614,7 +1621,7 @@ static int do_prevent_allow(struct fsg_dev *fsg)
1614static int do_read_format_capacities(struct fsg_dev *fsg, 1621static int do_read_format_capacities(struct fsg_dev *fsg,
1615 struct fsg_buffhd *bh) 1622 struct fsg_buffhd *bh)
1616{ 1623{
1617 struct fsg_lun *curlun = fsg->curlun; 1624 struct fsg_lun *curlun = fsg->common->curlun;
1618 u8 *buf = (u8 *) bh->buf; 1625 u8 *buf = (u8 *) bh->buf;
1619 1626
1620 buf[0] = buf[1] = buf[2] = 0; 1627 buf[0] = buf[1] = buf[2] = 0;
@@ -1631,7 +1638,7 @@ static int do_read_format_capacities(struct fsg_dev *fsg,
1631 1638
1632static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh) 1639static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
1633{ 1640{
1634 struct fsg_lun *curlun = fsg->curlun; 1641 struct fsg_lun *curlun = fsg->common->curlun;
1635 1642
1636 /* We don't support MODE SELECT */ 1643 /* We don't support MODE SELECT */
1637 curlun->sense_data = SS_INVALID_COMMAND; 1644 curlun->sense_data = SS_INVALID_COMMAND;
@@ -1688,7 +1695,7 @@ static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
1688 1695
1689static int pad_with_zeros(struct fsg_dev *fsg) 1696static int pad_with_zeros(struct fsg_dev *fsg)
1690{ 1697{
1691 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; 1698 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1692 u32 nkeep = bh->inreq->length; 1699 u32 nkeep = bh->inreq->length;
1693 u32 nsend; 1700 u32 nsend;
1694 int rc; 1701 int rc;
@@ -1710,7 +1717,7 @@ static int pad_with_zeros(struct fsg_dev *fsg)
1710 bh->inreq->zero = 0; 1717 bh->inreq->zero = 0;
1711 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1718 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1712 &bh->inreq_busy, &bh->state); 1719 &bh->inreq_busy, &bh->state);
1713 bh = fsg->next_buffhd_to_fill = bh->next; 1720 bh = fsg->common->next_buffhd_to_fill = bh->next;
1714 fsg->usb_amount_left -= nsend; 1721 fsg->usb_amount_left -= nsend;
1715 nkeep = 0; 1722 nkeep = 0;
1716 } 1723 }
@@ -1723,14 +1730,15 @@ static int throw_away_data(struct fsg_dev *fsg)
1723 u32 amount; 1730 u32 amount;
1724 int rc; 1731 int rc;
1725 1732
1726 while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY || 1733 for (bh = fsg->common->next_buffhd_to_drain;
1727 fsg->usb_amount_left > 0) { 1734 bh->state != BUF_STATE_EMPTY || fsg->usb_amount_left > 0;
1735 bh = fsg->common->next_buffhd_to_drain) {
1728 1736
1729 /* Throw away the data in a filled buffer */ 1737 /* Throw away the data in a filled buffer */
1730 if (bh->state == BUF_STATE_FULL) { 1738 if (bh->state == BUF_STATE_FULL) {
1731 smp_rmb(); 1739 smp_rmb();
1732 bh->state = BUF_STATE_EMPTY; 1740 bh->state = BUF_STATE_EMPTY;
1733 fsg->next_buffhd_to_drain = bh->next; 1741 fsg->common->next_buffhd_to_drain = bh->next;
1734 1742
1735 /* A short packet or an error ends everything */ 1743 /* A short packet or an error ends everything */
1736 if (bh->outreq->actual != bh->outreq->length || 1744 if (bh->outreq->actual != bh->outreq->length ||
@@ -1742,7 +1750,7 @@ static int throw_away_data(struct fsg_dev *fsg)
1742 } 1750 }
1743 1751
1744 /* Try to submit another request if we need one */ 1752 /* Try to submit another request if we need one */
1745 bh = fsg->next_buffhd_to_fill; 1753 bh = fsg->common->next_buffhd_to_fill;
1746 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) { 1754 if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
1747 amount = min(fsg->usb_amount_left, FSG_BUFLEN); 1755 amount = min(fsg->usb_amount_left, FSG_BUFLEN);
1748 1756
@@ -1753,7 +1761,7 @@ static int throw_away_data(struct fsg_dev *fsg)
1753 bh->outreq->short_not_ok = 1; 1761 bh->outreq->short_not_ok = 1;
1754 start_transfer(fsg, fsg->bulk_out, bh->outreq, 1762 start_transfer(fsg, fsg->bulk_out, bh->outreq,
1755 &bh->outreq_busy, &bh->state); 1763 &bh->outreq_busy, &bh->state);
1756 fsg->next_buffhd_to_fill = bh->next; 1764 fsg->common->next_buffhd_to_fill = bh->next;
1757 fsg->usb_amount_left -= amount; 1765 fsg->usb_amount_left -= amount;
1758 continue; 1766 continue;
1759 } 1767 }
@@ -1769,7 +1777,7 @@ static int throw_away_data(struct fsg_dev *fsg)
1769 1777
1770static int finish_reply(struct fsg_dev *fsg) 1778static int finish_reply(struct fsg_dev *fsg)
1771{ 1779{
1772 struct fsg_buffhd *bh = fsg->next_buffhd_to_fill; 1780 struct fsg_buffhd *bh = fsg->common->next_buffhd_to_fill;
1773 int rc = 0; 1781 int rc = 0;
1774 1782
1775 switch (fsg->data_dir) { 1783 switch (fsg->data_dir) {
@@ -1797,7 +1805,7 @@ static int finish_reply(struct fsg_dev *fsg)
1797 bh->inreq->zero = 0; 1805 bh->inreq->zero = 0;
1798 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1806 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1799 &bh->inreq_busy, &bh->state); 1807 &bh->inreq_busy, &bh->state);
1800 fsg->next_buffhd_to_fill = bh->next; 1808 fsg->common->next_buffhd_to_fill = bh->next;
1801 1809
1802 /* For Bulk-only, if we're allowed to stall then send the 1810 /* For Bulk-only, if we're allowed to stall then send the
1803 * short packet and halt the bulk-in endpoint. If we can't 1811 * short packet and halt the bulk-in endpoint. If we can't
@@ -1806,7 +1814,7 @@ static int finish_reply(struct fsg_dev *fsg)
1806 bh->inreq->zero = 1; 1814 bh->inreq->zero = 1;
1807 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1815 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1808 &bh->inreq_busy, &bh->state); 1816 &bh->inreq_busy, &bh->state);
1809 fsg->next_buffhd_to_fill = bh->next; 1817 fsg->common->next_buffhd_to_fill = bh->next;
1810 rc = halt_bulk_in_endpoint(fsg); 1818 rc = halt_bulk_in_endpoint(fsg);
1811 } else { 1819 } else {
1812 rc = pad_with_zeros(fsg); 1820 rc = pad_with_zeros(fsg);
@@ -1851,7 +1859,7 @@ static int finish_reply(struct fsg_dev *fsg)
1851 1859
1852static int send_status(struct fsg_dev *fsg) 1860static int send_status(struct fsg_dev *fsg)
1853{ 1861{
1854 struct fsg_lun *curlun = fsg->curlun; 1862 struct fsg_lun *curlun = fsg->common->curlun;
1855 struct fsg_buffhd *bh; 1863 struct fsg_buffhd *bh;
1856 struct bulk_cs_wrap *csw; 1864 struct bulk_cs_wrap *csw;
1857 int rc; 1865 int rc;
@@ -1859,7 +1867,7 @@ static int send_status(struct fsg_dev *fsg)
1859 u32 sd, sdinfo = 0; 1867 u32 sd, sdinfo = 0;
1860 1868
1861 /* Wait for the next buffer to become available */ 1869 /* Wait for the next buffer to become available */
1862 bh = fsg->next_buffhd_to_fill; 1870 bh = fsg->common->next_buffhd_to_fill;
1863 while (bh->state != BUF_STATE_EMPTY) { 1871 while (bh->state != BUF_STATE_EMPTY) {
1864 rc = sleep_thread(fsg); 1872 rc = sleep_thread(fsg);
1865 if (rc) 1873 if (rc)
@@ -1900,7 +1908,7 @@ static int send_status(struct fsg_dev *fsg)
1900 start_transfer(fsg, fsg->bulk_in, bh->inreq, 1908 start_transfer(fsg, fsg->bulk_in, bh->inreq,
1901 &bh->inreq_busy, &bh->state); 1909 &bh->inreq_busy, &bh->state);
1902 1910
1903 fsg->next_buffhd_to_fill = bh->next; 1911 fsg->common->next_buffhd_to_fill = bh->next;
1904 return 0; 1912 return 0;
1905} 1913}
1906 1914
@@ -1914,7 +1922,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1914 int needs_medium, const char *name) 1922 int needs_medium, const char *name)
1915{ 1923{
1916 int i; 1924 int i;
1917 int lun = fsg->cmnd[1] >> 5; 1925 int lun = fsg->common->cmnd[1] >> 5;
1918 static const char dirletter[4] = {'u', 'o', 'i', 'n'}; 1926 static const char dirletter[4] = {'u', 'o', 'i', 'n'};
1919 char hdlen[20]; 1927 char hdlen[20];
1920 struct fsg_lun *curlun; 1928 struct fsg_lun *curlun;
@@ -1925,7 +1933,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1925 fsg->data_size); 1933 fsg->data_size);
1926 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n", 1934 VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
1927 name, cmnd_size, dirletter[(int) data_dir], 1935 name, cmnd_size, dirletter[(int) data_dir],
1928 fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen); 1936 fsg->data_size_from_cmnd, fsg->common->cmnd_size, hdlen);
1929 1937
1930 /* We can't reply at all until we know the correct data direction 1938 /* We can't reply at all until we know the correct data direction
1931 * and size. */ 1939 * and size. */
@@ -1954,7 +1962,7 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1954 } 1962 }
1955 1963
1956 /* Verify the length of the command itself */ 1964 /* Verify the length of the command itself */
1957 if (cmnd_size != fsg->cmnd_size) { 1965 if (cmnd_size != fsg->common->cmnd_size) {
1958 1966
1959 /* Special case workaround: There are plenty of buggy SCSI 1967 /* Special case workaround: There are plenty of buggy SCSI
1960 * implementations. Many have issues with cbw->Length 1968 * implementations. Many have issues with cbw->Length
@@ -1968,11 +1976,11 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1968 * REQUEST SENSE with cbw->Length == 10 where it should 1976 * REQUEST SENSE with cbw->Length == 10 where it should
1969 * be 6 as well. 1977 * be 6 as well.
1970 */ 1978 */
1971 if (cmnd_size <= fsg->cmnd_size) { 1979 if (cmnd_size <= fsg->common->cmnd_size) {
1972 DBG(fsg, "%s is buggy! Expected length %d " 1980 DBG(fsg, "%s is buggy! Expected length %d "
1973 "but we got %d\n", name, 1981 "but we got %d\n", name,
1974 cmnd_size, fsg->cmnd_size); 1982 cmnd_size, fsg->common->cmnd_size);
1975 cmnd_size = fsg->cmnd_size; 1983 cmnd_size = fsg->common->cmnd_size;
1976 } else { 1984 } else {
1977 fsg->phase_error = 1; 1985 fsg->phase_error = 1;
1978 return -EINVAL; 1986 return -EINVAL;
@@ -1980,27 +1988,27 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
1980 } 1988 }
1981 1989
1982 /* Check that the LUN values are consistent */ 1990 /* Check that the LUN values are consistent */
1983 if (fsg->lun != lun) 1991 if (fsg->common->lun != lun)
1984 DBG(fsg, "using LUN %d from CBW, not LUN %d from CDB\n", 1992 DBG(fsg, "using LUN %d from CBW, not LUN %d from CDB\n",
1985 fsg->lun, lun); 1993 fsg->common->lun, lun);
1986 1994
1987 /* Check the LUN */ 1995 /* Check the LUN */
1988 if (fsg->lun >= 0 && fsg->lun < fsg->nluns) { 1996 if (fsg->common->lun >= 0 && fsg->common->lun < fsg->common->nluns) {
1989 fsg->curlun = curlun = &fsg->luns[fsg->lun]; 1997 fsg->common->curlun = curlun = &fsg->common->luns[fsg->common->lun];
1990 if (fsg->cmnd[0] != SC_REQUEST_SENSE) { 1998 if (fsg->common->cmnd[0] != SC_REQUEST_SENSE) {
1991 curlun->sense_data = SS_NO_SENSE; 1999 curlun->sense_data = SS_NO_SENSE;
1992 curlun->sense_data_info = 0; 2000 curlun->sense_data_info = 0;
1993 curlun->info_valid = 0; 2001 curlun->info_valid = 0;
1994 } 2002 }
1995 } else { 2003 } else {
1996 fsg->curlun = curlun = NULL; 2004 fsg->common->curlun = curlun = NULL;
1997 fsg->bad_lun_okay = 0; 2005 fsg->bad_lun_okay = 0;
1998 2006
1999 /* INQUIRY and REQUEST SENSE commands are explicitly allowed 2007 /* INQUIRY and REQUEST SENSE commands are explicitly allowed
2000 * to use unsupported LUNs; all others may not. */ 2008 * to use unsupported LUNs; all others may not. */
2001 if (fsg->cmnd[0] != SC_INQUIRY && 2009 if (fsg->common->cmnd[0] != SC_INQUIRY &&
2002 fsg->cmnd[0] != SC_REQUEST_SENSE) { 2010 fsg->common->cmnd[0] != SC_REQUEST_SENSE) {
2003 DBG(fsg, "unsupported LUN %d\n", fsg->lun); 2011 DBG(fsg, "unsupported LUN %d\n", fsg->common->lun);
2004 return -EINVAL; 2012 return -EINVAL;
2005 } 2013 }
2006 } 2014 }
@@ -2008,17 +2016,17 @@ static int check_command(struct fsg_dev *fsg, int cmnd_size,
2008 /* If a unit attention condition exists, only INQUIRY and 2016 /* If a unit attention condition exists, only INQUIRY and
2009 * REQUEST SENSE commands are allowed; anything else must fail. */ 2017 * REQUEST SENSE commands are allowed; anything else must fail. */
2010 if (curlun && curlun->unit_attention_data != SS_NO_SENSE && 2018 if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
2011 fsg->cmnd[0] != SC_INQUIRY && 2019 fsg->common->cmnd[0] != SC_INQUIRY &&
2012 fsg->cmnd[0] != SC_REQUEST_SENSE) { 2020 fsg->common->cmnd[0] != SC_REQUEST_SENSE) {
2013 curlun->sense_data = curlun->unit_attention_data; 2021 curlun->sense_data = curlun->unit_attention_data;
2014 curlun->unit_attention_data = SS_NO_SENSE; 2022 curlun->unit_attention_data = SS_NO_SENSE;
2015 return -EINVAL; 2023 return -EINVAL;
2016 } 2024 }
2017 2025
2018 /* Check that only command bytes listed in the mask are non-zero */ 2026 /* Check that only command bytes listed in the mask are non-zero */
2019 fsg->cmnd[1] &= 0x1f; // Mask away the LUN 2027 fsg->common->cmnd[1] &= 0x1f; // Mask away the LUN
2020 for (i = 1; i < cmnd_size; ++i) { 2028 for (i = 1; i < cmnd_size; ++i) {
2021 if (fsg->cmnd[i] && !(mask & (1 << i))) { 2029 if (fsg->common->cmnd[i] && !(mask & (1 << i))) {
2022 if (curlun) 2030 if (curlun)
2023 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 2031 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
2024 return -EINVAL; 2032 return -EINVAL;
@@ -2044,10 +2052,10 @@ static int do_scsi_command(struct fsg_dev *fsg)
2044 int i; 2052 int i;
2045 static char unknown[16]; 2053 static char unknown[16];
2046 2054
2047 dump_cdb(fsg); 2055 dump_cdb(fsg->common);
2048 2056
2049 /* Wait for the next buffer to become available for data or status */ 2057 /* Wait for the next buffer to become available for data or status */
2050 bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill; 2058 bh = fsg->common->next_buffhd_to_drain = fsg->common->next_buffhd_to_fill;
2051 while (bh->state != BUF_STATE_EMPTY) { 2059 while (bh->state != BUF_STATE_EMPTY) {
2052 rc = sleep_thread(fsg); 2060 rc = sleep_thread(fsg);
2053 if (rc) 2061 if (rc)
@@ -2056,11 +2064,11 @@ static int do_scsi_command(struct fsg_dev *fsg)
2056 fsg->phase_error = 0; 2064 fsg->phase_error = 0;
2057 fsg->short_packet_received = 0; 2065 fsg->short_packet_received = 0;
2058 2066
2059 down_read(&fsg->filesem); // We're using the backing file 2067 down_read(&fsg->common->filesem); // We're using the backing file
2060 switch (fsg->cmnd[0]) { 2068 switch (fsg->common->cmnd[0]) {
2061 2069
2062 case SC_INQUIRY: 2070 case SC_INQUIRY:
2063 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2071 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
2064 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2072 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2065 (1<<4), 0, 2073 (1<<4), 0,
2066 "INQUIRY")) == 0) 2074 "INQUIRY")) == 0)
@@ -2068,7 +2076,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2068 break; 2076 break;
2069 2077
2070 case SC_MODE_SELECT_6: 2078 case SC_MODE_SELECT_6:
2071 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2079 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
2072 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2080 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2073 (1<<1) | (1<<4), 0, 2081 (1<<1) | (1<<4), 0,
2074 "MODE SELECT(6)")) == 0) 2082 "MODE SELECT(6)")) == 0)
@@ -2076,7 +2084,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2076 break; 2084 break;
2077 2085
2078 case SC_MODE_SELECT_10: 2086 case SC_MODE_SELECT_10:
2079 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2087 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]);
2080 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2088 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2081 (1<<1) | (3<<7), 0, 2089 (1<<1) | (3<<7), 0,
2082 "MODE SELECT(10)")) == 0) 2090 "MODE SELECT(10)")) == 0)
@@ -2084,7 +2092,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2084 break; 2092 break;
2085 2093
2086 case SC_MODE_SENSE_6: 2094 case SC_MODE_SENSE_6:
2087 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2095 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
2088 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2096 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2089 (1<<1) | (1<<2) | (1<<4), 0, 2097 (1<<1) | (1<<2) | (1<<4), 0,
2090 "MODE SENSE(6)")) == 0) 2098 "MODE SENSE(6)")) == 0)
@@ -2092,7 +2100,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2092 break; 2100 break;
2093 2101
2094 case SC_MODE_SENSE_10: 2102 case SC_MODE_SENSE_10:
2095 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2103 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]);
2096 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2104 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2097 (1<<1) | (1<<2) | (3<<7), 0, 2105 (1<<1) | (1<<2) | (3<<7), 0,
2098 "MODE SENSE(10)")) == 0) 2106 "MODE SENSE(10)")) == 0)
@@ -2108,7 +2116,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2108 break; 2116 break;
2109 2117
2110 case SC_READ_6: 2118 case SC_READ_6:
2111 i = fsg->cmnd[4]; 2119 i = fsg->common->cmnd[4];
2112 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2120 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2113 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2121 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2114 (7<<1) | (1<<4), 1, 2122 (7<<1) | (1<<4), 1,
@@ -2118,7 +2126,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2118 2126
2119 case SC_READ_10: 2127 case SC_READ_10:
2120 fsg->data_size_from_cmnd = 2128 fsg->data_size_from_cmnd =
2121 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2129 get_unaligned_be16(&fsg->common->cmnd[7]) << 9;
2122 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2130 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2123 (1<<1) | (0xf<<2) | (3<<7), 1, 2131 (1<<1) | (0xf<<2) | (3<<7), 1,
2124 "READ(10)")) == 0) 2132 "READ(10)")) == 0)
@@ -2127,7 +2135,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2127 2135
2128 case SC_READ_12: 2136 case SC_READ_12:
2129 fsg->data_size_from_cmnd = 2137 fsg->data_size_from_cmnd =
2130 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2138 get_unaligned_be32(&fsg->common->cmnd[6]) << 9;
2131 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST, 2139 if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
2132 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2140 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2133 "READ(12)")) == 0) 2141 "READ(12)")) == 0)
@@ -2145,7 +2153,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2145 case SC_READ_HEADER: 2153 case SC_READ_HEADER:
2146 if (!mod_data.cdrom) 2154 if (!mod_data.cdrom)
2147 goto unknown_cmnd; 2155 goto unknown_cmnd;
2148 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2156 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]);
2149 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2157 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2150 (3<<7) | (0x1f<<1), 1, 2158 (3<<7) | (0x1f<<1), 1,
2151 "READ HEADER")) == 0) 2159 "READ HEADER")) == 0)
@@ -2155,7 +2163,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2155 case SC_READ_TOC: 2163 case SC_READ_TOC:
2156 if (!mod_data.cdrom) 2164 if (!mod_data.cdrom)
2157 goto unknown_cmnd; 2165 goto unknown_cmnd;
2158 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2166 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]);
2159 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2167 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2160 (7<<6) | (1<<1), 1, 2168 (7<<6) | (1<<1), 1,
2161 "READ TOC")) == 0) 2169 "READ TOC")) == 0)
@@ -2163,7 +2171,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2163 break; 2171 break;
2164 2172
2165 case SC_READ_FORMAT_CAPACITIES: 2173 case SC_READ_FORMAT_CAPACITIES:
2166 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]); 2174 fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->common->cmnd[7]);
2167 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST, 2175 if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
2168 (3<<7), 1, 2176 (3<<7), 1,
2169 "READ FORMAT CAPACITIES")) == 0) 2177 "READ FORMAT CAPACITIES")) == 0)
@@ -2171,7 +2179,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2171 break; 2179 break;
2172 2180
2173 case SC_REQUEST_SENSE: 2181 case SC_REQUEST_SENSE:
2174 fsg->data_size_from_cmnd = fsg->cmnd[4]; 2182 fsg->data_size_from_cmnd = fsg->common->cmnd[4];
2175 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST, 2183 if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
2176 (1<<4), 0, 2184 (1<<4), 0,
2177 "REQUEST SENSE")) == 0) 2185 "REQUEST SENSE")) == 0)
@@ -2212,7 +2220,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2212 break; 2220 break;
2213 2221
2214 case SC_WRITE_6: 2222 case SC_WRITE_6:
2215 i = fsg->cmnd[4]; 2223 i = fsg->common->cmnd[4];
2216 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9; 2224 fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
2217 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST, 2225 if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
2218 (7<<1) | (1<<4), 1, 2226 (7<<1) | (1<<4), 1,
@@ -2222,7 +2230,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2222 2230
2223 case SC_WRITE_10: 2231 case SC_WRITE_10:
2224 fsg->data_size_from_cmnd = 2232 fsg->data_size_from_cmnd =
2225 get_unaligned_be16(&fsg->cmnd[7]) << 9; 2233 get_unaligned_be16(&fsg->common->cmnd[7]) << 9;
2226 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST, 2234 if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
2227 (1<<1) | (0xf<<2) | (3<<7), 1, 2235 (1<<1) | (0xf<<2) | (3<<7), 1,
2228 "WRITE(10)")) == 0) 2236 "WRITE(10)")) == 0)
@@ -2231,7 +2239,7 @@ static int do_scsi_command(struct fsg_dev *fsg)
2231 2239
2232 case SC_WRITE_12: 2240 case SC_WRITE_12:
2233 fsg->data_size_from_cmnd = 2241 fsg->data_size_from_cmnd =
2234 get_unaligned_be32(&fsg->cmnd[6]) << 9; 2242 get_unaligned_be32(&fsg->common->cmnd[6]) << 9;
2235 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST, 2243 if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
2236 (1<<1) | (0xf<<2) | (0xf<<6), 1, 2244 (1<<1) | (0xf<<2) | (0xf<<6), 1,
2237 "WRITE(12)")) == 0) 2245 "WRITE(12)")) == 0)
@@ -2251,15 +2259,15 @@ static int do_scsi_command(struct fsg_dev *fsg)
2251 default: 2259 default:
2252 unknown_cmnd: 2260 unknown_cmnd:
2253 fsg->data_size_from_cmnd = 0; 2261 fsg->data_size_from_cmnd = 0;
2254 sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]); 2262 sprintf(unknown, "Unknown x%02x", fsg->common->cmnd[0]);
2255 if ((reply = check_command(fsg, fsg->cmnd_size, 2263 if ((reply = check_command(fsg, fsg->common->cmnd_size,
2256 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) { 2264 DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
2257 fsg->curlun->sense_data = SS_INVALID_COMMAND; 2265 fsg->common->curlun->sense_data = SS_INVALID_COMMAND;
2258 reply = -EINVAL; 2266 reply = -EINVAL;
2259 } 2267 }
2260 break; 2268 break;
2261 } 2269 }
2262 up_read(&fsg->filesem); 2270 up_read(&fsg->common->filesem);
2263 2271
2264 if (reply == -EINTR || signal_pending(current)) 2272 if (reply == -EINTR || signal_pending(current))
2265 return -EINTR; 2273 return -EINTR;
@@ -2328,8 +2336,8 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2328 } 2336 }
2329 2337
2330 /* Save the command for later */ 2338 /* Save the command for later */
2331 fsg->cmnd_size = cbw->Length; 2339 fsg->common->cmnd_size = cbw->Length;
2332 memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size); 2340 memcpy(fsg->common->cmnd, cbw->CDB, fsg->common->cmnd_size);
2333 if (cbw->Flags & USB_BULK_IN_FLAG) 2341 if (cbw->Flags & USB_BULK_IN_FLAG)
2334 fsg->data_dir = DATA_DIR_TO_HOST; 2342 fsg->data_dir = DATA_DIR_TO_HOST;
2335 else 2343 else
@@ -2337,7 +2345,7 @@ static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
2337 fsg->data_size = le32_to_cpu(cbw->DataTransferLength); 2345 fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
2338 if (fsg->data_size == 0) 2346 if (fsg->data_size == 0)
2339 fsg->data_dir = DATA_DIR_NONE; 2347 fsg->data_dir = DATA_DIR_NONE;
2340 fsg->lun = cbw->Lun; 2348 fsg->common->lun = cbw->Lun;
2341 fsg->tag = cbw->Tag; 2349 fsg->tag = cbw->Tag;
2342 return 0; 2350 return 0;
2343} 2351}
@@ -2349,7 +2357,7 @@ static int get_next_command(struct fsg_dev *fsg)
2349 int rc = 0; 2357 int rc = 0;
2350 2358
2351 /* Wait for the next buffer to become available */ 2359 /* Wait for the next buffer to become available */
2352 bh = fsg->next_buffhd_to_fill; 2360 bh = fsg->common->next_buffhd_to_fill;
2353 while (bh->state != BUF_STATE_EMPTY) { 2361 while (bh->state != BUF_STATE_EMPTY) {
2354 rc = sleep_thread(fsg); 2362 rc = sleep_thread(fsg);
2355 if (rc) 2363 if (rc)
@@ -2421,7 +2429,7 @@ static int do_set_interface(struct fsg_dev *fsg, int altsetting)
2421reset: 2429reset:
2422 /* Deallocate the requests */ 2430 /* Deallocate the requests */
2423 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2431 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2424 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2432 struct fsg_buffhd *bh = &fsg->common->buffhds[i];
2425 2433
2426 if (bh->inreq) { 2434 if (bh->inreq) {
2427 usb_ep_free_request(fsg->bulk_in, bh->inreq); 2435 usb_ep_free_request(fsg->bulk_in, bh->inreq);
@@ -2466,7 +2474,7 @@ reset:
2466 2474
2467 /* Allocate the requests */ 2475 /* Allocate the requests */
2468 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2476 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2469 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2477 struct fsg_buffhd *bh = &fsg->common->buffhds[i];
2470 2478
2471 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0) 2479 if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
2472 goto reset; 2480 goto reset;
@@ -2479,8 +2487,8 @@ reset:
2479 } 2487 }
2480 2488
2481 fsg->running = 1; 2489 fsg->running = 1;
2482 for (i = 0; i < fsg->nluns; ++i) 2490 for (i = 0; i < fsg->common->nluns; ++i)
2483 fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; 2491 fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2484 return rc; 2492 return rc;
2485} 2493}
2486 2494
@@ -2532,7 +2540,6 @@ static void handle_exception(struct fsg_dev *fsg)
2532 siginfo_t info; 2540 siginfo_t info;
2533 int sig; 2541 int sig;
2534 int i; 2542 int i;
2535 int num_active;
2536 struct fsg_buffhd *bh; 2543 struct fsg_buffhd *bh;
2537 enum fsg_state old_state; 2544 enum fsg_state old_state;
2538 u8 new_config; 2545 u8 new_config;
@@ -2555,7 +2562,7 @@ static void handle_exception(struct fsg_dev *fsg)
2555 2562
2556 /* Cancel all the pending transfers */ 2563 /* Cancel all the pending transfers */
2557 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2564 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2558 bh = &fsg->buffhds[i]; 2565 bh = &fsg->common->buffhds[i];
2559 if (bh->inreq_busy) 2566 if (bh->inreq_busy)
2560 usb_ep_dequeue(fsg->bulk_in, bh->inreq); 2567 usb_ep_dequeue(fsg->bulk_in, bh->inreq);
2561 if (bh->outreq_busy) 2568 if (bh->outreq_busy)
@@ -2564,9 +2571,9 @@ static void handle_exception(struct fsg_dev *fsg)
2564 2571
2565 /* Wait until everything is idle */ 2572 /* Wait until everything is idle */
2566 for (;;) { 2573 for (;;) {
2567 num_active = 0; 2574 int num_active = 0;
2568 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2575 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2569 bh = &fsg->buffhds[i]; 2576 bh = &fsg->common->buffhds[i];
2570 num_active += bh->inreq_busy + bh->outreq_busy; 2577 num_active += bh->inreq_busy + bh->outreq_busy;
2571 } 2578 }
2572 if (num_active == 0) 2579 if (num_active == 0)
@@ -2586,11 +2593,11 @@ static void handle_exception(struct fsg_dev *fsg)
2586 spin_lock_irq(&fsg->lock); 2593 spin_lock_irq(&fsg->lock);
2587 2594
2588 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2595 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2589 bh = &fsg->buffhds[i]; 2596 bh = &fsg->common->buffhds[i];
2590 bh->state = BUF_STATE_EMPTY; 2597 bh->state = BUF_STATE_EMPTY;
2591 } 2598 }
2592 fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain = 2599 fsg->common->next_buffhd_to_fill = fsg->common->next_buffhd_to_drain =
2593 &fsg->buffhds[0]; 2600 &fsg->common->buffhds[0];
2594 2601
2595 exception_req_tag = fsg->exception_req_tag; 2602 exception_req_tag = fsg->exception_req_tag;
2596 new_config = fsg->new_config; 2603 new_config = fsg->new_config;
@@ -2599,8 +2606,8 @@ static void handle_exception(struct fsg_dev *fsg)
2599 if (old_state == FSG_STATE_ABORT_BULK_OUT) 2606 if (old_state == FSG_STATE_ABORT_BULK_OUT)
2600 fsg->state = FSG_STATE_STATUS_PHASE; 2607 fsg->state = FSG_STATE_STATUS_PHASE;
2601 else { 2608 else {
2602 for (i = 0; i < fsg->nluns; ++i) { 2609 for (i = 0; i < fsg->common->nluns; ++i) {
2603 curlun = &fsg->luns[i]; 2610 curlun = &fsg->common->luns[i];
2604 curlun->prevent_medium_removal = 0; 2611 curlun->prevent_medium_removal = 0;
2605 curlun->sense_data = curlun->unit_attention_data = 2612 curlun->sense_data = curlun->unit_attention_data =
2606 SS_NO_SENSE; 2613 SS_NO_SENSE;
@@ -2637,8 +2644,8 @@ static void handle_exception(struct fsg_dev *fsg)
2637 /* Technically this should go here, but it would only be 2644 /* Technically this should go here, but it would only be
2638 * a waste of time. Ditto for the INTERFACE_CHANGE and 2645 * a waste of time. Ditto for the INTERFACE_CHANGE and
2639 * CONFIG_CHANGE cases. */ 2646 * CONFIG_CHANGE cases. */
2640 // for (i = 0; i < fsg->nluns; ++i) 2647 // for (i = 0; i < fsg->common->nluns; ++i)
2641 // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED; 2648 // fsg->common->luns[i].unit_attention_data = SS_RESET_OCCURRED;
2642 break; 2649 break;
2643 2650
2644 case FSG_STATE_INTERFACE_CHANGE: 2651 case FSG_STATE_INTERFACE_CHANGE:
@@ -2662,8 +2669,8 @@ static void handle_exception(struct fsg_dev *fsg)
2662 break; 2669 break;
2663 2670
2664 case FSG_STATE_DISCONNECT: 2671 case FSG_STATE_DISCONNECT:
2665 for (i = 0; i < fsg->nluns; ++i) 2672 for (i = 0; i < fsg->common->nluns; ++i)
2666 fsg_lun_fsync_sub(fsg->luns + i); 2673 fsg_lun_fsync_sub(&fsg->common->luns[i]);
2667 do_set_config(fsg, 0); // Unconfigured state 2674 do_set_config(fsg, 0); // Unconfigured state
2668 break; 2675 break;
2669 2676
@@ -2760,21 +2767,14 @@ static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
2760 2767
2761/*-------------------------------------------------------------------------*/ 2768/*-------------------------------------------------------------------------*/
2762 2769
2763static void fsg_release(struct kref *ref) 2770static void fsg_release(struct fsg_dev *fsg)
2764{ 2771{
2765 struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref); 2772 kfree(fsg->common->luns);
2766
2767 kfree(fsg->luns);
2768 kfree(fsg); 2773 kfree(fsg);
2769} 2774}
2770 2775
2771static void lun_release(struct device *dev) 2776static void lun_release(struct device *dev)
2772{ 2777{
2773 struct rw_semaphore *filesem = dev_get_drvdata(dev);
2774 struct fsg_dev *fsg =
2775 container_of(filesem, struct fsg_dev, filesem);
2776
2777 kref_put(&fsg->ref, fsg_release);
2778} 2778}
2779 2779
2780static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget) 2780static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
@@ -2788,8 +2788,8 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
2788 clear_bit(REGISTERED, &fsg->atomic_bitflags); 2788 clear_bit(REGISTERED, &fsg->atomic_bitflags);
2789 2789
2790 /* Unregister the sysfs attribute files and the LUNs */ 2790 /* Unregister the sysfs attribute files and the LUNs */
2791 for (i = 0; i < fsg->nluns; ++i) { 2791 for (i = 0; i < fsg->common->nluns; ++i) {
2792 curlun = &fsg->luns[i]; 2792 curlun = &fsg->common->luns[i];
2793 if (curlun->registered) { 2793 if (curlun->registered) {
2794 device_remove_file(&curlun->dev, &dev_attr_ro); 2794 device_remove_file(&curlun->dev, &dev_attr_ro);
2795 device_remove_file(&curlun->dev, &dev_attr_file); 2795 device_remove_file(&curlun->dev, &dev_attr_file);
@@ -2810,7 +2810,7 @@ static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
2810 2810
2811 /* Free the data buffers */ 2811 /* Free the data buffers */
2812 for (i = 0; i < FSG_NUM_BUFFERS; ++i) 2812 for (i = 0; i < FSG_NUM_BUFFERS; ++i)
2813 kfree(fsg->buffhds[i].buf); 2813 kfree(fsg->common->buffhds[i].buf);
2814 2814
2815 /* Free the request and buffer for endpoint 0 */ 2815 /* Free the request and buffer for endpoint 0 */
2816 if (req) { 2816 if (req) {
@@ -2891,15 +2891,15 @@ static int __init fsg_bind(struct usb_gadget *gadget)
2891 2891
2892 /* Create the LUNs, open their backing files, and register the 2892 /* Create the LUNs, open their backing files, and register the
2893 * LUN devices in sysfs. */ 2893 * LUN devices in sysfs. */
2894 fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL); 2894 fsg->common->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
2895 if (!fsg->luns) { 2895 if (!fsg->common->luns) {
2896 rc = -ENOMEM; 2896 rc = -ENOMEM;
2897 goto out; 2897 goto out;
2898 } 2898 }
2899 fsg->nluns = i; 2899 fsg->common->nluns = i;
2900 2900
2901 for (i = 0; i < fsg->nluns; ++i) { 2901 for (i = 0; i < fsg->common->nluns; ++i) {
2902 curlun = &fsg->luns[i]; 2902 curlun = &fsg->common->luns[i];
2903 curlun->cdrom = !!mod_data.cdrom; 2903 curlun->cdrom = !!mod_data.cdrom;
2904 curlun->ro = mod_data.cdrom || mod_data.ro[i]; 2904 curlun->ro = mod_data.cdrom || mod_data.ro[i];
2905 curlun->initially_ro = curlun->ro; 2905 curlun->initially_ro = curlun->ro;
@@ -2907,7 +2907,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
2907 curlun->dev.release = lun_release; 2907 curlun->dev.release = lun_release;
2908 curlun->dev.parent = &gadget->dev; 2908 curlun->dev.parent = &gadget->dev;
2909 curlun->dev.driver = &fsg_driver.driver; 2909 curlun->dev.driver = &fsg_driver.driver;
2910 dev_set_drvdata(&curlun->dev, &fsg->filesem); 2910 dev_set_drvdata(&curlun->dev, &fsg->common->filesem);
2911 dev_set_name(&curlun->dev,"%s-lun%d", 2911 dev_set_name(&curlun->dev,"%s-lun%d",
2912 dev_name(&gadget->dev), i); 2912 dev_name(&gadget->dev), i);
2913 2913
@@ -2923,7 +2923,6 @@ static int __init fsg_bind(struct usb_gadget *gadget)
2923 goto out; 2923 goto out;
2924 } 2924 }
2925 curlun->registered = 1; 2925 curlun->registered = 1;
2926 kref_get(&fsg->ref);
2927 2926
2928 if (mod_data.file[i] && *mod_data.file[i]) { 2927 if (mod_data.file[i] && *mod_data.file[i]) {
2929 if ((rc = fsg_lun_open(curlun, 2928 if ((rc = fsg_lun_open(curlun,
@@ -2981,7 +2980,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
2981 2980
2982 /* Allocate the data buffers */ 2981 /* Allocate the data buffers */
2983 for (i = 0; i < FSG_NUM_BUFFERS; ++i) { 2982 for (i = 0; i < FSG_NUM_BUFFERS; ++i) {
2984 struct fsg_buffhd *bh = &fsg->buffhds[i]; 2983 struct fsg_buffhd *bh = &fsg->common->buffhds[i];
2985 2984
2986 /* Allocate for the bulk-in endpoint. We assume that 2985 /* Allocate for the bulk-in endpoint. We assume that
2987 * the buffer will also work with the bulk-out (and 2986 * the buffer will also work with the bulk-out (and
@@ -2991,7 +2990,7 @@ static int __init fsg_bind(struct usb_gadget *gadget)
2991 goto out; 2990 goto out;
2992 bh->next = bh + 1; 2991 bh->next = bh + 1;
2993 } 2992 }
2994 fsg->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->buffhds[0]; 2993 fsg->common->buffhds[FSG_NUM_BUFFERS - 1].next = &fsg->common->buffhds[0];
2995 2994
2996 /* This should reflect the actual gadget power source */ 2995 /* This should reflect the actual gadget power source */
2997 usb_gadget_set_selfpowered(gadget); 2996 usb_gadget_set_selfpowered(gadget);
@@ -3019,11 +3018,11 @@ static int __init fsg_bind(struct usb_gadget *gadget)
3019 } 3018 }
3020 3019
3021 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n"); 3020 INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
3022 INFO(fsg, "Number of LUNs=%d\n", fsg->nluns); 3021 INFO(fsg, "Number of LUNs=%d\n", fsg->common->nluns);
3023 3022
3024 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); 3023 pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3025 for (i = 0; i < fsg->nluns; ++i) { 3024 for (i = 0; i < fsg->common->nluns; ++i) {
3026 curlun = &fsg->luns[i]; 3025 curlun = &fsg->common->luns[i];
3027 if (fsg_lun_is_open(curlun)) { 3026 if (fsg_lun_is_open(curlun)) {
3028 p = NULL; 3027 p = NULL;
3029 if (pathbuf) { 3028 if (pathbuf) {
@@ -3092,9 +3091,15 @@ static int __init fsg_alloc(void)
3092 fsg = kzalloc(sizeof *fsg, GFP_KERNEL); 3091 fsg = kzalloc(sizeof *fsg, GFP_KERNEL);
3093 if (!fsg) 3092 if (!fsg)
3094 return -ENOMEM; 3093 return -ENOMEM;
3094
3095 fsg->common = kzalloc(sizeof *fsg->common, GFP_KERNEL);
3096 if (!fsg->common) {
3097 kfree(fsg);
3098 return -ENOMEM;
3099 }
3100
3095 spin_lock_init(&fsg->lock); 3101 spin_lock_init(&fsg->lock);
3096 init_rwsem(&fsg->filesem); 3102 init_rwsem(&fsg->common->filesem);
3097 kref_init(&fsg->ref);
3098 init_completion(&fsg->thread_notifier); 3103 init_completion(&fsg->thread_notifier);
3099 3104
3100 the_fsg = fsg; 3105 the_fsg = fsg;
@@ -3111,7 +3116,7 @@ static int __init fsg_init(void)
3111 return rc; 3116 return rc;
3112 fsg = the_fsg; 3117 fsg = the_fsg;
3113 if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0) 3118 if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0)
3114 kref_put(&fsg->ref, fsg_release); 3119 fsg_release(fsg);
3115 return rc; 3120 return rc;
3116} 3121}
3117module_init(fsg_init); 3122module_init(fsg_init);
@@ -3128,6 +3133,6 @@ static void __exit fsg_cleanup(void)
3128 /* Wait for the thread to finish up */ 3133 /* Wait for the thread to finish up */
3129 wait_for_completion(&fsg->thread_notifier); 3134 wait_for_completion(&fsg->thread_notifier);
3130 3135
3131 kref_put(&fsg->ref, fsg_release); 3136 fsg_release(fsg);
3132} 3137}
3133module_exit(fsg_cleanup); 3138module_exit(fsg_cleanup);