diff options
Diffstat (limited to 'drivers/usb/gadget/f_mass_storage.c')
-rw-r--r-- | drivers/usb/gadget/f_mass_storage.c | 364 |
1 files changed, 360 insertions, 4 deletions
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 92b7736..ec1d520 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -297,6 +297,11 @@ #include "gadget_chips.h" +#ifdef CONFIG_USB_CDFS_SUPPORT +#ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE +#define _SUPPORT_MAC_ /* support to recognize CDFS on OSX (MAC PC) */ +#endif +#endif /*------------------------------------------------------------------------*/ @@ -456,6 +461,62 @@ struct fsg_dev { struct usb_ep *bulk_out; }; +#ifdef CONFIG_USB_CDFS_SUPPORT +#ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE +static int send_message(struct fsg_common *common, char *msg) +{ + char name_buf[120]; + char state_buf[120]; + char *envp[3]; + int env_offset = 0; + struct usb_gadget *gadget = common->gadget; + + DBG(common, "%s called\n", __func__); + printk(KERN_INFO "%s (%s)\n", __func__, msg); + + if (gadget) { + snprintf(name_buf, sizeof(name_buf), + "SWITCH_NAME=USB_MESSAGE"); + envp[env_offset++] = name_buf; + + snprintf(state_buf, sizeof(state_buf), + "SWITCH_STATE=%s", msg); + envp[env_offset++] = state_buf; + + envp[env_offset] = NULL; + + if (!gadget->dev.class) { + gadget->dev.class = class_create(THIS_MODULE, + "usb_msg"); + if (IS_ERR(gadget->dev.class)) + return -1; + } + + DBG(common, "Send cd eject message to daemon\n"); + + kobject_uevent_env(&gadget->dev.kobj, KOBJ_CHANGE, envp); + } + + return 0; +} + +static int do_autorun_check(struct fsg_common *common) +{ + printk(KERN_INFO "%s called\n", __func__); + send_message(common, "autorun"); + + return 0; +} + +static int do_switch_atmode(struct fsg_common *common) +{ + printk(KERN_INFO "%s called\n", __func__); + send_message(common, "Load AT"); + + return 0; +} +#endif /* CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE */ +#endif static inline int __fsg_is_set(struct fsg_common *common, const char *func, unsigned line) { @@ -738,6 +799,243 @@ static int sleep_thread(struct fsg_common *common) return rc; } +#ifdef CONFIG_USB_CDFS_SUPPORT +#ifdef _SUPPORT_MAC_ +static void _lba_to_msf(u8 *buf, int lba) +{ + lba += 150; + buf[0] = (lba / 75) / 60; + buf[1] = (lba / 75) % 60; + buf[2] = lba % 75; +} + +static int _read_toc_raw(struct fsg_common *common, struct fsg_buffhd *bh) +{ + struct fsg_lun *curlun = common->curlun; + int msf = common->cmnd[1] & 0x02; + u8 *buf = (u8 *) bh->buf; + + u8 *q; + int len; + + q = buf + 2; + *q++ = 1; /* first session */ + *q++ = 1; /* last session */ + + *q++ = 1; /* session number */ + *q++ = 0x14; /* data track */ + *q++ = 0; /* track number */ + *q++ = 0xa0; /* lead-in */ + *q++ = 0; /* min */ + *q++ = 0; /* sec */ + *q++ = 0; /* frame */ + *q++ = 0; + *q++ = 1; /* first track */ + *q++ = 0x00; /* disk type */ + *q++ = 0x00; + + *q++ = 1; /* session number */ + *q++ = 0x14; /* data track */ + *q++ = 0; /* track number */ + *q++ = 0xa1; + *q++ = 0; /* min */ + *q++ = 0; /* sec */ + *q++ = 0; /* frame */ + *q++ = 0; + *q++ = 1; /* last track */ + *q++ = 0x00; + *q++ = 0x00; + + *q++ = 1; /* session number */ + *q++ = 0x14; /* data track */ + *q++ = 0; /* track number */ + *q++ = 0xa2; /* lead-out */ + *q++ = 0; /* min */ + *q++ = 0; /* sec */ + *q++ = 0; /* frame */ + if (msf) { + *q++ = 0; /* reserved */ + _lba_to_msf(q, curlun->num_sectors); + q += 3; + } else { + put_unaligned_be32(curlun->num_sectors, q); + q += 4; + } + + *q++ = 1; /* session number */ + *q++ = 0x14; /* ADR, control */ + *q++ = 0; /* track number */ + *q++ = 1; /* point */ + *q++ = 0; /* min */ + *q++ = 0; /* sec */ + *q++ = 0; /* frame */ + if (msf) { + *q++ = 0; + _lba_to_msf(q, 0); + q += 3; + } else { + *q++ = 0; + *q++ = 0; + *q++ = 0; + *q++ = 0; + } + + len = q - buf; + put_unaligned_be16(len - 2, buf); + + return len; +} + +static void cd_data_to_raw(u8 *buf, int lba) +{ + /* sync bytes */ + buf[0] = 0x00; + memset(buf + 1, 0xff, 10); + buf[11] = 0x00; + buf += 12; + /* MSF */ + _lba_to_msf(buf, lba); + buf[3] = 0x01; /* mode 1 data */ + buf += 4; + /* data */ + buf += 2048; + /* XXX: ECC not computed */ + memset(buf, 0, 288); +} + +static int do_read_cd(struct fsg_common *common) +{ + struct fsg_lun *curlun = common->curlun; + u32 lba; + struct fsg_buffhd *bh; + int rc; + u32 amount_left; + loff_t file_offset, file_offset_tmp; + unsigned int amount; + unsigned int partial_page; + ssize_t nread; + + u32 nb_sectors, transfer_request; + + nb_sectors = (common->cmnd[6] << 16) | + (common->cmnd[7] << 8) | common->cmnd[8]; + lba = get_unaligned_be32(&common->cmnd[2]); + + if (nb_sectors == 0) + return 0; + + if (lba >= curlun->num_sectors) { + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; + return -EINVAL; + } + + transfer_request = common->cmnd[9]; + if ((transfer_request & 0xf8) == 0xf8) { + file_offset = ((loff_t) lba) << 11; + /* read all data - 2352 byte */ + amount_left = 2352; + } else { + file_offset = ((loff_t) lba) << 9; + /* Carry out the file reads */ + amount_left = common->data_size_from_cmnd; + } + + if (unlikely(amount_left == 0)) + return -EIO; /* No default reply */ + + for (;;) { + + /* Figure out how much we need to read: + * Try to read the remaining amount. + * But don't read more than the buffer size. + * And don't try to read past the end of the file. + * Finally, if we're not at a page boundary, don't read past + * the next page. + * If this means reading 0 then we were asked to read past + * the end of file. */ + amount = min(amount_left, FSG_BUFLEN); + amount = min((loff_t) amount, + curlun->file_length - file_offset); + partial_page = file_offset & (PAGE_CACHE_SIZE - 1); + if (partial_page > 0) + amount = min(amount, (unsigned int) PAGE_CACHE_SIZE - + partial_page); + + /* Wait for the next buffer to become available */ + bh = common->next_buffhd_to_fill; + while (bh->state != BUF_STATE_EMPTY) { + rc = sleep_thread(common); + if (rc) + return rc; + } + + /* If we were asked to read past the end of file, + * end with an empty buffer. */ + if (amount == 0) { + curlun->sense_data = + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; + curlun->sense_data_info = file_offset >> 9; + curlun->info_valid = 1; + bh->inreq->length = 0; + bh->state = BUF_STATE_FULL; + break; + } + + /* Perform the read */ + file_offset_tmp = file_offset; + if ((transfer_request & 0xf8) == 0xf8) { + nread = vfs_read(curlun->filp, + ((char __user *)bh->buf)+16, + amount, &file_offset_tmp); + } else { + nread = vfs_read(curlun->filp, + (char __user *)bh->buf, + amount, &file_offset_tmp); + } + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount, + (unsigned long long) file_offset, + (int) nread); + if (signal_pending(current)) + return -EINTR; + + if (nread < 0) { + LDBG(curlun, "error in file read: %d\n", + (int) nread); + nread = 0; + } else if (nread < amount) { + LDBG(curlun, "partial file read: %d/%u\n", + (int) nread, amount); + nread -= (nread & 511); /* Round down to a block */ + } + file_offset += nread; + amount_left -= nread; + common->residue -= nread; + bh->inreq->length = nread; + bh->state = BUF_STATE_FULL; + + /* If an error occurred, report it and its position */ + if (nread < amount) { + curlun->sense_data = SS_UNRECOVERED_READ_ERROR; + curlun->sense_data_info = file_offset >> 9; + curlun->info_valid = 1; + break; + } + + if (amount_left == 0) + break; /* No more left to read */ + + /* Send this buffer and go read some more */ + start_in_transfer(common, bh); + common->next_buffhd_to_fill = bh->next; + } + + if ((transfer_request & 0xf8) == 0xf8) + cd_data_to_raw(bh->buf, lba); + + return -EIO; /* No default reply */ +} +#endif /* _SUPPORT_MAC_ */ +#endif /*-------------------------------------------------------------------------*/ @@ -1397,13 +1695,22 @@ static int do_read_toc(struct fsg_common *common, struct fsg_buffhd *bh) int msf = common->cmnd[1] & 0x02; int start_track = common->cmnd[6]; u8 *buf = (u8 *)bh->buf; - +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef _SUPPORT_MAC_ + int format = (common->cmnd[9] & 0xC0) >> 6; +#endif +#endif if ((common->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */ start_track > 1) { curlun->sense_data = SS_INVALID_FIELD_IN_CDB; return -EINVAL; } - +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef _SUPPORT_MAC_ + if (format == 2) + return _read_toc_raw(common, bh); +#endif +#endif memset(buf, 0, 20); buf[1] = (20-2); /* TOC data length */ buf[2] = 1; /* First track number */ @@ -1528,6 +1835,12 @@ static int do_start_stop(struct fsg_common *common) * available for use as soon as it is loaded. */ if (start) { +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE + if (loej) + send_message(common, "Load AT"); +#endif +#endif if (!fsg_lun_is_open(curlun)) { curlun->sense_data = SS_MEDIUM_NOT_PRESENT; return -EINVAL; @@ -1561,6 +1874,12 @@ static int do_start_stop(struct fsg_common *common) up_write(&common->filesem); down_read(&common->filesem); +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE + send_message(common, "Load User"); +#endif +#endif + return common->ops && common->ops->post_eject ? min(0, common->ops->post_eject(common, curlun, curlun - common->luns)) @@ -2153,7 +2472,16 @@ static int do_scsi_command(struct fsg_common *common) common->data_size_from_cmnd = get_unaligned_be16(&common->cmnd[7]); reply = check_command(common, 10, DATA_DIR_TO_HOST, +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef _SUPPORT_MAC_ + (0xf<<6) | (1<<1), 1, +#else (7<<6) | (1<<1), 1, +#endif +#else + (7<<6) | (1<<1), 1, +#endif + "READ TOC"); if (reply == 0) reply = do_read_toc(common, bh); @@ -2246,15 +2574,43 @@ static int do_scsi_command(struct fsg_common *common) reply = do_write(common); break; - /* - * Some mandatory commands that we recognize but don't implement. +#if defined(CONFIG_USB_CDFS_SUPPORT) +#ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE + case RELEASE: /* SC_AUTORUN_CHECK0 : 0x17 */ + reply = do_switch_atmode(common); + break; + + case RESERVE: /* SC_AUTORUN_CHECK1 : 0x16 */ + reply = do_autorun_check(common); + break; + +#ifdef _SUPPORT_MAC_ + case READ_CD: + common->data_size_from_cmnd = ((common->cmnd[6] << 16) + | (common->cmnd[7] << 8) + | (common->cmnd[8])) << 9; + reply = check_command(common, 12, DATA_DIR_TO_HOST, + (0xf<<2) | (7<<7), 1, + "READ CD"); + if (reply == 0) + reply = do_read_cd(common); + break; + +#endif /* _SUPPORT_MAC_ */ +#endif +#endif + /* Some mandatory commands that we recognize but don't implement. * They don't mean much in this setting. It's left as an exercise * for anyone interested to implement RESERVE and RELEASE in terms * of Posix locks. */ case FORMAT_UNIT: +#ifndef CONFIG_USB_CDFS_SUPPORT +#ifndef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE case RELEASE: case RESERVE: +#endif +#endif case SEND_DIAGNOSTIC: /* Fall through */ |