| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095 |
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include "amt630h.h"
- #include "typedef.h"
- #include "uart.h"
- #include "timer.h"
- #include "list.h"
- #include "scsi.h"
- #include "usb.h"
- #include "dwc2.h"
- #include "fs/ff.h"
- #include "fs/diskio.h"
- #define USB_BUFSIZ 512
- #define HUB_SHORT_RESET_TIME 20
- #define HUB_LONG_RESET_TIME 200
- #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
- #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
- #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
- #endif
- /* Part types */
- #define PART_TYPE_UNKNOWN 0x00
- #define PART_TYPE_MAC 0x01
- #define PART_TYPE_DOS 0x02
- #define PART_TYPE_ISO 0x03
- #define PART_TYPE_AMIGA 0x04
- #define PART_TYPE_EFI 0x05
- /* device types */
- #define DEV_TYPE_UNKNOWN 0xff /* not connected */
- #define DEV_TYPE_HARDDISK 0x00 /* harddisk */
- #define DEV_TYPE_TAPE 0x01 /* Tape */
- #define DEV_TYPE_CDROM 0x05 /* CD-ROM */
- #define DEV_TYPE_OPDISK 0x07 /* optical disk */
- #define PART_NAME_LEN 32
- #define PART_TYPE_LEN 32
- #define MAX_SEARCH_PARTITIONS 64
- typedef unsigned long lbaint_t;
- typedef struct {
- u8 b[16];
- } efi_guid_t;
- /* Interface types: */
- enum if_type {
- IF_TYPE_UNKNOWN = 0,
- IF_TYPE_IDE,
- IF_TYPE_SCSI,
- IF_TYPE_ATAPI,
- IF_TYPE_USB,
- IF_TYPE_DOC,
- IF_TYPE_MMC,
- IF_TYPE_SD,
- IF_TYPE_SATA,
- IF_TYPE_HOST,
- IF_TYPE_NVME,
- IF_TYPE_EFI,
- IF_TYPE_COUNT, /* Number of interface types */
- };
- #define BLK_VEN_SIZE 40
- #define BLK_PRD_SIZE 20
- #define BLK_REV_SIZE 8
- /*
- * Identifies the partition table type (ie. MBR vs GPT GUID) signature
- */
- enum sig_type {
- SIG_TYPE_NONE,
- SIG_TYPE_MBR,
- SIG_TYPE_GUID,
- SIG_TYPE_COUNT /* Number of signature types */
- };
- /*
- * With driver model (CONFIG_BLK) this is uclass platform data, accessible
- * with dev_get_uclass_platdata(dev)
- */
- struct blk_desc {
- /*
- * TODO: With driver model we should be able to use the parent
- * device's uclass instead.
- */
- enum if_type if_type; /* type of the interface */
- int devnum; /* device number */
- unsigned char part_type; /* partition type */
- unsigned char target; /* target SCSI ID */
- unsigned char lun; /* target LUN */
- unsigned char hwpart; /* HW partition, e.g. for eMMC */
- unsigned char type; /* device type */
- unsigned char removable; /* removable device */
- lbaint_t lba; /* number of blocks */
- unsigned long blksz; /* block size */
- int log2blksz; /* for convenience: log2(blksz) */
- char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
- char product[BLK_PRD_SIZE + 1]; /* device product number */
- char revision[BLK_REV_SIZE + 1]; /* firmware revision */
- enum sig_type sig_type; /* Partition table signature type */
- union {
- u32 mbr_sig; /* MBR integer signature */
- efi_guid_t guid_sig; /* GPT GUID Signature */
- };
- unsigned long (*block_read)(struct blk_desc *block_dev,
- lbaint_t start,
- lbaint_t blkcnt,
- void *buffer);
- unsigned long (*block_write)(struct blk_desc *block_dev,
- lbaint_t start,
- lbaint_t blkcnt,
- const void *buffer);
- unsigned long (*block_erase)(struct blk_desc *block_dev,
- lbaint_t start,
- lbaint_t blkcnt);
- void *priv; /* driver private struct pointer */
- };
- static int asynch_allowed;
- char usb_started; /* flag for the started/stopped USB status */
- static struct usb_device usb_dev[USB_MAX_DEVICE];
- static int dev_index;
- struct usb_device_scan {
- struct list_head list;
- struct usb_device *dev; /* USB hub device to scan */
- struct usb_hub_device *hub; /* USB hub struct */
- int port; /* USB port to scan */
- };
- static LIST_HEAD(usb_scan_list);
- static struct usb_hub_device hub_dev[USB_MAX_HUB];
- static int usb_hub_index;
- static int usb_stor_curr_dev = -1; /* current device */
- /* direction table -- this indicates the direction of the data
- * transfer for each command code -- a 1 indicates input
- */
- static const unsigned char us_direction[256/8] = {
- 0x28, 0x81, 0x14, 0x14, 0x20, 0x01, 0x90, 0x77,
- 0x0C, 0x20, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- #define US_DIRECTION(x) ((us_direction[x>>3] >> (x & 7)) & 1)
- #pragma pack(ARCH_DMA_MINALIGN)
- static struct scsi_cmd usb_ccb;
- static u32 CBWTag;
- static int usb_max_devs; /* number of highest available usb device */
- static struct blk_desc usb_dev_desc[USB_MAX_STOR_DEV];
- struct us_data;
- typedef int (*trans_cmnd)(struct scsi_cmd *cb, struct us_data *data);
- typedef int (*trans_reset)(struct us_data *data);
- #define USB_READY (1 << 0)
- struct us_data {
- struct usb_device *pusb_dev; /* this usb_device */
- unsigned int flags; /* from filter initially */
- unsigned char ifnum; /* interface number */
- unsigned char ep_in; /* in endpoint */
- unsigned char ep_out; /* out ....... */
- unsigned char ep_int; /* interrupt . */
- unsigned char subclass; /* as in overview */
- unsigned char protocol; /* .............. */
- unsigned char attention_done; /* force attn on first cmd */
- unsigned short ip_data; /* interrupt data */
- int action; /* what to do */
- int ip_wanted; /* needed */
- int *irq_handle; /* for USB int requests */
- unsigned int irqpipe; /* pipe for release_irq */
- unsigned char irqmaxp; /* max packed for irq Pipe */
- unsigned char irqinterval; /* Intervall for IRQ Pipe */
- struct scsi_cmd *srb; /* current srb */
- trans_reset transport_reset; /* reset routine */
- trans_cmnd transport; /* transport routine */
- unsigned short max_xfer_blk; /* maximum transfer blocks */
- };
- static struct us_data usb_stor[USB_MAX_STOR_DEV];
- #define USB_STOR_TRANSPORT_GOOD 0
- #define USB_STOR_TRANSPORT_FAILED -1
- #define USB_STOR_TRANSPORT_ERROR -2
- void usb_hub_reset(void)
- {
- usb_hub_index = 0;
- /* Zero out global hub_dev in case its re-used again */
- memset(hub_dev, 0, sizeof(hub_dev));
- }
- static struct usb_hub_device *usb_hub_allocate(void)
- {
- if (usb_hub_index < USB_MAX_HUB)
- return &hub_dev[usb_hub_index++];
- return NULL;
- }
- /*-------------------------------------------------------------------
- * Max Packet stuff
- */
- /*
- * returns the max packet size, depending on the pipe direction and
- * the configurations values
- */
- int usb_maxpacket(struct usb_device *dev, unsigned long pipe)
- {
- /* direction is out -> use emaxpacket out */
- if ((pipe & USB_DIR_IN) == 0)
- return dev->epmaxpacketout[((pipe>>15) & 0xf)];
- else
- return dev->epmaxpacketin[((pipe>>15) & 0xf)];
- }
- /*
- * The routine usb_set_maxpacket_ep() is extracted from the loop of routine
- * usb_set_maxpacket(), because the optimizer of GCC 4.x chokes on this routine
- * when it is inlined in 1 single routine. What happens is that the register r3
- * is used as loop-count 'i', but gets overwritten later on.
- * This is clearly a compiler bug, but it is easier to workaround it here than
- * to update the compiler (Occurs with at least several GCC 4.{1,2},x
- * CodeSourcery compilers like e.g. 2007q3, 2008q1, 2008q3 lite editions on ARM)
- *
- * NOTE: Similar behaviour was observed with GCC4.6 on ARMv5.
- */
- static void usb_set_maxpacket_ep(struct usb_device *dev, int if_idx, int ep_idx)
- {
- int b;
- struct usb_endpoint_descriptor *ep;
- u16 ep_wMaxPacketSize = 0;
- unsigned char *tmp;
- (void)ep_wMaxPacketSize;
- ep = &dev->config.if_desc[if_idx].ep_desc[ep_idx];
- b = ep->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
- tmp = (unsigned char*)&ep->wMaxPacketSize;
- ep_wMaxPacketSize = (tmp[1] << 8) | tmp[0];//get_unaligned(&ep->wMaxPacketSize);
- if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
- USB_ENDPOINT_XFER_CONTROL) {
- /* Control => bidirectional */
- dev->epmaxpacketout[b] = ep_wMaxPacketSize;
- dev->epmaxpacketin[b] = ep_wMaxPacketSize;
- /* debug("##Control EP epmaxpacketout/in[%d] = %d\n",
- b, dev->epmaxpacketin[b]); */
- } else {
- if ((ep->bEndpointAddress & 0x80) == 0) {
- /* OUT Endpoint */
- if (ep_wMaxPacketSize > dev->epmaxpacketout[b]) {
- dev->epmaxpacketout[b] = ep_wMaxPacketSize;
- /* debug("##EP epmaxpacketout[%d] = %d\n",
- b, dev->epmaxpacketout[b]); */
- }
- } else {
- /* IN Endpoint */
- if (ep_wMaxPacketSize > dev->epmaxpacketin[b]) {
- dev->epmaxpacketin[b] = ep_wMaxPacketSize;
- /* debug("##EP epmaxpacketin[%d] = %d\n",
- b, dev->epmaxpacketin[b]); */
- }
- } /* if out */
- } /* if control */
- }
- /*
- * set the max packed value of all endpoints in the given configuration
- */
- static int usb_set_maxpacket(struct usb_device *dev)
- {
- int i, ii;
- for (i = 0; i < dev->config.desc.bNumInterfaces; i++)
- for (ii = 0; ii < dev->config.if_desc[i].desc.bNumEndpoints; ii++)
- usb_set_maxpacket_ep(dev, i, ii);
- return 0;
- }
- /*-------------------------------------------------------------------
- * Message wrappers.
- *
- */
- /*
- * submits an Interrupt Message
- */
- int usb_submit_int_msg(struct usb_device *dev, unsigned long pipe,
- void *buffer, int transfer_len, int interval)
- {
- return submit_int_msg(dev, pipe, buffer, transfer_len, interval);
- }
- /*
- * submits a control message and waits for comletion (at least timeout * 1ms)
- * If timeout is 0, we don't wait for completion (used as example to set and
- * clear keyboards LEDs). For data transfers, (storage transfers) we don't
- * allow control messages with 0 timeout, by previousely resetting the flag
- * asynch_allowed (usb_disable_asynch(1)).
- * returns the transferred length if OK or -1 if error. The transferred length
- * and the current status are stored in the dev->act_len and dev->status.
- */
- int usb_control_msg(struct usb_device *dev, unsigned int pipe,
- unsigned char request, unsigned char requesttype,
- unsigned short value, unsigned short index,
- void *data, unsigned short size, int timeout)
- {
- ALLOC_CACHE_ALIGN_BUFFER(struct devrequest, setup_packet, 1);
- int err;
- if ((timeout == 0) && (!asynch_allowed)) {
- /* request for a asynch control pipe is not allowed */
- return -EINVAL;
- }
- /* set setup command */
- setup_packet->requesttype = requesttype;
- setup_packet->request = request;
- setup_packet->value = value;
- setup_packet->index = index;
- setup_packet->length = size;
- /* debug("usb_control_msg: request: 0x%X, requesttype: 0x%X, " \
- "value 0x%X index 0x%X length 0x%X\n",
- request, requesttype, value, index, size); */
- dev->status = USB_ST_NOT_PROC; /*not yet processed */
- err = submit_control_msg(dev, pipe, data, size, setup_packet);
- if (err < 0)
- return err;
- if (timeout == 0)
- return (int)size;
- /*
- * Wait for status to update until timeout expires, USB driver
- * interrupt handler may set the status when the USB operation has
- * been completed.
- */
- while (timeout--) {
- if (!(dev->status & USB_ST_NOT_PROC))
- break;
- mdelay(1);
- }
- if (dev->status)
- return -1;
- return dev->act_len;
- }
- /*-------------------------------------------------------------------
- * submits bulk message, and waits for completion. returns 0 if Ok or
- * negative if Error.
- * synchronous behavior
- */
- int usb_bulk_msg(struct usb_device *dev, unsigned int pipe,
- void *data, int len, int *actual_length, int timeout)
- {
- if (len < 0)
- return -EINVAL;
- dev->status = USB_ST_NOT_PROC; /*not yet processed */
- if (submit_bulk_msg(dev, pipe, data, len) < 0)
- return -EIO;
- while (timeout--) {
- if (!(dev->status & USB_ST_NOT_PROC))
- break;
- mdelay(1);
- }
- *actual_length = dev->act_len;
- if (dev->status == 0)
- return 0;
- else
- return -EIO;
- }
- /*******************************************************************************
- * Parse the config, located in buffer, and fills the dev->config structure.
- * Note that all little/big endian swapping are done automatically.
- * (wTotalLength has already been swapped and sanitized when it was read.)
- */
- static int usb_parse_config(struct usb_device *dev,
- unsigned char *buffer, int cfgno)
- {
- struct usb_descriptor_header *head;
- int index, ifno, epno, curr_if_num;
- u16 ep_wMaxPacketSize = 0;
- unsigned char *tmp;
- struct usb_interface *if_desc = NULL;
- (void)ep_wMaxPacketSize;
- ifno = -1;
- epno = -1;
- curr_if_num = -1;
- dev->configno = cfgno;
- head = (struct usb_descriptor_header *) &buffer[0];
- if (head->bDescriptorType != USB_DT_CONFIG) {
- /* printf(" ERROR: NOT USB_CONFIG_DESC %x\n",
- head->bDescriptorType); */
- return -EINVAL;
- }
- if (head->bLength != USB_DT_CONFIG_SIZE) {
- //printf("ERROR: Invalid USB CFG length (%d)\n", head->bLength);
- return -EINVAL;
- }
- memcpy(&dev->config, head, USB_DT_CONFIG_SIZE);
- dev->config.no_of_if = 0;
- index = dev->config.desc.bLength;
- /* Ok the first entry must be a configuration entry,
- * now process the others */
- head = (struct usb_descriptor_header *) &buffer[index];
- while (index + 1 < dev->config.desc.wTotalLength && head->bLength) {
- switch (head->bDescriptorType) {
- case USB_DT_INTERFACE:
- if (head->bLength != USB_DT_INTERFACE_SIZE) {
- /* printf("ERROR: Invalid USB IF length (%d)\n",
- head->bLength); */
- break;
- }
- if (index + USB_DT_INTERFACE_SIZE >
- dev->config.desc.wTotalLength) {
- //puts("USB IF descriptor overflowed buffer!\n");
- break;
- }
- if (((struct usb_interface_descriptor *) \
- head)->bInterfaceNumber != curr_if_num) {
- /* this is a new interface, copy new desc */
- ifno = dev->config.no_of_if;
- if (ifno >= USB_MAXINTERFACES) {
- //puts("Too many USB interfaces!\n");
- /* try to go on with what we have */
- return -EINVAL;
- }
- if_desc = &dev->config.if_desc[ifno];
- dev->config.no_of_if++;
- memcpy(if_desc, head,
- USB_DT_INTERFACE_SIZE);
- if_desc->no_of_ep = 0;
- if_desc->num_altsetting = 1;
- curr_if_num =
- if_desc->desc.bInterfaceNumber;
- } else {
- /* found alternate setting for the interface */
- if (ifno >= 0) {
- if_desc = &dev->config.if_desc[ifno];
- if_desc->num_altsetting++;
- }
- }
- break;
- case USB_DT_ENDPOINT:
- if (head->bLength != USB_DT_ENDPOINT_SIZE &&
- head->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) {
- /* printf("ERROR: Invalid USB EP length (%d)\n",
- head->bLength); */
- break;
- }
- if (index + head->bLength >
- dev->config.desc.wTotalLength) {
- //puts("USB EP descriptor overflowed buffer!\n");
- break;
- }
- if (ifno < 0) {
- //puts("Endpoint descriptor out of order!\n");
- break;
- }
- epno = dev->config.if_desc[ifno].no_of_ep;
- if_desc = &dev->config.if_desc[ifno];
- if (epno >= USB_MAXENDPOINTS) {
- /* printf("Interface %d has too many endpoints!\n",
- if_desc->desc.bInterfaceNumber); */
- return -EINVAL;
- }
- /* found an endpoint */
- if_desc->no_of_ep++;
- memcpy(&if_desc->ep_desc[epno], head,
- USB_DT_ENDPOINT_SIZE);
- /* ep_wMaxPacketSize = get_unaligned(&dev->config.\
- if_desc[ifno].\
- ep_desc[epno].\
- wMaxPacketSize); */
- tmp = (unsigned char*)&dev->config.if_desc[ifno].ep_desc[epno].wMaxPacketSize;
- ep_wMaxPacketSize = (tmp[1] << 8) | tmp[0];
- /* put_unaligned(le16_to_cpu(ep_wMaxPacketSize),
- &dev->config.\
- if_desc[ifno].\
- ep_desc[epno].\
- wMaxPacketSize); */
- //debug("if %d, ep %d\n", ifno, epno);
- break;
- case USB_DT_SS_ENDPOINT_COMP:
- if (head->bLength != USB_DT_SS_EP_COMP_SIZE) {
- /* printf("ERROR: Invalid USB EPC length (%d)\n",
- head->bLength); */
- break;
- }
- if (index + USB_DT_SS_EP_COMP_SIZE >
- dev->config.desc.wTotalLength) {
- //puts("USB EPC descriptor overflowed buffer!\n");
- break;
- }
- if (ifno < 0 || epno < 0) {
- //puts("EPC descriptor out of order!\n");
- break;
- }
- if_desc = &dev->config.if_desc[ifno];
- memcpy(&if_desc->ss_ep_comp_desc[epno], head,
- USB_DT_SS_EP_COMP_SIZE);
- break;
- default:
- if (head->bLength == 0)
- return -EINVAL;
- /* debug("unknown Description Type : %x\n",
- head->bDescriptorType); */
- #ifdef DEBUG
- {
- unsigned char *ch = (unsigned char *)head;
- int i;
- for (i = 0; i < head->bLength; i++)
- debug("%02X ", *ch++);
- debug("\n\n\n");
- }
- #endif
- break;
- }
- index += head->bLength;
- head = (struct usb_descriptor_header *)&buffer[index];
- }
- return 0;
- }
- /********************************************************************
- * get string index in buffer
- */
- static int usb_get_string(struct usb_device *dev, unsigned short langid,
- unsigned char index, void *buf, int size)
- {
- int i;
- int result;
- for (i = 0; i < 3; ++i) {
- /* some devices are flaky */
- result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
- USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
- (USB_DT_STRING << 8) + index, langid, buf, size,
- USB_CNTL_TIMEOUT);
- if (result > 0)
- break;
- }
- return result;
- }
- static void usb_try_string_workarounds(unsigned char *buf, int *length)
- {
- int newlength, oldlength = *length;
- for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
- if (!isprint(buf[newlength]) || buf[newlength + 1])
- break;
- if (newlength > 2) {
- buf[0] = newlength;
- *length = newlength;
- }
- }
- static int usb_string_sub(struct usb_device *dev, unsigned int langid,
- unsigned int index, unsigned char *buf)
- {
- int rc;
- /* Try to read the string descriptor by asking for the maximum
- * possible number of bytes */
- rc = usb_get_string(dev, langid, index, buf, 255);
- /* If that failed try to read the descriptor length, then
- * ask for just that many bytes */
- if (rc < 2) {
- rc = usb_get_string(dev, langid, index, buf, 2);
- if (rc == 2)
- rc = usb_get_string(dev, langid, index, buf, buf[0]);
- }
- if (rc >= 2) {
- if (!buf[0] && !buf[1])
- usb_try_string_workarounds(buf, &rc);
- /* There might be extra junk at the end of the descriptor */
- if (buf[0] < rc)
- rc = buf[0];
- rc = rc - (rc & 1); /* force a multiple of two */
- }
- if (rc < 2)
- rc = -EINVAL;
- return rc;
- }
- /********************************************************************
- * usb_string:
- * Get string index and translate it to ascii.
- * returns string length (> 0) or error (< 0)
- */
- int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
- {
- ALLOC_CACHE_ALIGN_BUFFER(unsigned char, mybuf, USB_BUFSIZ);
- unsigned char *tbuf;
- int err;
- unsigned int u, idx;
- if (size <= 0 || !buf || !index)
- return -EINVAL;
- buf[0] = 0;
- tbuf = &mybuf[0];
- /* get langid for strings if it's not yet known */
- if (!dev->have_langid) {
- err = usb_string_sub(dev, 0, 0, tbuf);
- if (err < 0) {
- /* debug("error getting string descriptor 0 " \
- "(error=%lx)\n", dev->status); */
- return -EIO;
- } else if (tbuf[0] < 4) {
- //debug("string descriptor 0 too short\n");
- return -EIO;
- } else {
- dev->have_langid = -1;
- dev->string_langid = tbuf[2] | (tbuf[3] << 8);
- /* always use the first langid listed */
- /* debug("USB device number %d default " \
- "language ID 0x%x\n",
- dev->devnum, dev->string_langid); */
- }
- }
- err = usb_string_sub(dev, dev->string_langid, index, tbuf);
- if (err < 0)
- return err;
- size--; /* leave room for trailing NULL char in output buffer */
- for (idx = 0, u = 2; u < err; u += 2) {
- if (idx >= size)
- break;
- if (tbuf[u+1]) /* high byte */
- buf[idx++] = '?'; /* non-ASCII character */
- else
- buf[idx++] = tbuf[u];
- }
- buf[idx] = 0;
- err = idx;
- return err;
- }
- struct usb_device *usb_get_dev_index(int index)
- {
- if (usb_dev[index].devnum == -1)
- return NULL;
- else
- return &usb_dev[index];
- }
- void usb_free_device(void *controller)
- {
- dev_index--;
- //debug("Freeing device node: %d\n", dev_index);
- memset(&usb_dev[dev_index], 0, sizeof(struct usb_device));
- usb_dev[dev_index].devnum = -1;
- }
- int usb_alloc_device(struct usb_device *udev)
- {
- return 0;
- }
- /***********************************************************************
- * Clears an endpoint
- * endp: endpoint number in bits 0-3;
- * direction flag in bit 7 (1 = IN, 0 = OUT)
- */
- int usb_clear_halt(struct usb_device *dev, int pipe)
- {
- int result;
- int endp = usb_pipeendpoint(pipe)|(usb_pipein(pipe)<<7);
- result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
- USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
- endp, NULL, 0, USB_CNTL_TIMEOUT * 3);
- /* don't clear if failed */
- if (result < 0)
- return result;
- /*
- * NOTE: we do not get status and verify reset was successful
- * as some devices are reported to lock up upon this check..
- */
- usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
- /* toggle is reset on clear */
- usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
- return 0;
- }
- /**********************************************************************
- * get_descriptor type
- */
- static int usb_get_descriptor(struct usb_device *dev, unsigned char type,
- unsigned char index, void *buf, int size)
- {
- return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
- USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
- (type << 8) + index, 0, buf, size,
- USB_CNTL_TIMEOUT);
- }
- /**********************************************************************
- * gets len of configuration cfgno
- */
- int usb_get_configuration_len(struct usb_device *dev, int cfgno)
- {
- int result;
- ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 9);
- struct usb_config_descriptor *config;
- config = (struct usb_config_descriptor *)&buffer[0];
- result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, 9);
- if (result < 9) {
- if (result < 0)
- ;/* printf("unable to get descriptor, error %lX\n",
- dev->status); */
- else
- ;/* printf("config descriptor too short " \
- "(expected %i, got %i)\n", 9, result); */
- return -EIO;
- }
- return config->wTotalLength;
- }
- /**********************************************************************
- * gets configuration cfgno and store it in the buffer
- */
- int usb_get_configuration_no(struct usb_device *dev, int cfgno,
- unsigned char *buffer, int length)
- {
- int result;
- struct usb_config_descriptor *config;
- config = (struct usb_config_descriptor *)&buffer[0];
- result = usb_get_descriptor(dev, USB_DT_CONFIG, cfgno, buffer, length);
- /* debug("get_conf_no %d Result %d, wLength %d\n", cfgno, result,
- le16_to_cpu(config->wTotalLength)); */
- config->wTotalLength = result; /* validated, with CPU byte order */
- return result;
- }
- static int get_descriptor_len(struct usb_device *dev, int len, int expect_len)
- {
- struct usb_device_descriptor *desc;
- ALLOC_CACHE_ALIGN_BUFFER(unsigned char, tmpbuf, USB_BUFSIZ);
- int err;
- desc = (struct usb_device_descriptor *)tmpbuf;
- err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, len);
- if (err < expect_len) {
- if (err < 0) {
- /* printf("unable to get device descriptor (error=%d)\n",
- err); */
- return err;
- } else {
- /* printf("USB device descriptor short read (expected %i, got %i)\n",
- expect_len, err); */
- return -EIO;
- }
- }
- memcpy(&dev->descriptor, tmpbuf, sizeof(dev->descriptor));
- return 0;
- }
- /********************************************************************
- * set address of a device to the value in dev->devnum.
- * This can only be done by addressing the device via the default address (0)
- */
- static int usb_set_address(struct usb_device *dev)
- {
- //debug("set address %d\n", dev->devnum);
- return usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
- 0, (dev->devnum), 0, NULL, 0, USB_CNTL_TIMEOUT);
- }
- /********************************************************************
- * set interface number to interface
- */
- int usb_set_interface(struct usb_device *dev, int interface, int alternate)
- {
- struct usb_interface *if_face = NULL;
- int ret, i;
- for (i = 0; i < dev->config.desc.bNumInterfaces; i++) {
- if (dev->config.if_desc[i].desc.bInterfaceNumber == interface) {
- if_face = &dev->config.if_desc[i];
- break;
- }
- }
- if (!if_face) {
- //printf("selecting invalid interface %d", interface);
- return -EINVAL;
- }
- /*
- * We should return now for devices with only one alternate setting.
- * According to 9.4.10 of the Universal Serial Bus Specification
- * Revision 2.0 such devices can return with a STALL. This results in
- * some USB sticks timeouting during initialization and then being
- * unusable in U-Boot.
- */
- if (if_face->num_altsetting == 1)
- return 0;
- ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
- USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
- alternate, interface, NULL, 0,
- USB_CNTL_TIMEOUT * 5);
- if (ret < 0)
- return ret;
- return 0;
- }
- /********************************************************************
- * set configuration number to configuration
- */
- static int usb_set_configuration(struct usb_device *dev, int configuration)
- {
- int res;
- //debug("set configuration %d\n", configuration);
- /* set setup command */
- res = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
- USB_REQ_SET_CONFIGURATION, 0,
- configuration, 0,
- NULL, 0, USB_CNTL_TIMEOUT);
- if (res == 0) {
- dev->toggle[0] = 0;
- dev->toggle[1] = 0;
- return 0;
- } else
- return -EIO;
- }
- static int usb_setup_descriptor(struct usb_device *dev, bool do_read)
- {
- /*
- * This is a Windows scheme of initialization sequence, with double
- * reset of the device (Linux uses the same sequence)
- * Some equipment is said to work only with such init sequence; this
- * patch is based on the work by Alan Stern:
- * http://sourceforge.net/mailarchive/forum.php?
- * thread_id=5729457&forum_id=5398
- */
- /*
- * send 64-byte GET-DEVICE-DESCRIPTOR request. Since the descriptor is
- * only 18 bytes long, this will terminate with a short packet. But if
- * the maxpacket size is 8 or 16 the device may be waiting to transmit
- * some more, or keeps on retransmitting the 8 byte header.
- */
- if (dev->speed == USB_SPEED_LOW) {
- dev->descriptor.bMaxPacketSize0 = 8;
- dev->maxpacketsize = PACKET_SIZE_8;
- } else {
- dev->descriptor.bMaxPacketSize0 = 64;
- dev->maxpacketsize = PACKET_SIZE_64;
- }
- dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
- dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
- if (do_read && dev->speed == USB_SPEED_FULL) {
- int err;
- /*
- * Validate we've received only at least 8 bytes, not that
- * we've received the entire descriptor. The reasoning is:
- * - The code only uses fields in the first 8 bytes, so
- * that's all we need to have fetched at this stage.
- * - The smallest maxpacket size is 8 bytes. Before we know
- * the actual maxpacket the device uses, the USB controller
- * may only accept a single packet. Consequently we are only
- * guaranteed to receive 1 packet (at least 8 bytes) even in
- * a non-error case.
- *
- * At least the DWC2 controller needs to be programmed with
- * the number of packets in addition to the number of bytes.
- * A request for 64 bytes of data with the maxpacket guessed
- * as 64 (above) yields a request for 1 packet.
- */
- err = get_descriptor_len(dev, 64, 8);
- if (err)
- return err;
- }
- dev->epmaxpacketin[0] = dev->descriptor.bMaxPacketSize0;
- dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
- switch (dev->descriptor.bMaxPacketSize0) {
- case 8:
- dev->maxpacketsize = PACKET_SIZE_8;
- break;
- case 16:
- dev->maxpacketsize = PACKET_SIZE_16;
- break;
- case 32:
- dev->maxpacketsize = PACKET_SIZE_32;
- break;
- case 64:
- dev->maxpacketsize = PACKET_SIZE_64;
- break;
- default:
- //printf("%s: invalid max packet size\n", __func__);
- return -EIO;
- }
- return 0;
- }
- int usb_alloc_new_device(void *controller, struct usb_device **devp)
- {
- int i;
- if (dev_index == USB_MAX_DEVICE) {
- SendUartString("ERROR, too many USB Devices\n");
- return -ENOSPC;
- }
- /* default Address is 0, real addresses start with 1 */
- usb_dev[dev_index].devnum = dev_index + 1;
- usb_dev[dev_index].maxchild = 0;
- for (i = 0; i < USB_MAXCHILDREN; i++)
- usb_dev[dev_index].children[i] = NULL;
- usb_dev[dev_index].parent = NULL;
- usb_dev[dev_index].controller = controller;
- dev_index++;
- *devp = &usb_dev[dev_index - 1];
- return 0;
- }
- static int usb_prepare_device(struct usb_device *dev, int addr, bool do_read,
- struct usb_device *parent)
- {
- int err;
- /*
- * Allocate usb 3.0 device context.
- * USB 3.0 (xHCI) protocol tries to allocate device slot
- * and related data structures first. This call does that.
- * Refer to sec 4.3.2 in xHCI spec rev1.0
- */
- err = usb_alloc_device(dev);
- if (err) {
- //printf("Cannot allocate device context to get SLOT_ID\n");
- return err;
- }
- err = usb_setup_descriptor(dev, do_read);
- if (err)
- return err;
- /* err = usb_hub_port_reset(dev, parent);
- if (err)
- return err; */
- dev->devnum = addr;
- err = usb_set_address(dev); /* set address */
- if (err < 0) {
- /* printf("\n USB device not accepting new address " \
- "(error=%lX)\n", dev->status); */
- return err;
- }
- mdelay(10); /* Let the SET_ADDRESS settle */
- /*
- * If we haven't read device descriptor before, read it here
- * after device is assigned an address. This is only applicable
- * to xHCI so far.
- */
- if (!do_read) {
- err = usb_setup_descriptor(dev, true);
- if (err)
- return err;
- }
- return 0;
- }
- int usb_select_config(struct usb_device *dev)
- {
- unsigned char *rawtmpbuf = NULL, *tmpbuf = NULL;
- int err;
- err = get_descriptor_len(dev, USB_DT_DEVICE_SIZE, USB_DT_DEVICE_SIZE);
- if (err)
- return err;
- /*
- * Kingston DT Ultimate 32GB USB 3.0 seems to be extremely sensitive
- * about this first Get Descriptor request. If there are any other
- * requests in the first microframe, the stick crashes. Wait about
- * one microframe duration here (1mS for USB 1.x , 125uS for USB 2.0).
- */
- mdelay(1);
- /* only support for one config for now */
- err = usb_get_configuration_len(dev, 0);
- if (err >= 0) {
- rawtmpbuf = (unsigned char *)malloc(err + ARCH_DMA_MINALIGN - 1);
- if (!rawtmpbuf)
- err = -ENOMEM;
- else {
- tmpbuf = (unsigned char*)ALIGN((uintptr_t)rawtmpbuf, ARCH_DMA_MINALIGN);
- err = usb_get_configuration_no(dev, 0, tmpbuf, err);
- }
- }
- if (err < 0) {
- /* printf("usb_new_device: Cannot read configuration, " \
- "skipping device %04x:%04x\n",
- dev->descriptor.idVendor, dev->descriptor.idProduct); */
- if (rawtmpbuf)
- free(rawtmpbuf);
- return err;
- }
- usb_parse_config(dev, tmpbuf, 0);
- free(rawtmpbuf);
- usb_set_maxpacket(dev);
- /*
- * we set the default configuration here
- * This seems premature. If the driver wants a different configuration
- * it will need to select itself.
- */
- err = usb_set_configuration(dev, dev->config.desc.bConfigurationValue);
- if (err < 0) {
- /* printf("failed to set default configuration " \
- "len %d, status %lX\n", dev->act_len, dev->status); */
- return err;
- }
- /*
- * Wait until the Set Configuration request gets processed by the
- * device. This is required by at least SanDisk Cruzer Pop USB 2.0
- * and Kingston DT Ultimate 32GB USB 3.0 on DWC2 OTG controller.
- */
- mdelay(10);
- /* debug("new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
- dev->descriptor.iManufacturer, dev->descriptor.iProduct,
- dev->descriptor.iSerialNumber); */
- memset(dev->mf, 0, sizeof(dev->mf));
- memset(dev->prod, 0, sizeof(dev->prod));
- memset(dev->serial, 0, sizeof(dev->serial));
- if (dev->descriptor.iManufacturer)
- usb_string(dev, dev->descriptor.iManufacturer,
- dev->mf, sizeof(dev->mf));
- if (dev->descriptor.iProduct)
- usb_string(dev, dev->descriptor.iProduct,
- dev->prod, sizeof(dev->prod));
- if (dev->descriptor.iSerialNumber)
- usb_string(dev, dev->descriptor.iSerialNumber,
- dev->serial, sizeof(dev->serial));
- //debug("Manufacturer %s\n", dev->mf);
- //debug("Product %s\n", dev->prod);
- //debug("SerialNumber %s\n", dev->serial);
- return 0;
- }
- int usb_setup_device(struct usb_device *dev, bool do_read,
- struct usb_device *parent)
- {
- int addr;
- int ret;
- /* We still haven't set the Address yet */
- addr = dev->devnum;
- dev->devnum = 0;
- ret = usb_prepare_device(dev, addr, do_read, parent);
- if (ret)
- return ret;
- ret = usb_select_config(dev);
- return ret;
- }
- bool usb_device_has_child_on_port(struct usb_device *parent, int port)
- {
- return parent->children[port] != NULL;
- }
- static inline bool usb_hub_is_superspeed(struct usb_device *hdev)
- {
- return hdev->descriptor.bDeviceProtocol == 3;
- }
- static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
- {
- unsigned short dtype = USB_DT_HUB;
- if (usb_hub_is_superspeed(dev))
- dtype = USB_DT_SS_HUB;
- return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
- USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
- dtype << 8, 0, data, size, USB_CNTL_TIMEOUT);
- }
- static struct usb_hub_device *usb_get_hub_device(struct usb_device *dev)
- {
- struct usb_hub_device *hub;
- /* "allocate" Hub device */
- hub = usb_hub_allocate();
- return hub;
- }
- static int usb_get_hub_status(struct usb_device *dev, void *data)
- {
- return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
- USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
- data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
- }
- static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
- {
- return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
- USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
- port, NULL, 0, USB_CNTL_TIMEOUT);
- }
- static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
- {
- return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
- USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
- port, NULL, 0, USB_CNTL_TIMEOUT);
- }
- int usb_get_port_status(struct usb_device *dev, int port, void *data)
- {
- int ret;
- ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
- USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
- data, sizeof(struct usb_port_status), USB_CNTL_TIMEOUT);
- return ret;
- }
- static void usb_hub_power_on(struct usb_hub_device *hub)
- {
- int i;
- struct usb_device *dev;
- unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
- //const char *env;
- dev = hub->pusb_dev;
- //debug("enabling power on all ports\n");
- for (i = 0; i < dev->maxchild; i++) {
- usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
- //debug("port %d returns %lX\n", i + 1, dev->status);
- }
- /*
- * Wait for power to become stable,
- * plus spec-defined max time for device to connect
- * but allow this time to be increased via env variable as some
- * devices break the spec and require longer warm-up times
- */
- /* env = env_get("usb_pgood_delay");
- if (env)
- pgood_delay = max(pgood_delay,
- (unsigned)simple_strtol(env, NULL, 0));
- debug("pgood_delay=%dms\n", pgood_delay); */
- /*
- * Do a minimum delay of the larger value of 100ms or pgood_delay
- * so that the power can stablize before the devices are queried
- */
- hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
- /*
- * Record the power-on timeout here. The max. delay (timeout)
- * will be done based on this value in the USB port loop in
- * usb_hub_configure() later.
- */
- hub->connect_timeout = hub->query_delay + 1000;
- /* debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
- dev->devnum, max(100, (int)pgood_delay),
- max(100, (int)pgood_delay) + 1000); */
- }
- #define MAX_TRIES 5
- /**
- * usb_hub_port_reset() - reset a port given its usb_device pointer
- *
- * Reset a hub port and see if a device is present on that port, providing
- * sufficient time for it to show itself. The port status is returned.
- *
- * @dev: USB device to reset
- * @port: Port number to reset (note ports are numbered from 0 here)
- * @portstat: Returns port status
- */
- static int usb_hub_port_reset(struct usb_device *dev, int port,
- unsigned short *portstat)
- {
- int err, tries;
- ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
- unsigned short portstatus, portchange;
- int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
- //debug("%s: resetting port %d...\n", __func__, port + 1);
- for (tries = 0; tries < MAX_TRIES; tries++) {
- err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
- if (err < 0)
- return err;
- mdelay(delay);
- if (usb_get_port_status(dev, port + 1, portsts) < 0) {
- /* debug("get_port_status failed status %lX\n",
- dev->status); */
- return -1;
- }
- portstatus = le16_to_cpu(portsts->wPortStatus);
- portchange = le16_to_cpu(portsts->wPortChange);
- (void)portchange;
- /* debug("portstatus %x, change %x, %s\n", portstatus, portchange,
- portspeed(portstatus));
- debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
- " USB_PORT_STAT_ENABLE %d\n",
- (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
- (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
- (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0); */
- /*
- * Perhaps we should check for the following here:
- * - C_CONNECTION hasn't been set.
- * - CONNECTION is still set.
- *
- * Doing so would ensure that the device is still connected
- * to the bus, and hasn't been unplugged or replaced while the
- * USB bus reset was going on.
- *
- * However, if we do that, then (at least) a San Disk Ultra
- * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
- * Tegra Jetson TK1 board. For some reason, the device appears
- * to briefly drop off the bus when this second bus reset is
- * executed, yet if we retry this loop, it'll eventually come
- * back after another reset or two.
- */
- if (portstatus & USB_PORT_STAT_ENABLE)
- break;
- /* Switch to long reset delay for the next round */
- delay = HUB_LONG_RESET_TIME;
- }
- if (tries == MAX_TRIES) {
- /* debug("Cannot enable port %i after %i retries, " \
- "disabling port.\n", port + 1, MAX_TRIES);
- debug("Maybe the USB cable is bad?\n"); */
- return -1;
- }
- usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
- *portstat = portstatus;
- return 0;
- }
- int usb_new_device(struct usb_device *dev);
- int usb_hub_port_connect_change(struct usb_device *dev, int port)
- {
- ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
- unsigned short portstatus;
- int ret, speed;
- /* Check status */
- ret = usb_get_port_status(dev, port + 1, portsts);
- if (ret < 0) {
- //debug("get_port_status failed\n");
- return ret;
- }
- portstatus = portsts->wPortStatus;
- /* debug("portstatus %x, change %x, %s\n",
- portstatus,
- le16_to_cpu(portsts->wPortChange),
- portspeed(portstatus)); */
- /* Clear the connection change status */
- usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
- /* Disconnect any existing devices under this port */
- if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
- (!(portstatus & USB_PORT_STAT_ENABLE))) ||
- usb_device_has_child_on_port(dev, port)) {
- //debug("usb_disconnect(&hub->children[port]);\n");
- /* Return now if nothing is connected */
- if (!(portstatus & USB_PORT_STAT_CONNECTION))
- return -ENOTCONN;
- }
- /* Reset the port */
- ret = usb_hub_port_reset(dev, port, &portstatus);
- if (ret < 0) {
- if (ret != -ENXIO)
- ;//printf("cannot reset port %i!?\n", port + 1);
- return ret;
- }
- switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
- case USB_PORT_STAT_SUPER_SPEED:
- speed = USB_SPEED_SUPER;
- break;
- case USB_PORT_STAT_HIGH_SPEED:
- speed = USB_SPEED_HIGH;
- break;
- case USB_PORT_STAT_LOW_SPEED:
- speed = USB_SPEED_LOW;
- break;
- default:
- speed = USB_SPEED_FULL;
- break;
- }
- struct usb_device *usb;
- ret = usb_alloc_new_device(dev->controller, &usb);
- if (ret) {
- //printf("cannot create new device: ret=%d", ret);
- return ret;
- }
- dev->children[port] = usb;
- usb->speed = speed;
- usb->parent = dev;
- usb->portnr = port + 1;
- /* Run it through the hoops (find a driver, etc) */
- ret = usb_new_device(usb);
- if (ret < 0) {
- /* Woops, disable the port */
- usb_free_device(dev->controller);
- dev->children[port] = NULL;
- }
- if (ret < 0) {
- //debug("hub: disabling port %d\n", port + 1);
- usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
- }
- return ret;
- }
- static int usb_scan_port(struct usb_device_scan *usb_scan)
- {
- ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
- unsigned short portstatus;
- unsigned short portchange;
- struct usb_device *dev;
- struct usb_hub_device *hub;
- int ret = 0;
- int i;
- dev = usb_scan->dev;
- hub = usb_scan->hub;
- i = usb_scan->port;
- /*
- * Don't talk to the device before the query delay is expired.
- * This is needed for voltages to stabalize.
- */
- if (get_timer(0) < hub->query_delay)
- return 0;
- ret = usb_get_port_status(dev, i + 1, portsts);
- if (ret < 0) {
- //debug("get_port_status failed\n");
- if (get_timer(0) >= hub->connect_timeout) {
- /* debug("devnum=%d port=%d: timeout\n",
- dev->devnum, i + 1); */
- /* Remove this device from scanning list */
- list_del(&usb_scan->list);
- free(usb_scan);
- return 0;
- }
- return 0;
- }
- portstatus = portsts->wPortStatus;
- portchange = portsts->wPortChange;
- //debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
- /*
- * No connection change happened, wait a bit more.
- *
- * For some situation, the hub reports no connection change but a
- * device is connected to the port (eg: CCS bit is set but CSC is not
- * in the PORTSC register of a root hub), ignore such case.
- */
- if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
- !(portstatus & USB_PORT_STAT_CONNECTION)) {
- if (get_timer(0) >= hub->connect_timeout) {
- /* debug("devnum=%d port=%d: timeout\n",
- dev->devnum, i + 1); */
- /* Remove this device from scanning list */
- list_del(&usb_scan->list);
- free(usb_scan);
- return 0;
- }
- return 0;
- }
- if (portchange & USB_PORT_STAT_C_RESET) {
- //debug("port %d reset change\n", i + 1);
- usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
- }
- if ((portchange & USB_SS_PORT_STAT_C_BH_RESET) &&
- usb_hub_is_superspeed(dev)) {
- //debug("port %d BH reset change\n", i + 1);
- usb_clear_port_feature(dev, i + 1, USB_SS_PORT_FEAT_C_BH_RESET);
- }
- /* A new USB device is ready at this point */
- //debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
- usb_hub_port_connect_change(dev, i);
- if (portchange & USB_PORT_STAT_C_ENABLE) {
- //debug("port %d enable change, status %x\n", i + 1, portstatus);
- usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
- /*
- * The following hack causes a ghost device problem
- * to Faraday EHCI
- */
- /*
- * EM interference sometimes causes bad shielded USB
- * devices to be shutdown by the hub, this hack enables
- * them again. Works at least with mouse driver
- */
- if (!(portstatus & USB_PORT_STAT_ENABLE) &&
- (portstatus & USB_PORT_STAT_CONNECTION) &&
- usb_device_has_child_on_port(dev, i)) {
- /* debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
- i + 1); */
- usb_hub_port_connect_change(dev, i);
- }
- }
- if (portstatus & USB_PORT_STAT_SUSPEND) {
- //debug("port %d suspend change\n", i + 1);
- usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
- }
- if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
- //debug("port %d over-current change\n", i + 1);
- usb_clear_port_feature(dev, i + 1,
- USB_PORT_FEAT_C_OVER_CURRENT);
- /* Only power-on this one port */
- usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
- hub->overcurrent_count[i]++;
- /*
- * If the max-scan-count is not reached, return without removing
- * the device from scan-list. This will re-issue a new scan.
- */
- if (hub->overcurrent_count[i] <=
- PORT_OVERCURRENT_MAX_SCAN_COUNT)
- return 0;
- /* Otherwise the device will get removed */
- /* printf("Port %d over-current occurred %d times\n", i + 1,
- hub->overcurrent_count[i]); */
- }
- /*
- * We're done with this device, so let's remove this device from
- * scanning list
- */
- list_del(&usb_scan->list);
- free(usb_scan);
- return 0;
- }
- static int usb_device_list_scan(void)
- {
- struct usb_device_scan *usb_scan;
- struct usb_device_scan *tmp;
- static int running;
- int ret = 0;
- /* Only run this loop once for each controller */
- if (running)
- return 0;
- running = 1;
- while (1) {
- /* We're done, once the list is empty again */
- if (list_empty(&usb_scan_list))
- goto out;
- for (usb_scan = (struct usb_device_scan*)((&usb_scan_list)->next),
- tmp = (struct usb_device_scan*)(usb_scan->list.next);
- &usb_scan->list != &usb_scan_list;
- usb_scan = tmp, tmp = (struct usb_device_scan*)(tmp->list.next)) {
- int ret;
- /* Scan this port */
- ret = usb_scan_port(usb_scan);
- if (ret)
- goto out;
- }
- }
- out:
- /*
- * This USB controller has finished scanning all its connected
- * USB devices. Set "running" back to 0, so that other USB controllers
- * will scan their devices too.
- */
- running = 0;
- return ret;
- }
- static int usb_hub_configure(struct usb_device *dev)
- {
- int i, length;
- ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
- unsigned char *bitmap;
- short hubCharacteristics;
- struct usb_hub_descriptor *descriptor;
- struct usb_hub_device *hub;
- //struct usb_hub_status *hubsts;
- unsigned char *tmp;
- unsigned short tmpval;
- int ret;
- hub = usb_get_hub_device(dev);
- if (hub == NULL)
- return -ENOMEM;
- hub->pusb_dev = dev;
- /* Get the the hub descriptor */
- ret = usb_get_hub_descriptor(dev, buffer, 4);
- if (ret < 0) {
- /* debug("usb_hub_configure: failed to get hub " \
- "descriptor, giving up %lX\n", dev->status); */
- return ret;
- }
- descriptor = (struct usb_hub_descriptor *)buffer;
- length = min(descriptor->bLength,
- sizeof(struct usb_hub_descriptor));
- ret = usb_get_hub_descriptor(dev, buffer, length);
- if (ret < 0) {
- /* debug("usb_hub_configure: failed to get hub " \
- "descriptor 2nd giving up %lX\n", dev->status); */
- return ret;
- }
- memcpy((unsigned char *)&hub->desc, buffer, length);
- /* adjust 16bit values */
- /* put_unaligned(le16_to_cpu(get_unaligned(
- &descriptor->wHubCharacteristics)),
- &hub->desc.wHubCharacteristics); */
- tmp = (unsigned char*)&descriptor->wHubCharacteristics;
- tmpval = (tmp[1] << 8) | tmp[0];
- tmp = (unsigned char*)&hub->desc.wHubCharacteristics;
- tmp[0] = tmpval & 0xff;
- tmp[1] = (tmpval >> 8) & 0xff;
-
- /* set the bitmap */
- bitmap = (unsigned char *)&hub->desc.u.hs.DeviceRemovable[0];
- /* devices not removable by default */
- memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
- bitmap = (unsigned char *)&hub->desc.u.hs.PortPowerCtrlMask[0];
- memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
- for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
- hub->desc.u.hs.DeviceRemovable[i] =
- descriptor->u.hs.DeviceRemovable[i];
- for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
- hub->desc.u.hs.PortPowerCtrlMask[i] =
- descriptor->u.hs.PortPowerCtrlMask[i];
- dev->maxchild = descriptor->bNbrPorts;
- //debug("%d ports detected\n", dev->maxchild);
- tmp = (unsigned char*)&hub->desc.wHubCharacteristics;
- hubCharacteristics = (tmp[1] << 8) | tmp[0];//get_unaligned(&hub->desc.wHubCharacteristics);
- switch (hubCharacteristics & HUB_CHAR_LPSM) {
- case 0x00:
- //debug("ganged power switching\n");
- break;
- case 0x01:
- //debug("individual port power switching\n");
- break;
- case 0x02:
- case 0x03:
- //debug("unknown reserved power switching mode\n");
- break;
- }
- if (hubCharacteristics & HUB_CHAR_COMPOUND)
- ;//debug("part of a compound device\n");
- else
- ;//debug("standalone hub\n");
- switch (hubCharacteristics & HUB_CHAR_OCPM) {
- case 0x00:
- //debug("global over-current protection\n");
- break;
- case 0x08:
- //debug("individual port over-current protection\n");
- break;
- case 0x10:
- case 0x18:
- //debug("no over-current protection\n");
- break;
- }
- switch (dev->descriptor.bDeviceProtocol) {
- case USB_HUB_PR_FS:
- break;
- case USB_HUB_PR_HS_SINGLE_TT:
- //debug("Single TT\n");
- break;
- case USB_HUB_PR_HS_MULTI_TT:
- ret = usb_set_interface(dev, 0, 1);
- if (ret == 0) {
- //debug("TT per port\n");
- hub->tt.multi = true;
- } else {
- ;//debug("Using single TT (err %d)\n", ret);
- }
- break;
- case USB_HUB_PR_SS:
- /* USB 3.0 hubs don't have a TT */
- break;
- default:
- /* debug("Unrecognized hub protocol %d\n",
- dev->descriptor.bDeviceProtocol); */
- break;
- }
- /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
- switch (hubCharacteristics & HUB_CHAR_TTTT) {
- case HUB_TTTT_8_BITS:
- if (dev->descriptor.bDeviceProtocol != 0) {
- hub->tt.think_time = 666;
- /* debug("TT requires at most %d FS bit times (%d ns)\n",
- 8, hub->tt.think_time); */
- }
- break;
- case HUB_TTTT_16_BITS:
- hub->tt.think_time = 666 * 2;
- /* debug("TT requires at most %d FS bit times (%d ns)\n",
- 16, hub->tt.think_time); */
- break;
- case HUB_TTTT_24_BITS:
- hub->tt.think_time = 666 * 3;
- /* debug("TT requires at most %d FS bit times (%d ns)\n",
- 24, hub->tt.think_time); */
- break;
- case HUB_TTTT_32_BITS:
- hub->tt.think_time = 666 * 4;
- /* debug("TT requires at most %d FS bit times (%d ns)\n",
- 32, hub->tt.think_time); */
- break;
- }
- /* debug("power on to power good time: %dms\n",
- descriptor->bPwrOn2PwrGood * 2);
- debug("hub controller current requirement: %dmA\n",
- descriptor->bHubContrCurrent);
- for (i = 0; i < dev->maxchild; i++)
- debug("port %d is%s removable\n", i + 1,
- hub->desc.u.hs.DeviceRemovable[(i + 1) / 8] & \
- (1 << ((i + 1) % 8)) ? " not" : ""); */
- if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
- /* debug("usb_hub_configure: failed to get Status - " \
- "too long: %d\n", descriptor->bLength); */
- return -EFBIG;
- }
- ret = usb_get_hub_status(dev, buffer);
- if (ret < 0) {
- /* debug("usb_hub_configure: failed to get Status %lX\n",
- dev->status); */
- return ret;
- }
- /* hubsts = (struct usb_hub_status *)buffer;
- debug("get_hub_status returned status %X, change %X\n",
- le16_to_cpu(hubsts->wHubStatus),
- le16_to_cpu(hubsts->wHubChange));
- debug("local power source is %s\n",
- (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
- "lost (inactive)" : "good");
- debug("%sover-current condition exists\n",
- (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
- "" : "no "); */
-
- usb_hub_power_on(hub);
- /*
- * Reset any devices that may be in a bad state when applying
- * the power. This is a __weak function. Resetting of the devices
- * should occur in the board file of the device.
- */
- /* for (i = 0; i < dev->maxchild; i++)
- usb_hub_reset_devices(hub, i + 1); */
- /*
- * Only add the connected USB devices, including potential hubs,
- * to a scanning list. This list will get scanned and devices that
- * are detected (either via port connected or via port timeout)
- * will get removed from this list. Scanning of the devices on this
- * list will continue until all devices are removed.
- */
- for (i = 0; i < dev->maxchild; i++) {
- struct usb_device_scan *usb_scan;
- usb_scan = calloc(1, sizeof(*usb_scan));
- if (!usb_scan) {
- //printf("Can't allocate memory for USB device!\n");
- return -ENOMEM;
- }
- usb_scan->dev = dev;
- usb_scan->hub = hub;
- usb_scan->port = i;
- list_add_tail(&usb_scan->list, &usb_scan_list);
- }
- /*
- * And now call the scanning code which loops over the generated list
- */
- ret = usb_device_list_scan();
- return ret;
- }
- static int usb_hub_check(struct usb_device *dev, int ifnum)
- {
- struct usb_interface *iface;
- struct usb_endpoint_descriptor *ep = NULL;
- iface = &dev->config.if_desc[ifnum];
- /* Is it a hub? */
- if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
- goto err;
- /* Some hubs have a subclass of 1, which AFAICT according to the */
- /* specs is not defined, but it works */
- if ((iface->desc.bInterfaceSubClass != 0) &&
- (iface->desc.bInterfaceSubClass != 1))
- goto err;
- /* Multiple endpoints? What kind of mutant ninja-hub is this? */
- if (iface->desc.bNumEndpoints != 1)
- goto err;
- ep = &iface->ep_desc[0];
- /* Output endpoint? Curiousier and curiousier.. */
- if (!(ep->bEndpointAddress & USB_DIR_IN))
- goto err;
- /* If it's not an interrupt endpoint, we'd better punt! */
- if ((ep->bmAttributes & 3) != 3)
- goto err;
- /* We found a hub */
- //debug("USB hub found\n");
- return 0;
- err:
- /* debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
- iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
- iface->desc.bNumEndpoints);
- if (ep) {
- debug(" bEndpointAddress=%#x, bmAttributes=%d",
- ep->bEndpointAddress, ep->bmAttributes);
- } */
- return -ENOENT;
- }
- int usb_hub_probe(struct usb_device *dev, int ifnum)
- {
- int ret;
- ret = usb_hub_check(dev, ifnum);
- if (ret)
- return 0;
- ret = usb_hub_configure(dev);
- return ret;
- }
- int usb_new_device(struct usb_device *dev)
- {
- bool do_read = true;
- int err;
- err = usb_setup_device(dev, do_read, dev->parent);
- if (err)
- return err;
- /* Now probe if the device is a hub */
- err = usb_hub_probe(dev, 0);
- if (err < 0)
- return err;
- return 0;
- }
- /***************************************************************************
- * Init USB Device
- */
- int usb_init(void)
- {
- void *ctrl;
- struct usb_device *dev;
- int i, start_index = 0;
- int controllers_initialized = 0;
- int ret;
- dev_index = 0;
- asynch_allowed = 1;
- usb_hub_reset();
- /* first make all devices unknown */
- for (i = 0; i < USB_MAX_DEVICE; i++) {
- memset(&usb_dev[i], 0, sizeof(struct usb_device));
- usb_dev[i].devnum = -1;
- }
- /* init low_level USB */
- for (i = 0; i < CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
- /* init low_level USB */
- ret = usb_lowlevel_init(i, USB_INIT_HOST, &ctrl);
- if (ret == -ENODEV) { /* No such device. */
- SendUartString("Port not available.\n");
- controllers_initialized++;
- continue;
- }
- if (ret) { /* Other error. */
- SendUartString("lowlevel init failed\n");
- continue;
- }
- /*
- * lowlevel init is OK, now scan the bus for devices
- * i.e. search HUBs and configure them
- */
- controllers_initialized++;
- start_index = dev_index;
- ret = usb_alloc_new_device(ctrl, &dev);
- if (ret)
- break;
- /*
- * device 0 is always present
- * (root hub, so let it analyze)
- */
- ret = usb_new_device(dev);
- if (ret)
- usb_free_device(dev->controller);
- if (start_index == dev_index) {
- SendUartString("No USB Device found\n");
- continue;
- } else {
- SendUartString("USB Device(s) found\n");
- }
- usb_started = 1;
- }
- /* if we were not able to find at least one working bus, bail out */
- if (controllers_initialized == 0)
- SendUartString("USB error: all controllers failed lowlevel init\n");
- return usb_started ? 0 : -ENODEV;
- }
- /*
- * disables the asynch behaviour of the control message. This is used for data
- * transfers that uses the exclusiv access to the control and bulk messages.
- * Returns the old value so it can be restored later.
- */
- int usb_disable_asynch(int disable)
- {
- int old_value = asynch_allowed;
- asynch_allowed = !disable;
- return old_value;
- }
- /***********************************************************************
- * Data transfer routines
- ***********************************************************************/
- static int us_one_transfer(struct us_data *us, int pipe, char *buf, int length)
- {
- int max_size;
- int this_xfer;
- int result;
- int partial;
- int maxtry;
- int stat;
- /* determine the maximum packet size for these transfers */
- max_size = usb_maxpacket(us->pusb_dev, pipe) * 16;
- /* while we have data left to transfer */
- while (length) {
- /* calculate how long this will be -- maximum or a remainder */
- this_xfer = length > max_size ? max_size : length;
- length -= this_xfer;
- /* setup the retry counter */
- maxtry = 10;
- /* set up the transfer loop */
- do {
- /* transfer the data */
- /* debug("Bulk xfer 0x%lx(%d) try #%d\n",
- (ulong)map_to_sysmem(buf), this_xfer,
- 11 - maxtry); */
- result = usb_bulk_msg(us->pusb_dev, pipe, buf,
- this_xfer, &partial,
- USB_CNTL_TIMEOUT * 5);
- /* debug("bulk_msg returned %d xferred %d/%d\n",
- result, partial, this_xfer); */
- if (us->pusb_dev->status != 0) {
- /* if we stall, we need to clear it before
- * we go on
- */
- if (us->pusb_dev->status & USB_ST_STALLED) {
- /* debug("stalled ->clearing endpoint" \
- "halt for pipe 0x%x\n", pipe); */
- stat = us->pusb_dev->status;
- usb_clear_halt(us->pusb_dev, pipe);
- us->pusb_dev->status = stat;
- if (this_xfer == partial) {
- /* debug("bulk transferred" \
- "with error %lX," \
- " but data ok\n",
- us->pusb_dev->status); */
- return 0;
- }
- else
- return result;
- }
- if (us->pusb_dev->status & USB_ST_NAK_REC) {
- //debug("Device NAKed bulk_msg\n");
- return result;
- }
- //debug("bulk transferred with error");
- if (this_xfer == partial) {
- /* debug(" %ld, but data ok\n",
- us->pusb_dev->status); */
- return 0;
- }
- /* if our try counter reaches 0, bail out */
- /* debug(" %ld, data %d\n",
- us->pusb_dev->status, partial); */
- if (!maxtry--)
- return result;
- }
- /* update to show what data was transferred */
- this_xfer -= partial;
- buf += partial;
- /* continue until this transfer is done */
- } while (this_xfer);
- }
- /* if we get here, we're done and successful */
- return 0;
- }
- static unsigned int usb_get_max_lun(struct us_data *us)
- {
- int len;
- ALLOC_CACHE_ALIGN_BUFFER(unsigned char, result, 1);
- len = usb_control_msg(us->pusb_dev,
- usb_rcvctrlpipe(us->pusb_dev, 0),
- US_BBB_GET_MAX_LUN,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
- 0, us->ifnum,
- result, sizeof(char),
- USB_CNTL_TIMEOUT * 5);
- //debug("Get Max LUN -> len = %i, result = %i\n", len, (int) *result);
- return (len > 0) ? *result : 0;
- }
- static int usb_stor_BBB_reset(struct us_data *us)
- {
- int result;
- unsigned int pipe;
- /*
- * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
- *
- * For Reset Recovery the host shall issue in the following order:
- * a) a Bulk-Only Mass Storage Reset
- * b) a Clear Feature HALT to the Bulk-In endpoint
- * c) a Clear Feature HALT to the Bulk-Out endpoint
- *
- * This is done in 3 steps.
- *
- * If the reset doesn't succeed, the device should be port reset.
- *
- * This comment stolen from FreeBSD's /sys/dev/usb/umass.c.
- */
- //debug("BBB_reset\n");
- result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
- US_BBB_RESET,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE,
- 0, us->ifnum, NULL, 0, USB_CNTL_TIMEOUT * 5);
- if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
- //debug("RESET:stall\n");
- return -1;
- }
- /* long wait for reset */
- mdelay(150);
- /* debug("BBB_reset result %d: status %lX reset\n",
- result, us->pusb_dev->status); */
- pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
- result = usb_clear_halt(us->pusb_dev, pipe);
- /* long wait for reset */
- mdelay(150);
- /* debug("BBB_reset result %d: status %lX clearing IN endpoint\n",
- result, us->pusb_dev->status); */
- /* long wait for reset */
- pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
- result = usb_clear_halt(us->pusb_dev, pipe);
- mdelay(150);
- /* debug("BBB_reset result %d: status %lX clearing OUT endpoint\n",
- result, us->pusb_dev->status);
- debug("BBB_reset done\n"); */
- return 0;
- }
- /* FIXME: this reset function doesn't really reset the port, and it
- * should. Actually it should probably do what it's doing here, and
- * reset the port physically
- */
- static int usb_stor_CB_reset(struct us_data *us)
- {
- unsigned char cmd[12];
- int result;
- //debug("CB_reset\n");
- memset(cmd, 0xff, sizeof(cmd));
- cmd[0] = SCSI_SEND_DIAG;
- cmd[1] = 4;
- result = usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
- US_CBI_ADSC,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE,
- 0, us->ifnum, cmd, sizeof(cmd),
- USB_CNTL_TIMEOUT * 5);
- (void)result;
- /* long wait for reset */
- mdelay(1500);
- /* debug("CB_reset result %d: status %lX clearing endpoint halt\n",
- result, us->pusb_dev->status); */
- usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_in));
- usb_clear_halt(us->pusb_dev, usb_rcvbulkpipe(us->pusb_dev, us->ep_out));
- //debug("CB_reset done\n");
- return 0;
- }
- /*
- * Set up the command for a BBB device. Note that the actual SCSI
- * command is copied into cbw.CBWCDB.
- */
- static int usb_stor_BBB_comdat(struct scsi_cmd *srb, struct us_data *us)
- {
- int result;
- int actlen;
- int dir_in;
- unsigned int pipe;
- ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_cbw, cbw, 1);
- dir_in = US_DIRECTION(srb->cmd[0]);
- /* sanity checks */
- if (!(srb->cmdlen <= CBWCDBLENGTH)) {
- //debug("usb_stor_BBB_comdat:cmdlen too large\n");
- return -1;
- }
- /* always OUT to the ep */
- pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
- cbw->dCBWSignature = cpu_to_le32(CBWSIGNATURE);
- cbw->dCBWTag = cpu_to_le32(CBWTag++);
- cbw->dCBWDataTransferLength = cpu_to_le32(srb->datalen);
- cbw->bCBWFlags = (dir_in ? CBWFLAGS_IN : CBWFLAGS_OUT);
- cbw->bCBWLUN = srb->lun;
- cbw->bCDBLength = srb->cmdlen;
- /* copy the command data into the CBW command data buffer */
- /* DST SRC LEN!!! */
- memcpy(cbw->CBWCDB, srb->cmd, srb->cmdlen);
- result = usb_bulk_msg(us->pusb_dev, pipe, cbw, UMASS_BBB_CBW_SIZE,
- &actlen, USB_CNTL_TIMEOUT * 5);
- if (result < 0)
- ;//debug("usb_stor_BBB_comdat:usb_bulk_msg error\n");
- return result;
- }
- /* FIXME: we also need a CBI_command which sets up the completion
- * interrupt, and waits for it
- */
- static int usb_stor_CB_comdat(struct scsi_cmd *srb, struct us_data *us)
- {
- int result = 0;
- int dir_in, retry;
- unsigned int pipe;
- unsigned long status;
- retry = 5;
- dir_in = US_DIRECTION(srb->cmd[0]);
- if (dir_in)
- pipe = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
- else
- pipe = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
- while (retry--) {
- //debug("CBI gets a command: Try %d\n", 5 - retry);
- /* let's send the command via the control pipe */
- result = usb_control_msg(us->pusb_dev,
- usb_sndctrlpipe(us->pusb_dev , 0),
- US_CBI_ADSC,
- USB_TYPE_CLASS | USB_RECIP_INTERFACE,
- 0, us->ifnum,
- srb->cmd, srb->cmdlen,
- USB_CNTL_TIMEOUT * 5);
- /* debug("CB_transport: control msg returned %d, status %lX\n",
- result, us->pusb_dev->status); */
- /* check the return code for the command */
- if (result < 0) {
- if (us->pusb_dev->status & USB_ST_STALLED) {
- status = us->pusb_dev->status;
- /* debug(" stall during command found," \
- " clear pipe\n"); */
- usb_clear_halt(us->pusb_dev,
- usb_sndctrlpipe(us->pusb_dev, 0));
- us->pusb_dev->status = status;
- }
- /* debug(" error during command %02X" \
- " Stat = %lX\n", srb->cmd[0],
- us->pusb_dev->status); */
- return result;
- }
- /* transfer the data payload for this command, if one exists*/
- /* debug("CB_transport: control msg returned %d," \
- " direction is %s to go 0x%lx\n", result,
- dir_in ? "IN" : "OUT", srb->datalen); */
- if (srb->datalen) {
- result = us_one_transfer(us, pipe, (char *)srb->pdata,
- srb->datalen);
- /* debug("CBI attempted to transfer data," \
- " result is %d status %lX, len %d\n",
- result, us->pusb_dev->status,
- us->pusb_dev->act_len); */
- if (!(us->pusb_dev->status & USB_ST_NAK_REC))
- break;
- } /* if (srb->datalen) */
- else
- break;
- }
- /* return result */
- return result;
- }
- static int usb_stor_CBI_get_status(struct scsi_cmd *srb, struct us_data *us)
- {
- int timeout;
- us->ip_wanted = 1;
- submit_int_msg(us->pusb_dev, us->irqpipe,
- (void *) &us->ip_data, us->irqmaxp, us->irqinterval);
- timeout = 1000;
- while (timeout--) {
- if (us->ip_wanted == 0)
- break;
- mdelay(10);
- }
- if (us->ip_wanted) {
- //printf(" Did not get interrupt on CBI\n");
- us->ip_wanted = 0;
- return USB_STOR_TRANSPORT_ERROR;
- }
- /* debug("Got interrupt data 0x%x, transferred %d status 0x%lX\n",
- us->ip_data, us->pusb_dev->irq_act_len,
- us->pusb_dev->irq_status); */
- /* UFI gives us ASC and ASCQ, like a request sense */
- if (us->subclass == US_SC_UFI) {
- if (srb->cmd[0] == SCSI_REQ_SENSE ||
- srb->cmd[0] == SCSI_INQUIRY)
- return USB_STOR_TRANSPORT_GOOD; /* Good */
- else if (us->ip_data)
- return USB_STOR_TRANSPORT_FAILED;
- else
- return USB_STOR_TRANSPORT_GOOD;
- }
- /* otherwise, we interpret the data normally */
- switch (us->ip_data) {
- case 0x0001:
- return USB_STOR_TRANSPORT_GOOD;
- case 0x0002:
- return USB_STOR_TRANSPORT_FAILED;
- default:
- return USB_STOR_TRANSPORT_ERROR;
- } /* switch */
- //return USB_STOR_TRANSPORT_ERROR;
- }
- #define USB_TRANSPORT_UNKNOWN_RETRY 5
- #define USB_TRANSPORT_NOT_READY_RETRY 10
- /* clear a stall on an endpoint - special for BBB devices */
- static int usb_stor_BBB_clear_endpt_stall(struct us_data *us, u8 endpt)
- {
- /* ENDPOINT_HALT = 0, so set value to 0 */
- return usb_control_msg(us->pusb_dev, usb_sndctrlpipe(us->pusb_dev, 0),
- USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0,
- endpt, NULL, 0, USB_CNTL_TIMEOUT * 5);
- }
- static int usb_stor_BBB_transport(struct scsi_cmd *srb, struct us_data *us)
- {
- int result, retry;
- int dir_in;
- int actlen, data_actlen;
- unsigned int pipe, pipein, pipeout;
- ALLOC_CACHE_ALIGN_BUFFER(struct umass_bbb_csw, csw, 1);
- dir_in = US_DIRECTION(srb->cmd[0]);
- /* COMMAND phase */
- //debug("COMMAND phase\n");
- result = usb_stor_BBB_comdat(srb, us);
- if (result < 0) {
- /* debug("failed to send CBW status %ld\n",
- us->pusb_dev->status); */
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- }
- if (!(us->flags & USB_READY))
- mdelay(5);
- pipein = usb_rcvbulkpipe(us->pusb_dev, us->ep_in);
- pipeout = usb_sndbulkpipe(us->pusb_dev, us->ep_out);
- /* DATA phase + error handling */
- data_actlen = 0;
- /* no data, go immediately to the STATUS phase */
- if (srb->datalen == 0)
- goto st;
- //debug("DATA phase\n");
- if (dir_in)
- pipe = pipein;
- else
- pipe = pipeout;
- result = usb_bulk_msg(us->pusb_dev, pipe, srb->pdata, srb->datalen,
- &data_actlen, USB_CNTL_TIMEOUT * 5);
- /* special handling of STALL in DATA phase */
- if ((result < 0) && (us->pusb_dev->status & USB_ST_STALLED)) {
- //debug("DATA:stall\n");
- /* clear the STALL on the endpoint */
- result = usb_stor_BBB_clear_endpt_stall(us,
- dir_in ? us->ep_in : us->ep_out);
- if (result >= 0)
- /* continue on to STATUS phase */
- goto st;
- }
- if (result < 0) {
- /* debug("usb_bulk_msg error status %ld\n",
- us->pusb_dev->status); */
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- }
- /* STATUS phase + error handling */
- st:
- retry = 0;
- again:
- //debug("STATUS phase\n");
- result = usb_bulk_msg(us->pusb_dev, pipein, csw, UMASS_BBB_CSW_SIZE,
- &actlen, USB_CNTL_TIMEOUT*5);
- /* special handling of STALL in STATUS phase */
- if ((result < 0) && (retry < 1) &&
- (us->pusb_dev->status & USB_ST_STALLED)) {
- //debug("STATUS:stall\n");
- /* clear the STALL on the endpoint */
- result = usb_stor_BBB_clear_endpt_stall(us, us->ep_in);
- if (result >= 0 && (retry++ < 1))
- /* do a retry */
- goto again;
- }
- if (result < 0) {
- /* debug("usb_bulk_msg error status %ld\n",
- us->pusb_dev->status); */
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- }
- /* misuse pipe to get the residue */
- pipe = le32_to_cpu(csw->dCSWDataResidue);
- if (pipe == 0 && srb->datalen != 0 && srb->datalen - data_actlen != 0)
- pipe = srb->datalen - data_actlen;
- if (CSWSIGNATURE != le32_to_cpu(csw->dCSWSignature)) {
- //debug("!CSWSIGNATURE\n");
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- } else if ((CBWTag - 1) != le32_to_cpu(csw->dCSWTag)) {
- //debug("!Tag\n");
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- } else if (csw->bCSWStatus > CSWSTATUS_PHASE) {
- //debug(">PHASE\n");
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- } else if (csw->bCSWStatus == CSWSTATUS_PHASE) {
- //debug("=PHASE\n");
- usb_stor_BBB_reset(us);
- return USB_STOR_TRANSPORT_FAILED;
- } else if (data_actlen > srb->datalen) {
- /* debug("transferred %dB instead of %ldB\n",
- data_actlen, srb->datalen); */
- return USB_STOR_TRANSPORT_FAILED;
- } else if (csw->bCSWStatus == CSWSTATUS_FAILED) {
- //debug("FAILED\n");
- return USB_STOR_TRANSPORT_FAILED;
- }
- return result;
- }
- static int usb_stor_CB_transport(struct scsi_cmd *srb, struct us_data *us)
- {
- int result, status;
- struct scsi_cmd *psrb;
- struct scsi_cmd reqsrb;
- int retry, notready;
- psrb = &reqsrb;
- status = USB_STOR_TRANSPORT_GOOD;
- retry = 0;
- notready = 0;
- /* issue the command */
- do_retry:
- result = usb_stor_CB_comdat(srb, us);
- /* debug("command / Data returned %d, status %lX\n",
- result, us->pusb_dev->status); */
- /* if this is an CBI Protocol, get IRQ */
- if (us->protocol == US_PR_CBI) {
- status = usb_stor_CBI_get_status(srb, us);
- /* if the status is error, report it */
- if (status == USB_STOR_TRANSPORT_ERROR) {
- //debug(" USB CBI Command Error\n");
- return status;
- }
- srb->sense_buf[12] = (unsigned char)(us->ip_data >> 8);
- srb->sense_buf[13] = (unsigned char)(us->ip_data & 0xff);
- if (!us->ip_data) {
- /* if the status is good, report it */
- if (status == USB_STOR_TRANSPORT_GOOD) {
- //debug(" USB CBI Command Good\n");
- return status;
- }
- }
- }
- /* do we have to issue an auto request? */
- /* HERE we have to check the result */
- if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
- //debug("ERROR %lX\n", us->pusb_dev->status);
- us->transport_reset(us);
- return USB_STOR_TRANSPORT_ERROR;
- }
- if ((us->protocol == US_PR_CBI) &&
- ((srb->cmd[0] == SCSI_REQ_SENSE) ||
- (srb->cmd[0] == SCSI_INQUIRY))) {
- /* do not issue an autorequest after request sense */
- //debug("No auto request and good\n");
- return USB_STOR_TRANSPORT_GOOD;
- }
- /* issue an request_sense */
- memset(&psrb->cmd[0], 0, 12);
- psrb->cmd[0] = SCSI_REQ_SENSE;
- psrb->cmd[1] = srb->lun << 5;
- psrb->cmd[4] = 18;
- psrb->datalen = 18;
- psrb->pdata = &srb->sense_buf[0];
- psrb->cmdlen = 12;
- /* issue the command */
- result = usb_stor_CB_comdat(psrb, us);
- //debug("auto request returned %d\n", result);
- /* if this is an CBI Protocol, get IRQ */
- if (us->protocol == US_PR_CBI)
- status = usb_stor_CBI_get_status(psrb, us);
- if ((result < 0) && !(us->pusb_dev->status & USB_ST_STALLED)) {
- /* debug(" AUTO REQUEST ERROR %ld\n",
- us->pusb_dev->status); */
- return USB_STOR_TRANSPORT_ERROR;
- }
- /* debug("autorequest returned 0x%02X 0x%02X 0x%02X 0x%02X\n",
- srb->sense_buf[0], srb->sense_buf[2],
- srb->sense_buf[12], srb->sense_buf[13]); */
- /* Check the auto request result */
- if ((srb->sense_buf[2] == 0) &&
- (srb->sense_buf[12] == 0) &&
- (srb->sense_buf[13] == 0)) {
- /* ok, no sense */
- return USB_STOR_TRANSPORT_GOOD;
- }
- /* Check the auto request result */
- switch (srb->sense_buf[2]) {
- case 0x01:
- /* Recovered Error */
- return USB_STOR_TRANSPORT_GOOD;
- break;
- case 0x02:
- /* Not Ready */
- if (notready++ > USB_TRANSPORT_NOT_READY_RETRY) {
- /* printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
- " 0x%02X (NOT READY)\n", srb->cmd[0],
- srb->sense_buf[0], srb->sense_buf[2],
- srb->sense_buf[12], srb->sense_buf[13]); */
- return USB_STOR_TRANSPORT_FAILED;
- } else {
- mdelay(100);
- goto do_retry;
- }
- break;
- default:
- if (retry++ > USB_TRANSPORT_UNKNOWN_RETRY) {
- /* printf("cmd 0x%02X returned 0x%02X 0x%02X 0x%02X"
- " 0x%02X\n", srb->cmd[0], srb->sense_buf[0],
- srb->sense_buf[2], srb->sense_buf[12],
- srb->sense_buf[13]); */
- return USB_STOR_TRANSPORT_FAILED;
- } else
- goto do_retry;
- break;
- }
- //return USB_STOR_TRANSPORT_FAILED;
- }
- static int usb_stor_irq(struct usb_device *dev)
- {
- struct us_data *us;
- us = (struct us_data *)dev->privptr;
- if (us->ip_wanted)
- us->ip_wanted = 0;
- return 0;
- }
- static void usb_stor_set_max_xfer_blk(struct usb_device *udev,
- struct us_data *us)
- {
- unsigned short blk = 20;
- us->max_xfer_blk = blk;
- }
- static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss)
- {
- int retry, i;
- retry = 5;
- do {
- memset(&srb->cmd[0], 0, 12);
- srb->cmd[0] = SCSI_INQUIRY;
- srb->cmd[1] = srb->lun << 5;
- srb->cmd[4] = 36;
- srb->datalen = 36;
- srb->cmdlen = 12;
- i = ss->transport(srb, ss);
- //debug("inquiry returns %d\n", i);
- if (i == 0)
- break;
- } while (--retry);
- if (!retry) {
- //printf("error in inquiry\n");
- return -1;
- }
- return 0;
- }
- static int usb_request_sense(struct scsi_cmd *srb, struct us_data *ss)
- {
- char *ptr;
- ptr = (char *)srb->pdata;
- memset(&srb->cmd[0], 0, 12);
- srb->cmd[0] = SCSI_REQ_SENSE;
- srb->cmd[1] = srb->lun << 5;
- srb->cmd[4] = 18;
- srb->datalen = 18;
- srb->pdata = &srb->sense_buf[0];
- srb->cmdlen = 12;
- ss->transport(srb, ss);
- /* debug("Request Sense returned %02X %02X %02X\n",
- srb->sense_buf[2], srb->sense_buf[12],
- srb->sense_buf[13]); */
- srb->pdata = (unsigned char *)ptr;
- return 0;
- }
- static int usb_test_unit_ready(struct scsi_cmd *srb, struct us_data *ss)
- {
- int retries = 10;
- do {
- memset(&srb->cmd[0], 0, 12);
- srb->cmd[0] = SCSI_TST_U_RDY;
- srb->cmd[1] = srb->lun << 5;
- srb->datalen = 0;
- srb->cmdlen = 12;
- if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD) {
- ss->flags |= USB_READY;
- return 0;
- }
- usb_request_sense(srb, ss);
- /*
- * Check the Key Code Qualifier, if it matches
- * "Not Ready - medium not present"
- * (the sense Key equals 0x2 and the ASC is 0x3a)
- * return immediately as the medium being absent won't change
- * unless there is a user action.
- */
- if ((srb->sense_buf[2] == 0x02) &&
- (srb->sense_buf[12] == 0x3a))
- return -1;
- mdelay(100);
- } while (retries--);
- return -1;
- }
- static int usb_read_capacity(struct scsi_cmd *srb, struct us_data *ss)
- {
- int retry;
- /* XXX retries */
- retry = 3;
- do {
- memset(&srb->cmd[0], 0, 12);
- srb->cmd[0] = SCSI_RD_CAPAC;
- srb->cmd[1] = srb->lun << 5;
- srb->datalen = 8;
- srb->cmdlen = 12;
- if (ss->transport(srb, ss) == USB_STOR_TRANSPORT_GOOD)
- return 0;
- } while (retry--);
- return -1;
- }
- static int usb_read_10(struct scsi_cmd *srb, struct us_data *ss,
- unsigned long start, unsigned short blocks)
- {
- memset(&srb->cmd[0], 0, 12);
- srb->cmd[0] = SCSI_READ10;
- srb->cmd[1] = srb->lun << 5;
- srb->cmd[2] = ((unsigned char) (start >> 24)) & 0xff;
- srb->cmd[3] = ((unsigned char) (start >> 16)) & 0xff;
- srb->cmd[4] = ((unsigned char) (start >> 8)) & 0xff;
- srb->cmd[5] = ((unsigned char) (start)) & 0xff;
- srb->cmd[7] = ((unsigned char) (blocks >> 8)) & 0xff;
- srb->cmd[8] = (unsigned char) blocks & 0xff;
- srb->cmdlen = 12;
- //debug("read10: start %lx blocks %x\n", start, blocks);
- return ss->transport(srb, ss);
- }
- /* Probe to see if a new device is actually a Storage device */
- int usb_storage_probe(struct usb_device *dev, unsigned int ifnum,
- struct us_data *ss)
- {
- struct usb_interface *iface;
- int i;
- struct usb_endpoint_descriptor *ep_desc;
- unsigned int flags = 0;
- /* let's examine the device now */
- iface = &dev->config.if_desc[ifnum];
- if (dev->descriptor.bDeviceClass != 0 ||
- iface->desc.bInterfaceClass != USB_CLASS_MASS_STORAGE ||
- iface->desc.bInterfaceSubClass < US_SC_MIN ||
- iface->desc.bInterfaceSubClass > US_SC_MAX) {
- //SendUartString("Not mass storage\n");
- /* if it's not a mass storage, we go no further */
- return 0;
- }
- memset(ss, 0, sizeof(struct us_data));
- /* At this point, we know we've got a live one */
- //debug("\n\nUSB Mass Storage device detected\n");
- /* Initialize the us_data structure with some useful info */
- ss->flags = flags;
- ss->ifnum = ifnum;
- ss->pusb_dev = dev;
- ss->attention_done = 0;
- ss->subclass = iface->desc.bInterfaceSubClass;
- ss->protocol = iface->desc.bInterfaceProtocol;
- /* set the handler pointers based on the protocol */
- //debug("Transport: ");
- switch (ss->protocol) {
- case US_PR_CB:
- //debug("Control/Bulk\n");
- ss->transport = usb_stor_CB_transport;
- ss->transport_reset = usb_stor_CB_reset;
- break;
- case US_PR_CBI:
- //debug("Control/Bulk/Interrupt\n");
- ss->transport = usb_stor_CB_transport;
- ss->transport_reset = usb_stor_CB_reset;
- break;
- case US_PR_BULK:
- //debug("Bulk/Bulk/Bulk\n");
- ss->transport = usb_stor_BBB_transport;
- ss->transport_reset = usb_stor_BBB_reset;
- break;
- default:
- //printf("USB Storage Transport unknown / not yet implemented\n");
- return 0;
- break;
- }
- /*
- * We are expecting a minimum of 2 endpoints - in and out (bulk).
- * An optional interrupt is OK (necessary for CBI protocol).
- * We will ignore any others.
- */
- for (i = 0; i < iface->desc.bNumEndpoints; i++) {
- ep_desc = &iface->ep_desc[i];
- /* is it an BULK endpoint? */
- if ((ep_desc->bmAttributes &
- USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
- if (ep_desc->bEndpointAddress & USB_DIR_IN)
- ss->ep_in = ep_desc->bEndpointAddress &
- USB_ENDPOINT_NUMBER_MASK;
- else
- ss->ep_out =
- ep_desc->bEndpointAddress &
- USB_ENDPOINT_NUMBER_MASK;
- }
- /* is it an interrupt endpoint? */
- if ((ep_desc->bmAttributes &
- USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
- ss->ep_int = ep_desc->bEndpointAddress &
- USB_ENDPOINT_NUMBER_MASK;
- ss->irqinterval = ep_desc->bInterval;
- }
- }
- /* debug("Endpoints In %d Out %d Int %d\n",
- ss->ep_in, ss->ep_out, ss->ep_int); */
- /* Do some basic sanity checks, and bail if we find a problem */
- if (usb_set_interface(dev, iface->desc.bInterfaceNumber, 0) ||
- !ss->ep_in || !ss->ep_out ||
- (ss->protocol == US_PR_CBI && ss->ep_int == 0)) {
- //debug("Problems with device\n");
- return 0;
- }
- /* set class specific stuff */
- /* We only handle certain protocols. Currently, these are
- * the only ones.
- * The SFF8070 accepts the requests used in u-boot
- */
- if (ss->subclass != US_SC_UFI && ss->subclass != US_SC_SCSI &&
- ss->subclass != US_SC_8070) {
- //printf("Sorry, protocol %d not yet supported.\n", ss->subclass);
- return 0;
- }
- if (ss->ep_int) {
- /* we had found an interrupt endpoint, prepare irq pipe
- * set up the IRQ pipe and handler
- */
- ss->irqinterval = (ss->irqinterval > 0) ? ss->irqinterval : 255;
- ss->irqpipe = usb_rcvintpipe(ss->pusb_dev, ss->ep_int);
- ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe);
- dev->irq_handle = usb_stor_irq;
- }
- /* Set the maximum transfer size per host controller setting */
- usb_stor_set_max_xfer_blk(dev, ss);
- dev->privptr = (void *)ss;
- return 1;
- }
- int usb_stor_get_info(struct usb_device *dev, struct us_data *ss,
- struct blk_desc *dev_desc)
- {
- unsigned char perq, modi;
- ALLOC_CACHE_ALIGN_BUFFER(u32, cap, 2);
- ALLOC_CACHE_ALIGN_BUFFER(u8, usb_stor_buf, 36);
- u32 capacity, blksz;
- struct scsi_cmd *pccb = &usb_ccb;
- pccb->pdata = usb_stor_buf;
- dev_desc->target = dev->devnum;
- pccb->lun = dev_desc->lun;
- //debug(" address %d\n", dev_desc->target);
- if (usb_inquiry(pccb, ss)) {
- //debug("%s: usb_inquiry() failed\n", __func__);
- return -1;
- }
- perq = usb_stor_buf[0];
- modi = usb_stor_buf[1];
- /*
- * Skip unknown devices (0x1f) and enclosure service devices (0x0d),
- * they would not respond to test_unit_ready .
- */
- if (((perq & 0x1f) == 0x1f) || ((perq & 0x1f) == 0x0d)) {
- //debug("%s: unknown/unsupported device\n", __func__);
- return 0;
- }
- if ((modi&0x80) == 0x80) {
- /* drive is removable */
- dev_desc->removable = 1;
- }
- memcpy(dev_desc->vendor, (const void *)&usb_stor_buf[8], 8);
- memcpy(dev_desc->product, (const void *)&usb_stor_buf[16], 16);
- memcpy(dev_desc->revision, (const void *)&usb_stor_buf[32], 4);
- dev_desc->vendor[8] = 0;
- dev_desc->product[16] = 0;
- dev_desc->revision[4] = 0;
- /* debug("ISO Vers %X, Response Data %X\n", usb_stor_buf[2],
- usb_stor_buf[3]); */
- if (usb_test_unit_ready(pccb, ss)) {
- /* printf("Device NOT ready\n"
- " Request Sense returned %02X %02X %02X\n",
- pccb->sense_buf[2], pccb->sense_buf[12],
- pccb->sense_buf[13]); */
- if (dev_desc->removable == 1)
- dev_desc->type = perq;
- return 0;
- }
- pccb->pdata = (unsigned char *)cap;
- memset(pccb->pdata, 0, 8);
- if (usb_read_capacity(pccb, ss) != 0) {
- //printf("READ_CAP ERROR\n");
- cap[0] = 2880;
- cap[1] = 0x200;
- }
- ss->flags &= ~USB_READY;
- //debug("Read Capacity returns: 0x%08x, 0x%08x\n", cap[0], cap[1]);
- #if 0
- if (cap[0] > (0x200000 * 10)) /* greater than 10 GByte */
- cap[0] >>= 16;
- cap[0] = cpu_to_be32(cap[0]);
- cap[1] = cpu_to_be32(cap[1]);
- #endif
- capacity = be32_to_cpu(cap[0]) + 1;
- blksz = be32_to_cpu(cap[1]);
- dev_desc->lba = capacity;
- dev_desc->blksz = blksz;
- dev_desc->log2blksz = LOG2(dev_desc->blksz);
- dev_desc->type = perq;
- return 1;
- }
- static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr,
- lbaint_t blkcnt, void *buffer)
- {
- lbaint_t start, blks;
- uintptr_t buf_addr;
- unsigned short smallblks;
- struct usb_device *udev;
- struct us_data *ss;
- int retry;
- struct scsi_cmd *srb = &usb_ccb;
- if (blkcnt == 0)
- return 0;
- /* Setup device */
- //debug("\nusb_read: udev %d\n", block_dev->devnum);
- udev = usb_dev_desc[block_dev->devnum].priv;
- if (!udev) {
- //debug("%s: No device\n", __func__);
- return 0;
- }
- ss = (struct us_data *)udev->privptr;
- usb_disable_asynch(1); /* asynch transfer not allowed */
- srb->lun = block_dev->lun;
- buf_addr = (uintptr_t)buffer;
- start = blknr;
- blks = blkcnt;
- /* debug("\nusb_read: dev %d startblk " LBAF ", blccnt " LBAF " buffer %"
- PRIxPTR "\n", block_dev->devnum, start, blks, buf_addr); */
- do {
- /* XXX need some comment here */
- retry = 2;
- srb->pdata = (unsigned char *)buf_addr;
- if (blks > ss->max_xfer_blk)
- smallblks = ss->max_xfer_blk;
- else
- smallblks = (unsigned short) blks;
- retry_it:
- /* if (smallblks == ss->max_xfer_blk)
- usb_show_progress(); */
- srb->datalen = block_dev->blksz * smallblks;
- srb->pdata = (unsigned char *)buf_addr;
- if (usb_read_10(srb, ss, start, smallblks)) {
- //debug("Read ERROR\n");
- usb_request_sense(srb, ss);
- if (retry--)
- goto retry_it;
- blkcnt -= blks;
- break;
- }
- start += smallblks;
- blks -= smallblks;
- buf_addr += srb->datalen;
- } while (blks != 0);
- ss->flags &= ~USB_READY;
- /* debug("usb_read: end startblk " LBAF
- ", blccnt %x buffer %" PRIxPTR "\n",
- start, smallblks, buf_addr); */
- usb_disable_asynch(0); /* asynch transfer allowed */
- /* if (blkcnt >= ss->max_xfer_blk)
- debug("\n"); */
- return blkcnt;
- }
- void usb_find_usb2_hub_address_port(struct usb_device *udev,
- uint8_t *hub_address, uint8_t *hub_port)
- {
- /* Find out the nearest parent which is high speed */
- while (udev->parent->parent != NULL)
- if (udev->parent->speed != USB_SPEED_HIGH) {
- udev = udev->parent;
- } else {
- *hub_address = udev->parent->devnum;
- *hub_port = udev->portnr;
- return;
- }
- *hub_address = 0;
- *hub_port = 0;
- }
- static int usb_stor_probe_device(struct usb_device *udev)
- {
- int lun, max_lun;
- int start;
- if (udev == NULL)
- return -ENOENT; /* no more devices available */
- //debug("\n\nProbing for storage\n");
- /* We don't have space to even probe if we hit the maximum */
- if (usb_max_devs == USB_MAX_STOR_DEV) {
- /* printf("max USB Storage Device reached: %d stopping\n",
- usb_max_devs); */
- return -ENOSPC;
- }
- if (!usb_storage_probe(udev, 0, &usb_stor[usb_max_devs]))
- return 0;
- /*
- * OK, it's a storage device. Iterate over its LUNs and populate
- * usb_dev_desc'
- */
- start = usb_max_devs;
- max_lun = usb_get_max_lun(&usb_stor[usb_max_devs]);
- for (lun = 0; lun <= max_lun && usb_max_devs < USB_MAX_STOR_DEV;
- lun++) {
- struct blk_desc *blkdev;
- blkdev = &usb_dev_desc[usb_max_devs];
- memset(blkdev, '\0', sizeof(struct blk_desc));
- blkdev->if_type = IF_TYPE_USB;
- blkdev->devnum = usb_max_devs;
- blkdev->part_type = PART_TYPE_UNKNOWN;
- blkdev->target = 0xff;
- blkdev->type = DEV_TYPE_UNKNOWN;
- blkdev->block_read = usb_stor_read;
- //blkdev->block_write = usb_stor_write;
- blkdev->lun = lun;
- blkdev->priv = udev;
- if (usb_stor_get_info(udev, &usb_stor[start],
- &usb_dev_desc[usb_max_devs]) == 1) {
- //debug("partype: %d\n", blkdev->part_type);
- //part_init(blkdev);
- //debug("partype: %d\n", blkdev->part_type);
- usb_max_devs++;
- //debug("%s: Found device %p\n", __func__, udev);
- }
- }
- return 0;
- }
- void usb_stor_reset(void)
- {
- usb_max_devs = 0;
- }
- /*******************************************************************************
- * scan the usb and reports device info
- * to the user if mode = 1
- * returns current device or -1 if no
- */
- int usb_stor_scan(int mode)
- {
- unsigned char i;
-
- if (mode == 1)
- SendUartString("\rScanning usb for storage devices... ");
- usb_disable_asynch(1); /* asynch transfer not allowed */
- usb_stor_reset();
- for (i = 0; i < USB_MAX_DEVICE; i++) {
- struct usb_device *dev;
- dev = usb_get_dev_index(i); /* get device */
- //debug("i=%d\n", i);
- if (usb_stor_probe_device(dev))
- break;
- } /* for */
- usb_disable_asynch(0); /* asynch transfer allowed */
- SendUartString("\rStorage Device(s) found\r\n");
- if (usb_max_devs > 0)
- return 0;
- return -1;
- }
- extern void UpdateFromMedia(int drv);
- void updateFromUSB(void)
- {
- /* set usb0 host mode */
- rSYS_ANA_CFG &= ~(1 << 25);
- rSYS_ANA_CFG |= (1 << 24);
- udelay(100);
- /* soft reset phy */
- rSYS_SOFT_RST &= ~(1 << 30);
- udelay(100);
- rSYS_SOFT_RST |= (1 << 30);
- udelay(1000);
- /* soft reset controller */
- rSYS_SOFT_RST &= ~(1 << 3);
- udelay(100);
- rSYS_SOFT_RST |= (1 << 3);
- udelay(100);
- if (usb_init() < 0) {
- SendUartString("usb_init fail.\n");
- return;
- }
- usb_stor_curr_dev = usb_stor_scan(1);
- if (usb_stor_curr_dev < 0) {
- SendUartString("usb_stor_scan fail.\n");
- return;
- }
- SendUartString("usb_stor_scan ok.\n");
- UpdateFromMedia(USB);
- }
- int USB_disk_initialize(void)
- {
- return 0;
- }
- int USB_disk_read(void *buff, DWORD sector, BYTE count)
- {
- struct blk_desc *dev_desc = &usb_dev_desc[0];
- return dev_desc->block_read(dev_desc, sector, count, buff);
- }
- int USB_disk_ioctl(BYTE ctrl, void *buff)
- {
- struct blk_desc *dev_desc = &usb_dev_desc[0];
-
- switch(ctrl)
- {
- case CTRL_SYNC:
- break;
- case GET_SECTOR_COUNT:
- *(DWORD*)buff = dev_desc->lba;
- break;
- case GET_BLOCK_SIZE:
- *(DWORD*)buff = 1;
- break;
- default:
- return -1;
- }
- return 0;
- }
|