void* mem2_malloc(u32 size) { void *ptr; ptr = __lwp_heap_allocate(&mem2_heap, size); Debug ("mem2 allocated %u bytes", mem2_getblocksize (ptr)); return ptr; }
s32 USBStorageOGC_Initialize() { u32 level; if(__inited) return IPC_OK; _CPU_ISR_Disable(level); LWP_InitQueue(&__usbstorage_waitq); if(!arena_ptr) { arena_ptr = (u8*)ROUNDDOWN32(((u32)SYS_GetArena2Hi() - HEAP_SIZE)); if((u32)arena_ptr < (u32)SYS_GetArena2Lo()) { _CPU_ISR_Restore(level); return IPC_ENOMEM; } SYS_SetArena2Hi(arena_ptr); } __lwp_heap_init(&__heap, arena_ptr, HEAP_SIZE, 32); cbw_buffer=(u8*)__lwp_heap_allocate(&__heap, 32); __inited = true; _CPU_ISR_Restore(level); return IPC_OK; }
static bool __usbstorage_IsInserted(void) { usb_device_entry *buffer; u8 device_count; u8 i, j; u16 vid, pid; s32 maxLun; s32 retval; u32 sectorsize, numSectors; if(__mounted) { // device is not a USB DVD drive - always return true if (__usbfd.sector_size[__lun] != 2048) return true; // check if DVD is inserted if (USBStorageOGC_ReadCapacity(&__usbfd, __lun, §orsize, &numSectors) < 0) return false; else return true; } if(!__inited) return false; buffer = (usb_device_entry*)__lwp_heap_allocate(&__heap, DEVLIST_MAXSIZE * sizeof(usb_device_entry)); if (!buffer) return false; memset(buffer, 0, DEVLIST_MAXSIZE * sizeof(usb_device_entry)); if (USB_OGC_GetDeviceList(buffer, DEVLIST_MAXSIZE, USB_CLASS_MASS_STORAGE, &device_count) < 0) { if (__vid != 0 || __pid != 0) USBStorageOGC_Close(&__usbfd); __lwp_heap_free(&__heap, buffer); return false; } usleep(100); if (__vid != 0 || __pid != 0) { for(i = 0; i < device_count; i++) { vid = buffer[i].vid; pid = buffer[i].pid; if(vid != 0 || pid != 0) { if((vid == __vid) && (pid == __pid)) { __mounted = true; __lwp_heap_free(&__heap,buffer); usleep(50); // I don't know why I have to wait but it's needed return true; } } } USBStorageOGC_Close(&__usbfd); // device changed or unplugged, return false the first time to notify to the client that he must unmount devices __lwp_heap_free(&__heap,buffer); return false; } for (i = 0; i < device_count; i++) { vid = buffer[i].vid; pid = buffer[i].pid; if (vid == 0 || pid == 0) continue; if (vid == 0x0b95 && pid == 0x7720) // USB LAN continue; if (USBStorageOGC_Open(&__usbfd, buffer[i].device_id, vid, pid) < 0) continue; maxLun = USBStorageOGC_GetMaxLUN(&__usbfd); for (j = 0; j < maxLun; j++) { retval = USBStorageOGC_MountLUN(&__usbfd, j); if (retval == INVALID_LUN) continue; if (retval < 0) { __usbstorage_reset(&__usbfd); continue; } __mounted = true; __lun = j; __vid = vid; __pid = pid; usb_last_used = gettime()-secs_to_ticks(100); usleep(10000); break; } if (__mounted) break; USBStorageOGC_Close(&__usbfd); } __lwp_heap_free(&__heap, buffer); return __mounted; }
s32 USBStorageOGC_Open(usbstorage_handle *dev, s32 device_id, u16 vid, u16 pid) { s32 retval = -1; u8 conf = -1; u8 *max_lun; u32 iConf, iInterface, iEp; usb_devdesc udd; usb_configurationdesc *ucd; usb_interfacedesc *uid; usb_endpointdesc *ued; max_lun = __lwp_heap_allocate(&__heap, 1); if (!max_lun) return IPC_ENOMEM; memset(dev, 0, sizeof(*dev)); dev->usb_fd = -1; dev->tag = TAG_START; if (LWP_MutexInit(&dev->lock, false) < 0) goto free_and_return; if (SYS_CreateAlarm(&dev->alarm) < 0) goto free_and_return; retval = USB_OGC_OpenDevice(device_id, vid, pid, &dev->usb_fd); if (retval < 0) goto free_and_return; retval = USB_OGC_GetDescriptors(dev->usb_fd, &udd); if (retval < 0) goto free_and_return; for (iConf = 0; iConf < udd.bNumConfigurations; iConf++) { ucd = &udd.configurations[iConf]; for (iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++) { uid = &ucd->interfaces[iInterface]; if(uid->bInterfaceClass == USB_CLASS_MASS_STORAGE && /* (uid->bInterfaceSubClass == MASS_STORAGE_SCSI_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_RBC_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_ATA_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_QIC_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_UFI_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_SFF8070_COMMANDS) &&*/ uid->bInterfaceProtocol == MASS_STORAGE_BULK_ONLY) { if (uid->bNumEndpoints < 2) continue; dev->ep_in = dev->ep_out = 0; for (iEp = 0; iEp < uid->bNumEndpoints; iEp++) { ued = &uid->endpoints[iEp]; if (ued->bmAttributes != USB_ENDPOINT_BULK) continue; if (ued->bEndpointAddress & USB_ENDPOINT_IN) { dev->ep_in = ued->bEndpointAddress; } else { dev->ep_out = ued->bEndpointAddress; if(ued->wMaxPacketSize > 64 && (dev->usb_fd>=0x20 || dev->usb_fd<-1)) usb2_mode=true; else usb2_mode=false; } } if (dev->ep_in != 0 && dev->ep_out != 0) { dev->configuration = ucd->bConfigurationValue; dev->interface = uid->bInterfaceNumber; dev->altInterface = uid->bAlternateSetting; goto found; } } } } USB_OGC_FreeDescriptors(&udd); retval = USBSTORAGE_ENOINTERFACE; goto free_and_return; found: dev->bInterfaceSubClass = uid->bInterfaceSubClass; USB_OGC_FreeDescriptors(&udd); retval = USBSTORAGE_EINIT; // some devices return an error, ignore it USB_OGC_GetConfiguration(dev->usb_fd, &conf); if (conf != dev->configuration) USB_OGC_SetConfiguration(dev->usb_fd, dev->configuration); if (dev->altInterface !=0) USB_OGC_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface); if(!usb2_mode) retval = USBStorageOGC_Reset(dev); dev->suspended = 0; LWP_MutexLock(dev->lock); retval = __USB_CtrlMsgTimeout(dev, (USB_CTRLTYPE_DIR_DEVICE2HOST | USB_CTRLTYPE_TYPE_CLASS | USB_CTRLTYPE_REC_INTERFACE), USBSTORAGE_GET_MAX_LUN, 0, dev->interface, 1, max_lun); LWP_MutexUnlock(dev->lock); if (retval < 0) dev->max_lun = 1; else dev->max_lun = *max_lun + 1; if (retval == USBSTORAGE_ETIMEDOUT) goto free_and_return; retval = USBSTORAGE_OK; dev->sector_size = (u32 *) calloc(dev->max_lun, sizeof(u32)); if(!dev->sector_size) { retval = IPC_ENOMEM; goto free_and_return; } /* taken from linux usbstorage module (drivers/usb/storage/transport.c) * * Some devices (i.e. Iomega Zip100) need this -- apparently * the bulk pipes get STALLed when the GetMaxLUN request is * processed. This is, in theory, harmless to all other devices * (regardless of if they stall or not). * * 8/9/10: If anyone wants to actually use a Zip100, they can add this back. * But for now, it seems to be breaking things more than it is helping. */ //USB_ClearHalt(dev->usb_fd, dev->ep_in); //USB_ClearHalt(dev->usb_fd, dev->ep_out); if(!dev->buffer) dev->buffer = __lwp_heap_allocate(&__heap, MAX_TRANSFER_SIZE_V5); if(!dev->buffer) { retval = IPC_ENOMEM; } else { USB_OGC_DeviceRemovalNotifyAsync(dev->usb_fd,__usb_deviceremoved_cb,dev); retval = USBSTORAGE_OK; } free_and_return: if (max_lun) __lwp_heap_free(&__heap, max_lun); if (retval < 0) { USBStorageOGC_Close(dev); return retval; } return 0; }
void* mem2_malloc(u32 size) { return __lwp_heap_allocate(&mem2_heap, size); }
static bool __usbstorage_IsInserted(void) { usb_device_entry *buffer; u8 device_count; u8 i, j; u16 vid, pid; s32 maxLun; s32 retval; #ifdef DEBUG_USB usb_log("__usbstorage_IsInserted\n"); if(__mounted) usb_log("device previously mounted.\n"); #endif if(__mounted) return true; if(!__inited) return false; buffer = (usb_device_entry*)__lwp_heap_allocate(&__heap, DEVLIST_MAXSIZE * sizeof(usb_device_entry)); if (!buffer) return false; memset(buffer, 0, DEVLIST_MAXSIZE * sizeof(usb_device_entry)); if (USB_GetDeviceList(buffer, DEVLIST_MAXSIZE, USB_CLASS_MASS_STORAGE, &device_count) < 0) { if (__vid != 0 || __pid != 0) USBStorage_Close(&__usbfd); __lwp_heap_free(&__heap, buffer); usb_log("Error in USB_GetDeviceList\n"); return false; } usleep(100); if (__vid != 0 || __pid != 0) { for(i = 0; i < device_count; i++) { vid = buffer[i].vid; pid = buffer[i].pid; if(vid != 0 || pid != 0) { if((vid == __vid) && (pid == __pid)) { __mounted = true; __lwp_heap_free(&__heap,buffer); usleep(50); // I don't know why I have to wait but it's needed return true; } } } USBStorage_Close(&__usbfd); // device changed or unplugged, return false the first time to notify to the client that he must unmount devices __lwp_heap_free(&__heap,buffer); usb_log("USB_GetDeviceList. device_count: %i\n",device_count); return false; } usb_log("USB_GetDeviceList. device_count: %i\n",device_count); for (i = 0; i < device_count; i++) { vid = buffer[i].vid; pid = buffer[i].pid; if (vid == 0 || pid == 0) continue; if (USBStorage_Open(&__usbfd, buffer[i].device_id, vid, pid) < 0) { usb_log("Error USBStorage_Open (%i,%i)\n",vid, pid); continue; } maxLun = USBStorage_GetMaxLUN(&__usbfd); usb_log("GetMaxLUN: %i\n",maxLun); for (j = 0; j < maxLun; j++) { retval = USBStorage_MountLUN(&__usbfd, j); if(retval<0) usb_log("Error USBStorage_MountLUN(%i): %i\n",j,retval); else usb_log("USBStorage_MountLUN(%i) Ok\n",j); //if (retval == USBSTORAGE_ETIMEDOUT) // break; if (retval < 0) { __usbstorage_reset(&__usbfd); continue; } __mounted = true; __lun = j; __vid = vid; __pid = pid; usb_last_used = gettime()-secs_to_ticks(100); usleep(100); #ifdef DEBUG_USB { u8 bf[2048]; if(USBStorage_Read(&__usbfd, __lun, 0, 1, bf)<0) { usb_log("Error reading sector 0\n"); USBStorage_Close(&__usbfd); return false; } else usb_log("Read sector 0 ok\n"); } #endif break; } if (__mounted) break; USBStorage_Close(&__usbfd); } __lwp_heap_free(&__heap, buffer); return __mounted; }