u132-hcd.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242
  1. /*
  2. * Host Controller Driver for the Elan Digital Systems U132 adapter
  3. *
  4. * Copyright(C) 2006 Elan Digital Systems Limited
  5. * http://www.elandigitalsystems.com
  6. *
  7. * Author and Maintainer - Tony Olech - Elan Digital Systems
  8. * tony.olech@elandigitalsystems.com
  9. *
  10. * This program is free software;you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation, version 2.
  13. *
  14. *
  15. * This driver was written by Tony Olech(tony.olech@elandigitalsystems.com)
  16. * based on various USB host drivers in the 2.6.15 linux kernel
  17. * with constant reference to the 3rd Edition of Linux Device Drivers
  18. * published by O'Reilly
  19. *
  20. * The U132 adapter is a USB to CardBus adapter specifically designed
  21. * for PC cards that contain an OHCI host controller. Typical PC cards
  22. * are the Orange Mobile 3G Option GlobeTrotter Fusion card.
  23. *
  24. * The U132 adapter will *NOT *work with PC cards that do not contain
  25. * an OHCI controller. A simple way to test whether a PC card has an
  26. * OHCI controller as an interface is to insert the PC card directly
  27. * into a laptop(or desktop) with a CardBus slot and if "lspci" shows
  28. * a new USB controller and "lsusb -v" shows a new OHCI Host Controller
  29. * then there is a good chance that the U132 adapter will support the
  30. * PC card.(you also need the specific client driver for the PC card)
  31. *
  32. * Please inform the Author and Maintainer about any PC cards that
  33. * contain OHCI Host Controller and work when directly connected to
  34. * an embedded CardBus slot but do not work when they are connected
  35. * via an ELAN U132 adapter.
  36. *
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/delay.h>
  42. #include <linux/ioport.h>
  43. #include <linux/pci_ids.h>
  44. #include <linux/sched.h>
  45. #include <linux/slab.h>
  46. #include <linux/errno.h>
  47. #include <linux/init.h>
  48. #include <linux/timer.h>
  49. #include <linux/list.h>
  50. #include <linux/interrupt.h>
  51. #include <linux/usb.h>
  52. #include <linux/usb/hcd.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/platform_device.h>
  55. #include <linux/mutex.h>
  56. #include <asm/io.h>
  57. #include <asm/irq.h>
  58. #include <asm/byteorder.h>
  59. /* FIXME ohci.h is ONLY for internal use by the OHCI driver.
  60. * If you're going to try stuff like this, you need to split
  61. * out shareable stuff (register declarations?) into its own
  62. * file, maybe name <linux/usb/ohci.h>
  63. */
  64. #include "ohci.h"
  65. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  66. #define OHCI_INTR_INIT (OHCI_INTR_MIE | OHCI_INTR_UE | OHCI_INTR_RD | \
  67. OHCI_INTR_WDH)
  68. MODULE_AUTHOR("Tony Olech - Elan Digital Systems Limited");
  69. MODULE_DESCRIPTION("U132 USB Host Controller Driver");
  70. MODULE_LICENSE("GPL");
  71. #define INT_MODULE_PARM(n, v) static int n = v;module_param(n, int, 0444)
  72. INT_MODULE_PARM(testing, 0);
  73. /* Some boards misreport power switching/overcurrent*/
  74. static bool distrust_firmware = 1;
  75. module_param(distrust_firmware, bool, 0);
  76. MODULE_PARM_DESC(distrust_firmware, "true to distrust firmware power/overcurren"
  77. "t setup");
  78. static DECLARE_WAIT_QUEUE_HEAD(u132_hcd_wait);
  79. /*
  80. * u132_module_lock exists to protect access to global variables
  81. *
  82. */
  83. static struct mutex u132_module_lock;
  84. static int u132_exiting;
  85. static int u132_instances;
  86. static struct list_head u132_static_list;
  87. /*
  88. * end of the global variables protected by u132_module_lock
  89. */
  90. static struct workqueue_struct *workqueue;
  91. #define MAX_U132_PORTS 7
  92. #define MAX_U132_ADDRS 128
  93. #define MAX_U132_UDEVS 4
  94. #define MAX_U132_ENDPS 100
  95. #define MAX_U132_RINGS 4
  96. static const char *cc_to_text[16] = {
  97. "No Error ",
  98. "CRC Error ",
  99. "Bit Stuff ",
  100. "Data Togg ",
  101. "Stall ",
  102. "DevNotResp ",
  103. "PIDCheck ",
  104. "UnExpPID ",
  105. "DataOver ",
  106. "DataUnder ",
  107. "(for hw) ",
  108. "(for hw) ",
  109. "BufferOver ",
  110. "BuffUnder ",
  111. "(for HCD) ",
  112. "(for HCD) "
  113. };
  114. struct u132_port {
  115. struct u132 *u132;
  116. int reset;
  117. int enable;
  118. int power;
  119. int Status;
  120. };
  121. struct u132_addr {
  122. u8 address;
  123. };
  124. struct u132_udev {
  125. struct kref kref;
  126. struct usb_device *usb_device;
  127. u8 enumeration;
  128. u8 udev_number;
  129. u8 usb_addr;
  130. u8 portnumber;
  131. u8 endp_number_in[16];
  132. u8 endp_number_out[16];
  133. };
  134. #define ENDP_QUEUE_SHIFT 3
  135. #define ENDP_QUEUE_SIZE (1<<ENDP_QUEUE_SHIFT)
  136. #define ENDP_QUEUE_MASK (ENDP_QUEUE_SIZE-1)
  137. struct u132_urbq {
  138. struct list_head urb_more;
  139. struct urb *urb;
  140. };
  141. struct u132_spin {
  142. spinlock_t slock;
  143. };
  144. struct u132_endp {
  145. struct kref kref;
  146. u8 udev_number;
  147. u8 endp_number;
  148. u8 usb_addr;
  149. u8 usb_endp;
  150. struct u132 *u132;
  151. struct list_head endp_ring;
  152. struct u132_ring *ring;
  153. unsigned toggle_bits:2;
  154. unsigned active:1;
  155. unsigned delayed:1;
  156. unsigned input:1;
  157. unsigned output:1;
  158. unsigned pipetype:2;
  159. unsigned dequeueing:1;
  160. unsigned edset_flush:1;
  161. unsigned spare_bits:14;
  162. unsigned long jiffies;
  163. struct usb_host_endpoint *hep;
  164. struct u132_spin queue_lock;
  165. u16 queue_size;
  166. u16 queue_last;
  167. u16 queue_next;
  168. struct urb *urb_list[ENDP_QUEUE_SIZE];
  169. struct list_head urb_more;
  170. struct delayed_work scheduler;
  171. };
  172. struct u132_ring {
  173. unsigned in_use:1;
  174. unsigned length:7;
  175. u8 number;
  176. struct u132 *u132;
  177. struct u132_endp *curr_endp;
  178. struct delayed_work scheduler;
  179. };
  180. struct u132 {
  181. struct kref kref;
  182. struct list_head u132_list;
  183. struct mutex sw_lock;
  184. struct mutex scheduler_lock;
  185. struct u132_platform_data *board;
  186. struct platform_device *platform_dev;
  187. struct u132_ring ring[MAX_U132_RINGS];
  188. int sequence_num;
  189. int going;
  190. int power;
  191. int reset;
  192. int num_ports;
  193. u32 hc_control;
  194. u32 hc_fminterval;
  195. u32 hc_roothub_status;
  196. u32 hc_roothub_a;
  197. u32 hc_roothub_portstatus[MAX_ROOT_PORTS];
  198. int flags;
  199. unsigned long next_statechange;
  200. struct delayed_work monitor;
  201. int num_endpoints;
  202. struct u132_addr addr[MAX_U132_ADDRS];
  203. struct u132_udev udev[MAX_U132_UDEVS];
  204. struct u132_port port[MAX_U132_PORTS];
  205. struct u132_endp *endp[MAX_U132_ENDPS];
  206. };
  207. /*
  208. * these cannot be inlines because we need the structure offset!!
  209. * Does anyone have a better way?????
  210. */
  211. #define ftdi_read_pcimem(pdev, member, data) usb_ftdi_elan_read_pcimem(pdev, \
  212. offsetof(struct ohci_regs, member), 0, data);
  213. #define ftdi_write_pcimem(pdev, member, data) usb_ftdi_elan_write_pcimem(pdev, \
  214. offsetof(struct ohci_regs, member), 0, data);
  215. #define u132_read_pcimem(u132, member, data) \
  216. usb_ftdi_elan_read_pcimem(u132->platform_dev, offsetof(struct \
  217. ohci_regs, member), 0, data);
  218. #define u132_write_pcimem(u132, member, data) \
  219. usb_ftdi_elan_write_pcimem(u132->platform_dev, offsetof(struct \
  220. ohci_regs, member), 0, data);
  221. static inline struct u132 *udev_to_u132(struct u132_udev *udev)
  222. {
  223. u8 udev_number = udev->udev_number;
  224. return container_of(udev, struct u132, udev[udev_number]);
  225. }
  226. static inline struct u132 *hcd_to_u132(struct usb_hcd *hcd)
  227. {
  228. return (struct u132 *)(hcd->hcd_priv);
  229. }
  230. static inline struct usb_hcd *u132_to_hcd(struct u132 *u132)
  231. {
  232. return container_of((void *)u132, struct usb_hcd, hcd_priv);
  233. }
  234. static inline void u132_disable(struct u132 *u132)
  235. {
  236. u132_to_hcd(u132)->state = HC_STATE_HALT;
  237. }
  238. #define kref_to_u132(d) container_of(d, struct u132, kref)
  239. #define kref_to_u132_endp(d) container_of(d, struct u132_endp, kref)
  240. #define kref_to_u132_udev(d) container_of(d, struct u132_udev, kref)
  241. #include "../misc/usb_u132.h"
  242. static const char hcd_name[] = "u132_hcd";
  243. #define PORT_C_MASK ((USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | \
  244. USB_PORT_STAT_C_SUSPEND | USB_PORT_STAT_C_OVERCURRENT | \
  245. USB_PORT_STAT_C_RESET) << 16)
  246. static void u132_hcd_delete(struct kref *kref)
  247. {
  248. struct u132 *u132 = kref_to_u132(kref);
  249. struct platform_device *pdev = u132->platform_dev;
  250. struct usb_hcd *hcd = u132_to_hcd(u132);
  251. u132->going += 1;
  252. mutex_lock(&u132_module_lock);
  253. list_del_init(&u132->u132_list);
  254. u132_instances -= 1;
  255. mutex_unlock(&u132_module_lock);
  256. dev_warn(&u132->platform_dev->dev, "FREEING the hcd=%p and thus the u13"
  257. "2=%p going=%d pdev=%p\n", hcd, u132, u132->going, pdev);
  258. usb_put_hcd(hcd);
  259. }
  260. static inline void u132_u132_put_kref(struct u132 *u132)
  261. {
  262. kref_put(&u132->kref, u132_hcd_delete);
  263. }
  264. static inline void u132_u132_init_kref(struct u132 *u132)
  265. {
  266. kref_init(&u132->kref);
  267. }
  268. static void u132_udev_delete(struct kref *kref)
  269. {
  270. struct u132_udev *udev = kref_to_u132_udev(kref);
  271. udev->udev_number = 0;
  272. udev->usb_device = NULL;
  273. udev->usb_addr = 0;
  274. udev->enumeration = 0;
  275. }
  276. static inline void u132_udev_put_kref(struct u132 *u132, struct u132_udev *udev)
  277. {
  278. kref_put(&udev->kref, u132_udev_delete);
  279. }
  280. static inline void u132_udev_get_kref(struct u132 *u132, struct u132_udev *udev)
  281. {
  282. kref_get(&udev->kref);
  283. }
  284. static inline void u132_udev_init_kref(struct u132 *u132,
  285. struct u132_udev *udev)
  286. {
  287. kref_init(&udev->kref);
  288. }
  289. static inline void u132_ring_put_kref(struct u132 *u132, struct u132_ring *ring)
  290. {
  291. kref_put(&u132->kref, u132_hcd_delete);
  292. }
  293. static void u132_ring_requeue_work(struct u132 *u132, struct u132_ring *ring,
  294. unsigned int delta)
  295. {
  296. if (delta > 0) {
  297. if (queue_delayed_work(workqueue, &ring->scheduler, delta))
  298. return;
  299. } else if (queue_delayed_work(workqueue, &ring->scheduler, 0))
  300. return;
  301. kref_put(&u132->kref, u132_hcd_delete);
  302. }
  303. static void u132_ring_queue_work(struct u132 *u132, struct u132_ring *ring,
  304. unsigned int delta)
  305. {
  306. kref_get(&u132->kref);
  307. u132_ring_requeue_work(u132, ring, delta);
  308. }
  309. static void u132_ring_cancel_work(struct u132 *u132, struct u132_ring *ring)
  310. {
  311. if (cancel_delayed_work(&ring->scheduler))
  312. kref_put(&u132->kref, u132_hcd_delete);
  313. }
  314. static void u132_endp_delete(struct kref *kref)
  315. {
  316. struct u132_endp *endp = kref_to_u132_endp(kref);
  317. struct u132 *u132 = endp->u132;
  318. u8 usb_addr = endp->usb_addr;
  319. u8 usb_endp = endp->usb_endp;
  320. u8 address = u132->addr[usb_addr].address;
  321. struct u132_udev *udev = &u132->udev[address];
  322. u8 endp_number = endp->endp_number;
  323. struct usb_host_endpoint *hep = endp->hep;
  324. struct u132_ring *ring = endp->ring;
  325. struct list_head *head = &endp->endp_ring;
  326. ring->length -= 1;
  327. if (endp == ring->curr_endp) {
  328. if (list_empty(head)) {
  329. ring->curr_endp = NULL;
  330. list_del(head);
  331. } else {
  332. struct u132_endp *next_endp = list_entry(head->next,
  333. struct u132_endp, endp_ring);
  334. ring->curr_endp = next_endp;
  335. list_del(head);
  336. }
  337. } else
  338. list_del(head);
  339. if (endp->input) {
  340. udev->endp_number_in[usb_endp] = 0;
  341. u132_udev_put_kref(u132, udev);
  342. }
  343. if (endp->output) {
  344. udev->endp_number_out[usb_endp] = 0;
  345. u132_udev_put_kref(u132, udev);
  346. }
  347. u132->endp[endp_number - 1] = NULL;
  348. hep->hcpriv = NULL;
  349. kfree(endp);
  350. u132_u132_put_kref(u132);
  351. }
  352. static inline void u132_endp_put_kref(struct u132 *u132, struct u132_endp *endp)
  353. {
  354. kref_put(&endp->kref, u132_endp_delete);
  355. }
  356. static inline void u132_endp_get_kref(struct u132 *u132, struct u132_endp *endp)
  357. {
  358. kref_get(&endp->kref);
  359. }
  360. static inline void u132_endp_init_kref(struct u132 *u132,
  361. struct u132_endp *endp)
  362. {
  363. kref_init(&endp->kref);
  364. kref_get(&u132->kref);
  365. }
  366. static void u132_endp_queue_work(struct u132 *u132, struct u132_endp *endp,
  367. unsigned int delta)
  368. {
  369. if (queue_delayed_work(workqueue, &endp->scheduler, delta))
  370. kref_get(&endp->kref);
  371. }
  372. static void u132_endp_cancel_work(struct u132 *u132, struct u132_endp *endp)
  373. {
  374. if (cancel_delayed_work(&endp->scheduler))
  375. kref_put(&endp->kref, u132_endp_delete);
  376. }
  377. static inline void u132_monitor_put_kref(struct u132 *u132)
  378. {
  379. kref_put(&u132->kref, u132_hcd_delete);
  380. }
  381. static void u132_monitor_queue_work(struct u132 *u132, unsigned int delta)
  382. {
  383. if (queue_delayed_work(workqueue, &u132->monitor, delta))
  384. kref_get(&u132->kref);
  385. }
  386. static void u132_monitor_requeue_work(struct u132 *u132, unsigned int delta)
  387. {
  388. if (!queue_delayed_work(workqueue, &u132->monitor, delta))
  389. kref_put(&u132->kref, u132_hcd_delete);
  390. }
  391. static void u132_monitor_cancel_work(struct u132 *u132)
  392. {
  393. if (cancel_delayed_work(&u132->monitor))
  394. kref_put(&u132->kref, u132_hcd_delete);
  395. }
  396. static int read_roothub_info(struct u132 *u132)
  397. {
  398. u32 revision;
  399. int retval;
  400. retval = u132_read_pcimem(u132, revision, &revision);
  401. if (retval) {
  402. dev_err(&u132->platform_dev->dev, "error %d accessing device co"
  403. "ntrol\n", retval);
  404. return retval;
  405. } else if ((revision & 0xFF) == 0x10) {
  406. } else if ((revision & 0xFF) == 0x11) {
  407. } else {
  408. dev_err(&u132->platform_dev->dev, "device revision is not valid"
  409. " %08X\n", revision);
  410. return -ENODEV;
  411. }
  412. retval = u132_read_pcimem(u132, control, &u132->hc_control);
  413. if (retval) {
  414. dev_err(&u132->platform_dev->dev, "error %d accessing device co"
  415. "ntrol\n", retval);
  416. return retval;
  417. }
  418. retval = u132_read_pcimem(u132, roothub.status,
  419. &u132->hc_roothub_status);
  420. if (retval) {
  421. dev_err(&u132->platform_dev->dev, "error %d accessing device re"
  422. "g roothub.status\n", retval);
  423. return retval;
  424. }
  425. retval = u132_read_pcimem(u132, roothub.a, &u132->hc_roothub_a);
  426. if (retval) {
  427. dev_err(&u132->platform_dev->dev, "error %d accessing device re"
  428. "g roothub.a\n", retval);
  429. return retval;
  430. }
  431. {
  432. int I = u132->num_ports;
  433. int i = 0;
  434. while (I-- > 0) {
  435. retval = u132_read_pcimem(u132, roothub.portstatus[i],
  436. &u132->hc_roothub_portstatus[i]);
  437. if (retval) {
  438. dev_err(&u132->platform_dev->dev, "error %d acc"
  439. "essing device roothub.portstatus[%d]\n"
  440. , retval, i);
  441. return retval;
  442. } else
  443. i += 1;
  444. }
  445. }
  446. return 0;
  447. }
  448. static void u132_hcd_monitor_work(struct work_struct *work)
  449. {
  450. struct u132 *u132 = container_of(work, struct u132, monitor.work);
  451. if (u132->going > 1) {
  452. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  453. , u132->going);
  454. u132_monitor_put_kref(u132);
  455. return;
  456. } else if (u132->going > 0) {
  457. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  458. u132_monitor_put_kref(u132);
  459. return;
  460. } else {
  461. int retval;
  462. mutex_lock(&u132->sw_lock);
  463. retval = read_roothub_info(u132);
  464. if (retval) {
  465. struct usb_hcd *hcd = u132_to_hcd(u132);
  466. u132_disable(u132);
  467. u132->going = 1;
  468. mutex_unlock(&u132->sw_lock);
  469. usb_hc_died(hcd);
  470. ftdi_elan_gone_away(u132->platform_dev);
  471. u132_monitor_put_kref(u132);
  472. return;
  473. } else {
  474. u132_monitor_requeue_work(u132, 500);
  475. mutex_unlock(&u132->sw_lock);
  476. return;
  477. }
  478. }
  479. }
  480. static void u132_hcd_giveback_urb(struct u132 *u132, struct u132_endp *endp,
  481. struct urb *urb, int status)
  482. {
  483. struct u132_ring *ring;
  484. unsigned long irqs;
  485. struct usb_hcd *hcd = u132_to_hcd(u132);
  486. urb->error_count = 0;
  487. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  488. usb_hcd_unlink_urb_from_ep(hcd, urb);
  489. endp->queue_next += 1;
  490. if (ENDP_QUEUE_SIZE > --endp->queue_size) {
  491. endp->active = 0;
  492. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  493. } else {
  494. struct list_head *next = endp->urb_more.next;
  495. struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
  496. urb_more);
  497. list_del(next);
  498. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
  499. urbq->urb;
  500. endp->active = 0;
  501. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  502. kfree(urbq);
  503. }
  504. mutex_lock(&u132->scheduler_lock);
  505. ring = endp->ring;
  506. ring->in_use = 0;
  507. u132_ring_cancel_work(u132, ring);
  508. u132_ring_queue_work(u132, ring, 0);
  509. mutex_unlock(&u132->scheduler_lock);
  510. u132_endp_put_kref(u132, endp);
  511. usb_hcd_giveback_urb(hcd, urb, status);
  512. }
  513. static void u132_hcd_forget_urb(struct u132 *u132, struct u132_endp *endp,
  514. struct urb *urb, int status)
  515. {
  516. u132_endp_put_kref(u132, endp);
  517. }
  518. static void u132_hcd_abandon_urb(struct u132 *u132, struct u132_endp *endp,
  519. struct urb *urb, int status)
  520. {
  521. unsigned long irqs;
  522. struct usb_hcd *hcd = u132_to_hcd(u132);
  523. urb->error_count = 0;
  524. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  525. usb_hcd_unlink_urb_from_ep(hcd, urb);
  526. endp->queue_next += 1;
  527. if (ENDP_QUEUE_SIZE > --endp->queue_size) {
  528. endp->active = 0;
  529. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  530. } else {
  531. struct list_head *next = endp->urb_more.next;
  532. struct u132_urbq *urbq = list_entry(next, struct u132_urbq,
  533. urb_more);
  534. list_del(next);
  535. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
  536. urbq->urb;
  537. endp->active = 0;
  538. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  539. kfree(urbq);
  540. }
  541. usb_hcd_giveback_urb(hcd, urb, status);
  542. }
  543. static inline int edset_input(struct u132 *u132, struct u132_ring *ring,
  544. struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
  545. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  546. int toggle_bits, int error_count, int condition_code, int repeat_number,
  547. int halted, int skipped, int actual, int non_null))
  548. {
  549. return usb_ftdi_elan_edset_input(u132->platform_dev, ring->number, endp,
  550. urb, address, endp->usb_endp, toggle_bits, callback);
  551. }
  552. static inline int edset_setup(struct u132 *u132, struct u132_ring *ring,
  553. struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
  554. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  555. int toggle_bits, int error_count, int condition_code, int repeat_number,
  556. int halted, int skipped, int actual, int non_null))
  557. {
  558. return usb_ftdi_elan_edset_setup(u132->platform_dev, ring->number, endp,
  559. urb, address, endp->usb_endp, toggle_bits, callback);
  560. }
  561. static inline int edset_single(struct u132 *u132, struct u132_ring *ring,
  562. struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
  563. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  564. int toggle_bits, int error_count, int condition_code, int repeat_number,
  565. int halted, int skipped, int actual, int non_null))
  566. {
  567. return usb_ftdi_elan_edset_single(u132->platform_dev, ring->number,
  568. endp, urb, address, endp->usb_endp, toggle_bits, callback);
  569. }
  570. static inline int edset_output(struct u132 *u132, struct u132_ring *ring,
  571. struct u132_endp *endp, struct urb *urb, u8 address, u8 toggle_bits,
  572. void (*callback) (void *endp, struct urb *urb, u8 *buf, int len,
  573. int toggle_bits, int error_count, int condition_code, int repeat_number,
  574. int halted, int skipped, int actual, int non_null))
  575. {
  576. return usb_ftdi_elan_edset_output(u132->platform_dev, ring->number,
  577. endp, urb, address, endp->usb_endp, toggle_bits, callback);
  578. }
  579. /*
  580. * must not LOCK sw_lock
  581. *
  582. */
  583. static void u132_hcd_interrupt_recv(void *data, struct urb *urb, u8 *buf,
  584. int len, int toggle_bits, int error_count, int condition_code,
  585. int repeat_number, int halted, int skipped, int actual, int non_null)
  586. {
  587. struct u132_endp *endp = data;
  588. struct u132 *u132 = endp->u132;
  589. u8 address = u132->addr[endp->usb_addr].address;
  590. struct u132_udev *udev = &u132->udev[address];
  591. mutex_lock(&u132->scheduler_lock);
  592. if (u132->going > 1) {
  593. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  594. , u132->going);
  595. mutex_unlock(&u132->scheduler_lock);
  596. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  597. return;
  598. } else if (endp->dequeueing) {
  599. endp->dequeueing = 0;
  600. mutex_unlock(&u132->scheduler_lock);
  601. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  602. return;
  603. } else if (u132->going > 0) {
  604. dev_err(&u132->platform_dev->dev, "device is being removed "
  605. "urb=%p\n", urb);
  606. mutex_unlock(&u132->scheduler_lock);
  607. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  608. return;
  609. } else if (!urb->unlinked) {
  610. struct u132_ring *ring = endp->ring;
  611. u8 *u = urb->transfer_buffer + urb->actual_length;
  612. u8 *b = buf;
  613. int L = len;
  614. while (L-- > 0)
  615. *u++ = *b++;
  616. urb->actual_length += len;
  617. if ((condition_code == TD_CC_NOERROR) &&
  618. (urb->transfer_buffer_length > urb->actual_length)) {
  619. endp->toggle_bits = toggle_bits;
  620. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  621. 1 & toggle_bits);
  622. if (urb->actual_length > 0) {
  623. int retval;
  624. mutex_unlock(&u132->scheduler_lock);
  625. retval = edset_single(u132, ring, endp, urb,
  626. address, endp->toggle_bits,
  627. u132_hcd_interrupt_recv);
  628. if (retval != 0)
  629. u132_hcd_giveback_urb(u132, endp, urb,
  630. retval);
  631. } else {
  632. ring->in_use = 0;
  633. endp->active = 0;
  634. endp->jiffies = jiffies +
  635. msecs_to_jiffies(urb->interval);
  636. u132_ring_cancel_work(u132, ring);
  637. u132_ring_queue_work(u132, ring, 0);
  638. mutex_unlock(&u132->scheduler_lock);
  639. u132_endp_put_kref(u132, endp);
  640. }
  641. return;
  642. } else if ((condition_code == TD_DATAUNDERRUN) &&
  643. ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
  644. endp->toggle_bits = toggle_bits;
  645. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  646. 1 & toggle_bits);
  647. mutex_unlock(&u132->scheduler_lock);
  648. u132_hcd_giveback_urb(u132, endp, urb, 0);
  649. return;
  650. } else {
  651. if (condition_code == TD_CC_NOERROR) {
  652. endp->toggle_bits = toggle_bits;
  653. usb_settoggle(udev->usb_device, endp->usb_endp,
  654. 0, 1 & toggle_bits);
  655. } else if (condition_code == TD_CC_STALL) {
  656. endp->toggle_bits = 0x2;
  657. usb_settoggle(udev->usb_device, endp->usb_endp,
  658. 0, 0);
  659. } else {
  660. endp->toggle_bits = 0x2;
  661. usb_settoggle(udev->usb_device, endp->usb_endp,
  662. 0, 0);
  663. dev_err(&u132->platform_dev->dev, "urb=%p givin"
  664. "g back INTERRUPT %s\n", urb,
  665. cc_to_text[condition_code]);
  666. }
  667. mutex_unlock(&u132->scheduler_lock);
  668. u132_hcd_giveback_urb(u132, endp, urb,
  669. cc_to_error[condition_code]);
  670. return;
  671. }
  672. } else {
  673. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  674. "unlinked=%d\n", urb, urb->unlinked);
  675. mutex_unlock(&u132->scheduler_lock);
  676. u132_hcd_giveback_urb(u132, endp, urb, 0);
  677. return;
  678. }
  679. }
  680. static void u132_hcd_bulk_output_sent(void *data, struct urb *urb, u8 *buf,
  681. int len, int toggle_bits, int error_count, int condition_code,
  682. int repeat_number, int halted, int skipped, int actual, int non_null)
  683. {
  684. struct u132_endp *endp = data;
  685. struct u132 *u132 = endp->u132;
  686. u8 address = u132->addr[endp->usb_addr].address;
  687. mutex_lock(&u132->scheduler_lock);
  688. if (u132->going > 1) {
  689. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  690. , u132->going);
  691. mutex_unlock(&u132->scheduler_lock);
  692. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  693. return;
  694. } else if (endp->dequeueing) {
  695. endp->dequeueing = 0;
  696. mutex_unlock(&u132->scheduler_lock);
  697. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  698. return;
  699. } else if (u132->going > 0) {
  700. dev_err(&u132->platform_dev->dev, "device is being removed "
  701. "urb=%p\n", urb);
  702. mutex_unlock(&u132->scheduler_lock);
  703. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  704. return;
  705. } else if (!urb->unlinked) {
  706. struct u132_ring *ring = endp->ring;
  707. urb->actual_length += len;
  708. endp->toggle_bits = toggle_bits;
  709. if (urb->transfer_buffer_length > urb->actual_length) {
  710. int retval;
  711. mutex_unlock(&u132->scheduler_lock);
  712. retval = edset_output(u132, ring, endp, urb, address,
  713. endp->toggle_bits, u132_hcd_bulk_output_sent);
  714. if (retval != 0)
  715. u132_hcd_giveback_urb(u132, endp, urb, retval);
  716. return;
  717. } else {
  718. mutex_unlock(&u132->scheduler_lock);
  719. u132_hcd_giveback_urb(u132, endp, urb, 0);
  720. return;
  721. }
  722. } else {
  723. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  724. "unlinked=%d\n", urb, urb->unlinked);
  725. mutex_unlock(&u132->scheduler_lock);
  726. u132_hcd_giveback_urb(u132, endp, urb, 0);
  727. return;
  728. }
  729. }
  730. static void u132_hcd_bulk_input_recv(void *data, struct urb *urb, u8 *buf,
  731. int len, int toggle_bits, int error_count, int condition_code,
  732. int repeat_number, int halted, int skipped, int actual, int non_null)
  733. {
  734. struct u132_endp *endp = data;
  735. struct u132 *u132 = endp->u132;
  736. u8 address = u132->addr[endp->usb_addr].address;
  737. struct u132_udev *udev = &u132->udev[address];
  738. mutex_lock(&u132->scheduler_lock);
  739. if (u132->going > 1) {
  740. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  741. , u132->going);
  742. mutex_unlock(&u132->scheduler_lock);
  743. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  744. return;
  745. } else if (endp->dequeueing) {
  746. endp->dequeueing = 0;
  747. mutex_unlock(&u132->scheduler_lock);
  748. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  749. return;
  750. } else if (u132->going > 0) {
  751. dev_err(&u132->platform_dev->dev, "device is being removed "
  752. "urb=%p\n", urb);
  753. mutex_unlock(&u132->scheduler_lock);
  754. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  755. return;
  756. } else if (!urb->unlinked) {
  757. struct u132_ring *ring = endp->ring;
  758. u8 *u = urb->transfer_buffer + urb->actual_length;
  759. u8 *b = buf;
  760. int L = len;
  761. while (L-- > 0)
  762. *u++ = *b++;
  763. urb->actual_length += len;
  764. if ((condition_code == TD_CC_NOERROR) &&
  765. (urb->transfer_buffer_length > urb->actual_length)) {
  766. int retval;
  767. endp->toggle_bits = toggle_bits;
  768. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  769. 1 & toggle_bits);
  770. mutex_unlock(&u132->scheduler_lock);
  771. retval = usb_ftdi_elan_edset_input(u132->platform_dev,
  772. ring->number, endp, urb, address,
  773. endp->usb_endp, endp->toggle_bits,
  774. u132_hcd_bulk_input_recv);
  775. if (retval != 0)
  776. u132_hcd_giveback_urb(u132, endp, urb, retval);
  777. return;
  778. } else if (condition_code == TD_CC_NOERROR) {
  779. endp->toggle_bits = toggle_bits;
  780. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  781. 1 & toggle_bits);
  782. mutex_unlock(&u132->scheduler_lock);
  783. u132_hcd_giveback_urb(u132, endp, urb,
  784. cc_to_error[condition_code]);
  785. return;
  786. } else if ((condition_code == TD_DATAUNDERRUN) &&
  787. ((urb->transfer_flags & URB_SHORT_NOT_OK) == 0)) {
  788. endp->toggle_bits = toggle_bits;
  789. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  790. 1 & toggle_bits);
  791. mutex_unlock(&u132->scheduler_lock);
  792. u132_hcd_giveback_urb(u132, endp, urb, 0);
  793. return;
  794. } else if (condition_code == TD_DATAUNDERRUN) {
  795. endp->toggle_bits = toggle_bits;
  796. usb_settoggle(udev->usb_device, endp->usb_endp, 0,
  797. 1 & toggle_bits);
  798. dev_warn(&u132->platform_dev->dev, "urb=%p(SHORT NOT OK"
  799. ") giving back BULK IN %s\n", urb,
  800. cc_to_text[condition_code]);
  801. mutex_unlock(&u132->scheduler_lock);
  802. u132_hcd_giveback_urb(u132, endp, urb, 0);
  803. return;
  804. } else if (condition_code == TD_CC_STALL) {
  805. endp->toggle_bits = 0x2;
  806. usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
  807. mutex_unlock(&u132->scheduler_lock);
  808. u132_hcd_giveback_urb(u132, endp, urb,
  809. cc_to_error[condition_code]);
  810. return;
  811. } else {
  812. endp->toggle_bits = 0x2;
  813. usb_settoggle(udev->usb_device, endp->usb_endp, 0, 0);
  814. dev_err(&u132->platform_dev->dev, "urb=%p giving back B"
  815. "ULK IN code=%d %s\n", urb, condition_code,
  816. cc_to_text[condition_code]);
  817. mutex_unlock(&u132->scheduler_lock);
  818. u132_hcd_giveback_urb(u132, endp, urb,
  819. cc_to_error[condition_code]);
  820. return;
  821. }
  822. } else {
  823. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  824. "unlinked=%d\n", urb, urb->unlinked);
  825. mutex_unlock(&u132->scheduler_lock);
  826. u132_hcd_giveback_urb(u132, endp, urb, 0);
  827. return;
  828. }
  829. }
  830. static void u132_hcd_configure_empty_sent(void *data, struct urb *urb, u8 *buf,
  831. int len, int toggle_bits, int error_count, int condition_code,
  832. int repeat_number, int halted, int skipped, int actual, int non_null)
  833. {
  834. struct u132_endp *endp = data;
  835. struct u132 *u132 = endp->u132;
  836. mutex_lock(&u132->scheduler_lock);
  837. if (u132->going > 1) {
  838. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  839. , u132->going);
  840. mutex_unlock(&u132->scheduler_lock);
  841. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  842. return;
  843. } else if (endp->dequeueing) {
  844. endp->dequeueing = 0;
  845. mutex_unlock(&u132->scheduler_lock);
  846. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  847. return;
  848. } else if (u132->going > 0) {
  849. dev_err(&u132->platform_dev->dev, "device is being removed "
  850. "urb=%p\n", urb);
  851. mutex_unlock(&u132->scheduler_lock);
  852. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  853. return;
  854. } else if (!urb->unlinked) {
  855. mutex_unlock(&u132->scheduler_lock);
  856. u132_hcd_giveback_urb(u132, endp, urb, 0);
  857. return;
  858. } else {
  859. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  860. "unlinked=%d\n", urb, urb->unlinked);
  861. mutex_unlock(&u132->scheduler_lock);
  862. u132_hcd_giveback_urb(u132, endp, urb, 0);
  863. return;
  864. }
  865. }
  866. static void u132_hcd_configure_input_recv(void *data, struct urb *urb, u8 *buf,
  867. int len, int toggle_bits, int error_count, int condition_code,
  868. int repeat_number, int halted, int skipped, int actual, int non_null)
  869. {
  870. struct u132_endp *endp = data;
  871. struct u132 *u132 = endp->u132;
  872. u8 address = u132->addr[endp->usb_addr].address;
  873. mutex_lock(&u132->scheduler_lock);
  874. if (u132->going > 1) {
  875. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  876. , u132->going);
  877. mutex_unlock(&u132->scheduler_lock);
  878. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  879. return;
  880. } else if (endp->dequeueing) {
  881. endp->dequeueing = 0;
  882. mutex_unlock(&u132->scheduler_lock);
  883. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  884. return;
  885. } else if (u132->going > 0) {
  886. dev_err(&u132->platform_dev->dev, "device is being removed "
  887. "urb=%p\n", urb);
  888. mutex_unlock(&u132->scheduler_lock);
  889. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  890. return;
  891. } else if (!urb->unlinked) {
  892. struct u132_ring *ring = endp->ring;
  893. u8 *u = urb->transfer_buffer;
  894. u8 *b = buf;
  895. int L = len;
  896. while (L-- > 0)
  897. *u++ = *b++;
  898. urb->actual_length = len;
  899. if ((condition_code == TD_CC_NOERROR) || ((condition_code ==
  900. TD_DATAUNDERRUN) && ((urb->transfer_flags &
  901. URB_SHORT_NOT_OK) == 0))) {
  902. int retval;
  903. mutex_unlock(&u132->scheduler_lock);
  904. retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
  905. ring->number, endp, urb, address,
  906. endp->usb_endp, 0x3,
  907. u132_hcd_configure_empty_sent);
  908. if (retval != 0)
  909. u132_hcd_giveback_urb(u132, endp, urb, retval);
  910. return;
  911. } else if (condition_code == TD_CC_STALL) {
  912. mutex_unlock(&u132->scheduler_lock);
  913. dev_warn(&u132->platform_dev->dev, "giving back SETUP I"
  914. "NPUT STALL urb %p\n", urb);
  915. u132_hcd_giveback_urb(u132, endp, urb,
  916. cc_to_error[condition_code]);
  917. return;
  918. } else {
  919. mutex_unlock(&u132->scheduler_lock);
  920. dev_err(&u132->platform_dev->dev, "giving back SETUP IN"
  921. "PUT %s urb %p\n", cc_to_text[condition_code],
  922. urb);
  923. u132_hcd_giveback_urb(u132, endp, urb,
  924. cc_to_error[condition_code]);
  925. return;
  926. }
  927. } else {
  928. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  929. "unlinked=%d\n", urb, urb->unlinked);
  930. mutex_unlock(&u132->scheduler_lock);
  931. u132_hcd_giveback_urb(u132, endp, urb, 0);
  932. return;
  933. }
  934. }
  935. static void u132_hcd_configure_empty_recv(void *data, struct urb *urb, u8 *buf,
  936. int len, int toggle_bits, int error_count, int condition_code,
  937. int repeat_number, int halted, int skipped, int actual, int non_null)
  938. {
  939. struct u132_endp *endp = data;
  940. struct u132 *u132 = endp->u132;
  941. mutex_lock(&u132->scheduler_lock);
  942. if (u132->going > 1) {
  943. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  944. , u132->going);
  945. mutex_unlock(&u132->scheduler_lock);
  946. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  947. return;
  948. } else if (endp->dequeueing) {
  949. endp->dequeueing = 0;
  950. mutex_unlock(&u132->scheduler_lock);
  951. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  952. return;
  953. } else if (u132->going > 0) {
  954. dev_err(&u132->platform_dev->dev, "device is being removed "
  955. "urb=%p\n", urb);
  956. mutex_unlock(&u132->scheduler_lock);
  957. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  958. return;
  959. } else if (!urb->unlinked) {
  960. mutex_unlock(&u132->scheduler_lock);
  961. u132_hcd_giveback_urb(u132, endp, urb, 0);
  962. return;
  963. } else {
  964. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  965. "unlinked=%d\n", urb, urb->unlinked);
  966. mutex_unlock(&u132->scheduler_lock);
  967. u132_hcd_giveback_urb(u132, endp, urb, 0);
  968. return;
  969. }
  970. }
  971. static void u132_hcd_configure_setup_sent(void *data, struct urb *urb, u8 *buf,
  972. int len, int toggle_bits, int error_count, int condition_code,
  973. int repeat_number, int halted, int skipped, int actual, int non_null)
  974. {
  975. struct u132_endp *endp = data;
  976. struct u132 *u132 = endp->u132;
  977. u8 address = u132->addr[endp->usb_addr].address;
  978. mutex_lock(&u132->scheduler_lock);
  979. if (u132->going > 1) {
  980. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  981. , u132->going);
  982. mutex_unlock(&u132->scheduler_lock);
  983. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  984. return;
  985. } else if (endp->dequeueing) {
  986. endp->dequeueing = 0;
  987. mutex_unlock(&u132->scheduler_lock);
  988. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  989. return;
  990. } else if (u132->going > 0) {
  991. dev_err(&u132->platform_dev->dev, "device is being removed "
  992. "urb=%p\n", urb);
  993. mutex_unlock(&u132->scheduler_lock);
  994. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  995. return;
  996. } else if (!urb->unlinked) {
  997. if (usb_pipein(urb->pipe)) {
  998. int retval;
  999. struct u132_ring *ring = endp->ring;
  1000. mutex_unlock(&u132->scheduler_lock);
  1001. retval = usb_ftdi_elan_edset_input(u132->platform_dev,
  1002. ring->number, endp, urb, address,
  1003. endp->usb_endp, 0,
  1004. u132_hcd_configure_input_recv);
  1005. if (retval != 0)
  1006. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1007. return;
  1008. } else {
  1009. int retval;
  1010. struct u132_ring *ring = endp->ring;
  1011. mutex_unlock(&u132->scheduler_lock);
  1012. retval = usb_ftdi_elan_edset_input(u132->platform_dev,
  1013. ring->number, endp, urb, address,
  1014. endp->usb_endp, 0,
  1015. u132_hcd_configure_empty_recv);
  1016. if (retval != 0)
  1017. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1018. return;
  1019. }
  1020. } else {
  1021. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1022. "unlinked=%d\n", urb, urb->unlinked);
  1023. mutex_unlock(&u132->scheduler_lock);
  1024. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1025. return;
  1026. }
  1027. }
  1028. static void u132_hcd_enumeration_empty_recv(void *data, struct urb *urb,
  1029. u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
  1030. int repeat_number, int halted, int skipped, int actual, int non_null)
  1031. {
  1032. struct u132_endp *endp = data;
  1033. struct u132 *u132 = endp->u132;
  1034. u8 address = u132->addr[endp->usb_addr].address;
  1035. struct u132_udev *udev = &u132->udev[address];
  1036. mutex_lock(&u132->scheduler_lock);
  1037. if (u132->going > 1) {
  1038. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1039. , u132->going);
  1040. mutex_unlock(&u132->scheduler_lock);
  1041. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  1042. return;
  1043. } else if (endp->dequeueing) {
  1044. endp->dequeueing = 0;
  1045. mutex_unlock(&u132->scheduler_lock);
  1046. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  1047. return;
  1048. } else if (u132->going > 0) {
  1049. dev_err(&u132->platform_dev->dev, "device is being removed "
  1050. "urb=%p\n", urb);
  1051. mutex_unlock(&u132->scheduler_lock);
  1052. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  1053. return;
  1054. } else if (!urb->unlinked) {
  1055. u132->addr[0].address = 0;
  1056. endp->usb_addr = udev->usb_addr;
  1057. mutex_unlock(&u132->scheduler_lock);
  1058. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1059. return;
  1060. } else {
  1061. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1062. "unlinked=%d\n", urb, urb->unlinked);
  1063. mutex_unlock(&u132->scheduler_lock);
  1064. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1065. return;
  1066. }
  1067. }
  1068. static void u132_hcd_enumeration_address_sent(void *data, struct urb *urb,
  1069. u8 *buf, int len, int toggle_bits, int error_count, int condition_code,
  1070. int repeat_number, int halted, int skipped, int actual, int non_null)
  1071. {
  1072. struct u132_endp *endp = data;
  1073. struct u132 *u132 = endp->u132;
  1074. mutex_lock(&u132->scheduler_lock);
  1075. if (u132->going > 1) {
  1076. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1077. , u132->going);
  1078. mutex_unlock(&u132->scheduler_lock);
  1079. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  1080. return;
  1081. } else if (endp->dequeueing) {
  1082. endp->dequeueing = 0;
  1083. mutex_unlock(&u132->scheduler_lock);
  1084. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  1085. return;
  1086. } else if (u132->going > 0) {
  1087. dev_err(&u132->platform_dev->dev, "device is being removed "
  1088. "urb=%p\n", urb);
  1089. mutex_unlock(&u132->scheduler_lock);
  1090. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  1091. return;
  1092. } else if (!urb->unlinked) {
  1093. int retval;
  1094. struct u132_ring *ring = endp->ring;
  1095. mutex_unlock(&u132->scheduler_lock);
  1096. retval = usb_ftdi_elan_edset_input(u132->platform_dev,
  1097. ring->number, endp, urb, 0, endp->usb_endp, 0,
  1098. u132_hcd_enumeration_empty_recv);
  1099. if (retval != 0)
  1100. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1101. return;
  1102. } else {
  1103. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1104. "unlinked=%d\n", urb, urb->unlinked);
  1105. mutex_unlock(&u132->scheduler_lock);
  1106. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1107. return;
  1108. }
  1109. }
  1110. static void u132_hcd_initial_empty_sent(void *data, struct urb *urb, u8 *buf,
  1111. int len, int toggle_bits, int error_count, int condition_code,
  1112. int repeat_number, int halted, int skipped, int actual, int non_null)
  1113. {
  1114. struct u132_endp *endp = data;
  1115. struct u132 *u132 = endp->u132;
  1116. mutex_lock(&u132->scheduler_lock);
  1117. if (u132->going > 1) {
  1118. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1119. , u132->going);
  1120. mutex_unlock(&u132->scheduler_lock);
  1121. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  1122. return;
  1123. } else if (endp->dequeueing) {
  1124. endp->dequeueing = 0;
  1125. mutex_unlock(&u132->scheduler_lock);
  1126. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  1127. return;
  1128. } else if (u132->going > 0) {
  1129. dev_err(&u132->platform_dev->dev, "device is being removed "
  1130. "urb=%p\n", urb);
  1131. mutex_unlock(&u132->scheduler_lock);
  1132. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  1133. return;
  1134. } else if (!urb->unlinked) {
  1135. mutex_unlock(&u132->scheduler_lock);
  1136. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1137. return;
  1138. } else {
  1139. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1140. "unlinked=%d\n", urb, urb->unlinked);
  1141. mutex_unlock(&u132->scheduler_lock);
  1142. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1143. return;
  1144. }
  1145. }
  1146. static void u132_hcd_initial_input_recv(void *data, struct urb *urb, u8 *buf,
  1147. int len, int toggle_bits, int error_count, int condition_code,
  1148. int repeat_number, int halted, int skipped, int actual, int non_null)
  1149. {
  1150. struct u132_endp *endp = data;
  1151. struct u132 *u132 = endp->u132;
  1152. u8 address = u132->addr[endp->usb_addr].address;
  1153. mutex_lock(&u132->scheduler_lock);
  1154. if (u132->going > 1) {
  1155. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1156. , u132->going);
  1157. mutex_unlock(&u132->scheduler_lock);
  1158. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  1159. return;
  1160. } else if (endp->dequeueing) {
  1161. endp->dequeueing = 0;
  1162. mutex_unlock(&u132->scheduler_lock);
  1163. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  1164. return;
  1165. } else if (u132->going > 0) {
  1166. dev_err(&u132->platform_dev->dev, "device is being removed "
  1167. "urb=%p\n", urb);
  1168. mutex_unlock(&u132->scheduler_lock);
  1169. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  1170. return;
  1171. } else if (!urb->unlinked) {
  1172. int retval;
  1173. struct u132_ring *ring = endp->ring;
  1174. u8 *u = urb->transfer_buffer;
  1175. u8 *b = buf;
  1176. int L = len;
  1177. while (L-- > 0)
  1178. *u++ = *b++;
  1179. urb->actual_length = len;
  1180. mutex_unlock(&u132->scheduler_lock);
  1181. retval = usb_ftdi_elan_edset_empty(u132->platform_dev,
  1182. ring->number, endp, urb, address, endp->usb_endp, 0x3,
  1183. u132_hcd_initial_empty_sent);
  1184. if (retval != 0)
  1185. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1186. return;
  1187. } else {
  1188. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1189. "unlinked=%d\n", urb, urb->unlinked);
  1190. mutex_unlock(&u132->scheduler_lock);
  1191. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1192. return;
  1193. }
  1194. }
  1195. static void u132_hcd_initial_setup_sent(void *data, struct urb *urb, u8 *buf,
  1196. int len, int toggle_bits, int error_count, int condition_code,
  1197. int repeat_number, int halted, int skipped, int actual, int non_null)
  1198. {
  1199. struct u132_endp *endp = data;
  1200. struct u132 *u132 = endp->u132;
  1201. u8 address = u132->addr[endp->usb_addr].address;
  1202. mutex_lock(&u132->scheduler_lock);
  1203. if (u132->going > 1) {
  1204. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1205. , u132->going);
  1206. mutex_unlock(&u132->scheduler_lock);
  1207. u132_hcd_forget_urb(u132, endp, urb, -ENODEV);
  1208. return;
  1209. } else if (endp->dequeueing) {
  1210. endp->dequeueing = 0;
  1211. mutex_unlock(&u132->scheduler_lock);
  1212. u132_hcd_giveback_urb(u132, endp, urb, -EINTR);
  1213. return;
  1214. } else if (u132->going > 0) {
  1215. dev_err(&u132->platform_dev->dev, "device is being removed "
  1216. "urb=%p\n", urb);
  1217. mutex_unlock(&u132->scheduler_lock);
  1218. u132_hcd_giveback_urb(u132, endp, urb, -ENODEV);
  1219. return;
  1220. } else if (!urb->unlinked) {
  1221. int retval;
  1222. struct u132_ring *ring = endp->ring;
  1223. mutex_unlock(&u132->scheduler_lock);
  1224. retval = usb_ftdi_elan_edset_input(u132->platform_dev,
  1225. ring->number, endp, urb, address, endp->usb_endp, 0,
  1226. u132_hcd_initial_input_recv);
  1227. if (retval != 0)
  1228. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1229. return;
  1230. } else {
  1231. dev_err(&u132->platform_dev->dev, "CALLBACK called urb=%p "
  1232. "unlinked=%d\n", urb, urb->unlinked);
  1233. mutex_unlock(&u132->scheduler_lock);
  1234. u132_hcd_giveback_urb(u132, endp, urb, 0);
  1235. return;
  1236. }
  1237. }
  1238. /*
  1239. * this work function is only executed from the work queue
  1240. *
  1241. */
  1242. static void u132_hcd_ring_work_scheduler(struct work_struct *work)
  1243. {
  1244. struct u132_ring *ring =
  1245. container_of(work, struct u132_ring, scheduler.work);
  1246. struct u132 *u132 = ring->u132;
  1247. mutex_lock(&u132->scheduler_lock);
  1248. if (ring->in_use) {
  1249. mutex_unlock(&u132->scheduler_lock);
  1250. u132_ring_put_kref(u132, ring);
  1251. return;
  1252. } else if (ring->curr_endp) {
  1253. struct u132_endp *last_endp = ring->curr_endp;
  1254. struct list_head *scan;
  1255. struct list_head *head = &last_endp->endp_ring;
  1256. unsigned long wakeup = 0;
  1257. list_for_each(scan, head) {
  1258. struct u132_endp *endp = list_entry(scan,
  1259. struct u132_endp, endp_ring);
  1260. if (endp->queue_next == endp->queue_last) {
  1261. } else if ((endp->delayed == 0)
  1262. || time_after_eq(jiffies, endp->jiffies)) {
  1263. ring->curr_endp = endp;
  1264. u132_endp_cancel_work(u132, last_endp);
  1265. u132_endp_queue_work(u132, last_endp, 0);
  1266. mutex_unlock(&u132->scheduler_lock);
  1267. u132_ring_put_kref(u132, ring);
  1268. return;
  1269. } else {
  1270. unsigned long delta = endp->jiffies - jiffies;
  1271. if (delta > wakeup)
  1272. wakeup = delta;
  1273. }
  1274. }
  1275. if (last_endp->queue_next == last_endp->queue_last) {
  1276. } else if ((last_endp->delayed == 0) || time_after_eq(jiffies,
  1277. last_endp->jiffies)) {
  1278. u132_endp_cancel_work(u132, last_endp);
  1279. u132_endp_queue_work(u132, last_endp, 0);
  1280. mutex_unlock(&u132->scheduler_lock);
  1281. u132_ring_put_kref(u132, ring);
  1282. return;
  1283. } else {
  1284. unsigned long delta = last_endp->jiffies - jiffies;
  1285. if (delta > wakeup)
  1286. wakeup = delta;
  1287. }
  1288. if (wakeup > 0) {
  1289. u132_ring_requeue_work(u132, ring, wakeup);
  1290. mutex_unlock(&u132->scheduler_lock);
  1291. return;
  1292. } else {
  1293. mutex_unlock(&u132->scheduler_lock);
  1294. u132_ring_put_kref(u132, ring);
  1295. return;
  1296. }
  1297. } else {
  1298. mutex_unlock(&u132->scheduler_lock);
  1299. u132_ring_put_kref(u132, ring);
  1300. return;
  1301. }
  1302. }
  1303. static void u132_hcd_endp_work_scheduler(struct work_struct *work)
  1304. {
  1305. struct u132_ring *ring;
  1306. struct u132_endp *endp =
  1307. container_of(work, struct u132_endp, scheduler.work);
  1308. struct u132 *u132 = endp->u132;
  1309. mutex_lock(&u132->scheduler_lock);
  1310. ring = endp->ring;
  1311. if (endp->edset_flush) {
  1312. endp->edset_flush = 0;
  1313. if (endp->dequeueing)
  1314. usb_ftdi_elan_edset_flush(u132->platform_dev,
  1315. ring->number, endp);
  1316. mutex_unlock(&u132->scheduler_lock);
  1317. u132_endp_put_kref(u132, endp);
  1318. return;
  1319. } else if (endp->active) {
  1320. mutex_unlock(&u132->scheduler_lock);
  1321. u132_endp_put_kref(u132, endp);
  1322. return;
  1323. } else if (ring->in_use) {
  1324. mutex_unlock(&u132->scheduler_lock);
  1325. u132_endp_put_kref(u132, endp);
  1326. return;
  1327. } else if (endp->queue_next == endp->queue_last) {
  1328. mutex_unlock(&u132->scheduler_lock);
  1329. u132_endp_put_kref(u132, endp);
  1330. return;
  1331. } else if (endp->pipetype == PIPE_INTERRUPT) {
  1332. u8 address = u132->addr[endp->usb_addr].address;
  1333. if (ring->in_use) {
  1334. mutex_unlock(&u132->scheduler_lock);
  1335. u132_endp_put_kref(u132, endp);
  1336. return;
  1337. } else {
  1338. int retval;
  1339. struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
  1340. endp->queue_next];
  1341. endp->active = 1;
  1342. ring->curr_endp = endp;
  1343. ring->in_use = 1;
  1344. mutex_unlock(&u132->scheduler_lock);
  1345. retval = edset_single(u132, ring, endp, urb, address,
  1346. endp->toggle_bits, u132_hcd_interrupt_recv);
  1347. if (retval != 0)
  1348. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1349. return;
  1350. }
  1351. } else if (endp->pipetype == PIPE_CONTROL) {
  1352. u8 address = u132->addr[endp->usb_addr].address;
  1353. if (ring->in_use) {
  1354. mutex_unlock(&u132->scheduler_lock);
  1355. u132_endp_put_kref(u132, endp);
  1356. return;
  1357. } else if (address == 0) {
  1358. int retval;
  1359. struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
  1360. endp->queue_next];
  1361. endp->active = 1;
  1362. ring->curr_endp = endp;
  1363. ring->in_use = 1;
  1364. mutex_unlock(&u132->scheduler_lock);
  1365. retval = edset_setup(u132, ring, endp, urb, address,
  1366. 0x2, u132_hcd_initial_setup_sent);
  1367. if (retval != 0)
  1368. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1369. return;
  1370. } else if (endp->usb_addr == 0) {
  1371. int retval;
  1372. struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
  1373. endp->queue_next];
  1374. endp->active = 1;
  1375. ring->curr_endp = endp;
  1376. ring->in_use = 1;
  1377. mutex_unlock(&u132->scheduler_lock);
  1378. retval = edset_setup(u132, ring, endp, urb, 0, 0x2,
  1379. u132_hcd_enumeration_address_sent);
  1380. if (retval != 0)
  1381. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1382. return;
  1383. } else {
  1384. int retval;
  1385. struct urb *urb = endp->urb_list[ENDP_QUEUE_MASK &
  1386. endp->queue_next];
  1387. address = u132->addr[endp->usb_addr].address;
  1388. endp->active = 1;
  1389. ring->curr_endp = endp;
  1390. ring->in_use = 1;
  1391. mutex_unlock(&u132->scheduler_lock);
  1392. retval = edset_setup(u132, ring, endp, urb, address,
  1393. 0x2, u132_hcd_configure_setup_sent);
  1394. if (retval != 0)
  1395. u132_hcd_giveback_urb(u132, endp, urb, retval);
  1396. return;
  1397. }
  1398. } else {
  1399. if (endp->input) {
  1400. u8 address = u132->addr[endp->usb_addr].address;
  1401. if (ring->in_use) {
  1402. mutex_unlock(&u132->scheduler_lock);
  1403. u132_endp_put_kref(u132, endp);
  1404. return;
  1405. } else {
  1406. int retval;
  1407. struct urb *urb = endp->urb_list[
  1408. ENDP_QUEUE_MASK & endp->queue_next];
  1409. endp->active = 1;
  1410. ring->curr_endp = endp;
  1411. ring->in_use = 1;
  1412. mutex_unlock(&u132->scheduler_lock);
  1413. retval = edset_input(u132, ring, endp, urb,
  1414. address, endp->toggle_bits,
  1415. u132_hcd_bulk_input_recv);
  1416. if (retval == 0) {
  1417. } else
  1418. u132_hcd_giveback_urb(u132, endp, urb,
  1419. retval);
  1420. return;
  1421. }
  1422. } else { /* output pipe */
  1423. u8 address = u132->addr[endp->usb_addr].address;
  1424. if (ring->in_use) {
  1425. mutex_unlock(&u132->scheduler_lock);
  1426. u132_endp_put_kref(u132, endp);
  1427. return;
  1428. } else {
  1429. int retval;
  1430. struct urb *urb = endp->urb_list[
  1431. ENDP_QUEUE_MASK & endp->queue_next];
  1432. endp->active = 1;
  1433. ring->curr_endp = endp;
  1434. ring->in_use = 1;
  1435. mutex_unlock(&u132->scheduler_lock);
  1436. retval = edset_output(u132, ring, endp, urb,
  1437. address, endp->toggle_bits,
  1438. u132_hcd_bulk_output_sent);
  1439. if (retval == 0) {
  1440. } else
  1441. u132_hcd_giveback_urb(u132, endp, urb,
  1442. retval);
  1443. return;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. #ifdef CONFIG_PM
  1449. static void port_power(struct u132 *u132, int pn, int is_on)
  1450. {
  1451. u132->port[pn].power = is_on;
  1452. }
  1453. #endif
  1454. static void u132_power(struct u132 *u132, int is_on)
  1455. {
  1456. struct usb_hcd *hcd = u132_to_hcd(u132)
  1457. ; /* hub is inactive unless the port is powered */
  1458. if (is_on) {
  1459. if (u132->power)
  1460. return;
  1461. u132->power = 1;
  1462. } else {
  1463. u132->power = 0;
  1464. hcd->state = HC_STATE_HALT;
  1465. }
  1466. }
  1467. static int u132_periodic_reinit(struct u132 *u132)
  1468. {
  1469. int retval;
  1470. u32 fi = u132->hc_fminterval & 0x03fff;
  1471. u32 fit;
  1472. u32 fminterval;
  1473. retval = u132_read_pcimem(u132, fminterval, &fminterval);
  1474. if (retval)
  1475. return retval;
  1476. fit = fminterval & FIT;
  1477. retval = u132_write_pcimem(u132, fminterval,
  1478. (fit ^ FIT) | u132->hc_fminterval);
  1479. if (retval)
  1480. return retval;
  1481. return u132_write_pcimem(u132, periodicstart,
  1482. ((9 * fi) / 10) & 0x3fff);
  1483. }
  1484. static char *hcfs2string(int state)
  1485. {
  1486. switch (state) {
  1487. case OHCI_USB_RESET:
  1488. return "reset";
  1489. case OHCI_USB_RESUME:
  1490. return "resume";
  1491. case OHCI_USB_OPER:
  1492. return "operational";
  1493. case OHCI_USB_SUSPEND:
  1494. return "suspend";
  1495. }
  1496. return "?";
  1497. }
  1498. static int u132_init(struct u132 *u132)
  1499. {
  1500. int retval;
  1501. u32 control;
  1502. u132_disable(u132);
  1503. u132->next_statechange = jiffies;
  1504. retval = u132_write_pcimem(u132, intrdisable, OHCI_INTR_MIE);
  1505. if (retval)
  1506. return retval;
  1507. retval = u132_read_pcimem(u132, control, &control);
  1508. if (retval)
  1509. return retval;
  1510. if (u132->num_ports == 0) {
  1511. u32 rh_a = -1;
  1512. retval = u132_read_pcimem(u132, roothub.a, &rh_a);
  1513. if (retval)
  1514. return retval;
  1515. u132->num_ports = rh_a & RH_A_NDP;
  1516. retval = read_roothub_info(u132);
  1517. if (retval)
  1518. return retval;
  1519. }
  1520. if (u132->num_ports > MAX_U132_PORTS)
  1521. return -EINVAL;
  1522. return 0;
  1523. }
  1524. /* Start an OHCI controller, set the BUS operational
  1525. * resets USB and controller
  1526. * enable interrupts
  1527. */
  1528. static int u132_run(struct u132 *u132)
  1529. {
  1530. int retval;
  1531. u32 control;
  1532. u32 status;
  1533. u32 fminterval;
  1534. u32 periodicstart;
  1535. u32 cmdstatus;
  1536. u32 roothub_a;
  1537. int mask = OHCI_INTR_INIT;
  1538. int first = u132->hc_fminterval == 0;
  1539. int sleep_time = 0;
  1540. int reset_timeout = 30; /* ... allow extra time */
  1541. u132_disable(u132);
  1542. if (first) {
  1543. u32 temp;
  1544. retval = u132_read_pcimem(u132, fminterval, &temp);
  1545. if (retval)
  1546. return retval;
  1547. u132->hc_fminterval = temp & 0x3fff;
  1548. u132->hc_fminterval |= FSMP(u132->hc_fminterval) << 16;
  1549. }
  1550. retval = u132_read_pcimem(u132, control, &u132->hc_control);
  1551. if (retval)
  1552. return retval;
  1553. dev_info(&u132->platform_dev->dev, "resetting from state '%s', control "
  1554. "= %08X\n", hcfs2string(u132->hc_control & OHCI_CTRL_HCFS),
  1555. u132->hc_control);
  1556. switch (u132->hc_control & OHCI_CTRL_HCFS) {
  1557. case OHCI_USB_OPER:
  1558. sleep_time = 0;
  1559. break;
  1560. case OHCI_USB_SUSPEND:
  1561. case OHCI_USB_RESUME:
  1562. u132->hc_control &= OHCI_CTRL_RWC;
  1563. u132->hc_control |= OHCI_USB_RESUME;
  1564. sleep_time = 10;
  1565. break;
  1566. default:
  1567. u132->hc_control &= OHCI_CTRL_RWC;
  1568. u132->hc_control |= OHCI_USB_RESET;
  1569. sleep_time = 50;
  1570. break;
  1571. }
  1572. retval = u132_write_pcimem(u132, control, u132->hc_control);
  1573. if (retval)
  1574. return retval;
  1575. retval = u132_read_pcimem(u132, control, &control);
  1576. if (retval)
  1577. return retval;
  1578. msleep(sleep_time);
  1579. retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
  1580. if (retval)
  1581. return retval;
  1582. if (!(roothub_a & RH_A_NPS)) {
  1583. int temp; /* power down each port */
  1584. for (temp = 0; temp < u132->num_ports; temp++) {
  1585. retval = u132_write_pcimem(u132,
  1586. roothub.portstatus[temp], RH_PS_LSDA);
  1587. if (retval)
  1588. return retval;
  1589. }
  1590. }
  1591. retval = u132_read_pcimem(u132, control, &control);
  1592. if (retval)
  1593. return retval;
  1594. retry:
  1595. retval = u132_read_pcimem(u132, cmdstatus, &status);
  1596. if (retval)
  1597. return retval;
  1598. retval = u132_write_pcimem(u132, cmdstatus, OHCI_HCR);
  1599. if (retval)
  1600. return retval;
  1601. extra: {
  1602. retval = u132_read_pcimem(u132, cmdstatus, &status);
  1603. if (retval)
  1604. return retval;
  1605. if (0 != (status & OHCI_HCR)) {
  1606. if (--reset_timeout == 0) {
  1607. dev_err(&u132->platform_dev->dev, "USB HC reset"
  1608. " timed out!\n");
  1609. return -ENODEV;
  1610. } else {
  1611. msleep(5);
  1612. goto extra;
  1613. }
  1614. }
  1615. }
  1616. if (u132->flags & OHCI_QUIRK_INITRESET) {
  1617. retval = u132_write_pcimem(u132, control, u132->hc_control);
  1618. if (retval)
  1619. return retval;
  1620. retval = u132_read_pcimem(u132, control, &control);
  1621. if (retval)
  1622. return retval;
  1623. }
  1624. retval = u132_write_pcimem(u132, ed_controlhead, 0x00000000);
  1625. if (retval)
  1626. return retval;
  1627. retval = u132_write_pcimem(u132, ed_bulkhead, 0x11000000);
  1628. if (retval)
  1629. return retval;
  1630. retval = u132_write_pcimem(u132, hcca, 0x00000000);
  1631. if (retval)
  1632. return retval;
  1633. retval = u132_periodic_reinit(u132);
  1634. if (retval)
  1635. return retval;
  1636. retval = u132_read_pcimem(u132, fminterval, &fminterval);
  1637. if (retval)
  1638. return retval;
  1639. retval = u132_read_pcimem(u132, periodicstart, &periodicstart);
  1640. if (retval)
  1641. return retval;
  1642. if (0 == (fminterval & 0x3fff0000) || 0 == periodicstart) {
  1643. if (!(u132->flags & OHCI_QUIRK_INITRESET)) {
  1644. u132->flags |= OHCI_QUIRK_INITRESET;
  1645. goto retry;
  1646. } else
  1647. dev_err(&u132->platform_dev->dev, "init err(%08x %04x)"
  1648. "\n", fminterval, periodicstart);
  1649. } /* start controller operations */
  1650. u132->hc_control &= OHCI_CTRL_RWC;
  1651. u132->hc_control |= OHCI_CONTROL_INIT | OHCI_CTRL_BLE | OHCI_USB_OPER;
  1652. retval = u132_write_pcimem(u132, control, u132->hc_control);
  1653. if (retval)
  1654. return retval;
  1655. retval = u132_write_pcimem(u132, cmdstatus, OHCI_BLF);
  1656. if (retval)
  1657. return retval;
  1658. retval = u132_read_pcimem(u132, cmdstatus, &cmdstatus);
  1659. if (retval)
  1660. return retval;
  1661. retval = u132_read_pcimem(u132, control, &control);
  1662. if (retval)
  1663. return retval;
  1664. u132_to_hcd(u132)->state = HC_STATE_RUNNING;
  1665. retval = u132_write_pcimem(u132, roothub.status, RH_HS_DRWE);
  1666. if (retval)
  1667. return retval;
  1668. retval = u132_write_pcimem(u132, intrstatus, mask);
  1669. if (retval)
  1670. return retval;
  1671. retval = u132_write_pcimem(u132, intrdisable,
  1672. OHCI_INTR_MIE | OHCI_INTR_OC | OHCI_INTR_RHSC | OHCI_INTR_FNO |
  1673. OHCI_INTR_UE | OHCI_INTR_RD | OHCI_INTR_SF | OHCI_INTR_WDH |
  1674. OHCI_INTR_SO);
  1675. if (retval)
  1676. return retval; /* handle root hub init quirks ... */
  1677. retval = u132_read_pcimem(u132, roothub.a, &roothub_a);
  1678. if (retval)
  1679. return retval;
  1680. roothub_a &= ~(RH_A_PSM | RH_A_OCPM);
  1681. if (u132->flags & OHCI_QUIRK_SUPERIO) {
  1682. roothub_a |= RH_A_NOCP;
  1683. roothub_a &= ~(RH_A_POTPGT | RH_A_NPS);
  1684. retval = u132_write_pcimem(u132, roothub.a, roothub_a);
  1685. if (retval)
  1686. return retval;
  1687. } else if ((u132->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
  1688. roothub_a |= RH_A_NPS;
  1689. retval = u132_write_pcimem(u132, roothub.a, roothub_a);
  1690. if (retval)
  1691. return retval;
  1692. }
  1693. retval = u132_write_pcimem(u132, roothub.status, RH_HS_LPSC);
  1694. if (retval)
  1695. return retval;
  1696. retval = u132_write_pcimem(u132, roothub.b,
  1697. (roothub_a & RH_A_NPS) ? 0 : RH_B_PPCM);
  1698. if (retval)
  1699. return retval;
  1700. retval = u132_read_pcimem(u132, control, &control);
  1701. if (retval)
  1702. return retval;
  1703. mdelay((roothub_a >> 23) & 0x1fe);
  1704. u132_to_hcd(u132)->state = HC_STATE_RUNNING;
  1705. return 0;
  1706. }
  1707. static void u132_hcd_stop(struct usb_hcd *hcd)
  1708. {
  1709. struct u132 *u132 = hcd_to_u132(hcd);
  1710. if (u132->going > 1) {
  1711. dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p) has b"
  1712. "een removed %d\n", u132, hcd, u132->going);
  1713. } else if (u132->going > 0) {
  1714. dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
  1715. "ed\n", hcd);
  1716. } else {
  1717. mutex_lock(&u132->sw_lock);
  1718. msleep(100);
  1719. u132_power(u132, 0);
  1720. mutex_unlock(&u132->sw_lock);
  1721. }
  1722. }
  1723. static int u132_hcd_start(struct usb_hcd *hcd)
  1724. {
  1725. struct u132 *u132 = hcd_to_u132(hcd);
  1726. if (u132->going > 1) {
  1727. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1728. , u132->going);
  1729. return -ENODEV;
  1730. } else if (u132->going > 0) {
  1731. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  1732. return -ESHUTDOWN;
  1733. } else if (hcd->self.controller) {
  1734. int retval;
  1735. struct platform_device *pdev =
  1736. to_platform_device(hcd->self.controller);
  1737. u16 vendor = ((struct u132_platform_data *)
  1738. dev_get_platdata(&pdev->dev))->vendor;
  1739. u16 device = ((struct u132_platform_data *)
  1740. dev_get_platdata(&pdev->dev))->device;
  1741. mutex_lock(&u132->sw_lock);
  1742. msleep(10);
  1743. if (vendor == PCI_VENDOR_ID_AMD && device == 0x740c) {
  1744. u132->flags = OHCI_QUIRK_AMD756;
  1745. } else if (vendor == PCI_VENDOR_ID_OPTI && device == 0xc861) {
  1746. dev_err(&u132->platform_dev->dev, "WARNING: OPTi workar"
  1747. "ounds unavailable\n");
  1748. } else if (vendor == PCI_VENDOR_ID_COMPAQ && device == 0xa0f8)
  1749. u132->flags |= OHCI_QUIRK_ZFMICRO;
  1750. retval = u132_run(u132);
  1751. if (retval) {
  1752. u132_disable(u132);
  1753. u132->going = 1;
  1754. }
  1755. msleep(100);
  1756. mutex_unlock(&u132->sw_lock);
  1757. return retval;
  1758. } else {
  1759. dev_err(&u132->platform_dev->dev, "platform_device missing\n");
  1760. return -ENODEV;
  1761. }
  1762. }
  1763. static int u132_hcd_reset(struct usb_hcd *hcd)
  1764. {
  1765. struct u132 *u132 = hcd_to_u132(hcd);
  1766. if (u132->going > 1) {
  1767. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  1768. , u132->going);
  1769. return -ENODEV;
  1770. } else if (u132->going > 0) {
  1771. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  1772. return -ESHUTDOWN;
  1773. } else {
  1774. int retval;
  1775. mutex_lock(&u132->sw_lock);
  1776. retval = u132_init(u132);
  1777. if (retval) {
  1778. u132_disable(u132);
  1779. u132->going = 1;
  1780. }
  1781. mutex_unlock(&u132->sw_lock);
  1782. return retval;
  1783. }
  1784. }
  1785. static int create_endpoint_and_queue_int(struct u132 *u132,
  1786. struct u132_udev *udev, struct urb *urb,
  1787. struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
  1788. gfp_t mem_flags)
  1789. {
  1790. struct u132_ring *ring;
  1791. unsigned long irqs;
  1792. int rc;
  1793. u8 endp_number;
  1794. struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
  1795. if (!endp)
  1796. return -ENOMEM;
  1797. spin_lock_init(&endp->queue_lock.slock);
  1798. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  1799. rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
  1800. if (rc) {
  1801. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  1802. kfree(endp);
  1803. return rc;
  1804. }
  1805. endp_number = ++u132->num_endpoints;
  1806. urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
  1807. INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
  1808. INIT_LIST_HEAD(&endp->urb_more);
  1809. ring = endp->ring = &u132->ring[0];
  1810. if (ring->curr_endp) {
  1811. list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
  1812. } else {
  1813. INIT_LIST_HEAD(&endp->endp_ring);
  1814. ring->curr_endp = endp;
  1815. }
  1816. ring->length += 1;
  1817. endp->dequeueing = 0;
  1818. endp->edset_flush = 0;
  1819. endp->active = 0;
  1820. endp->delayed = 0;
  1821. endp->endp_number = endp_number;
  1822. endp->u132 = u132;
  1823. endp->hep = urb->ep;
  1824. endp->pipetype = usb_pipetype(urb->pipe);
  1825. u132_endp_init_kref(u132, endp);
  1826. if (usb_pipein(urb->pipe)) {
  1827. endp->toggle_bits = 0x2;
  1828. usb_settoggle(udev->usb_device, usb_endp, 0, 0);
  1829. endp->input = 1;
  1830. endp->output = 0;
  1831. udev->endp_number_in[usb_endp] = endp_number;
  1832. u132_udev_get_kref(u132, udev);
  1833. } else {
  1834. endp->toggle_bits = 0x2;
  1835. usb_settoggle(udev->usb_device, usb_endp, 1, 0);
  1836. endp->input = 0;
  1837. endp->output = 1;
  1838. udev->endp_number_out[usb_endp] = endp_number;
  1839. u132_udev_get_kref(u132, udev);
  1840. }
  1841. urb->hcpriv = u132;
  1842. endp->delayed = 1;
  1843. endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
  1844. endp->udev_number = address;
  1845. endp->usb_addr = usb_addr;
  1846. endp->usb_endp = usb_endp;
  1847. endp->queue_size = 1;
  1848. endp->queue_last = 0;
  1849. endp->queue_next = 0;
  1850. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  1851. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  1852. u132_endp_queue_work(u132, endp, msecs_to_jiffies(urb->interval));
  1853. return 0;
  1854. }
  1855. static int queue_int_on_old_endpoint(struct u132 *u132,
  1856. struct u132_udev *udev, struct urb *urb,
  1857. struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
  1858. u8 usb_endp, u8 address)
  1859. {
  1860. urb->hcpriv = u132;
  1861. endp->delayed = 1;
  1862. endp->jiffies = jiffies + msecs_to_jiffies(urb->interval);
  1863. if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
  1864. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  1865. } else {
  1866. struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
  1867. GFP_ATOMIC);
  1868. if (urbq == NULL) {
  1869. endp->queue_size -= 1;
  1870. return -ENOMEM;
  1871. } else {
  1872. list_add_tail(&urbq->urb_more, &endp->urb_more);
  1873. urbq->urb = urb;
  1874. }
  1875. }
  1876. return 0;
  1877. }
  1878. static int create_endpoint_and_queue_bulk(struct u132 *u132,
  1879. struct u132_udev *udev, struct urb *urb,
  1880. struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp, u8 address,
  1881. gfp_t mem_flags)
  1882. {
  1883. int ring_number;
  1884. struct u132_ring *ring;
  1885. unsigned long irqs;
  1886. int rc;
  1887. u8 endp_number;
  1888. struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
  1889. if (!endp)
  1890. return -ENOMEM;
  1891. spin_lock_init(&endp->queue_lock.slock);
  1892. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  1893. rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
  1894. if (rc) {
  1895. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  1896. kfree(endp);
  1897. return rc;
  1898. }
  1899. endp_number = ++u132->num_endpoints;
  1900. urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
  1901. INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
  1902. INIT_LIST_HEAD(&endp->urb_more);
  1903. endp->dequeueing = 0;
  1904. endp->edset_flush = 0;
  1905. endp->active = 0;
  1906. endp->delayed = 0;
  1907. endp->endp_number = endp_number;
  1908. endp->u132 = u132;
  1909. endp->hep = urb->ep;
  1910. endp->pipetype = usb_pipetype(urb->pipe);
  1911. u132_endp_init_kref(u132, endp);
  1912. if (usb_pipein(urb->pipe)) {
  1913. endp->toggle_bits = 0x2;
  1914. usb_settoggle(udev->usb_device, usb_endp, 0, 0);
  1915. ring_number = 3;
  1916. endp->input = 1;
  1917. endp->output = 0;
  1918. udev->endp_number_in[usb_endp] = endp_number;
  1919. u132_udev_get_kref(u132, udev);
  1920. } else {
  1921. endp->toggle_bits = 0x2;
  1922. usb_settoggle(udev->usb_device, usb_endp, 1, 0);
  1923. ring_number = 2;
  1924. endp->input = 0;
  1925. endp->output = 1;
  1926. udev->endp_number_out[usb_endp] = endp_number;
  1927. u132_udev_get_kref(u132, udev);
  1928. }
  1929. ring = endp->ring = &u132->ring[ring_number - 1];
  1930. if (ring->curr_endp) {
  1931. list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
  1932. } else {
  1933. INIT_LIST_HEAD(&endp->endp_ring);
  1934. ring->curr_endp = endp;
  1935. }
  1936. ring->length += 1;
  1937. urb->hcpriv = u132;
  1938. endp->udev_number = address;
  1939. endp->usb_addr = usb_addr;
  1940. endp->usb_endp = usb_endp;
  1941. endp->queue_size = 1;
  1942. endp->queue_last = 0;
  1943. endp->queue_next = 0;
  1944. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  1945. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  1946. u132_endp_queue_work(u132, endp, 0);
  1947. return 0;
  1948. }
  1949. static int queue_bulk_on_old_endpoint(struct u132 *u132, struct u132_udev *udev,
  1950. struct urb *urb,
  1951. struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
  1952. u8 usb_endp, u8 address)
  1953. {
  1954. urb->hcpriv = u132;
  1955. if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
  1956. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  1957. } else {
  1958. struct u132_urbq *urbq = kmalloc(sizeof(struct u132_urbq),
  1959. GFP_ATOMIC);
  1960. if (urbq == NULL) {
  1961. endp->queue_size -= 1;
  1962. return -ENOMEM;
  1963. } else {
  1964. list_add_tail(&urbq->urb_more, &endp->urb_more);
  1965. urbq->urb = urb;
  1966. }
  1967. }
  1968. return 0;
  1969. }
  1970. static int create_endpoint_and_queue_control(struct u132 *u132,
  1971. struct urb *urb,
  1972. struct usb_device *usb_dev, u8 usb_addr, u8 usb_endp,
  1973. gfp_t mem_flags)
  1974. {
  1975. struct u132_ring *ring;
  1976. unsigned long irqs;
  1977. int rc;
  1978. u8 endp_number;
  1979. struct u132_endp *endp = kmalloc(sizeof(struct u132_endp), mem_flags);
  1980. if (!endp)
  1981. return -ENOMEM;
  1982. spin_lock_init(&endp->queue_lock.slock);
  1983. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  1984. rc = usb_hcd_link_urb_to_ep(u132_to_hcd(u132), urb);
  1985. if (rc) {
  1986. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  1987. kfree(endp);
  1988. return rc;
  1989. }
  1990. endp_number = ++u132->num_endpoints;
  1991. urb->ep->hcpriv = u132->endp[endp_number - 1] = endp;
  1992. INIT_DELAYED_WORK(&endp->scheduler, u132_hcd_endp_work_scheduler);
  1993. INIT_LIST_HEAD(&endp->urb_more);
  1994. ring = endp->ring = &u132->ring[0];
  1995. if (ring->curr_endp) {
  1996. list_add_tail(&endp->endp_ring, &ring->curr_endp->endp_ring);
  1997. } else {
  1998. INIT_LIST_HEAD(&endp->endp_ring);
  1999. ring->curr_endp = endp;
  2000. }
  2001. ring->length += 1;
  2002. endp->dequeueing = 0;
  2003. endp->edset_flush = 0;
  2004. endp->active = 0;
  2005. endp->delayed = 0;
  2006. endp->endp_number = endp_number;
  2007. endp->u132 = u132;
  2008. endp->hep = urb->ep;
  2009. u132_endp_init_kref(u132, endp);
  2010. u132_endp_get_kref(u132, endp);
  2011. if (usb_addr == 0) {
  2012. u8 address = u132->addr[usb_addr].address;
  2013. struct u132_udev *udev = &u132->udev[address];
  2014. endp->udev_number = address;
  2015. endp->usb_addr = usb_addr;
  2016. endp->usb_endp = usb_endp;
  2017. endp->input = 1;
  2018. endp->output = 1;
  2019. endp->pipetype = usb_pipetype(urb->pipe);
  2020. u132_udev_init_kref(u132, udev);
  2021. u132_udev_get_kref(u132, udev);
  2022. udev->endp_number_in[usb_endp] = endp_number;
  2023. udev->endp_number_out[usb_endp] = endp_number;
  2024. urb->hcpriv = u132;
  2025. endp->queue_size = 1;
  2026. endp->queue_last = 0;
  2027. endp->queue_next = 0;
  2028. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  2029. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2030. u132_endp_queue_work(u132, endp, 0);
  2031. return 0;
  2032. } else { /*(usb_addr > 0) */
  2033. u8 address = u132->addr[usb_addr].address;
  2034. struct u132_udev *udev = &u132->udev[address];
  2035. endp->udev_number = address;
  2036. endp->usb_addr = usb_addr;
  2037. endp->usb_endp = usb_endp;
  2038. endp->input = 1;
  2039. endp->output = 1;
  2040. endp->pipetype = usb_pipetype(urb->pipe);
  2041. u132_udev_get_kref(u132, udev);
  2042. udev->enumeration = 2;
  2043. udev->endp_number_in[usb_endp] = endp_number;
  2044. udev->endp_number_out[usb_endp] = endp_number;
  2045. urb->hcpriv = u132;
  2046. endp->queue_size = 1;
  2047. endp->queue_last = 0;
  2048. endp->queue_next = 0;
  2049. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] = urb;
  2050. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2051. u132_endp_queue_work(u132, endp, 0);
  2052. return 0;
  2053. }
  2054. }
  2055. static int queue_control_on_old_endpoint(struct u132 *u132,
  2056. struct urb *urb,
  2057. struct usb_device *usb_dev, struct u132_endp *endp, u8 usb_addr,
  2058. u8 usb_endp)
  2059. {
  2060. if (usb_addr == 0) {
  2061. if (usb_pipein(urb->pipe)) {
  2062. urb->hcpriv = u132;
  2063. if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
  2064. endp->urb_list[ENDP_QUEUE_MASK &
  2065. endp->queue_last++] = urb;
  2066. } else {
  2067. struct u132_urbq *urbq =
  2068. kmalloc(sizeof(struct u132_urbq),
  2069. GFP_ATOMIC);
  2070. if (urbq == NULL) {
  2071. endp->queue_size -= 1;
  2072. return -ENOMEM;
  2073. } else {
  2074. list_add_tail(&urbq->urb_more,
  2075. &endp->urb_more);
  2076. urbq->urb = urb;
  2077. }
  2078. }
  2079. return 0;
  2080. } else { /* usb_pipeout(urb->pipe) */
  2081. struct u132_addr *addr = &u132->addr[usb_dev->devnum];
  2082. int I = MAX_U132_UDEVS;
  2083. int i = 0;
  2084. while (--I > 0) {
  2085. struct u132_udev *udev = &u132->udev[++i];
  2086. if (udev->usb_device) {
  2087. continue;
  2088. } else {
  2089. udev->enumeration = 1;
  2090. u132->addr[0].address = i;
  2091. endp->udev_number = i;
  2092. udev->udev_number = i;
  2093. udev->usb_addr = usb_dev->devnum;
  2094. u132_udev_init_kref(u132, udev);
  2095. udev->endp_number_in[usb_endp] =
  2096. endp->endp_number;
  2097. u132_udev_get_kref(u132, udev);
  2098. udev->endp_number_out[usb_endp] =
  2099. endp->endp_number;
  2100. udev->usb_device = usb_dev;
  2101. ((u8 *) (urb->setup_packet))[2] =
  2102. addr->address = i;
  2103. u132_udev_get_kref(u132, udev);
  2104. break;
  2105. }
  2106. }
  2107. if (I == 0) {
  2108. dev_err(&u132->platform_dev->dev, "run out of d"
  2109. "evice space\n");
  2110. return -EINVAL;
  2111. }
  2112. urb->hcpriv = u132;
  2113. if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
  2114. endp->urb_list[ENDP_QUEUE_MASK &
  2115. endp->queue_last++] = urb;
  2116. } else {
  2117. struct u132_urbq *urbq =
  2118. kmalloc(sizeof(struct u132_urbq),
  2119. GFP_ATOMIC);
  2120. if (urbq == NULL) {
  2121. endp->queue_size -= 1;
  2122. return -ENOMEM;
  2123. } else {
  2124. list_add_tail(&urbq->urb_more,
  2125. &endp->urb_more);
  2126. urbq->urb = urb;
  2127. }
  2128. }
  2129. return 0;
  2130. }
  2131. } else { /*(usb_addr > 0) */
  2132. u8 address = u132->addr[usb_addr].address;
  2133. struct u132_udev *udev = &u132->udev[address];
  2134. urb->hcpriv = u132;
  2135. if (udev->enumeration != 2)
  2136. udev->enumeration = 2;
  2137. if (endp->queue_size++ < ENDP_QUEUE_SIZE) {
  2138. endp->urb_list[ENDP_QUEUE_MASK & endp->queue_last++] =
  2139. urb;
  2140. } else {
  2141. struct u132_urbq *urbq =
  2142. kmalloc(sizeof(struct u132_urbq), GFP_ATOMIC);
  2143. if (urbq == NULL) {
  2144. endp->queue_size -= 1;
  2145. return -ENOMEM;
  2146. } else {
  2147. list_add_tail(&urbq->urb_more, &endp->urb_more);
  2148. urbq->urb = urb;
  2149. }
  2150. }
  2151. return 0;
  2152. }
  2153. }
  2154. static int u132_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  2155. gfp_t mem_flags)
  2156. {
  2157. struct u132 *u132 = hcd_to_u132(hcd);
  2158. if (irqs_disabled()) {
  2159. if (gfpflags_allow_blocking(mem_flags)) {
  2160. printk(KERN_ERR "invalid context for function that might sleep\n");
  2161. return -EINVAL;
  2162. }
  2163. }
  2164. if (u132->going > 1) {
  2165. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2166. , u132->going);
  2167. return -ENODEV;
  2168. } else if (u132->going > 0) {
  2169. dev_err(&u132->platform_dev->dev, "device is being removed "
  2170. "urb=%p\n", urb);
  2171. return -ESHUTDOWN;
  2172. } else {
  2173. u8 usb_addr = usb_pipedevice(urb->pipe);
  2174. u8 usb_endp = usb_pipeendpoint(urb->pipe);
  2175. struct usb_device *usb_dev = urb->dev;
  2176. if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
  2177. u8 address = u132->addr[usb_addr].address;
  2178. struct u132_udev *udev = &u132->udev[address];
  2179. struct u132_endp *endp = urb->ep->hcpriv;
  2180. urb->actual_length = 0;
  2181. if (endp) {
  2182. unsigned long irqs;
  2183. int retval;
  2184. spin_lock_irqsave(&endp->queue_lock.slock,
  2185. irqs);
  2186. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2187. if (retval == 0) {
  2188. retval = queue_int_on_old_endpoint(
  2189. u132, udev, urb,
  2190. usb_dev, endp,
  2191. usb_addr, usb_endp,
  2192. address);
  2193. if (retval)
  2194. usb_hcd_unlink_urb_from_ep(
  2195. hcd, urb);
  2196. }
  2197. spin_unlock_irqrestore(&endp->queue_lock.slock,
  2198. irqs);
  2199. if (retval) {
  2200. return retval;
  2201. } else {
  2202. u132_endp_queue_work(u132, endp,
  2203. msecs_to_jiffies(urb->interval))
  2204. ;
  2205. return 0;
  2206. }
  2207. } else if (u132->num_endpoints == MAX_U132_ENDPS) {
  2208. return -EINVAL;
  2209. } else { /*(endp == NULL) */
  2210. return create_endpoint_and_queue_int(u132, udev,
  2211. urb, usb_dev, usb_addr,
  2212. usb_endp, address, mem_flags);
  2213. }
  2214. } else if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  2215. dev_err(&u132->platform_dev->dev, "the hardware does no"
  2216. "t support PIPE_ISOCHRONOUS\n");
  2217. return -EINVAL;
  2218. } else if (usb_pipetype(urb->pipe) == PIPE_BULK) {
  2219. u8 address = u132->addr[usb_addr].address;
  2220. struct u132_udev *udev = &u132->udev[address];
  2221. struct u132_endp *endp = urb->ep->hcpriv;
  2222. urb->actual_length = 0;
  2223. if (endp) {
  2224. unsigned long irqs;
  2225. int retval;
  2226. spin_lock_irqsave(&endp->queue_lock.slock,
  2227. irqs);
  2228. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2229. if (retval == 0) {
  2230. retval = queue_bulk_on_old_endpoint(
  2231. u132, udev, urb,
  2232. usb_dev, endp,
  2233. usb_addr, usb_endp,
  2234. address);
  2235. if (retval)
  2236. usb_hcd_unlink_urb_from_ep(
  2237. hcd, urb);
  2238. }
  2239. spin_unlock_irqrestore(&endp->queue_lock.slock,
  2240. irqs);
  2241. if (retval) {
  2242. return retval;
  2243. } else {
  2244. u132_endp_queue_work(u132, endp, 0);
  2245. return 0;
  2246. }
  2247. } else if (u132->num_endpoints == MAX_U132_ENDPS) {
  2248. return -EINVAL;
  2249. } else
  2250. return create_endpoint_and_queue_bulk(u132,
  2251. udev, urb, usb_dev, usb_addr,
  2252. usb_endp, address, mem_flags);
  2253. } else {
  2254. struct u132_endp *endp = urb->ep->hcpriv;
  2255. u16 urb_size = 8;
  2256. u8 *b = urb->setup_packet;
  2257. int i = 0;
  2258. char data[30 * 3 + 4];
  2259. char *d = data;
  2260. int m = (sizeof(data) - 1) / 3;
  2261. int l = 0;
  2262. data[0] = 0;
  2263. while (urb_size-- > 0) {
  2264. if (i > m) {
  2265. } else if (i++ < m) {
  2266. int w = sprintf(d, " %02X", *b++);
  2267. d += w;
  2268. l += w;
  2269. } else
  2270. d += sprintf(d, " ..");
  2271. }
  2272. if (endp) {
  2273. unsigned long irqs;
  2274. int retval;
  2275. spin_lock_irqsave(&endp->queue_lock.slock,
  2276. irqs);
  2277. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  2278. if (retval == 0) {
  2279. retval = queue_control_on_old_endpoint(
  2280. u132, urb, usb_dev,
  2281. endp, usb_addr,
  2282. usb_endp);
  2283. if (retval)
  2284. usb_hcd_unlink_urb_from_ep(
  2285. hcd, urb);
  2286. }
  2287. spin_unlock_irqrestore(&endp->queue_lock.slock,
  2288. irqs);
  2289. if (retval) {
  2290. return retval;
  2291. } else {
  2292. u132_endp_queue_work(u132, endp, 0);
  2293. return 0;
  2294. }
  2295. } else if (u132->num_endpoints == MAX_U132_ENDPS) {
  2296. return -EINVAL;
  2297. } else
  2298. return create_endpoint_and_queue_control(u132,
  2299. urb, usb_dev, usb_addr, usb_endp,
  2300. mem_flags);
  2301. }
  2302. }
  2303. }
  2304. static int dequeue_from_overflow_chain(struct u132 *u132,
  2305. struct u132_endp *endp, struct urb *urb)
  2306. {
  2307. struct list_head *scan;
  2308. struct list_head *head = &endp->urb_more;
  2309. list_for_each(scan, head) {
  2310. struct u132_urbq *urbq = list_entry(scan, struct u132_urbq,
  2311. urb_more);
  2312. if (urbq->urb == urb) {
  2313. struct usb_hcd *hcd = u132_to_hcd(u132);
  2314. list_del(scan);
  2315. endp->queue_size -= 1;
  2316. urb->error_count = 0;
  2317. usb_hcd_giveback_urb(hcd, urb, 0);
  2318. return 0;
  2319. } else
  2320. continue;
  2321. }
  2322. dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]=%p ring"
  2323. "[%d] %c%c usb_endp=%d usb_addr=%d size=%d next=%04X last=%04X"
  2324. "\n", urb, endp->endp_number, endp, endp->ring->number,
  2325. endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
  2326. endp->usb_endp, endp->usb_addr, endp->queue_size,
  2327. endp->queue_next, endp->queue_last);
  2328. return -EINVAL;
  2329. }
  2330. static int u132_endp_urb_dequeue(struct u132 *u132, struct u132_endp *endp,
  2331. struct urb *urb, int status)
  2332. {
  2333. unsigned long irqs;
  2334. int rc;
  2335. spin_lock_irqsave(&endp->queue_lock.slock, irqs);
  2336. rc = usb_hcd_check_unlink_urb(u132_to_hcd(u132), urb, status);
  2337. if (rc) {
  2338. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2339. return rc;
  2340. }
  2341. if (endp->queue_size == 0) {
  2342. dev_err(&u132->platform_dev->dev, "urb=%p not found in endp[%d]"
  2343. "=%p ring[%d] %c%c usb_endp=%d usb_addr=%d\n", urb,
  2344. endp->endp_number, endp, endp->ring->number,
  2345. endp->input ? 'I' : ' ', endp->output ? 'O' : ' ',
  2346. endp->usb_endp, endp->usb_addr);
  2347. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2348. return -EINVAL;
  2349. }
  2350. if (urb == endp->urb_list[ENDP_QUEUE_MASK & endp->queue_next]) {
  2351. if (endp->active) {
  2352. endp->dequeueing = 1;
  2353. endp->edset_flush = 1;
  2354. u132_endp_queue_work(u132, endp, 0);
  2355. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2356. return 0;
  2357. } else {
  2358. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2359. u132_hcd_abandon_urb(u132, endp, urb, status);
  2360. return 0;
  2361. }
  2362. } else {
  2363. u16 queue_list = 0;
  2364. u16 queue_size = endp->queue_size;
  2365. u16 queue_scan = endp->queue_next;
  2366. struct urb **urb_slot = NULL;
  2367. while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
  2368. if (urb == endp->urb_list[ENDP_QUEUE_MASK &
  2369. ++queue_scan]) {
  2370. urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
  2371. queue_scan];
  2372. break;
  2373. } else
  2374. continue;
  2375. }
  2376. while (++queue_list < ENDP_QUEUE_SIZE && --queue_size > 0) {
  2377. *urb_slot = endp->urb_list[ENDP_QUEUE_MASK &
  2378. ++queue_scan];
  2379. urb_slot = &endp->urb_list[ENDP_QUEUE_MASK &
  2380. queue_scan];
  2381. }
  2382. if (urb_slot) {
  2383. struct usb_hcd *hcd = u132_to_hcd(u132);
  2384. usb_hcd_unlink_urb_from_ep(hcd, urb);
  2385. endp->queue_size -= 1;
  2386. if (list_empty(&endp->urb_more)) {
  2387. spin_unlock_irqrestore(&endp->queue_lock.slock,
  2388. irqs);
  2389. } else {
  2390. struct list_head *next = endp->urb_more.next;
  2391. struct u132_urbq *urbq = list_entry(next,
  2392. struct u132_urbq, urb_more);
  2393. list_del(next);
  2394. *urb_slot = urbq->urb;
  2395. spin_unlock_irqrestore(&endp->queue_lock.slock,
  2396. irqs);
  2397. kfree(urbq);
  2398. } urb->error_count = 0;
  2399. usb_hcd_giveback_urb(hcd, urb, status);
  2400. return 0;
  2401. } else if (list_empty(&endp->urb_more)) {
  2402. dev_err(&u132->platform_dev->dev, "urb=%p not found in "
  2403. "endp[%d]=%p ring[%d] %c%c usb_endp=%d usb_addr"
  2404. "=%d size=%d next=%04X last=%04X\n", urb,
  2405. endp->endp_number, endp, endp->ring->number,
  2406. endp->input ? 'I' : ' ',
  2407. endp->output ? 'O' : ' ', endp->usb_endp,
  2408. endp->usb_addr, endp->queue_size,
  2409. endp->queue_next, endp->queue_last);
  2410. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2411. return -EINVAL;
  2412. } else {
  2413. int retval;
  2414. usb_hcd_unlink_urb_from_ep(u132_to_hcd(u132), urb);
  2415. retval = dequeue_from_overflow_chain(u132, endp,
  2416. urb);
  2417. spin_unlock_irqrestore(&endp->queue_lock.slock, irqs);
  2418. return retval;
  2419. }
  2420. }
  2421. }
  2422. static int u132_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  2423. {
  2424. struct u132 *u132 = hcd_to_u132(hcd);
  2425. if (u132->going > 2) {
  2426. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2427. , u132->going);
  2428. return -ENODEV;
  2429. } else {
  2430. u8 usb_addr = usb_pipedevice(urb->pipe);
  2431. u8 usb_endp = usb_pipeendpoint(urb->pipe);
  2432. u8 address = u132->addr[usb_addr].address;
  2433. struct u132_udev *udev = &u132->udev[address];
  2434. if (usb_pipein(urb->pipe)) {
  2435. u8 endp_number = udev->endp_number_in[usb_endp];
  2436. struct u132_endp *endp = u132->endp[endp_number - 1];
  2437. return u132_endp_urb_dequeue(u132, endp, urb, status);
  2438. } else {
  2439. u8 endp_number = udev->endp_number_out[usb_endp];
  2440. struct u132_endp *endp = u132->endp[endp_number - 1];
  2441. return u132_endp_urb_dequeue(u132, endp, urb, status);
  2442. }
  2443. }
  2444. }
  2445. static void u132_endpoint_disable(struct usb_hcd *hcd,
  2446. struct usb_host_endpoint *hep)
  2447. {
  2448. struct u132 *u132 = hcd_to_u132(hcd);
  2449. if (u132->going > 2) {
  2450. dev_err(&u132->platform_dev->dev, "u132 device %p(hcd=%p hep=%p"
  2451. ") has been removed %d\n", u132, hcd, hep,
  2452. u132->going);
  2453. } else {
  2454. struct u132_endp *endp = hep->hcpriv;
  2455. if (endp)
  2456. u132_endp_put_kref(u132, endp);
  2457. }
  2458. }
  2459. static int u132_get_frame(struct usb_hcd *hcd)
  2460. {
  2461. struct u132 *u132 = hcd_to_u132(hcd);
  2462. if (u132->going > 1) {
  2463. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2464. , u132->going);
  2465. return -ENODEV;
  2466. } else if (u132->going > 0) {
  2467. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  2468. return -ESHUTDOWN;
  2469. } else {
  2470. int frame = 0;
  2471. dev_err(&u132->platform_dev->dev, "TODO: u132_get_frame\n");
  2472. mdelay(100);
  2473. return frame;
  2474. }
  2475. }
  2476. static int u132_roothub_descriptor(struct u132 *u132,
  2477. struct usb_hub_descriptor *desc)
  2478. {
  2479. int retval;
  2480. u16 temp;
  2481. u32 rh_a = -1;
  2482. u32 rh_b = -1;
  2483. retval = u132_read_pcimem(u132, roothub.a, &rh_a);
  2484. if (retval)
  2485. return retval;
  2486. desc->bDescriptorType = USB_DT_HUB;
  2487. desc->bPwrOn2PwrGood = (rh_a & RH_A_POTPGT) >> 24;
  2488. desc->bHubContrCurrent = 0;
  2489. desc->bNbrPorts = u132->num_ports;
  2490. temp = 1 + (u132->num_ports / 8);
  2491. desc->bDescLength = 7 + 2 * temp;
  2492. temp = HUB_CHAR_COMMON_LPSM | HUB_CHAR_COMMON_OCPM;
  2493. if (rh_a & RH_A_NPS)
  2494. temp |= HUB_CHAR_NO_LPSM;
  2495. if (rh_a & RH_A_PSM)
  2496. temp |= HUB_CHAR_INDV_PORT_LPSM;
  2497. if (rh_a & RH_A_NOCP)
  2498. temp |= HUB_CHAR_NO_OCPM;
  2499. else if (rh_a & RH_A_OCPM)
  2500. temp |= HUB_CHAR_INDV_PORT_OCPM;
  2501. desc->wHubCharacteristics = cpu_to_le16(temp);
  2502. retval = u132_read_pcimem(u132, roothub.b, &rh_b);
  2503. if (retval)
  2504. return retval;
  2505. memset(desc->u.hs.DeviceRemovable, 0xff,
  2506. sizeof(desc->u.hs.DeviceRemovable));
  2507. desc->u.hs.DeviceRemovable[0] = rh_b & RH_B_DR;
  2508. if (u132->num_ports > 7) {
  2509. desc->u.hs.DeviceRemovable[1] = (rh_b & RH_B_DR) >> 8;
  2510. desc->u.hs.DeviceRemovable[2] = 0xff;
  2511. } else
  2512. desc->u.hs.DeviceRemovable[1] = 0xff;
  2513. return 0;
  2514. }
  2515. static int u132_roothub_status(struct u132 *u132, __le32 *desc)
  2516. {
  2517. u32 rh_status = -1;
  2518. int ret_status = u132_read_pcimem(u132, roothub.status, &rh_status);
  2519. *desc = cpu_to_le32(rh_status);
  2520. return ret_status;
  2521. }
  2522. static int u132_roothub_portstatus(struct u132 *u132, __le32 *desc, u16 wIndex)
  2523. {
  2524. if (wIndex == 0 || wIndex > u132->num_ports) {
  2525. return -EINVAL;
  2526. } else {
  2527. int port = wIndex - 1;
  2528. u32 rh_portstatus = -1;
  2529. int ret_portstatus = u132_read_pcimem(u132,
  2530. roothub.portstatus[port], &rh_portstatus);
  2531. *desc = cpu_to_le32(rh_portstatus);
  2532. if (*(u16 *) (desc + 2)) {
  2533. dev_info(&u132->platform_dev->dev, "Port %d Status Chan"
  2534. "ge = %08X\n", port, *desc);
  2535. }
  2536. return ret_portstatus;
  2537. }
  2538. }
  2539. /* this timer value might be vendor-specific ... */
  2540. #define PORT_RESET_HW_MSEC 10
  2541. #define PORT_RESET_MSEC 10
  2542. /* wrap-aware logic morphed from <linux/jiffies.h> */
  2543. #define tick_before(t1, t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
  2544. static int u132_roothub_portreset(struct u132 *u132, int port_index)
  2545. {
  2546. int retval;
  2547. u32 fmnumber;
  2548. u16 now;
  2549. u16 reset_done;
  2550. retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
  2551. if (retval)
  2552. return retval;
  2553. now = fmnumber;
  2554. reset_done = now + PORT_RESET_MSEC;
  2555. do {
  2556. u32 portstat;
  2557. do {
  2558. retval = u132_read_pcimem(u132,
  2559. roothub.portstatus[port_index], &portstat);
  2560. if (retval)
  2561. return retval;
  2562. if (RH_PS_PRS & portstat)
  2563. continue;
  2564. else
  2565. break;
  2566. } while (tick_before(now, reset_done));
  2567. if (RH_PS_PRS & portstat)
  2568. return -ENODEV;
  2569. if (RH_PS_CCS & portstat) {
  2570. if (RH_PS_PRSC & portstat) {
  2571. retval = u132_write_pcimem(u132,
  2572. roothub.portstatus[port_index],
  2573. RH_PS_PRSC);
  2574. if (retval)
  2575. return retval;
  2576. }
  2577. } else
  2578. break; /* start the next reset,
  2579. sleep till it's probably done */
  2580. retval = u132_write_pcimem(u132, roothub.portstatus[port_index],
  2581. RH_PS_PRS);
  2582. if (retval)
  2583. return retval;
  2584. msleep(PORT_RESET_HW_MSEC);
  2585. retval = u132_read_pcimem(u132, fmnumber, &fmnumber);
  2586. if (retval)
  2587. return retval;
  2588. now = fmnumber;
  2589. } while (tick_before(now, reset_done));
  2590. return 0;
  2591. }
  2592. static int u132_roothub_setportfeature(struct u132 *u132, u16 wValue,
  2593. u16 wIndex)
  2594. {
  2595. if (wIndex == 0 || wIndex > u132->num_ports) {
  2596. return -EINVAL;
  2597. } else {
  2598. int port_index = wIndex - 1;
  2599. struct u132_port *port = &u132->port[port_index];
  2600. port->Status &= ~(1 << wValue);
  2601. switch (wValue) {
  2602. case USB_PORT_FEAT_SUSPEND:
  2603. return u132_write_pcimem(u132,
  2604. roothub.portstatus[port_index], RH_PS_PSS);
  2605. case USB_PORT_FEAT_POWER:
  2606. return u132_write_pcimem(u132,
  2607. roothub.portstatus[port_index], RH_PS_PPS);
  2608. case USB_PORT_FEAT_RESET:
  2609. return u132_roothub_portreset(u132, port_index);
  2610. default:
  2611. return -EPIPE;
  2612. }
  2613. }
  2614. }
  2615. static int u132_roothub_clearportfeature(struct u132 *u132, u16 wValue,
  2616. u16 wIndex)
  2617. {
  2618. if (wIndex == 0 || wIndex > u132->num_ports) {
  2619. return -EINVAL;
  2620. } else {
  2621. int port_index = wIndex - 1;
  2622. u32 temp;
  2623. struct u132_port *port = &u132->port[port_index];
  2624. port->Status &= ~(1 << wValue);
  2625. switch (wValue) {
  2626. case USB_PORT_FEAT_ENABLE:
  2627. temp = RH_PS_CCS;
  2628. break;
  2629. case USB_PORT_FEAT_C_ENABLE:
  2630. temp = RH_PS_PESC;
  2631. break;
  2632. case USB_PORT_FEAT_SUSPEND:
  2633. temp = RH_PS_POCI;
  2634. if ((u132->hc_control & OHCI_CTRL_HCFS)
  2635. != OHCI_USB_OPER) {
  2636. dev_err(&u132->platform_dev->dev, "TODO resume_"
  2637. "root_hub\n");
  2638. }
  2639. break;
  2640. case USB_PORT_FEAT_C_SUSPEND:
  2641. temp = RH_PS_PSSC;
  2642. break;
  2643. case USB_PORT_FEAT_POWER:
  2644. temp = RH_PS_LSDA;
  2645. break;
  2646. case USB_PORT_FEAT_C_CONNECTION:
  2647. temp = RH_PS_CSC;
  2648. break;
  2649. case USB_PORT_FEAT_C_OVER_CURRENT:
  2650. temp = RH_PS_OCIC;
  2651. break;
  2652. case USB_PORT_FEAT_C_RESET:
  2653. temp = RH_PS_PRSC;
  2654. break;
  2655. default:
  2656. return -EPIPE;
  2657. }
  2658. return u132_write_pcimem(u132, roothub.portstatus[port_index],
  2659. temp);
  2660. }
  2661. }
  2662. /* the virtual root hub timer IRQ checks for hub status*/
  2663. static int u132_hub_status_data(struct usb_hcd *hcd, char *buf)
  2664. {
  2665. struct u132 *u132 = hcd_to_u132(hcd);
  2666. if (u132->going > 1) {
  2667. dev_err(&u132->platform_dev->dev, "device hcd=%p has been remov"
  2668. "ed %d\n", hcd, u132->going);
  2669. return -ENODEV;
  2670. } else if (u132->going > 0) {
  2671. dev_err(&u132->platform_dev->dev, "device hcd=%p is being remov"
  2672. "ed\n", hcd);
  2673. return -ESHUTDOWN;
  2674. } else {
  2675. int i, changed = 0, length = 1;
  2676. if (u132->flags & OHCI_QUIRK_AMD756) {
  2677. if ((u132->hc_roothub_a & RH_A_NDP) > MAX_ROOT_PORTS) {
  2678. dev_err(&u132->platform_dev->dev, "bogus NDP, r"
  2679. "ereads as NDP=%d\n",
  2680. u132->hc_roothub_a & RH_A_NDP);
  2681. goto done;
  2682. }
  2683. }
  2684. if (u132->hc_roothub_status & (RH_HS_LPSC | RH_HS_OCIC))
  2685. buf[0] = changed = 1;
  2686. else
  2687. buf[0] = 0;
  2688. if (u132->num_ports > 7) {
  2689. buf[1] = 0;
  2690. length++;
  2691. }
  2692. for (i = 0; i < u132->num_ports; i++) {
  2693. if (u132->hc_roothub_portstatus[i] & (RH_PS_CSC |
  2694. RH_PS_PESC | RH_PS_PSSC | RH_PS_OCIC |
  2695. RH_PS_PRSC)) {
  2696. changed = 1;
  2697. if (i < 7)
  2698. buf[0] |= 1 << (i + 1);
  2699. else
  2700. buf[1] |= 1 << (i - 7);
  2701. continue;
  2702. }
  2703. if (!(u132->hc_roothub_portstatus[i] & RH_PS_CCS))
  2704. continue;
  2705. if ((u132->hc_roothub_portstatus[i] & RH_PS_PSS))
  2706. continue;
  2707. }
  2708. done:
  2709. return changed ? length : 0;
  2710. }
  2711. }
  2712. static int u132_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  2713. u16 wIndex, char *buf, u16 wLength)
  2714. {
  2715. struct u132 *u132 = hcd_to_u132(hcd);
  2716. if (u132->going > 1) {
  2717. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2718. , u132->going);
  2719. return -ENODEV;
  2720. } else if (u132->going > 0) {
  2721. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  2722. return -ESHUTDOWN;
  2723. } else {
  2724. int retval = 0;
  2725. mutex_lock(&u132->sw_lock);
  2726. switch (typeReq) {
  2727. case ClearHubFeature:
  2728. switch (wValue) {
  2729. case C_HUB_OVER_CURRENT:
  2730. case C_HUB_LOCAL_POWER:
  2731. break;
  2732. default:
  2733. goto stall;
  2734. }
  2735. break;
  2736. case SetHubFeature:
  2737. switch (wValue) {
  2738. case C_HUB_OVER_CURRENT:
  2739. case C_HUB_LOCAL_POWER:
  2740. break;
  2741. default:
  2742. goto stall;
  2743. }
  2744. break;
  2745. case ClearPortFeature:{
  2746. retval = u132_roothub_clearportfeature(u132,
  2747. wValue, wIndex);
  2748. if (retval)
  2749. goto error;
  2750. break;
  2751. }
  2752. case GetHubDescriptor:{
  2753. retval = u132_roothub_descriptor(u132,
  2754. (struct usb_hub_descriptor *)buf);
  2755. if (retval)
  2756. goto error;
  2757. break;
  2758. }
  2759. case GetHubStatus:{
  2760. retval = u132_roothub_status(u132,
  2761. (__le32 *) buf);
  2762. if (retval)
  2763. goto error;
  2764. break;
  2765. }
  2766. case GetPortStatus:{
  2767. retval = u132_roothub_portstatus(u132,
  2768. (__le32 *) buf, wIndex);
  2769. if (retval)
  2770. goto error;
  2771. break;
  2772. }
  2773. case SetPortFeature:{
  2774. retval = u132_roothub_setportfeature(u132,
  2775. wValue, wIndex);
  2776. if (retval)
  2777. goto error;
  2778. break;
  2779. }
  2780. default:
  2781. goto stall;
  2782. error:
  2783. u132_disable(u132);
  2784. u132->going = 1;
  2785. break;
  2786. stall:
  2787. retval = -EPIPE;
  2788. break;
  2789. }
  2790. mutex_unlock(&u132->sw_lock);
  2791. return retval;
  2792. }
  2793. }
  2794. static int u132_start_port_reset(struct usb_hcd *hcd, unsigned port_num)
  2795. {
  2796. struct u132 *u132 = hcd_to_u132(hcd);
  2797. if (u132->going > 1) {
  2798. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2799. , u132->going);
  2800. return -ENODEV;
  2801. } else if (u132->going > 0) {
  2802. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  2803. return -ESHUTDOWN;
  2804. } else
  2805. return 0;
  2806. }
  2807. #ifdef CONFIG_PM
  2808. static int u132_bus_suspend(struct usb_hcd *hcd)
  2809. {
  2810. struct u132 *u132 = hcd_to_u132(hcd);
  2811. if (u132->going > 1) {
  2812. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2813. , u132->going);
  2814. return -ENODEV;
  2815. } else if (u132->going > 0) {
  2816. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  2817. return -ESHUTDOWN;
  2818. } else
  2819. return 0;
  2820. }
  2821. static int u132_bus_resume(struct usb_hcd *hcd)
  2822. {
  2823. struct u132 *u132 = hcd_to_u132(hcd);
  2824. if (u132->going > 1) {
  2825. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  2826. , u132->going);
  2827. return -ENODEV;
  2828. } else if (u132->going > 0) {
  2829. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  2830. return -ESHUTDOWN;
  2831. } else
  2832. return 0;
  2833. }
  2834. #else
  2835. #define u132_bus_suspend NULL
  2836. #define u132_bus_resume NULL
  2837. #endif
  2838. static struct hc_driver u132_hc_driver = {
  2839. .description = hcd_name,
  2840. .hcd_priv_size = sizeof(struct u132),
  2841. .irq = NULL,
  2842. .flags = HCD_USB11 | HCD_MEMORY,
  2843. .reset = u132_hcd_reset,
  2844. .start = u132_hcd_start,
  2845. .stop = u132_hcd_stop,
  2846. .urb_enqueue = u132_urb_enqueue,
  2847. .urb_dequeue = u132_urb_dequeue,
  2848. .endpoint_disable = u132_endpoint_disable,
  2849. .get_frame_number = u132_get_frame,
  2850. .hub_status_data = u132_hub_status_data,
  2851. .hub_control = u132_hub_control,
  2852. .bus_suspend = u132_bus_suspend,
  2853. .bus_resume = u132_bus_resume,
  2854. .start_port_reset = u132_start_port_reset,
  2855. };
  2856. /*
  2857. * This function may be called by the USB core whilst the "usb_all_devices_rwsem"
  2858. * is held for writing, thus this module must not call usb_remove_hcd()
  2859. * synchronously - but instead should immediately stop activity to the
  2860. * device and asynchronously call usb_remove_hcd()
  2861. */
  2862. static int u132_remove(struct platform_device *pdev)
  2863. {
  2864. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  2865. if (hcd) {
  2866. struct u132 *u132 = hcd_to_u132(hcd);
  2867. if (u132->going++ > 1) {
  2868. dev_err(&u132->platform_dev->dev, "already being remove"
  2869. "d\n");
  2870. return -ENODEV;
  2871. } else {
  2872. int rings = MAX_U132_RINGS;
  2873. int endps = MAX_U132_ENDPS;
  2874. dev_err(&u132->platform_dev->dev, "removing device u132"
  2875. ".%d\n", u132->sequence_num);
  2876. msleep(100);
  2877. mutex_lock(&u132->sw_lock);
  2878. u132_monitor_cancel_work(u132);
  2879. while (rings-- > 0) {
  2880. struct u132_ring *ring = &u132->ring[rings];
  2881. u132_ring_cancel_work(u132, ring);
  2882. } while (endps-- > 0) {
  2883. struct u132_endp *endp = u132->endp[endps];
  2884. if (endp)
  2885. u132_endp_cancel_work(u132, endp);
  2886. }
  2887. u132->going += 1;
  2888. printk(KERN_INFO "removing device u132.%d\n",
  2889. u132->sequence_num);
  2890. mutex_unlock(&u132->sw_lock);
  2891. usb_remove_hcd(hcd);
  2892. u132_u132_put_kref(u132);
  2893. return 0;
  2894. }
  2895. } else
  2896. return 0;
  2897. }
  2898. static void u132_initialise(struct u132 *u132, struct platform_device *pdev)
  2899. {
  2900. int rings = MAX_U132_RINGS;
  2901. int ports = MAX_U132_PORTS;
  2902. int addrs = MAX_U132_ADDRS;
  2903. int udevs = MAX_U132_UDEVS;
  2904. int endps = MAX_U132_ENDPS;
  2905. u132->board = dev_get_platdata(&pdev->dev);
  2906. u132->platform_dev = pdev;
  2907. u132->power = 0;
  2908. u132->reset = 0;
  2909. mutex_init(&u132->sw_lock);
  2910. mutex_init(&u132->scheduler_lock);
  2911. while (rings-- > 0) {
  2912. struct u132_ring *ring = &u132->ring[rings];
  2913. ring->u132 = u132;
  2914. ring->number = rings + 1;
  2915. ring->length = 0;
  2916. ring->curr_endp = NULL;
  2917. INIT_DELAYED_WORK(&ring->scheduler,
  2918. u132_hcd_ring_work_scheduler);
  2919. }
  2920. mutex_lock(&u132->sw_lock);
  2921. INIT_DELAYED_WORK(&u132->monitor, u132_hcd_monitor_work);
  2922. while (ports-- > 0) {
  2923. struct u132_port *port = &u132->port[ports];
  2924. port->u132 = u132;
  2925. port->reset = 0;
  2926. port->enable = 0;
  2927. port->power = 0;
  2928. port->Status = 0;
  2929. }
  2930. while (addrs-- > 0) {
  2931. struct u132_addr *addr = &u132->addr[addrs];
  2932. addr->address = 0;
  2933. }
  2934. while (udevs-- > 0) {
  2935. struct u132_udev *udev = &u132->udev[udevs];
  2936. int i = ARRAY_SIZE(udev->endp_number_in);
  2937. int o = ARRAY_SIZE(udev->endp_number_out);
  2938. udev->usb_device = NULL;
  2939. udev->udev_number = 0;
  2940. udev->usb_addr = 0;
  2941. udev->portnumber = 0;
  2942. while (i-- > 0)
  2943. udev->endp_number_in[i] = 0;
  2944. while (o-- > 0)
  2945. udev->endp_number_out[o] = 0;
  2946. }
  2947. while (endps-- > 0)
  2948. u132->endp[endps] = NULL;
  2949. mutex_unlock(&u132->sw_lock);
  2950. }
  2951. static int u132_probe(struct platform_device *pdev)
  2952. {
  2953. struct usb_hcd *hcd;
  2954. int retval;
  2955. u32 control;
  2956. u32 rh_a = -1;
  2957. u32 num_ports;
  2958. msleep(100);
  2959. if (u132_exiting > 0)
  2960. return -ENODEV;
  2961. retval = ftdi_write_pcimem(pdev, intrdisable, OHCI_INTR_MIE);
  2962. if (retval)
  2963. return retval;
  2964. retval = ftdi_read_pcimem(pdev, control, &control);
  2965. if (retval)
  2966. return retval;
  2967. retval = ftdi_read_pcimem(pdev, roothub.a, &rh_a);
  2968. if (retval)
  2969. return retval;
  2970. num_ports = rh_a & RH_A_NDP; /* refuse to confuse usbcore */
  2971. if (pdev->dev.dma_mask)
  2972. return -EINVAL;
  2973. hcd = usb_create_hcd(&u132_hc_driver, &pdev->dev, dev_name(&pdev->dev));
  2974. if (!hcd) {
  2975. printk(KERN_ERR "failed to create the usb hcd struct for U132\n"
  2976. );
  2977. ftdi_elan_gone_away(pdev);
  2978. return -ENOMEM;
  2979. } else {
  2980. struct u132 *u132 = hcd_to_u132(hcd);
  2981. retval = 0;
  2982. hcd->rsrc_start = 0;
  2983. mutex_lock(&u132_module_lock);
  2984. list_add_tail(&u132->u132_list, &u132_static_list);
  2985. u132->sequence_num = ++u132_instances;
  2986. mutex_unlock(&u132_module_lock);
  2987. u132_u132_init_kref(u132);
  2988. u132_initialise(u132, pdev);
  2989. hcd->product_desc = "ELAN U132 Host Controller";
  2990. retval = usb_add_hcd(hcd, 0, 0);
  2991. if (retval != 0) {
  2992. dev_err(&u132->platform_dev->dev, "init error %d\n",
  2993. retval);
  2994. u132_u132_put_kref(u132);
  2995. return retval;
  2996. } else {
  2997. device_wakeup_enable(hcd->self.controller);
  2998. u132_monitor_queue_work(u132, 100);
  2999. return 0;
  3000. }
  3001. }
  3002. }
  3003. #ifdef CONFIG_PM
  3004. /*
  3005. * for this device there's no useful distinction between the controller
  3006. * and its root hub.
  3007. */
  3008. static int u132_suspend(struct platform_device *pdev, pm_message_t state)
  3009. {
  3010. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  3011. struct u132 *u132 = hcd_to_u132(hcd);
  3012. if (u132->going > 1) {
  3013. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  3014. , u132->going);
  3015. return -ENODEV;
  3016. } else if (u132->going > 0) {
  3017. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  3018. return -ESHUTDOWN;
  3019. } else {
  3020. int retval = 0, ports;
  3021. switch (state.event) {
  3022. case PM_EVENT_FREEZE:
  3023. retval = u132_bus_suspend(hcd);
  3024. break;
  3025. case PM_EVENT_SUSPEND:
  3026. case PM_EVENT_HIBERNATE:
  3027. ports = MAX_U132_PORTS;
  3028. while (ports-- > 0) {
  3029. port_power(u132, ports, 0);
  3030. }
  3031. break;
  3032. }
  3033. return retval;
  3034. }
  3035. }
  3036. static int u132_resume(struct platform_device *pdev)
  3037. {
  3038. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  3039. struct u132 *u132 = hcd_to_u132(hcd);
  3040. if (u132->going > 1) {
  3041. dev_err(&u132->platform_dev->dev, "device has been removed %d\n"
  3042. , u132->going);
  3043. return -ENODEV;
  3044. } else if (u132->going > 0) {
  3045. dev_err(&u132->platform_dev->dev, "device is being removed\n");
  3046. return -ESHUTDOWN;
  3047. } else {
  3048. int retval = 0;
  3049. if (!u132->port[0].power) {
  3050. int ports = MAX_U132_PORTS;
  3051. while (ports-- > 0) {
  3052. port_power(u132, ports, 1);
  3053. }
  3054. retval = 0;
  3055. } else {
  3056. retval = u132_bus_resume(hcd);
  3057. }
  3058. return retval;
  3059. }
  3060. }
  3061. #else
  3062. #define u132_suspend NULL
  3063. #define u132_resume NULL
  3064. #endif
  3065. /*
  3066. * this driver is loaded explicitly by ftdi_u132
  3067. *
  3068. * the platform_driver struct is static because it is per type of module
  3069. */
  3070. static struct platform_driver u132_platform_driver = {
  3071. .probe = u132_probe,
  3072. .remove = u132_remove,
  3073. .suspend = u132_suspend,
  3074. .resume = u132_resume,
  3075. .driver = {
  3076. .name = hcd_name,
  3077. },
  3078. };
  3079. static int __init u132_hcd_init(void)
  3080. {
  3081. int retval;
  3082. INIT_LIST_HEAD(&u132_static_list);
  3083. u132_instances = 0;
  3084. u132_exiting = 0;
  3085. mutex_init(&u132_module_lock);
  3086. if (usb_disabled())
  3087. return -ENODEV;
  3088. printk(KERN_INFO "driver %s\n", hcd_name);
  3089. workqueue = create_singlethread_workqueue("u132");
  3090. retval = platform_driver_register(&u132_platform_driver);
  3091. return retval;
  3092. }
  3093. module_init(u132_hcd_init);
  3094. static void __exit u132_hcd_exit(void)
  3095. {
  3096. struct u132 *u132;
  3097. struct u132 *temp;
  3098. mutex_lock(&u132_module_lock);
  3099. u132_exiting += 1;
  3100. mutex_unlock(&u132_module_lock);
  3101. list_for_each_entry_safe(u132, temp, &u132_static_list, u132_list) {
  3102. platform_device_unregister(u132->platform_dev);
  3103. }
  3104. platform_driver_unregister(&u132_platform_driver);
  3105. printk(KERN_INFO "u132-hcd driver deregistered\n");
  3106. wait_event(u132_hcd_wait, u132_instances == 0);
  3107. flush_workqueue(workqueue);
  3108. destroy_workqueue(workqueue);
  3109. }
  3110. module_exit(u132_hcd_exit);
  3111. MODULE_LICENSE("GPL");
  3112. MODULE_ALIAS("platform:u132_hcd");