sisusb.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455
  1. /*
  2. * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
  3. *
  4. * Main part
  5. *
  6. * Copyright (C) 2005 by Thomas Winischhofer, Vienna, Austria
  7. *
  8. * If distributed as part of the Linux kernel, this code is licensed under the
  9. * terms of the GPL v2.
  10. *
  11. * Otherwise, the following license terms apply:
  12. *
  13. * * Redistribution and use in source and binary forms, with or without
  14. * * modification, are permitted provided that the following conditions
  15. * * are met:
  16. * * 1) Redistributions of source code must retain the above copyright
  17. * * notice, this list of conditions and the following disclaimer.
  18. * * 2) Redistributions in binary form must reproduce the above copyright
  19. * * notice, this list of conditions and the following disclaimer in the
  20. * * documentation and/or other materials provided with the distribution.
  21. * * 3) The name of the author may not be used to endorse or promote products
  22. * * derived from this software without specific psisusbr written permission.
  23. * *
  24. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
  25. * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  27. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  33. * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Author: Thomas Winischhofer <thomas@winischhofer.net>
  36. *
  37. */
  38. #include <linux/mutex.h>
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/signal.h>
  42. #include <linux/errno.h>
  43. #include <linux/poll.h>
  44. #include <linux/init.h>
  45. #include <linux/slab.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/kref.h>
  48. #include <linux/usb.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/vmalloc.h>
  51. #include "sisusb.h"
  52. #include "sisusb_init.h"
  53. #ifdef INCL_SISUSB_CON
  54. #include <linux/font.h>
  55. #endif
  56. #define SISUSB_DONTSYNC
  57. /* Forward declarations / clean-up routines */
  58. #ifdef INCL_SISUSB_CON
  59. static int sisusb_first_vc = 0;
  60. static int sisusb_last_vc = 0;
  61. module_param_named(first, sisusb_first_vc, int, 0);
  62. module_param_named(last, sisusb_last_vc, int, 0);
  63. MODULE_PARM_DESC(first, "Number of first console to take over (1 - MAX_NR_CONSOLES)");
  64. MODULE_PARM_DESC(last, "Number of last console to take over (1 - MAX_NR_CONSOLES)");
  65. #endif
  66. static struct usb_driver sisusb_driver;
  67. static void
  68. sisusb_free_buffers(struct sisusb_usb_data *sisusb)
  69. {
  70. int i;
  71. for (i = 0; i < NUMOBUFS; i++) {
  72. if (sisusb->obuf[i]) {
  73. usb_buffer_free(sisusb->sisusb_dev, sisusb->obufsize,
  74. sisusb->obuf[i], sisusb->transfer_dma_out[i]);
  75. sisusb->obuf[i] = NULL;
  76. }
  77. }
  78. if (sisusb->ibuf) {
  79. usb_buffer_free(sisusb->sisusb_dev, sisusb->ibufsize,
  80. sisusb->ibuf, sisusb->transfer_dma_in);
  81. sisusb->ibuf = NULL;
  82. }
  83. }
  84. static void
  85. sisusb_free_urbs(struct sisusb_usb_data *sisusb)
  86. {
  87. int i;
  88. for (i = 0; i < NUMOBUFS; i++) {
  89. usb_free_urb(sisusb->sisurbout[i]);
  90. sisusb->sisurbout[i] = NULL;
  91. }
  92. usb_free_urb(sisusb->sisurbin);
  93. sisusb->sisurbin = NULL;
  94. }
  95. /* Level 0: USB transport layer */
  96. /* 1. out-bulks */
  97. /* out-urb management */
  98. /* Return 1 if all free, 0 otherwise */
  99. static int
  100. sisusb_all_free(struct sisusb_usb_data *sisusb)
  101. {
  102. int i;
  103. for (i = 0; i < sisusb->numobufs; i++) {
  104. if (sisusb->urbstatus[i] & SU_URB_BUSY)
  105. return 0;
  106. }
  107. return 1;
  108. }
  109. /* Kill all busy URBs */
  110. static void
  111. sisusb_kill_all_busy(struct sisusb_usb_data *sisusb)
  112. {
  113. int i;
  114. if (sisusb_all_free(sisusb))
  115. return;
  116. for (i = 0; i < sisusb->numobufs; i++) {
  117. if (sisusb->urbstatus[i] & SU_URB_BUSY)
  118. usb_kill_urb(sisusb->sisurbout[i]);
  119. }
  120. }
  121. /* Return 1 if ok, 0 if error (not all complete within timeout) */
  122. static int
  123. sisusb_wait_all_out_complete(struct sisusb_usb_data *sisusb)
  124. {
  125. int timeout = 5 * HZ, i = 1;
  126. wait_event_timeout(sisusb->wait_q,
  127. (i = sisusb_all_free(sisusb)),
  128. timeout);
  129. return i;
  130. }
  131. static int
  132. sisusb_outurb_available(struct sisusb_usb_data *sisusb)
  133. {
  134. int i;
  135. for (i = 0; i < sisusb->numobufs; i++) {
  136. if ((sisusb->urbstatus[i] & (SU_URB_BUSY|SU_URB_ALLOC)) == 0)
  137. return i;
  138. }
  139. return -1;
  140. }
  141. static int
  142. sisusb_get_free_outbuf(struct sisusb_usb_data *sisusb)
  143. {
  144. int i, timeout = 5 * HZ;
  145. wait_event_timeout(sisusb->wait_q,
  146. ((i = sisusb_outurb_available(sisusb)) >= 0),
  147. timeout);
  148. return i;
  149. }
  150. static int
  151. sisusb_alloc_outbuf(struct sisusb_usb_data *sisusb)
  152. {
  153. int i;
  154. i = sisusb_outurb_available(sisusb);
  155. if (i >= 0)
  156. sisusb->urbstatus[i] |= SU_URB_ALLOC;
  157. return i;
  158. }
  159. static void
  160. sisusb_free_outbuf(struct sisusb_usb_data *sisusb, int index)
  161. {
  162. if ((index >= 0) && (index < sisusb->numobufs))
  163. sisusb->urbstatus[index] &= ~SU_URB_ALLOC;
  164. }
  165. /* completion callback */
  166. static void
  167. sisusb_bulk_completeout(struct urb *urb)
  168. {
  169. struct sisusb_urb_context *context = urb->context;
  170. struct sisusb_usb_data *sisusb;
  171. if (!context)
  172. return;
  173. sisusb = context->sisusb;
  174. if (!sisusb || !sisusb->sisusb_dev || !sisusb->present)
  175. return;
  176. #ifndef SISUSB_DONTSYNC
  177. if (context->actual_length)
  178. *(context->actual_length) += urb->actual_length;
  179. #endif
  180. sisusb->urbstatus[context->urbindex] &= ~SU_URB_BUSY;
  181. wake_up(&sisusb->wait_q);
  182. }
  183. static int
  184. sisusb_bulkout_msg(struct sisusb_usb_data *sisusb, int index, unsigned int pipe, void *data,
  185. int len, int *actual_length, int timeout, unsigned int tflags,
  186. dma_addr_t transfer_dma)
  187. {
  188. struct urb *urb = sisusb->sisurbout[index];
  189. int retval, byteswritten = 0;
  190. /* Set up URB */
  191. urb->transfer_flags = 0;
  192. usb_fill_bulk_urb(urb, sisusb->sisusb_dev, pipe, data, len,
  193. sisusb_bulk_completeout, &sisusb->urbout_context[index]);
  194. urb->transfer_flags |= tflags;
  195. urb->actual_length = 0;
  196. if ((urb->transfer_dma = transfer_dma))
  197. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  198. /* Set up context */
  199. sisusb->urbout_context[index].actual_length = (timeout) ?
  200. NULL : actual_length;
  201. /* Declare this urb/buffer in use */
  202. sisusb->urbstatus[index] |= SU_URB_BUSY;
  203. /* Submit URB */
  204. retval = usb_submit_urb(urb, GFP_ATOMIC);
  205. /* If OK, and if timeout > 0, wait for completion */
  206. if ((retval == 0) && timeout) {
  207. wait_event_timeout(sisusb->wait_q,
  208. (!(sisusb->urbstatus[index] & SU_URB_BUSY)),
  209. timeout);
  210. if (sisusb->urbstatus[index] & SU_URB_BUSY) {
  211. /* URB timed out... kill it and report error */
  212. usb_kill_urb(urb);
  213. retval = -ETIMEDOUT;
  214. } else {
  215. /* Otherwise, report urb status */
  216. retval = urb->status;
  217. byteswritten = urb->actual_length;
  218. }
  219. }
  220. if (actual_length)
  221. *actual_length = byteswritten;
  222. return retval;
  223. }
  224. /* 2. in-bulks */
  225. /* completion callback */
  226. static void
  227. sisusb_bulk_completein(struct urb *urb)
  228. {
  229. struct sisusb_usb_data *sisusb = urb->context;
  230. if (!sisusb || !sisusb->sisusb_dev || !sisusb->present)
  231. return;
  232. sisusb->completein = 1;
  233. wake_up(&sisusb->wait_q);
  234. }
  235. static int
  236. sisusb_bulkin_msg(struct sisusb_usb_data *sisusb, unsigned int pipe, void *data, int len,
  237. int *actual_length, int timeout, unsigned int tflags, dma_addr_t transfer_dma)
  238. {
  239. struct urb *urb = sisusb->sisurbin;
  240. int retval, readbytes = 0;
  241. urb->transfer_flags = 0;
  242. usb_fill_bulk_urb(urb, sisusb->sisusb_dev, pipe, data, len,
  243. sisusb_bulk_completein, sisusb);
  244. urb->transfer_flags |= tflags;
  245. urb->actual_length = 0;
  246. if ((urb->transfer_dma = transfer_dma))
  247. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  248. sisusb->completein = 0;
  249. retval = usb_submit_urb(urb, GFP_ATOMIC);
  250. if (retval == 0) {
  251. wait_event_timeout(sisusb->wait_q, sisusb->completein, timeout);
  252. if (!sisusb->completein) {
  253. /* URB timed out... kill it and report error */
  254. usb_kill_urb(urb);
  255. retval = -ETIMEDOUT;
  256. } else {
  257. /* URB completed within timout */
  258. retval = urb->status;
  259. readbytes = urb->actual_length;
  260. }
  261. }
  262. if (actual_length)
  263. *actual_length = readbytes;
  264. return retval;
  265. }
  266. /* Level 1: */
  267. /* Send a bulk message of variable size
  268. *
  269. * To copy the data from userspace, give pointer to "userbuffer",
  270. * to copy from (non-DMA) kernel memory, give "kernbuffer". If
  271. * both of these are NULL, it is assumed, that the transfer
  272. * buffer "sisusb->obuf[index]" is set up with the data to send.
  273. * Index is ignored if either kernbuffer or userbuffer is set.
  274. * If async is nonzero, URBs will be sent without waiting for
  275. * completion of the previous URB.
  276. *
  277. * (return 0 on success)
  278. */
  279. static int sisusb_send_bulk_msg(struct sisusb_usb_data *sisusb, int ep, int len,
  280. char *kernbuffer, const char __user *userbuffer, int index,
  281. ssize_t *bytes_written, unsigned int tflags, int async)
  282. {
  283. int result = 0, retry, count = len;
  284. int passsize, thispass, transferred_len = 0;
  285. int fromuser = (userbuffer != NULL) ? 1 : 0;
  286. int fromkern = (kernbuffer != NULL) ? 1 : 0;
  287. unsigned int pipe;
  288. char *buffer;
  289. (*bytes_written) = 0;
  290. /* Sanity check */
  291. if (!sisusb || !sisusb->present || !sisusb->sisusb_dev)
  292. return -ENODEV;
  293. /* If we copy data from kernel or userspace, force the
  294. * allocation of a buffer/urb. If we have the data in
  295. * the transfer buffer[index] already, reuse the buffer/URB
  296. * if the length is > buffer size. (So, transmitting
  297. * large data amounts directly from the transfer buffer
  298. * treats the buffer as a ring buffer. However, we need
  299. * to sync in this case.)
  300. */
  301. if (fromuser || fromkern)
  302. index = -1;
  303. else if (len > sisusb->obufsize)
  304. async = 0;
  305. pipe = usb_sndbulkpipe(sisusb->sisusb_dev, ep);
  306. do {
  307. passsize = thispass = (sisusb->obufsize < count) ?
  308. sisusb->obufsize : count;
  309. if (index < 0)
  310. index = sisusb_get_free_outbuf(sisusb);
  311. if (index < 0)
  312. return -EIO;
  313. buffer = sisusb->obuf[index];
  314. if (fromuser) {
  315. if (copy_from_user(buffer, userbuffer, passsize))
  316. return -EFAULT;
  317. userbuffer += passsize;
  318. } else if (fromkern) {
  319. memcpy(buffer, kernbuffer, passsize);
  320. kernbuffer += passsize;
  321. }
  322. retry = 5;
  323. while (thispass) {
  324. if (!sisusb->sisusb_dev)
  325. return -ENODEV;
  326. result = sisusb_bulkout_msg(sisusb,
  327. index,
  328. pipe,
  329. buffer,
  330. thispass,
  331. &transferred_len,
  332. async ? 0 : 5 * HZ,
  333. tflags,
  334. sisusb->transfer_dma_out[index]);
  335. if (result == -ETIMEDOUT) {
  336. /* Will not happen if async */
  337. if (!retry--)
  338. return -ETIME;
  339. continue;
  340. } else if ((result == 0) && !async && transferred_len) {
  341. thispass -= transferred_len;
  342. if (thispass) {
  343. if (sisusb->transfer_dma_out) {
  344. /* If DMA, copy remaining
  345. * to beginning of buffer
  346. */
  347. memcpy(buffer,
  348. buffer + transferred_len,
  349. thispass);
  350. } else {
  351. /* If not DMA, simply increase
  352. * the pointer
  353. */
  354. buffer += transferred_len;
  355. }
  356. }
  357. } else
  358. break;
  359. };
  360. if (result)
  361. return result;
  362. (*bytes_written) += passsize;
  363. count -= passsize;
  364. /* Force new allocation in next iteration */
  365. if (fromuser || fromkern)
  366. index = -1;
  367. } while (count > 0);
  368. if (async) {
  369. #ifdef SISUSB_DONTSYNC
  370. (*bytes_written) = len;
  371. /* Some URBs/buffers might be busy */
  372. #else
  373. sisusb_wait_all_out_complete(sisusb);
  374. (*bytes_written) = transferred_len;
  375. /* All URBs and all buffers are available */
  376. #endif
  377. }
  378. return ((*bytes_written) == len) ? 0 : -EIO;
  379. }
  380. /* Receive a bulk message of variable size
  381. *
  382. * To copy the data to userspace, give pointer to "userbuffer",
  383. * to copy to kernel memory, give "kernbuffer". One of them
  384. * MUST be set. (There is no technique for letting the caller
  385. * read directly from the ibuf.)
  386. *
  387. */
  388. static int sisusb_recv_bulk_msg(struct sisusb_usb_data *sisusb, int ep, int len,
  389. void *kernbuffer, char __user *userbuffer, ssize_t *bytes_read,
  390. unsigned int tflags)
  391. {
  392. int result = 0, retry, count = len;
  393. int bufsize, thispass, transferred_len;
  394. unsigned int pipe;
  395. char *buffer;
  396. (*bytes_read) = 0;
  397. /* Sanity check */
  398. if (!sisusb || !sisusb->present || !sisusb->sisusb_dev)
  399. return -ENODEV;
  400. pipe = usb_rcvbulkpipe(sisusb->sisusb_dev, ep);
  401. buffer = sisusb->ibuf;
  402. bufsize = sisusb->ibufsize;
  403. retry = 5;
  404. #ifdef SISUSB_DONTSYNC
  405. if (!(sisusb_wait_all_out_complete(sisusb)))
  406. return -EIO;
  407. #endif
  408. while (count > 0) {
  409. if (!sisusb->sisusb_dev)
  410. return -ENODEV;
  411. thispass = (bufsize < count) ? bufsize : count;
  412. result = sisusb_bulkin_msg(sisusb,
  413. pipe,
  414. buffer,
  415. thispass,
  416. &transferred_len,
  417. 5 * HZ,
  418. tflags,
  419. sisusb->transfer_dma_in);
  420. if (transferred_len)
  421. thispass = transferred_len;
  422. else if (result == -ETIMEDOUT) {
  423. if (!retry--)
  424. return -ETIME;
  425. continue;
  426. } else
  427. return -EIO;
  428. if (thispass) {
  429. (*bytes_read) += thispass;
  430. count -= thispass;
  431. if (userbuffer) {
  432. if (copy_to_user(userbuffer, buffer, thispass))
  433. return -EFAULT;
  434. userbuffer += thispass;
  435. } else {
  436. memcpy(kernbuffer, buffer, thispass);
  437. kernbuffer += thispass;
  438. }
  439. }
  440. }
  441. return ((*bytes_read) == len) ? 0 : -EIO;
  442. }
  443. static int sisusb_send_packet(struct sisusb_usb_data *sisusb, int len,
  444. struct sisusb_packet *packet)
  445. {
  446. int ret;
  447. ssize_t bytes_transferred = 0;
  448. __le32 tmp;
  449. if (len == 6)
  450. packet->data = 0;
  451. #ifdef SISUSB_DONTSYNC
  452. if (!(sisusb_wait_all_out_complete(sisusb)))
  453. return 1;
  454. #endif
  455. /* Eventually correct endianness */
  456. SISUSB_CORRECT_ENDIANNESS_PACKET(packet);
  457. /* 1. send the packet */
  458. ret = sisusb_send_bulk_msg(sisusb, SISUSB_EP_GFX_OUT, len,
  459. (char *)packet, NULL, 0, &bytes_transferred, 0, 0);
  460. if ((ret == 0) && (len == 6)) {
  461. /* 2. if packet len == 6, it means we read, so wait for 32bit
  462. * return value and write it to packet->data
  463. */
  464. ret = sisusb_recv_bulk_msg(sisusb, SISUSB_EP_GFX_IN, 4,
  465. (char *)&tmp, NULL, &bytes_transferred, 0);
  466. packet->data = le32_to_cpu(tmp);
  467. }
  468. return ret;
  469. }
  470. static int sisusb_send_bridge_packet(struct sisusb_usb_data *sisusb, int len,
  471. struct sisusb_packet *packet,
  472. unsigned int tflags)
  473. {
  474. int ret;
  475. ssize_t bytes_transferred = 0;
  476. __le32 tmp;
  477. if (len == 6)
  478. packet->data = 0;
  479. #ifdef SISUSB_DONTSYNC
  480. if (!(sisusb_wait_all_out_complete(sisusb)))
  481. return 1;
  482. #endif
  483. /* Eventually correct endianness */
  484. SISUSB_CORRECT_ENDIANNESS_PACKET(packet);
  485. /* 1. send the packet */
  486. ret = sisusb_send_bulk_msg(sisusb, SISUSB_EP_BRIDGE_OUT, len,
  487. (char *)packet, NULL, 0, &bytes_transferred, tflags, 0);
  488. if ((ret == 0) && (len == 6)) {
  489. /* 2. if packet len == 6, it means we read, so wait for 32bit
  490. * return value and write it to packet->data
  491. */
  492. ret = sisusb_recv_bulk_msg(sisusb, SISUSB_EP_BRIDGE_IN, 4,
  493. (char *)&tmp, NULL, &bytes_transferred, 0);
  494. packet->data = le32_to_cpu(tmp);
  495. }
  496. return ret;
  497. }
  498. /* access video memory and mmio (return 0 on success) */
  499. /* Low level */
  500. /* The following routines assume being used to transfer byte, word,
  501. * long etc.
  502. * This means that
  503. * - the write routines expect "data" in machine endianness format.
  504. * The data will be converted to leXX in sisusb_xxx_packet.
  505. * - the read routines can expect read data in machine-endianess.
  506. */
  507. static int sisusb_write_memio_byte(struct sisusb_usb_data *sisusb, int type,
  508. u32 addr, u8 data)
  509. {
  510. struct sisusb_packet packet;
  511. int ret;
  512. packet.header = (1 << (addr & 3)) | (type << 6);
  513. packet.address = addr & ~3;
  514. packet.data = data << ((addr & 3) << 3);
  515. ret = sisusb_send_packet(sisusb, 10, &packet);
  516. return ret;
  517. }
  518. static int sisusb_write_memio_word(struct sisusb_usb_data *sisusb, int type,
  519. u32 addr, u16 data)
  520. {
  521. struct sisusb_packet packet;
  522. int ret = 0;
  523. packet.address = addr & ~3;
  524. switch (addr & 3) {
  525. case 0:
  526. packet.header = (type << 6) | 0x0003;
  527. packet.data = (u32)data;
  528. ret = sisusb_send_packet(sisusb, 10, &packet);
  529. break;
  530. case 1:
  531. packet.header = (type << 6) | 0x0006;
  532. packet.data = (u32)data << 8;
  533. ret = sisusb_send_packet(sisusb, 10, &packet);
  534. break;
  535. case 2:
  536. packet.header = (type << 6) | 0x000c;
  537. packet.data = (u32)data << 16;
  538. ret = sisusb_send_packet(sisusb, 10, &packet);
  539. break;
  540. case 3:
  541. packet.header = (type << 6) | 0x0008;
  542. packet.data = (u32)data << 24;
  543. ret = sisusb_send_packet(sisusb, 10, &packet);
  544. packet.header = (type << 6) | 0x0001;
  545. packet.address = (addr & ~3) + 4;
  546. packet.data = (u32)data >> 8;
  547. ret |= sisusb_send_packet(sisusb, 10, &packet);
  548. }
  549. return ret;
  550. }
  551. static int sisusb_write_memio_24bit(struct sisusb_usb_data *sisusb, int type,
  552. u32 addr, u32 data)
  553. {
  554. struct sisusb_packet packet;
  555. int ret = 0;
  556. packet.address = addr & ~3;
  557. switch (addr & 3) {
  558. case 0:
  559. packet.header = (type << 6) | 0x0007;
  560. packet.data = data & 0x00ffffff;
  561. ret = sisusb_send_packet(sisusb, 10, &packet);
  562. break;
  563. case 1:
  564. packet.header = (type << 6) | 0x000e;
  565. packet.data = data << 8;
  566. ret = sisusb_send_packet(sisusb, 10, &packet);
  567. break;
  568. case 2:
  569. packet.header = (type << 6) | 0x000c;
  570. packet.data = data << 16;
  571. ret = sisusb_send_packet(sisusb, 10, &packet);
  572. packet.header = (type << 6) | 0x0001;
  573. packet.address = (addr & ~3) + 4;
  574. packet.data = (data >> 16) & 0x00ff;
  575. ret |= sisusb_send_packet(sisusb, 10, &packet);
  576. break;
  577. case 3:
  578. packet.header = (type << 6) | 0x0008;
  579. packet.data = data << 24;
  580. ret = sisusb_send_packet(sisusb, 10, &packet);
  581. packet.header = (type << 6) | 0x0003;
  582. packet.address = (addr & ~3) + 4;
  583. packet.data = (data >> 8) & 0xffff;
  584. ret |= sisusb_send_packet(sisusb, 10, &packet);
  585. }
  586. return ret;
  587. }
  588. static int sisusb_write_memio_long(struct sisusb_usb_data *sisusb, int type,
  589. u32 addr, u32 data)
  590. {
  591. struct sisusb_packet packet;
  592. int ret = 0;
  593. packet.address = addr & ~3;
  594. switch (addr & 3) {
  595. case 0:
  596. packet.header = (type << 6) | 0x000f;
  597. packet.data = data;
  598. ret = sisusb_send_packet(sisusb, 10, &packet);
  599. break;
  600. case 1:
  601. packet.header = (type << 6) | 0x000e;
  602. packet.data = data << 8;
  603. ret = sisusb_send_packet(sisusb, 10, &packet);
  604. packet.header = (type << 6) | 0x0001;
  605. packet.address = (addr & ~3) + 4;
  606. packet.data = data >> 24;
  607. ret |= sisusb_send_packet(sisusb, 10, &packet);
  608. break;
  609. case 2:
  610. packet.header = (type << 6) | 0x000c;
  611. packet.data = data << 16;
  612. ret = sisusb_send_packet(sisusb, 10, &packet);
  613. packet.header = (type << 6) | 0x0003;
  614. packet.address = (addr & ~3) + 4;
  615. packet.data = data >> 16;
  616. ret |= sisusb_send_packet(sisusb, 10, &packet);
  617. break;
  618. case 3:
  619. packet.header = (type << 6) | 0x0008;
  620. packet.data = data << 24;
  621. ret = sisusb_send_packet(sisusb, 10, &packet);
  622. packet.header = (type << 6) | 0x0007;
  623. packet.address = (addr & ~3) + 4;
  624. packet.data = data >> 8;
  625. ret |= sisusb_send_packet(sisusb, 10, &packet);
  626. }
  627. return ret;
  628. }
  629. /* The xxx_bulk routines copy a buffer of variable size. They treat the
  630. * buffer as chars, therefore lsb/msb has to be corrected if using the
  631. * byte/word/long/etc routines for speed-up
  632. *
  633. * If data is from userland, set "userbuffer" (and clear "kernbuffer"),
  634. * if data is in kernel space, set "kernbuffer" (and clear "userbuffer");
  635. * if neither "kernbuffer" nor "userbuffer" are given, it is assumed
  636. * that the data already is in the transfer buffer "sisusb->obuf[index]".
  637. */
  638. static int sisusb_write_mem_bulk(struct sisusb_usb_data *sisusb, u32 addr,
  639. char *kernbuffer, int length,
  640. const char __user *userbuffer, int index,
  641. ssize_t *bytes_written)
  642. {
  643. struct sisusb_packet packet;
  644. int ret = 0;
  645. static int msgcount = 0;
  646. u8 swap8, fromkern = kernbuffer ? 1 : 0;
  647. u16 swap16;
  648. u32 swap32, flag = (length >> 28) & 1;
  649. char buf[4];
  650. /* if neither kernbuffer not userbuffer are given, assume
  651. * data in obuf
  652. */
  653. if (!fromkern && !userbuffer)
  654. kernbuffer = sisusb->obuf[index];
  655. (*bytes_written = 0);
  656. length &= 0x00ffffff;
  657. while (length) {
  658. switch (length) {
  659. case 1:
  660. if (userbuffer) {
  661. if (get_user(swap8, (u8 __user *)userbuffer))
  662. return -EFAULT;
  663. } else
  664. swap8 = kernbuffer[0];
  665. ret = sisusb_write_memio_byte(sisusb,
  666. SISUSB_TYPE_MEM,
  667. addr, swap8);
  668. if (!ret)
  669. (*bytes_written)++;
  670. return ret;
  671. case 2:
  672. if (userbuffer) {
  673. if (get_user(swap16, (u16 __user *)userbuffer))
  674. return -EFAULT;
  675. } else
  676. swap16 = *((u16 *)kernbuffer);
  677. ret = sisusb_write_memio_word(sisusb,
  678. SISUSB_TYPE_MEM,
  679. addr,
  680. swap16);
  681. if (!ret)
  682. (*bytes_written) += 2;
  683. return ret;
  684. case 3:
  685. if (userbuffer) {
  686. if (copy_from_user(&buf, userbuffer, 3))
  687. return -EFAULT;
  688. #ifdef __BIG_ENDIAN
  689. swap32 = (buf[0] << 16) |
  690. (buf[1] << 8) |
  691. buf[2];
  692. #else
  693. swap32 = (buf[2] << 16) |
  694. (buf[1] << 8) |
  695. buf[0];
  696. #endif
  697. } else
  698. #ifdef __BIG_ENDIAN
  699. swap32 = (kernbuffer[0] << 16) |
  700. (kernbuffer[1] << 8) |
  701. kernbuffer[2];
  702. #else
  703. swap32 = (kernbuffer[2] << 16) |
  704. (kernbuffer[1] << 8) |
  705. kernbuffer[0];
  706. #endif
  707. ret = sisusb_write_memio_24bit(sisusb,
  708. SISUSB_TYPE_MEM,
  709. addr,
  710. swap32);
  711. if (!ret)
  712. (*bytes_written) += 3;
  713. return ret;
  714. case 4:
  715. if (userbuffer) {
  716. if (get_user(swap32, (u32 __user *)userbuffer))
  717. return -EFAULT;
  718. } else
  719. swap32 = *((u32 *)kernbuffer);
  720. ret = sisusb_write_memio_long(sisusb,
  721. SISUSB_TYPE_MEM,
  722. addr,
  723. swap32);
  724. if (!ret)
  725. (*bytes_written) += 4;
  726. return ret;
  727. default:
  728. if ((length & ~3) > 0x10000) {
  729. packet.header = 0x001f;
  730. packet.address = 0x000001d4;
  731. packet.data = addr;
  732. ret = sisusb_send_bridge_packet(sisusb, 10,
  733. &packet, 0);
  734. packet.header = 0x001f;
  735. packet.address = 0x000001d0;
  736. packet.data = (length & ~3);
  737. ret |= sisusb_send_bridge_packet(sisusb, 10,
  738. &packet, 0);
  739. packet.header = 0x001f;
  740. packet.address = 0x000001c0;
  741. packet.data = flag | 0x16;
  742. ret |= sisusb_send_bridge_packet(sisusb, 10,
  743. &packet, 0);
  744. if (userbuffer) {
  745. ret |= sisusb_send_bulk_msg(sisusb,
  746. SISUSB_EP_GFX_LBULK_OUT,
  747. (length & ~3),
  748. NULL, userbuffer, 0,
  749. bytes_written, 0, 1);
  750. userbuffer += (*bytes_written);
  751. } else if (fromkern) {
  752. ret |= sisusb_send_bulk_msg(sisusb,
  753. SISUSB_EP_GFX_LBULK_OUT,
  754. (length & ~3),
  755. kernbuffer, NULL, 0,
  756. bytes_written, 0, 1);
  757. kernbuffer += (*bytes_written);
  758. } else {
  759. ret |= sisusb_send_bulk_msg(sisusb,
  760. SISUSB_EP_GFX_LBULK_OUT,
  761. (length & ~3),
  762. NULL, NULL, index,
  763. bytes_written, 0, 1);
  764. kernbuffer += ((*bytes_written) &
  765. (sisusb->obufsize-1));
  766. }
  767. } else {
  768. packet.header = 0x001f;
  769. packet.address = 0x00000194;
  770. packet.data = addr;
  771. ret = sisusb_send_bridge_packet(sisusb, 10,
  772. &packet, 0);
  773. packet.header = 0x001f;
  774. packet.address = 0x00000190;
  775. packet.data = (length & ~3);
  776. ret |= sisusb_send_bridge_packet(sisusb, 10,
  777. &packet, 0);
  778. if (sisusb->flagb0 != 0x16) {
  779. packet.header = 0x001f;
  780. packet.address = 0x00000180;
  781. packet.data = flag | 0x16;
  782. ret |= sisusb_send_bridge_packet(sisusb, 10,
  783. &packet, 0);
  784. sisusb->flagb0 = 0x16;
  785. }
  786. if (userbuffer) {
  787. ret |= sisusb_send_bulk_msg(sisusb,
  788. SISUSB_EP_GFX_BULK_OUT,
  789. (length & ~3),
  790. NULL, userbuffer, 0,
  791. bytes_written, 0, 1);
  792. userbuffer += (*bytes_written);
  793. } else if (fromkern) {
  794. ret |= sisusb_send_bulk_msg(sisusb,
  795. SISUSB_EP_GFX_BULK_OUT,
  796. (length & ~3),
  797. kernbuffer, NULL, 0,
  798. bytes_written, 0, 1);
  799. kernbuffer += (*bytes_written);
  800. } else {
  801. ret |= sisusb_send_bulk_msg(sisusb,
  802. SISUSB_EP_GFX_BULK_OUT,
  803. (length & ~3),
  804. NULL, NULL, index,
  805. bytes_written, 0, 1);
  806. kernbuffer += ((*bytes_written) &
  807. (sisusb->obufsize-1));
  808. }
  809. }
  810. if (ret) {
  811. msgcount++;
  812. if (msgcount < 500)
  813. printk(KERN_ERR
  814. "sisusbvga[%d]: Wrote %zd of "
  815. "%d bytes, error %d\n",
  816. sisusb->minor, *bytes_written,
  817. length, ret);
  818. else if (msgcount == 500)
  819. printk(KERN_ERR
  820. "sisusbvga[%d]: Too many errors"
  821. ", logging stopped\n",
  822. sisusb->minor);
  823. }
  824. addr += (*bytes_written);
  825. length -= (*bytes_written);
  826. }
  827. if (ret)
  828. break;
  829. }
  830. return ret ? -EIO : 0;
  831. }
  832. /* Remember: Read data in packet is in machine-endianess! So for
  833. * byte, word, 24bit, long no endian correction is necessary.
  834. */
  835. static int sisusb_read_memio_byte(struct sisusb_usb_data *sisusb, int type,
  836. u32 addr, u8 *data)
  837. {
  838. struct sisusb_packet packet;
  839. int ret;
  840. CLEARPACKET(&packet);
  841. packet.header = (1 << (addr & 3)) | (type << 6);
  842. packet.address = addr & ~3;
  843. ret = sisusb_send_packet(sisusb, 6, &packet);
  844. *data = (u8)(packet.data >> ((addr & 3) << 3));
  845. return ret;
  846. }
  847. static int sisusb_read_memio_word(struct sisusb_usb_data *sisusb, int type,
  848. u32 addr, u16 *data)
  849. {
  850. struct sisusb_packet packet;
  851. int ret = 0;
  852. CLEARPACKET(&packet);
  853. packet.address = addr & ~3;
  854. switch (addr & 3) {
  855. case 0:
  856. packet.header = (type << 6) | 0x0003;
  857. ret = sisusb_send_packet(sisusb, 6, &packet);
  858. *data = (u16)(packet.data);
  859. break;
  860. case 1:
  861. packet.header = (type << 6) | 0x0006;
  862. ret = sisusb_send_packet(sisusb, 6, &packet);
  863. *data = (u16)(packet.data >> 8);
  864. break;
  865. case 2:
  866. packet.header = (type << 6) | 0x000c;
  867. ret = sisusb_send_packet(sisusb, 6, &packet);
  868. *data = (u16)(packet.data >> 16);
  869. break;
  870. case 3:
  871. packet.header = (type << 6) | 0x0008;
  872. ret = sisusb_send_packet(sisusb, 6, &packet);
  873. *data = (u16)(packet.data >> 24);
  874. packet.header = (type << 6) | 0x0001;
  875. packet.address = (addr & ~3) + 4;
  876. ret |= sisusb_send_packet(sisusb, 6, &packet);
  877. *data |= (u16)(packet.data << 8);
  878. }
  879. return ret;
  880. }
  881. static int sisusb_read_memio_24bit(struct sisusb_usb_data *sisusb, int type,
  882. u32 addr, u32 *data)
  883. {
  884. struct sisusb_packet packet;
  885. int ret = 0;
  886. packet.address = addr & ~3;
  887. switch (addr & 3) {
  888. case 0:
  889. packet.header = (type << 6) | 0x0007;
  890. ret = sisusb_send_packet(sisusb, 6, &packet);
  891. *data = packet.data & 0x00ffffff;
  892. break;
  893. case 1:
  894. packet.header = (type << 6) | 0x000e;
  895. ret = sisusb_send_packet(sisusb, 6, &packet);
  896. *data = packet.data >> 8;
  897. break;
  898. case 2:
  899. packet.header = (type << 6) | 0x000c;
  900. ret = sisusb_send_packet(sisusb, 6, &packet);
  901. *data = packet.data >> 16;
  902. packet.header = (type << 6) | 0x0001;
  903. packet.address = (addr & ~3) + 4;
  904. ret |= sisusb_send_packet(sisusb, 6, &packet);
  905. *data |= ((packet.data & 0xff) << 16);
  906. break;
  907. case 3:
  908. packet.header = (type << 6) | 0x0008;
  909. ret = sisusb_send_packet(sisusb, 6, &packet);
  910. *data = packet.data >> 24;
  911. packet.header = (type << 6) | 0x0003;
  912. packet.address = (addr & ~3) + 4;
  913. ret |= sisusb_send_packet(sisusb, 6, &packet);
  914. *data |= ((packet.data & 0xffff) << 8);
  915. }
  916. return ret;
  917. }
  918. static int sisusb_read_memio_long(struct sisusb_usb_data *sisusb, int type,
  919. u32 addr, u32 *data)
  920. {
  921. struct sisusb_packet packet;
  922. int ret = 0;
  923. packet.address = addr & ~3;
  924. switch (addr & 3) {
  925. case 0:
  926. packet.header = (type << 6) | 0x000f;
  927. ret = sisusb_send_packet(sisusb, 6, &packet);
  928. *data = packet.data;
  929. break;
  930. case 1:
  931. packet.header = (type << 6) | 0x000e;
  932. ret = sisusb_send_packet(sisusb, 6, &packet);
  933. *data = packet.data >> 8;
  934. packet.header = (type << 6) | 0x0001;
  935. packet.address = (addr & ~3) + 4;
  936. ret |= sisusb_send_packet(sisusb, 6, &packet);
  937. *data |= (packet.data << 24);
  938. break;
  939. case 2:
  940. packet.header = (type << 6) | 0x000c;
  941. ret = sisusb_send_packet(sisusb, 6, &packet);
  942. *data = packet.data >> 16;
  943. packet.header = (type << 6) | 0x0003;
  944. packet.address = (addr & ~3) + 4;
  945. ret |= sisusb_send_packet(sisusb, 6, &packet);
  946. *data |= (packet.data << 16);
  947. break;
  948. case 3:
  949. packet.header = (type << 6) | 0x0008;
  950. ret = sisusb_send_packet(sisusb, 6, &packet);
  951. *data = packet.data >> 24;
  952. packet.header = (type << 6) | 0x0007;
  953. packet.address = (addr & ~3) + 4;
  954. ret |= sisusb_send_packet(sisusb, 6, &packet);
  955. *data |= (packet.data << 8);
  956. }
  957. return ret;
  958. }
  959. static int sisusb_read_mem_bulk(struct sisusb_usb_data *sisusb, u32 addr,
  960. char *kernbuffer, int length,
  961. char __user *userbuffer, ssize_t *bytes_read)
  962. {
  963. int ret = 0;
  964. char buf[4];
  965. u16 swap16;
  966. u32 swap32;
  967. (*bytes_read = 0);
  968. length &= 0x00ffffff;
  969. while (length) {
  970. switch (length) {
  971. case 1:
  972. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM,
  973. addr, &buf[0]);
  974. if (!ret) {
  975. (*bytes_read)++;
  976. if (userbuffer) {
  977. if (put_user(buf[0],
  978. (u8 __user *)userbuffer)) {
  979. return -EFAULT;
  980. }
  981. } else {
  982. kernbuffer[0] = buf[0];
  983. }
  984. }
  985. return ret;
  986. case 2:
  987. ret |= sisusb_read_memio_word(sisusb, SISUSB_TYPE_MEM,
  988. addr, &swap16);
  989. if (!ret) {
  990. (*bytes_read) += 2;
  991. if (userbuffer) {
  992. if (put_user(swap16,
  993. (u16 __user *)userbuffer))
  994. return -EFAULT;
  995. } else {
  996. *((u16 *)kernbuffer) = swap16;
  997. }
  998. }
  999. return ret;
  1000. case 3:
  1001. ret |= sisusb_read_memio_24bit(sisusb, SISUSB_TYPE_MEM,
  1002. addr, &swap32);
  1003. if (!ret) {
  1004. (*bytes_read) += 3;
  1005. #ifdef __BIG_ENDIAN
  1006. buf[0] = (swap32 >> 16) & 0xff;
  1007. buf[1] = (swap32 >> 8) & 0xff;
  1008. buf[2] = swap32 & 0xff;
  1009. #else
  1010. buf[2] = (swap32 >> 16) & 0xff;
  1011. buf[1] = (swap32 >> 8) & 0xff;
  1012. buf[0] = swap32 & 0xff;
  1013. #endif
  1014. if (userbuffer) {
  1015. if (copy_to_user(userbuffer, &buf[0], 3))
  1016. return -EFAULT;
  1017. } else {
  1018. kernbuffer[0] = buf[0];
  1019. kernbuffer[1] = buf[1];
  1020. kernbuffer[2] = buf[2];
  1021. }
  1022. }
  1023. return ret;
  1024. default:
  1025. ret |= sisusb_read_memio_long(sisusb, SISUSB_TYPE_MEM,
  1026. addr, &swap32);
  1027. if (!ret) {
  1028. (*bytes_read) += 4;
  1029. if (userbuffer) {
  1030. if (put_user(swap32,
  1031. (u32 __user *)userbuffer))
  1032. return -EFAULT;
  1033. userbuffer += 4;
  1034. } else {
  1035. *((u32 *)kernbuffer) = swap32;
  1036. kernbuffer += 4;
  1037. }
  1038. addr += 4;
  1039. length -= 4;
  1040. }
  1041. #if 0 /* That does not work, as EP 2 is an OUT EP! */
  1042. default:
  1043. CLEARPACKET(&packet);
  1044. packet.header = 0x001f;
  1045. packet.address = 0x000001a0;
  1046. packet.data = 0x00000006;
  1047. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1048. &packet, 0);
  1049. packet.header = 0x001f;
  1050. packet.address = 0x000001b0;
  1051. packet.data = (length & ~3) | 0x40000000;
  1052. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1053. &packet, 0);
  1054. packet.header = 0x001f;
  1055. packet.address = 0x000001b4;
  1056. packet.data = addr;
  1057. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1058. &packet, 0);
  1059. packet.header = 0x001f;
  1060. packet.address = 0x000001a4;
  1061. packet.data = 0x00000001;
  1062. ret |= sisusb_send_bridge_packet(sisusb, 10,
  1063. &packet, 0);
  1064. if (userbuffer) {
  1065. ret |= sisusb_recv_bulk_msg(sisusb,
  1066. SISUSB_EP_GFX_BULK_IN,
  1067. (length & ~3),
  1068. NULL, userbuffer,
  1069. bytes_read, 0);
  1070. if (!ret) userbuffer += (*bytes_read);
  1071. } else {
  1072. ret |= sisusb_recv_bulk_msg(sisusb,
  1073. SISUSB_EP_GFX_BULK_IN,
  1074. (length & ~3),
  1075. kernbuffer, NULL,
  1076. bytes_read, 0);
  1077. if (!ret) kernbuffer += (*bytes_read);
  1078. }
  1079. addr += (*bytes_read);
  1080. length -= (*bytes_read);
  1081. #endif
  1082. }
  1083. if (ret)
  1084. break;
  1085. }
  1086. return ret;
  1087. }
  1088. /* High level: Gfx (indexed) register access */
  1089. #ifdef INCL_SISUSB_CON
  1090. int
  1091. sisusb_setreg(struct sisusb_usb_data *sisusb, int port, u8 data)
  1092. {
  1093. return sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, data);
  1094. }
  1095. int
  1096. sisusb_getreg(struct sisusb_usb_data *sisusb, int port, u8 *data)
  1097. {
  1098. return sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port, data);
  1099. }
  1100. #endif
  1101. int
  1102. sisusb_setidxreg(struct sisusb_usb_data *sisusb, int port, u8 index, u8 data)
  1103. {
  1104. int ret;
  1105. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, index);
  1106. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, data);
  1107. return ret;
  1108. }
  1109. int
  1110. sisusb_getidxreg(struct sisusb_usb_data *sisusb, int port, u8 index, u8 *data)
  1111. {
  1112. int ret;
  1113. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, index);
  1114. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, data);
  1115. return ret;
  1116. }
  1117. int
  1118. sisusb_setidxregandor(struct sisusb_usb_data *sisusb, int port, u8 idx,
  1119. u8 myand, u8 myor)
  1120. {
  1121. int ret;
  1122. u8 tmp;
  1123. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, idx);
  1124. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, &tmp);
  1125. tmp &= myand;
  1126. tmp |= myor;
  1127. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, tmp);
  1128. return ret;
  1129. }
  1130. static int
  1131. sisusb_setidxregmask(struct sisusb_usb_data *sisusb, int port, u8 idx,
  1132. u8 data, u8 mask)
  1133. {
  1134. int ret;
  1135. u8 tmp;
  1136. ret = sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port, idx);
  1137. ret |= sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, &tmp);
  1138. tmp &= ~(mask);
  1139. tmp |= (data & mask);
  1140. ret |= sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, port + 1, tmp);
  1141. return ret;
  1142. }
  1143. int
  1144. sisusb_setidxregor(struct sisusb_usb_data *sisusb, int port, u8 index, u8 myor)
  1145. {
  1146. return(sisusb_setidxregandor(sisusb, port, index, 0xff, myor));
  1147. }
  1148. int
  1149. sisusb_setidxregand(struct sisusb_usb_data *sisusb, int port, u8 idx, u8 myand)
  1150. {
  1151. return(sisusb_setidxregandor(sisusb, port, idx, myand, 0x00));
  1152. }
  1153. /* Write/read video ram */
  1154. #ifdef INCL_SISUSB_CON
  1155. int
  1156. sisusb_writeb(struct sisusb_usb_data *sisusb, u32 adr, u8 data)
  1157. {
  1158. return(sisusb_write_memio_byte(sisusb, SISUSB_TYPE_MEM, adr, data));
  1159. }
  1160. int
  1161. sisusb_readb(struct sisusb_usb_data *sisusb, u32 adr, u8 *data)
  1162. {
  1163. return(sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM, adr, data));
  1164. }
  1165. #if 0
  1166. int
  1167. sisusb_writew(struct sisusb_usb_data *sisusb, u32 adr, u16 data)
  1168. {
  1169. return(sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM, adr, data));
  1170. }
  1171. int
  1172. sisusb_readw(struct sisusb_usb_data *sisusb, u32 adr, u16 *data)
  1173. {
  1174. return(sisusb_read_memio_word(sisusb, SISUSB_TYPE_MEM, adr, data));
  1175. }
  1176. #endif /* 0 */
  1177. int
  1178. sisusb_copy_memory(struct sisusb_usb_data *sisusb, char *src,
  1179. u32 dest, int length, size_t *bytes_written)
  1180. {
  1181. return(sisusb_write_mem_bulk(sisusb, dest, src, length, NULL, 0, bytes_written));
  1182. }
  1183. #ifdef SISUSBENDIANTEST
  1184. int
  1185. sisusb_read_memory(struct sisusb_usb_data *sisusb, char *dest,
  1186. u32 src, int length, size_t *bytes_written)
  1187. {
  1188. return(sisusb_read_mem_bulk(sisusb, src, dest, length, NULL, bytes_written));
  1189. }
  1190. #endif
  1191. #endif
  1192. #ifdef SISUSBENDIANTEST
  1193. static void
  1194. sisusb_testreadwrite(struct sisusb_usb_data *sisusb)
  1195. {
  1196. static char srcbuffer[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
  1197. char destbuffer[10];
  1198. size_t dummy;
  1199. int i,j;
  1200. sisusb_copy_memory(sisusb, srcbuffer, sisusb->vrambase, 7, &dummy);
  1201. for(i = 1; i <= 7; i++) {
  1202. printk(KERN_DEBUG "sisusb: rwtest %d bytes\n", i);
  1203. sisusb_read_memory(sisusb, destbuffer, sisusb->vrambase, i, &dummy);
  1204. for(j = 0; j < i; j++) {
  1205. printk(KERN_DEBUG "sisusb: rwtest read[%d] = %x\n", j, destbuffer[j]);
  1206. }
  1207. }
  1208. }
  1209. #endif
  1210. /* access pci config registers (reg numbers 0, 4, 8, etc) */
  1211. static int
  1212. sisusb_write_pci_config(struct sisusb_usb_data *sisusb, int regnum, u32 data)
  1213. {
  1214. struct sisusb_packet packet;
  1215. int ret;
  1216. packet.header = 0x008f;
  1217. packet.address = regnum | 0x10000;
  1218. packet.data = data;
  1219. ret = sisusb_send_packet(sisusb, 10, &packet);
  1220. return ret;
  1221. }
  1222. static int
  1223. sisusb_read_pci_config(struct sisusb_usb_data *sisusb, int regnum, u32 *data)
  1224. {
  1225. struct sisusb_packet packet;
  1226. int ret;
  1227. packet.header = 0x008f;
  1228. packet.address = (u32)regnum | 0x10000;
  1229. ret = sisusb_send_packet(sisusb, 6, &packet);
  1230. *data = packet.data;
  1231. return ret;
  1232. }
  1233. /* Clear video RAM */
  1234. static int
  1235. sisusb_clear_vram(struct sisusb_usb_data *sisusb, u32 address, int length)
  1236. {
  1237. int ret, i;
  1238. ssize_t j;
  1239. if (address < sisusb->vrambase)
  1240. return 1;
  1241. if (address >= sisusb->vrambase + sisusb->vramsize)
  1242. return 1;
  1243. if (address + length > sisusb->vrambase + sisusb->vramsize)
  1244. length = sisusb->vrambase + sisusb->vramsize - address;
  1245. if (length <= 0)
  1246. return 0;
  1247. /* allocate free buffer/urb and clear the buffer */
  1248. if ((i = sisusb_alloc_outbuf(sisusb)) < 0)
  1249. return -EBUSY;
  1250. memset(sisusb->obuf[i], 0, sisusb->obufsize);
  1251. /* We can write a length > buffer size here. The buffer
  1252. * data will simply be re-used (like a ring-buffer).
  1253. */
  1254. ret = sisusb_write_mem_bulk(sisusb, address, NULL, length, NULL, i, &j);
  1255. /* Free the buffer/urb */
  1256. sisusb_free_outbuf(sisusb, i);
  1257. return ret;
  1258. }
  1259. /* Initialize the graphics core (return 0 on success)
  1260. * This resets the graphics hardware and puts it into
  1261. * a defined mode (640x480@60Hz)
  1262. */
  1263. #define GETREG(r,d) sisusb_read_memio_byte(sisusb, SISUSB_TYPE_IO, r, d)
  1264. #define SETREG(r,d) sisusb_write_memio_byte(sisusb, SISUSB_TYPE_IO, r, d)
  1265. #define SETIREG(r,i,d) sisusb_setidxreg(sisusb, r, i, d)
  1266. #define GETIREG(r,i,d) sisusb_getidxreg(sisusb, r, i, d)
  1267. #define SETIREGOR(r,i,o) sisusb_setidxregor(sisusb, r, i, o)
  1268. #define SETIREGAND(r,i,a) sisusb_setidxregand(sisusb, r, i, a)
  1269. #define SETIREGANDOR(r,i,a,o) sisusb_setidxregandor(sisusb, r, i, a, o)
  1270. #define READL(a,d) sisusb_read_memio_long(sisusb, SISUSB_TYPE_MEM, a, d)
  1271. #define WRITEL(a,d) sisusb_write_memio_long(sisusb, SISUSB_TYPE_MEM, a, d)
  1272. #define READB(a,d) sisusb_read_memio_byte(sisusb, SISUSB_TYPE_MEM, a, d)
  1273. #define WRITEB(a,d) sisusb_write_memio_byte(sisusb, SISUSB_TYPE_MEM, a, d)
  1274. static int
  1275. sisusb_triggersr16(struct sisusb_usb_data *sisusb, u8 ramtype)
  1276. {
  1277. int ret;
  1278. u8 tmp8;
  1279. ret = GETIREG(SISSR, 0x16, &tmp8);
  1280. if (ramtype <= 1) {
  1281. tmp8 &= 0x3f;
  1282. ret |= SETIREG(SISSR, 0x16, tmp8);
  1283. tmp8 |= 0x80;
  1284. ret |= SETIREG(SISSR, 0x16, tmp8);
  1285. } else {
  1286. tmp8 |= 0xc0;
  1287. ret |= SETIREG(SISSR, 0x16, tmp8);
  1288. tmp8 &= 0x0f;
  1289. ret |= SETIREG(SISSR, 0x16, tmp8);
  1290. tmp8 |= 0x80;
  1291. ret |= SETIREG(SISSR, 0x16, tmp8);
  1292. tmp8 &= 0x0f;
  1293. ret |= SETIREG(SISSR, 0x16, tmp8);
  1294. tmp8 |= 0xd0;
  1295. ret |= SETIREG(SISSR, 0x16, tmp8);
  1296. tmp8 &= 0x0f;
  1297. ret |= SETIREG(SISSR, 0x16, tmp8);
  1298. tmp8 |= 0xa0;
  1299. ret |= SETIREG(SISSR, 0x16, tmp8);
  1300. }
  1301. return ret;
  1302. }
  1303. static int
  1304. sisusb_getbuswidth(struct sisusb_usb_data *sisusb, int *bw, int *chab)
  1305. {
  1306. int ret;
  1307. u8 ramtype, done = 0;
  1308. u32 t0, t1, t2, t3;
  1309. u32 ramptr = SISUSB_PCI_MEMBASE;
  1310. ret = GETIREG(SISSR, 0x3a, &ramtype);
  1311. ramtype &= 3;
  1312. ret |= SETIREG(SISSR, 0x13, 0x00);
  1313. if (ramtype <= 1) {
  1314. ret |= SETIREG(SISSR, 0x14, 0x12);
  1315. ret |= SETIREGAND(SISSR, 0x15, 0xef);
  1316. } else {
  1317. ret |= SETIREG(SISSR, 0x14, 0x02);
  1318. }
  1319. ret |= sisusb_triggersr16(sisusb, ramtype);
  1320. ret |= WRITEL(ramptr + 0, 0x01234567);
  1321. ret |= WRITEL(ramptr + 4, 0x456789ab);
  1322. ret |= WRITEL(ramptr + 8, 0x89abcdef);
  1323. ret |= WRITEL(ramptr + 12, 0xcdef0123);
  1324. ret |= WRITEL(ramptr + 16, 0x55555555);
  1325. ret |= WRITEL(ramptr + 20, 0x55555555);
  1326. ret |= WRITEL(ramptr + 24, 0xffffffff);
  1327. ret |= WRITEL(ramptr + 28, 0xffffffff);
  1328. ret |= READL(ramptr + 0, &t0);
  1329. ret |= READL(ramptr + 4, &t1);
  1330. ret |= READL(ramptr + 8, &t2);
  1331. ret |= READL(ramptr + 12, &t3);
  1332. if (ramtype <= 1) {
  1333. *chab = 0; *bw = 64;
  1334. if ((t3 != 0xcdef0123) || (t2 != 0x89abcdef)) {
  1335. if ((t1 == 0x456789ab) && (t0 == 0x01234567)) {
  1336. *chab = 0; *bw = 64;
  1337. ret |= SETIREGAND(SISSR, 0x14, 0xfd);
  1338. }
  1339. }
  1340. if ((t1 != 0x456789ab) || (t0 != 0x01234567)) {
  1341. *chab = 1; *bw = 64;
  1342. ret |= SETIREGANDOR(SISSR, 0x14, 0xfc,0x01);
  1343. ret |= sisusb_triggersr16(sisusb, ramtype);
  1344. ret |= WRITEL(ramptr + 0, 0x89abcdef);
  1345. ret |= WRITEL(ramptr + 4, 0xcdef0123);
  1346. ret |= WRITEL(ramptr + 8, 0x55555555);
  1347. ret |= WRITEL(ramptr + 12, 0x55555555);
  1348. ret |= WRITEL(ramptr + 16, 0xaaaaaaaa);
  1349. ret |= WRITEL(ramptr + 20, 0xaaaaaaaa);
  1350. ret |= READL(ramptr + 4, &t1);
  1351. if (t1 != 0xcdef0123) {
  1352. *bw = 32;
  1353. ret |= SETIREGOR(SISSR, 0x15, 0x10);
  1354. }
  1355. }
  1356. } else {
  1357. *chab = 0; *bw = 64; /* default: cha, bw = 64 */
  1358. done = 0;
  1359. if (t1 == 0x456789ab) {
  1360. if (t0 == 0x01234567) {
  1361. *chab = 0; *bw = 64;
  1362. done = 1;
  1363. }
  1364. } else {
  1365. if (t0 == 0x01234567) {
  1366. *chab = 0; *bw = 32;
  1367. ret |= SETIREG(SISSR, 0x14, 0x00);
  1368. done = 1;
  1369. }
  1370. }
  1371. if (!done) {
  1372. ret |= SETIREG(SISSR, 0x14, 0x03);
  1373. ret |= sisusb_triggersr16(sisusb, ramtype);
  1374. ret |= WRITEL(ramptr + 0, 0x01234567);
  1375. ret |= WRITEL(ramptr + 4, 0x456789ab);
  1376. ret |= WRITEL(ramptr + 8, 0x89abcdef);
  1377. ret |= WRITEL(ramptr + 12, 0xcdef0123);
  1378. ret |= WRITEL(ramptr + 16, 0x55555555);
  1379. ret |= WRITEL(ramptr + 20, 0x55555555);
  1380. ret |= WRITEL(ramptr + 24, 0xffffffff);
  1381. ret |= WRITEL(ramptr + 28, 0xffffffff);
  1382. ret |= READL(ramptr + 0, &t0);
  1383. ret |= READL(ramptr + 4, &t1);
  1384. if (t1 == 0x456789ab) {
  1385. if (t0 == 0x01234567) {
  1386. *chab = 1; *bw = 64;
  1387. return ret;
  1388. } /* else error */
  1389. } else {
  1390. if (t0 == 0x01234567) {
  1391. *chab = 1; *bw = 32;
  1392. ret |= SETIREG(SISSR, 0x14, 0x01);
  1393. } /* else error */
  1394. }
  1395. }
  1396. }
  1397. return ret;
  1398. }
  1399. static int
  1400. sisusb_verify_mclk(struct sisusb_usb_data *sisusb)
  1401. {
  1402. int ret = 0;
  1403. u32 ramptr = SISUSB_PCI_MEMBASE;
  1404. u8 tmp1, tmp2, i, j;
  1405. ret |= WRITEB(ramptr, 0xaa);
  1406. ret |= WRITEB(ramptr + 16, 0x55);
  1407. ret |= READB(ramptr, &tmp1);
  1408. ret |= READB(ramptr + 16, &tmp2);
  1409. if ((tmp1 != 0xaa) || (tmp2 != 0x55)) {
  1410. for (i = 0, j = 16; i < 2; i++, j += 16) {
  1411. ret |= GETIREG(SISSR, 0x21, &tmp1);
  1412. ret |= SETIREGAND(SISSR, 0x21, (tmp1 & 0xfb));
  1413. ret |= SETIREGOR(SISSR, 0x3c, 0x01); /* not on 330 */
  1414. ret |= SETIREGAND(SISSR, 0x3c, 0xfe); /* not on 330 */
  1415. ret |= SETIREG(SISSR, 0x21, tmp1);
  1416. ret |= WRITEB(ramptr + 16 + j, j);
  1417. ret |= READB(ramptr + 16 + j, &tmp1);
  1418. if (tmp1 == j) {
  1419. ret |= WRITEB(ramptr + j, j);
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. return ret;
  1425. }
  1426. static int
  1427. sisusb_set_rank(struct sisusb_usb_data *sisusb, int *iret, int index,
  1428. u8 rankno, u8 chab, const u8 dramtype[][5],
  1429. int bw)
  1430. {
  1431. int ret = 0, ranksize;
  1432. u8 tmp;
  1433. *iret = 0;
  1434. if ((rankno == 2) && (dramtype[index][0] == 2))
  1435. return ret;
  1436. ranksize = dramtype[index][3] / 2 * bw / 32;
  1437. if ((ranksize * rankno) > 128)
  1438. return ret;
  1439. tmp = 0;
  1440. while ((ranksize >>= 1) > 0) tmp += 0x10;
  1441. tmp |= ((rankno - 1) << 2);
  1442. tmp |= ((bw / 64) & 0x02);
  1443. tmp |= (chab & 0x01);
  1444. ret = SETIREG(SISSR, 0x14, tmp);
  1445. ret |= sisusb_triggersr16(sisusb, 0); /* sic! */
  1446. *iret = 1;
  1447. return ret;
  1448. }
  1449. static int
  1450. sisusb_check_rbc(struct sisusb_usb_data *sisusb, int *iret, u32 inc, int testn)
  1451. {
  1452. int ret = 0, i;
  1453. u32 j, tmp;
  1454. *iret = 0;
  1455. for (i = 0, j = 0; i < testn; i++) {
  1456. ret |= WRITEL(sisusb->vrambase + j, j);
  1457. j += inc;
  1458. }
  1459. for (i = 0, j = 0; i < testn; i++) {
  1460. ret |= READL(sisusb->vrambase + j, &tmp);
  1461. if (tmp != j) return ret;
  1462. j += inc;
  1463. }
  1464. *iret = 1;
  1465. return ret;
  1466. }
  1467. static int
  1468. sisusb_check_ranks(struct sisusb_usb_data *sisusb, int *iret, int rankno,
  1469. int idx, int bw, const u8 rtype[][5])
  1470. {
  1471. int ret = 0, i, i2ret;
  1472. u32 inc;
  1473. *iret = 0;
  1474. for (i = rankno; i >= 1; i--) {
  1475. inc = 1 << (rtype[idx][2] +
  1476. rtype[idx][1] +
  1477. rtype[idx][0] +
  1478. bw / 64 + i);
  1479. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 2);
  1480. if (!i2ret)
  1481. return ret;
  1482. }
  1483. inc = 1 << (rtype[idx][2] + bw / 64 + 2);
  1484. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 4);
  1485. if (!i2ret)
  1486. return ret;
  1487. inc = 1 << (10 + bw / 64);
  1488. ret |= sisusb_check_rbc(sisusb, &i2ret, inc, 2);
  1489. if (!i2ret)
  1490. return ret;
  1491. *iret = 1;
  1492. return ret;
  1493. }
  1494. static int
  1495. sisusb_get_sdram_size(struct sisusb_usb_data *sisusb, int *iret, int bw,
  1496. int chab)
  1497. {
  1498. int ret = 0, i2ret = 0, i, j;
  1499. static const u8 sdramtype[13][5] = {
  1500. { 2, 12, 9, 64, 0x35 },
  1501. { 1, 13, 9, 64, 0x44 },
  1502. { 2, 12, 8, 32, 0x31 },
  1503. { 2, 11, 9, 32, 0x25 },
  1504. { 1, 12, 9, 32, 0x34 },
  1505. { 1, 13, 8, 32, 0x40 },
  1506. { 2, 11, 8, 16, 0x21 },
  1507. { 1, 12, 8, 16, 0x30 },
  1508. { 1, 11, 9, 16, 0x24 },
  1509. { 1, 11, 8, 8, 0x20 },
  1510. { 2, 9, 8, 4, 0x01 },
  1511. { 1, 10, 8, 4, 0x10 },
  1512. { 1, 9, 8, 2, 0x00 }
  1513. };
  1514. *iret = 1; /* error */
  1515. for (i = 0; i < 13; i++) {
  1516. ret |= SETIREGANDOR(SISSR, 0x13, 0x80, sdramtype[i][4]);
  1517. for (j = 2; j > 0; j--) {
  1518. ret |= sisusb_set_rank(sisusb, &i2ret, i, j,
  1519. chab, sdramtype, bw);
  1520. if (!i2ret)
  1521. continue;
  1522. ret |= sisusb_check_ranks(sisusb, &i2ret, j, i,
  1523. bw, sdramtype);
  1524. if (i2ret) {
  1525. *iret = 0; /* ram size found */
  1526. return ret;
  1527. }
  1528. }
  1529. }
  1530. return ret;
  1531. }
  1532. static int
  1533. sisusb_setup_screen(struct sisusb_usb_data *sisusb, int clrall, int drwfr)
  1534. {
  1535. int ret = 0;
  1536. u32 address;
  1537. int i, length, modex, modey, bpp;
  1538. modex = 640; modey = 480; bpp = 2;
  1539. address = sisusb->vrambase; /* Clear video ram */
  1540. if (clrall)
  1541. length = sisusb->vramsize;
  1542. else
  1543. length = modex * bpp * modey;
  1544. ret = sisusb_clear_vram(sisusb, address, length);
  1545. if (!ret && drwfr) {
  1546. for (i = 0; i < modex; i++) {
  1547. address = sisusb->vrambase + (i * bpp);
  1548. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1549. address, 0xf100);
  1550. address += (modex * (modey-1) * bpp);
  1551. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1552. address, 0xf100);
  1553. }
  1554. for (i = 0; i < modey; i++) {
  1555. address = sisusb->vrambase + ((i * modex) * bpp);
  1556. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1557. address, 0xf100);
  1558. address += ((modex - 1) * bpp);
  1559. ret |= sisusb_write_memio_word(sisusb, SISUSB_TYPE_MEM,
  1560. address, 0xf100);
  1561. }
  1562. }
  1563. return ret;
  1564. }
  1565. static int
  1566. sisusb_set_default_mode(struct sisusb_usb_data *sisusb, int touchengines)
  1567. {
  1568. int ret = 0, i, j, modex, modey, bpp, du;
  1569. u8 sr31, cr63, tmp8;
  1570. static const char attrdata[] = {
  1571. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1572. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1573. 0x01,0x00,0x00,0x00
  1574. };
  1575. static const char crtcrdata[] = {
  1576. 0x5f,0x4f,0x50,0x82,0x54,0x80,0x0b,0x3e,
  1577. 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
  1578. 0xea,0x8c,0xdf,0x28,0x40,0xe7,0x04,0xa3,
  1579. 0xff
  1580. };
  1581. static const char grcdata[] = {
  1582. 0x00,0x00,0x00,0x00,0x00,0x40,0x05,0x0f,
  1583. 0xff
  1584. };
  1585. static const char crtcdata[] = {
  1586. 0x5f,0x4f,0x4f,0x83,0x55,0x81,0x0b,0x3e,
  1587. 0xe9,0x8b,0xdf,0xe8,0x0c,0x00,0x00,0x05,
  1588. 0x00
  1589. };
  1590. modex = 640; modey = 480; bpp = 2;
  1591. GETIREG(SISSR, 0x31, &sr31);
  1592. GETIREG(SISCR, 0x63, &cr63);
  1593. SETIREGOR(SISSR, 0x01, 0x20);
  1594. SETIREG(SISCR, 0x63, cr63 & 0xbf);
  1595. SETIREGOR(SISCR, 0x17, 0x80);
  1596. SETIREGOR(SISSR, 0x1f, 0x04);
  1597. SETIREGAND(SISSR, 0x07, 0xfb);
  1598. SETIREG(SISSR, 0x00, 0x03); /* seq */
  1599. SETIREG(SISSR, 0x01, 0x21);
  1600. SETIREG(SISSR, 0x02, 0x0f);
  1601. SETIREG(SISSR, 0x03, 0x00);
  1602. SETIREG(SISSR, 0x04, 0x0e);
  1603. SETREG(SISMISCW, 0x23); /* misc */
  1604. for (i = 0; i <= 0x18; i++) { /* crtc */
  1605. SETIREG(SISCR, i, crtcrdata[i]);
  1606. }
  1607. for (i = 0; i <= 0x13; i++) { /* att */
  1608. GETREG(SISINPSTAT, &tmp8);
  1609. SETREG(SISAR, i);
  1610. SETREG(SISAR, attrdata[i]);
  1611. }
  1612. GETREG(SISINPSTAT, &tmp8);
  1613. SETREG(SISAR, 0x14);
  1614. SETREG(SISAR, 0x00);
  1615. GETREG(SISINPSTAT, &tmp8);
  1616. SETREG(SISAR, 0x20);
  1617. GETREG(SISINPSTAT, &tmp8);
  1618. for (i = 0; i <= 0x08; i++) { /* grc */
  1619. SETIREG(SISGR, i, grcdata[i]);
  1620. }
  1621. SETIREGAND(SISGR, 0x05, 0xbf);
  1622. for (i = 0x0A; i <= 0x0E; i++) { /* clr ext */
  1623. SETIREG(SISSR, i, 0x00);
  1624. }
  1625. SETIREGAND(SISSR, 0x37, 0xfe);
  1626. SETREG(SISMISCW, 0xef); /* sync */
  1627. SETIREG(SISCR, 0x11, 0x00); /* crtc */
  1628. for (j = 0x00, i = 0; i <= 7; i++, j++) {
  1629. SETIREG(SISCR, j, crtcdata[i]);
  1630. }
  1631. for (j = 0x10; i <= 10; i++, j++) {
  1632. SETIREG(SISCR, j, crtcdata[i]);
  1633. }
  1634. for (j = 0x15; i <= 12; i++, j++) {
  1635. SETIREG(SISCR, j, crtcdata[i]);
  1636. }
  1637. for (j = 0x0A; i <= 15; i++, j++) {
  1638. SETIREG(SISSR, j, crtcdata[i]);
  1639. }
  1640. SETIREG(SISSR, 0x0E, (crtcdata[16] & 0xE0));
  1641. SETIREGANDOR(SISCR, 0x09, 0x5f, ((crtcdata[16] & 0x01) << 5));
  1642. SETIREG(SISCR, 0x14, 0x4f);
  1643. du = (modex / 16) * (bpp * 2); /* offset/pitch */
  1644. if (modex % 16) du += bpp;
  1645. SETIREGANDOR(SISSR, 0x0e, 0xf0, ((du >> 8) & 0x0f));
  1646. SETIREG(SISCR, 0x13, (du & 0xff));
  1647. du <<= 5;
  1648. tmp8 = du >> 8;
  1649. if (du & 0xff) tmp8++;
  1650. SETIREG(SISSR, 0x10, tmp8);
  1651. SETIREG(SISSR, 0x31, 0x00); /* VCLK */
  1652. SETIREG(SISSR, 0x2b, 0x1b);
  1653. SETIREG(SISSR, 0x2c, 0xe1);
  1654. SETIREG(SISSR, 0x2d, 0x01);
  1655. SETIREGAND(SISSR, 0x3d, 0xfe); /* FIFO */
  1656. SETIREG(SISSR, 0x08, 0xae);
  1657. SETIREGAND(SISSR, 0x09, 0xf0);
  1658. SETIREG(SISSR, 0x08, 0x34);
  1659. SETIREGOR(SISSR, 0x3d, 0x01);
  1660. SETIREGAND(SISSR, 0x1f, 0x3f); /* mode regs */
  1661. SETIREGANDOR(SISSR, 0x06, 0xc0, 0x0a);
  1662. SETIREG(SISCR, 0x19, 0x00);
  1663. SETIREGAND(SISCR, 0x1a, 0xfc);
  1664. SETIREGAND(SISSR, 0x0f, 0xb7);
  1665. SETIREGAND(SISSR, 0x31, 0xfb);
  1666. SETIREGANDOR(SISSR, 0x21, 0x1f, 0xa0);
  1667. SETIREGAND(SISSR, 0x32, 0xf3);
  1668. SETIREGANDOR(SISSR, 0x07, 0xf8, 0x03);
  1669. SETIREG(SISCR, 0x52, 0x6c);
  1670. SETIREG(SISCR, 0x0d, 0x00); /* adjust frame */
  1671. SETIREG(SISCR, 0x0c, 0x00);
  1672. SETIREG(SISSR, 0x0d, 0x00);
  1673. SETIREGAND(SISSR, 0x37, 0xfe);
  1674. SETIREG(SISCR, 0x32, 0x20);
  1675. SETIREGAND(SISSR, 0x01, 0xdf); /* enable display */
  1676. SETIREG(SISCR, 0x63, (cr63 & 0xbf));
  1677. SETIREG(SISSR, 0x31, (sr31 & 0xfb));
  1678. if (touchengines) {
  1679. SETIREG(SISSR, 0x20, 0xa1); /* enable engines */
  1680. SETIREGOR(SISSR, 0x1e, 0x5a);
  1681. SETIREG(SISSR, 0x26, 0x01); /* disable cmdqueue */
  1682. SETIREG(SISSR, 0x27, 0x1f);
  1683. SETIREG(SISSR, 0x26, 0x00);
  1684. }
  1685. SETIREG(SISCR, 0x34, 0x44); /* we just set std mode #44 */
  1686. return ret;
  1687. }
  1688. static int
  1689. sisusb_init_gfxcore(struct sisusb_usb_data *sisusb)
  1690. {
  1691. int ret = 0, i, j, bw, chab, iret, retry = 3;
  1692. u8 tmp8, ramtype;
  1693. u32 tmp32;
  1694. static const char mclktable[] = {
  1695. 0x3b, 0x22, 0x01, 143,
  1696. 0x3b, 0x22, 0x01, 143,
  1697. 0x3b, 0x22, 0x01, 143,
  1698. 0x3b, 0x22, 0x01, 143
  1699. };
  1700. static const char eclktable[] = {
  1701. 0x3b, 0x22, 0x01, 143,
  1702. 0x3b, 0x22, 0x01, 143,
  1703. 0x3b, 0x22, 0x01, 143,
  1704. 0x3b, 0x22, 0x01, 143
  1705. };
  1706. static const char ramtypetable1[] = {
  1707. 0x00, 0x04, 0x60, 0x60,
  1708. 0x0f, 0x0f, 0x1f, 0x1f,
  1709. 0xba, 0xba, 0xba, 0xba,
  1710. 0xa9, 0xa9, 0xac, 0xac,
  1711. 0xa0, 0xa0, 0xa0, 0xa8,
  1712. 0x00, 0x00, 0x02, 0x02,
  1713. 0x30, 0x30, 0x40, 0x40
  1714. };
  1715. static const char ramtypetable2[] = {
  1716. 0x77, 0x77, 0x44, 0x44,
  1717. 0x77, 0x77, 0x44, 0x44,
  1718. 0x00, 0x00, 0x00, 0x00,
  1719. 0x5b, 0x5b, 0xab, 0xab,
  1720. 0x00, 0x00, 0xf0, 0xf8
  1721. };
  1722. while (retry--) {
  1723. /* Enable VGA */
  1724. ret = GETREG(SISVGAEN, &tmp8);
  1725. ret |= SETREG(SISVGAEN, (tmp8 | 0x01));
  1726. /* Enable GPU access to VRAM */
  1727. ret |= GETREG(SISMISCR, &tmp8);
  1728. ret |= SETREG(SISMISCW, (tmp8 | 0x01));
  1729. if (ret) continue;
  1730. /* Reset registers */
  1731. ret |= SETIREGAND(SISCR, 0x5b, 0xdf);
  1732. ret |= SETIREG(SISSR, 0x05, 0x86);
  1733. ret |= SETIREGOR(SISSR, 0x20, 0x01);
  1734. ret |= SETREG(SISMISCW, 0x67);
  1735. for (i = 0x06; i <= 0x1f; i++) {
  1736. ret |= SETIREG(SISSR, i, 0x00);
  1737. }
  1738. for (i = 0x21; i <= 0x27; i++) {
  1739. ret |= SETIREG(SISSR, i, 0x00);
  1740. }
  1741. for (i = 0x31; i <= 0x3d; i++) {
  1742. ret |= SETIREG(SISSR, i, 0x00);
  1743. }
  1744. for (i = 0x12; i <= 0x1b; i++) {
  1745. ret |= SETIREG(SISSR, i, 0x00);
  1746. }
  1747. for (i = 0x79; i <= 0x7c; i++) {
  1748. ret |= SETIREG(SISCR, i, 0x00);
  1749. }
  1750. if (ret) continue;
  1751. ret |= SETIREG(SISCR, 0x63, 0x80);
  1752. ret |= GETIREG(SISSR, 0x3a, &ramtype);
  1753. ramtype &= 0x03;
  1754. ret |= SETIREG(SISSR, 0x28, mclktable[ramtype * 4]);
  1755. ret |= SETIREG(SISSR, 0x29, mclktable[(ramtype * 4) + 1]);
  1756. ret |= SETIREG(SISSR, 0x2a, mclktable[(ramtype * 4) + 2]);
  1757. ret |= SETIREG(SISSR, 0x2e, eclktable[ramtype * 4]);
  1758. ret |= SETIREG(SISSR, 0x2f, eclktable[(ramtype * 4) + 1]);
  1759. ret |= SETIREG(SISSR, 0x30, eclktable[(ramtype * 4) + 2]);
  1760. ret |= SETIREG(SISSR, 0x07, 0x18);
  1761. ret |= SETIREG(SISSR, 0x11, 0x0f);
  1762. if (ret) continue;
  1763. for (i = 0x15, j = 0; i <= 0x1b; i++, j++) {
  1764. ret |= SETIREG(SISSR, i, ramtypetable1[(j*4) + ramtype]);
  1765. }
  1766. for (i = 0x40, j = 0; i <= 0x44; i++, j++) {
  1767. ret |= SETIREG(SISCR, i, ramtypetable2[(j*4) + ramtype]);
  1768. }
  1769. ret |= SETIREG(SISCR, 0x49, 0xaa);
  1770. ret |= SETIREG(SISSR, 0x1f, 0x00);
  1771. ret |= SETIREG(SISSR, 0x20, 0xa0);
  1772. ret |= SETIREG(SISSR, 0x23, 0xf6);
  1773. ret |= SETIREG(SISSR, 0x24, 0x0d);
  1774. ret |= SETIREG(SISSR, 0x25, 0x33);
  1775. ret |= SETIREG(SISSR, 0x11, 0x0f);
  1776. ret |= SETIREGOR(SISPART1, 0x2f, 0x01);
  1777. ret |= SETIREGAND(SISCAP, 0x3f, 0xef);
  1778. if (ret) continue;
  1779. ret |= SETIREG(SISPART1, 0x00, 0x00);
  1780. ret |= GETIREG(SISSR, 0x13, &tmp8);
  1781. tmp8 >>= 4;
  1782. ret |= SETIREG(SISPART1, 0x02, 0x00);
  1783. ret |= SETIREG(SISPART1, 0x2e, 0x08);
  1784. ret |= sisusb_read_pci_config(sisusb, 0x50, &tmp32);
  1785. tmp32 &= 0x00f00000;
  1786. tmp8 = (tmp32 == 0x100000) ? 0x33 : 0x03;
  1787. ret |= SETIREG(SISSR, 0x25, tmp8);
  1788. tmp8 = (tmp32 == 0x100000) ? 0xaa : 0x88;
  1789. ret |= SETIREG(SISCR, 0x49, tmp8);
  1790. ret |= SETIREG(SISSR, 0x27, 0x1f);
  1791. ret |= SETIREG(SISSR, 0x31, 0x00);
  1792. ret |= SETIREG(SISSR, 0x32, 0x11);
  1793. ret |= SETIREG(SISSR, 0x33, 0x00);
  1794. if (ret) continue;
  1795. ret |= SETIREG(SISCR, 0x83, 0x00);
  1796. ret |= sisusb_set_default_mode(sisusb, 0);
  1797. ret |= SETIREGAND(SISSR, 0x21, 0xdf);
  1798. ret |= SETIREGOR(SISSR, 0x01, 0x20);
  1799. ret |= SETIREGOR(SISSR, 0x16, 0x0f);
  1800. ret |= sisusb_triggersr16(sisusb, ramtype);
  1801. /* Disable refresh */
  1802. ret |= SETIREGAND(SISSR, 0x17, 0xf8);
  1803. ret |= SETIREGOR(SISSR, 0x19, 0x03);
  1804. ret |= sisusb_getbuswidth(sisusb, &bw, &chab);
  1805. ret |= sisusb_verify_mclk(sisusb);
  1806. if (ramtype <= 1) {
  1807. ret |= sisusb_get_sdram_size(sisusb, &iret, bw, chab);
  1808. if (iret) {
  1809. printk(KERN_ERR "sisusbvga[%d]: RAM size "
  1810. "detection failed, "
  1811. "assuming 8MB video RAM\n",
  1812. sisusb->minor);
  1813. ret |= SETIREG(SISSR,0x14,0x31);
  1814. /* TODO */
  1815. }
  1816. } else {
  1817. printk(KERN_ERR "sisusbvga[%d]: DDR RAM device found, "
  1818. "assuming 8MB video RAM\n",
  1819. sisusb->minor);
  1820. ret |= SETIREG(SISSR,0x14,0x31);
  1821. /* *** TODO *** */
  1822. }
  1823. /* Enable refresh */
  1824. ret |= SETIREG(SISSR, 0x16, ramtypetable1[4 + ramtype]);
  1825. ret |= SETIREG(SISSR, 0x17, ramtypetable1[8 + ramtype]);
  1826. ret |= SETIREG(SISSR, 0x19, ramtypetable1[16 + ramtype]);
  1827. ret |= SETIREGOR(SISSR, 0x21, 0x20);
  1828. ret |= SETIREG(SISSR, 0x22, 0xfb);
  1829. ret |= SETIREG(SISSR, 0x21, 0xa5);
  1830. if (ret == 0)
  1831. break;
  1832. }
  1833. return ret;
  1834. }
  1835. #undef SETREG
  1836. #undef GETREG
  1837. #undef SETIREG
  1838. #undef GETIREG
  1839. #undef SETIREGOR
  1840. #undef SETIREGAND
  1841. #undef SETIREGANDOR
  1842. #undef READL
  1843. #undef WRITEL
  1844. static void
  1845. sisusb_get_ramconfig(struct sisusb_usb_data *sisusb)
  1846. {
  1847. u8 tmp8, tmp82, ramtype;
  1848. int bw = 0;
  1849. char *ramtypetext1 = NULL;
  1850. const char *ramtypetext2[] = { "SDR SDRAM", "SDR SGRAM",
  1851. "DDR SDRAM", "DDR SGRAM" };
  1852. static const int busSDR[4] = {64, 64, 128, 128};
  1853. static const int busDDR[4] = {32, 32, 64, 64};
  1854. static const int busDDRA[4] = {64+32, 64+32 , (64+32)*2, (64+32)*2};
  1855. sisusb_getidxreg(sisusb, SISSR, 0x14, &tmp8);
  1856. sisusb_getidxreg(sisusb, SISSR, 0x15, &tmp82);
  1857. sisusb_getidxreg(sisusb, SISSR, 0x3a, &ramtype);
  1858. sisusb->vramsize = (1 << ((tmp8 & 0xf0) >> 4)) * 1024 * 1024;
  1859. ramtype &= 0x03;
  1860. switch ((tmp8 >> 2) & 0x03) {
  1861. case 0: ramtypetext1 = "1 ch/1 r";
  1862. if (tmp82 & 0x10) {
  1863. bw = 32;
  1864. } else {
  1865. bw = busSDR[(tmp8 & 0x03)];
  1866. }
  1867. break;
  1868. case 1: ramtypetext1 = "1 ch/2 r";
  1869. sisusb->vramsize <<= 1;
  1870. bw = busSDR[(tmp8 & 0x03)];
  1871. break;
  1872. case 2: ramtypetext1 = "asymmeric";
  1873. sisusb->vramsize += sisusb->vramsize/2;
  1874. bw = busDDRA[(tmp8 & 0x03)];
  1875. break;
  1876. case 3: ramtypetext1 = "2 channel";
  1877. sisusb->vramsize <<= 1;
  1878. bw = busDDR[(tmp8 & 0x03)];
  1879. break;
  1880. }
  1881. printk(KERN_INFO "sisusbvga[%d]: %dMB %s %s, bus width %d\n",
  1882. sisusb->minor, (sisusb->vramsize >> 20), ramtypetext1,
  1883. ramtypetext2[ramtype], bw);
  1884. }
  1885. static int
  1886. sisusb_do_init_gfxdevice(struct sisusb_usb_data *sisusb)
  1887. {
  1888. struct sisusb_packet packet;
  1889. int ret;
  1890. u32 tmp32;
  1891. /* Do some magic */
  1892. packet.header = 0x001f;
  1893. packet.address = 0x00000324;
  1894. packet.data = 0x00000004;
  1895. ret = sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1896. packet.header = 0x001f;
  1897. packet.address = 0x00000364;
  1898. packet.data = 0x00000004;
  1899. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1900. packet.header = 0x001f;
  1901. packet.address = 0x00000384;
  1902. packet.data = 0x00000004;
  1903. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1904. packet.header = 0x001f;
  1905. packet.address = 0x00000100;
  1906. packet.data = 0x00000700;
  1907. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1908. packet.header = 0x000f;
  1909. packet.address = 0x00000004;
  1910. ret |= sisusb_send_bridge_packet(sisusb, 6, &packet, 0);
  1911. packet.data |= 0x17;
  1912. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1913. /* Init BAR 0 (VRAM) */
  1914. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1915. ret |= sisusb_write_pci_config(sisusb, 0x10, 0xfffffff0);
  1916. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1917. tmp32 &= 0x0f;
  1918. tmp32 |= SISUSB_PCI_MEMBASE;
  1919. ret |= sisusb_write_pci_config(sisusb, 0x10, tmp32);
  1920. /* Init BAR 1 (MMIO) */
  1921. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1922. ret |= sisusb_write_pci_config(sisusb, 0x14, 0xfffffff0);
  1923. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1924. tmp32 &= 0x0f;
  1925. tmp32 |= SISUSB_PCI_MMIOBASE;
  1926. ret |= sisusb_write_pci_config(sisusb, 0x14, tmp32);
  1927. /* Init BAR 2 (i/o ports) */
  1928. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1929. ret |= sisusb_write_pci_config(sisusb, 0x18, 0xfffffff0);
  1930. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1931. tmp32 &= 0x0f;
  1932. tmp32 |= SISUSB_PCI_IOPORTBASE;
  1933. ret |= sisusb_write_pci_config(sisusb, 0x18, tmp32);
  1934. /* Enable memory and i/o access */
  1935. ret |= sisusb_read_pci_config(sisusb, 0x04, &tmp32);
  1936. tmp32 |= 0x3;
  1937. ret |= sisusb_write_pci_config(sisusb, 0x04, tmp32);
  1938. if (ret == 0) {
  1939. /* Some further magic */
  1940. packet.header = 0x001f;
  1941. packet.address = 0x00000050;
  1942. packet.data = 0x000000ff;
  1943. ret |= sisusb_send_bridge_packet(sisusb, 10, &packet, 0);
  1944. }
  1945. return ret;
  1946. }
  1947. /* Initialize the graphics device (return 0 on success)
  1948. * This initializes the net2280 as well as the PCI registers
  1949. * of the graphics board.
  1950. */
  1951. static int
  1952. sisusb_init_gfxdevice(struct sisusb_usb_data *sisusb, int initscreen)
  1953. {
  1954. int ret = 0, test = 0;
  1955. u32 tmp32;
  1956. if (sisusb->devinit == 1) {
  1957. /* Read PCI BARs and see if they have been set up */
  1958. ret |= sisusb_read_pci_config(sisusb, 0x10, &tmp32);
  1959. if (ret) return ret;
  1960. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_MEMBASE) test++;
  1961. ret |= sisusb_read_pci_config(sisusb, 0x14, &tmp32);
  1962. if (ret) return ret;
  1963. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_MMIOBASE) test++;
  1964. ret |= sisusb_read_pci_config(sisusb, 0x18, &tmp32);
  1965. if (ret) return ret;
  1966. if ((tmp32 & 0xfffffff0) == SISUSB_PCI_IOPORTBASE) test++;
  1967. }
  1968. /* No? So reset the device */
  1969. if ((sisusb->devinit == 0) || (test != 3)) {
  1970. ret |= sisusb_do_init_gfxdevice(sisusb);
  1971. if (ret == 0)
  1972. sisusb->devinit = 1;
  1973. }
  1974. if (sisusb->devinit) {
  1975. /* Initialize the graphics core */
  1976. if (sisusb_init_gfxcore(sisusb) == 0) {
  1977. sisusb->gfxinit = 1;
  1978. sisusb_get_ramconfig(sisusb);
  1979. ret |= sisusb_set_default_mode(sisusb, 1);
  1980. ret |= sisusb_setup_screen(sisusb, 1, initscreen);
  1981. }
  1982. }
  1983. return ret;
  1984. }
  1985. #ifdef INCL_SISUSB_CON
  1986. /* Set up default text mode:
  1987. - Set text mode (0x03)
  1988. - Upload default font
  1989. - Upload user font (if available)
  1990. */
  1991. int
  1992. sisusb_reset_text_mode(struct sisusb_usb_data *sisusb, int init)
  1993. {
  1994. int ret = 0, slot = sisusb->font_slot, i;
  1995. const struct font_desc *myfont;
  1996. u8 *tempbuf;
  1997. u16 *tempbufb;
  1998. size_t written;
  1999. static const char bootstring[] = "SiSUSB VGA text console, (C) 2005 Thomas Winischhofer.";
  2000. static const char bootlogo[] = "(o_ //\\ V_/_";
  2001. /* sisusb->lock is down */
  2002. if (!sisusb->SiS_Pr)
  2003. return 1;
  2004. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2005. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2006. /* Set mode 0x03 */
  2007. SiSUSBSetMode(sisusb->SiS_Pr, 0x03);
  2008. if (!(myfont = find_font("VGA8x16")))
  2009. return 1;
  2010. if (!(tempbuf = vmalloc(8192)))
  2011. return 1;
  2012. for (i = 0; i < 256; i++)
  2013. memcpy(tempbuf + (i * 32), myfont->data + (i * 16), 16);
  2014. /* Upload default font */
  2015. ret = sisusbcon_do_font_op(sisusb, 1, 0, tempbuf, 8192, 0, 1, NULL, 16, 0);
  2016. vfree(tempbuf);
  2017. /* Upload user font (and reset current slot) */
  2018. if (sisusb->font_backup) {
  2019. ret |= sisusbcon_do_font_op(sisusb, 1, 2, sisusb->font_backup,
  2020. 8192, sisusb->font_backup_512, 1, NULL,
  2021. sisusb->font_backup_height, 0);
  2022. if (slot != 2)
  2023. sisusbcon_do_font_op(sisusb, 1, 0, NULL, 0, 0, 1,
  2024. NULL, 16, 0);
  2025. }
  2026. if (init && !sisusb->scrbuf) {
  2027. if ((tempbuf = vmalloc(8192))) {
  2028. i = 4096;
  2029. tempbufb = (u16 *)tempbuf;
  2030. while (i--)
  2031. *(tempbufb++) = 0x0720;
  2032. i = 0;
  2033. tempbufb = (u16 *)tempbuf;
  2034. while (bootlogo[i]) {
  2035. *(tempbufb++) = 0x0700 | bootlogo[i++];
  2036. if (!(i % 4))
  2037. tempbufb += 76;
  2038. }
  2039. i = 0;
  2040. tempbufb = (u16 *)tempbuf + 6;
  2041. while (bootstring[i])
  2042. *(tempbufb++) = 0x0700 | bootstring[i++];
  2043. ret |= sisusb_copy_memory(sisusb, tempbuf,
  2044. sisusb->vrambase, 8192, &written);
  2045. vfree(tempbuf);
  2046. }
  2047. } else if (sisusb->scrbuf) {
  2048. ret |= sisusb_copy_memory(sisusb, (char *)sisusb->scrbuf,
  2049. sisusb->vrambase, sisusb->scrbuf_size, &written);
  2050. }
  2051. if (sisusb->sisusb_cursor_size_from >= 0 &&
  2052. sisusb->sisusb_cursor_size_to >= 0) {
  2053. sisusb_setidxreg(sisusb, SISCR, 0x0a,
  2054. sisusb->sisusb_cursor_size_from);
  2055. sisusb_setidxregandor(sisusb, SISCR, 0x0b, 0xe0,
  2056. sisusb->sisusb_cursor_size_to);
  2057. } else {
  2058. sisusb_setidxreg(sisusb, SISCR, 0x0a, 0x2d);
  2059. sisusb_setidxreg(sisusb, SISCR, 0x0b, 0x0e);
  2060. sisusb->sisusb_cursor_size_to = -1;
  2061. }
  2062. slot = sisusb->sisusb_cursor_loc;
  2063. if(slot < 0) slot = 0;
  2064. sisusb->sisusb_cursor_loc = -1;
  2065. sisusb->bad_cursor_pos = 1;
  2066. sisusb_set_cursor(sisusb, slot);
  2067. sisusb_setidxreg(sisusb, SISCR, 0x0c, (sisusb->cur_start_addr >> 8));
  2068. sisusb_setidxreg(sisusb, SISCR, 0x0d, (sisusb->cur_start_addr & 0xff));
  2069. sisusb->textmodedestroyed = 0;
  2070. /* sisusb->lock is down */
  2071. return ret;
  2072. }
  2073. #endif
  2074. /* fops */
  2075. static int
  2076. sisusb_open(struct inode *inode, struct file *file)
  2077. {
  2078. struct sisusb_usb_data *sisusb;
  2079. struct usb_interface *interface;
  2080. int subminor = iminor(inode);
  2081. if (!(interface = usb_find_interface(&sisusb_driver, subminor))) {
  2082. printk(KERN_ERR "sisusb[%d]: Failed to find interface\n",
  2083. subminor);
  2084. return -ENODEV;
  2085. }
  2086. if (!(sisusb = usb_get_intfdata(interface)))
  2087. return -ENODEV;
  2088. mutex_lock(&sisusb->lock);
  2089. if (!sisusb->present || !sisusb->ready) {
  2090. mutex_unlock(&sisusb->lock);
  2091. return -ENODEV;
  2092. }
  2093. if (sisusb->isopen) {
  2094. mutex_unlock(&sisusb->lock);
  2095. return -EBUSY;
  2096. }
  2097. if (!sisusb->devinit) {
  2098. if (sisusb->sisusb_dev->speed == USB_SPEED_HIGH) {
  2099. if (sisusb_init_gfxdevice(sisusb, 0)) {
  2100. mutex_unlock(&sisusb->lock);
  2101. printk(KERN_ERR
  2102. "sisusbvga[%d]: Failed to initialize "
  2103. "device\n",
  2104. sisusb->minor);
  2105. return -EIO;
  2106. }
  2107. } else {
  2108. mutex_unlock(&sisusb->lock);
  2109. printk(KERN_ERR
  2110. "sisusbvga[%d]: Device not attached to "
  2111. "USB 2.0 hub\n",
  2112. sisusb->minor);
  2113. return -EIO;
  2114. }
  2115. }
  2116. /* Increment usage count for our sisusb */
  2117. kref_get(&sisusb->kref);
  2118. sisusb->isopen = 1;
  2119. file->private_data = sisusb;
  2120. mutex_unlock(&sisusb->lock);
  2121. return 0;
  2122. }
  2123. void
  2124. sisusb_delete(struct kref *kref)
  2125. {
  2126. struct sisusb_usb_data *sisusb = to_sisusb_dev(kref);
  2127. if (!sisusb)
  2128. return;
  2129. if (sisusb->sisusb_dev)
  2130. usb_put_dev(sisusb->sisusb_dev);
  2131. sisusb->sisusb_dev = NULL;
  2132. sisusb_free_buffers(sisusb);
  2133. sisusb_free_urbs(sisusb);
  2134. #ifdef INCL_SISUSB_CON
  2135. kfree(sisusb->SiS_Pr);
  2136. #endif
  2137. kfree(sisusb);
  2138. }
  2139. static int
  2140. sisusb_release(struct inode *inode, struct file *file)
  2141. {
  2142. struct sisusb_usb_data *sisusb;
  2143. int myminor;
  2144. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2145. return -ENODEV;
  2146. mutex_lock(&sisusb->lock);
  2147. if (sisusb->present) {
  2148. /* Wait for all URBs to finish if device still present */
  2149. if (!sisusb_wait_all_out_complete(sisusb))
  2150. sisusb_kill_all_busy(sisusb);
  2151. }
  2152. myminor = sisusb->minor;
  2153. sisusb->isopen = 0;
  2154. file->private_data = NULL;
  2155. mutex_unlock(&sisusb->lock);
  2156. /* decrement the usage count on our device */
  2157. kref_put(&sisusb->kref, sisusb_delete);
  2158. return 0;
  2159. }
  2160. static ssize_t
  2161. sisusb_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  2162. {
  2163. struct sisusb_usb_data *sisusb;
  2164. ssize_t bytes_read = 0;
  2165. int errno = 0;
  2166. u8 buf8;
  2167. u16 buf16;
  2168. u32 buf32, address;
  2169. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2170. return -ENODEV;
  2171. mutex_lock(&sisusb->lock);
  2172. /* Sanity check */
  2173. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2174. mutex_unlock(&sisusb->lock);
  2175. return -ENODEV;
  2176. }
  2177. if ((*ppos) >= SISUSB_PCI_PSEUDO_IOPORTBASE &&
  2178. (*ppos) < SISUSB_PCI_PSEUDO_IOPORTBASE + 128) {
  2179. address = (*ppos) -
  2180. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2181. SISUSB_PCI_IOPORTBASE;
  2182. /* Read i/o ports
  2183. * Byte, word and long(32) can be read. As this
  2184. * emulates inX instructions, the data returned is
  2185. * in machine-endianness.
  2186. */
  2187. switch (count) {
  2188. case 1:
  2189. if (sisusb_read_memio_byte(sisusb,
  2190. SISUSB_TYPE_IO,
  2191. address, &buf8))
  2192. errno = -EIO;
  2193. else if (put_user(buf8, (u8 __user *)buffer))
  2194. errno = -EFAULT;
  2195. else
  2196. bytes_read = 1;
  2197. break;
  2198. case 2:
  2199. if (sisusb_read_memio_word(sisusb,
  2200. SISUSB_TYPE_IO,
  2201. address, &buf16))
  2202. errno = -EIO;
  2203. else if (put_user(buf16, (u16 __user *)buffer))
  2204. errno = -EFAULT;
  2205. else
  2206. bytes_read = 2;
  2207. break;
  2208. case 4:
  2209. if (sisusb_read_memio_long(sisusb,
  2210. SISUSB_TYPE_IO,
  2211. address, &buf32))
  2212. errno = -EIO;
  2213. else if (put_user(buf32, (u32 __user *)buffer))
  2214. errno = -EFAULT;
  2215. else
  2216. bytes_read = 4;
  2217. break;
  2218. default:
  2219. errno = -EIO;
  2220. }
  2221. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MEMBASE &&
  2222. (*ppos) < SISUSB_PCI_PSEUDO_MEMBASE + sisusb->vramsize) {
  2223. address = (*ppos) -
  2224. SISUSB_PCI_PSEUDO_MEMBASE +
  2225. SISUSB_PCI_MEMBASE;
  2226. /* Read video ram
  2227. * Remember: Data delivered is never endian-corrected
  2228. */
  2229. errno = sisusb_read_mem_bulk(sisusb, address,
  2230. NULL, count, buffer, &bytes_read);
  2231. if (bytes_read)
  2232. errno = bytes_read;
  2233. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MMIOBASE &&
  2234. (*ppos) < SISUSB_PCI_PSEUDO_MMIOBASE + SISUSB_PCI_MMIOSIZE) {
  2235. address = (*ppos) -
  2236. SISUSB_PCI_PSEUDO_MMIOBASE +
  2237. SISUSB_PCI_MMIOBASE;
  2238. /* Read MMIO
  2239. * Remember: Data delivered is never endian-corrected
  2240. */
  2241. errno = sisusb_read_mem_bulk(sisusb, address,
  2242. NULL, count, buffer, &bytes_read);
  2243. if (bytes_read)
  2244. errno = bytes_read;
  2245. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_PCIBASE &&
  2246. (*ppos) <= SISUSB_PCI_PSEUDO_PCIBASE + 0x5c) {
  2247. if (count != 4) {
  2248. mutex_unlock(&sisusb->lock);
  2249. return -EINVAL;
  2250. }
  2251. address = (*ppos) - SISUSB_PCI_PSEUDO_PCIBASE;
  2252. /* Read PCI config register
  2253. * Return value delivered in machine endianness.
  2254. */
  2255. if (sisusb_read_pci_config(sisusb, address, &buf32))
  2256. errno = -EIO;
  2257. else if (put_user(buf32, (u32 __user *)buffer))
  2258. errno = -EFAULT;
  2259. else
  2260. bytes_read = 4;
  2261. } else {
  2262. errno = -EBADFD;
  2263. }
  2264. (*ppos) += bytes_read;
  2265. mutex_unlock(&sisusb->lock);
  2266. return errno ? errno : bytes_read;
  2267. }
  2268. static ssize_t
  2269. sisusb_write(struct file *file, const char __user *buffer, size_t count,
  2270. loff_t *ppos)
  2271. {
  2272. struct sisusb_usb_data *sisusb;
  2273. int errno = 0;
  2274. ssize_t bytes_written = 0;
  2275. u8 buf8;
  2276. u16 buf16;
  2277. u32 buf32, address;
  2278. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2279. return -ENODEV;
  2280. mutex_lock(&sisusb->lock);
  2281. /* Sanity check */
  2282. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2283. mutex_unlock(&sisusb->lock);
  2284. return -ENODEV;
  2285. }
  2286. if ((*ppos) >= SISUSB_PCI_PSEUDO_IOPORTBASE &&
  2287. (*ppos) < SISUSB_PCI_PSEUDO_IOPORTBASE + 128) {
  2288. address = (*ppos) -
  2289. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2290. SISUSB_PCI_IOPORTBASE;
  2291. /* Write i/o ports
  2292. * Byte, word and long(32) can be written. As this
  2293. * emulates outX instructions, the data is expected
  2294. * in machine-endianness.
  2295. */
  2296. switch (count) {
  2297. case 1:
  2298. if (get_user(buf8, (u8 __user *)buffer))
  2299. errno = -EFAULT;
  2300. else if (sisusb_write_memio_byte(sisusb,
  2301. SISUSB_TYPE_IO,
  2302. address, buf8))
  2303. errno = -EIO;
  2304. else
  2305. bytes_written = 1;
  2306. break;
  2307. case 2:
  2308. if (get_user(buf16, (u16 __user *)buffer))
  2309. errno = -EFAULT;
  2310. else if (sisusb_write_memio_word(sisusb,
  2311. SISUSB_TYPE_IO,
  2312. address, buf16))
  2313. errno = -EIO;
  2314. else
  2315. bytes_written = 2;
  2316. break;
  2317. case 4:
  2318. if (get_user(buf32, (u32 __user *)buffer))
  2319. errno = -EFAULT;
  2320. else if (sisusb_write_memio_long(sisusb,
  2321. SISUSB_TYPE_IO,
  2322. address, buf32))
  2323. errno = -EIO;
  2324. else
  2325. bytes_written = 4;
  2326. break;
  2327. default:
  2328. errno = -EIO;
  2329. }
  2330. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MEMBASE &&
  2331. (*ppos) < SISUSB_PCI_PSEUDO_MEMBASE + sisusb->vramsize) {
  2332. address = (*ppos) -
  2333. SISUSB_PCI_PSEUDO_MEMBASE +
  2334. SISUSB_PCI_MEMBASE;
  2335. /* Write video ram.
  2336. * Buffer is copied 1:1, therefore, on big-endian
  2337. * machines, the data must be swapped by userland
  2338. * in advance (if applicable; no swapping in 8bpp
  2339. * mode or if YUV data is being transferred).
  2340. */
  2341. errno = sisusb_write_mem_bulk(sisusb, address, NULL,
  2342. count, buffer, 0, &bytes_written);
  2343. if (bytes_written)
  2344. errno = bytes_written;
  2345. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_MMIOBASE &&
  2346. (*ppos) < SISUSB_PCI_PSEUDO_MMIOBASE + SISUSB_PCI_MMIOSIZE) {
  2347. address = (*ppos) -
  2348. SISUSB_PCI_PSEUDO_MMIOBASE +
  2349. SISUSB_PCI_MMIOBASE;
  2350. /* Write MMIO.
  2351. * Buffer is copied 1:1, therefore, on big-endian
  2352. * machines, the data must be swapped by userland
  2353. * in advance.
  2354. */
  2355. errno = sisusb_write_mem_bulk(sisusb, address, NULL,
  2356. count, buffer, 0, &bytes_written);
  2357. if (bytes_written)
  2358. errno = bytes_written;
  2359. } else if ((*ppos) >= SISUSB_PCI_PSEUDO_PCIBASE &&
  2360. (*ppos) <= SISUSB_PCI_PSEUDO_PCIBASE + SISUSB_PCI_PCONFSIZE) {
  2361. if (count != 4) {
  2362. mutex_unlock(&sisusb->lock);
  2363. return -EINVAL;
  2364. }
  2365. address = (*ppos) - SISUSB_PCI_PSEUDO_PCIBASE;
  2366. /* Write PCI config register.
  2367. * Given value expected in machine endianness.
  2368. */
  2369. if (get_user(buf32, (u32 __user *)buffer))
  2370. errno = -EFAULT;
  2371. else if (sisusb_write_pci_config(sisusb, address, buf32))
  2372. errno = -EIO;
  2373. else
  2374. bytes_written = 4;
  2375. } else {
  2376. /* Error */
  2377. errno = -EBADFD;
  2378. }
  2379. (*ppos) += bytes_written;
  2380. mutex_unlock(&sisusb->lock);
  2381. return errno ? errno : bytes_written;
  2382. }
  2383. static loff_t
  2384. sisusb_lseek(struct file *file, loff_t offset, int orig)
  2385. {
  2386. struct sisusb_usb_data *sisusb;
  2387. loff_t ret;
  2388. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2389. return -ENODEV;
  2390. mutex_lock(&sisusb->lock);
  2391. /* Sanity check */
  2392. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2393. mutex_unlock(&sisusb->lock);
  2394. return -ENODEV;
  2395. }
  2396. switch (orig) {
  2397. case 0:
  2398. file->f_pos = offset;
  2399. ret = file->f_pos;
  2400. /* never negative, no force_successful_syscall needed */
  2401. break;
  2402. case 1:
  2403. file->f_pos += offset;
  2404. ret = file->f_pos;
  2405. /* never negative, no force_successful_syscall needed */
  2406. break;
  2407. default:
  2408. /* seeking relative to "end of file" is not supported */
  2409. ret = -EINVAL;
  2410. }
  2411. mutex_unlock(&sisusb->lock);
  2412. return ret;
  2413. }
  2414. static int
  2415. sisusb_handle_command(struct sisusb_usb_data *sisusb, struct sisusb_command *y,
  2416. unsigned long arg)
  2417. {
  2418. int retval, port, length;
  2419. u32 address;
  2420. /* All our commands require the device
  2421. * to be initialized.
  2422. */
  2423. if (!sisusb->devinit)
  2424. return -ENODEV;
  2425. port = y->data3 -
  2426. SISUSB_PCI_PSEUDO_IOPORTBASE +
  2427. SISUSB_PCI_IOPORTBASE;
  2428. switch (y->operation) {
  2429. case SUCMD_GET:
  2430. retval = sisusb_getidxreg(sisusb, port,
  2431. y->data0, &y->data1);
  2432. if (!retval) {
  2433. if (copy_to_user((void __user *)arg, y,
  2434. sizeof(*y)))
  2435. retval = -EFAULT;
  2436. }
  2437. break;
  2438. case SUCMD_SET:
  2439. retval = sisusb_setidxreg(sisusb, port,
  2440. y->data0, y->data1);
  2441. break;
  2442. case SUCMD_SETOR:
  2443. retval = sisusb_setidxregor(sisusb, port,
  2444. y->data0, y->data1);
  2445. break;
  2446. case SUCMD_SETAND:
  2447. retval = sisusb_setidxregand(sisusb, port,
  2448. y->data0, y->data1);
  2449. break;
  2450. case SUCMD_SETANDOR:
  2451. retval = sisusb_setidxregandor(sisusb, port,
  2452. y->data0, y->data1, y->data2);
  2453. break;
  2454. case SUCMD_SETMASK:
  2455. retval = sisusb_setidxregmask(sisusb, port,
  2456. y->data0, y->data1, y->data2);
  2457. break;
  2458. case SUCMD_CLRSCR:
  2459. /* Gfx core must be initialized */
  2460. if (!sisusb->gfxinit)
  2461. return -ENODEV;
  2462. length = (y->data0 << 16) | (y->data1 << 8) | y->data2;
  2463. address = y->data3 -
  2464. SISUSB_PCI_PSEUDO_MEMBASE +
  2465. SISUSB_PCI_MEMBASE;
  2466. retval = sisusb_clear_vram(sisusb, address, length);
  2467. break;
  2468. case SUCMD_HANDLETEXTMODE:
  2469. retval = 0;
  2470. #ifdef INCL_SISUSB_CON
  2471. /* Gfx core must be initialized, SiS_Pr must exist */
  2472. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2473. return -ENODEV;
  2474. switch (y->data0) {
  2475. case 0:
  2476. retval = sisusb_reset_text_mode(sisusb, 0);
  2477. break;
  2478. case 1:
  2479. sisusb->textmodedestroyed = 1;
  2480. break;
  2481. }
  2482. #endif
  2483. break;
  2484. #ifdef INCL_SISUSB_CON
  2485. case SUCMD_SETMODE:
  2486. /* Gfx core must be initialized, SiS_Pr must exist */
  2487. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2488. return -ENODEV;
  2489. retval = 0;
  2490. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2491. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2492. if (SiSUSBSetMode(sisusb->SiS_Pr, y->data3))
  2493. retval = -EINVAL;
  2494. break;
  2495. case SUCMD_SETVESAMODE:
  2496. /* Gfx core must be initialized, SiS_Pr must exist */
  2497. if (!sisusb->gfxinit || !sisusb->SiS_Pr)
  2498. return -ENODEV;
  2499. retval = 0;
  2500. sisusb->SiS_Pr->IOAddress = SISUSB_PCI_IOPORTBASE + 0x30;
  2501. sisusb->SiS_Pr->sisusb = (void *)sisusb;
  2502. if (SiSUSBSetVESAMode(sisusb->SiS_Pr, y->data3))
  2503. retval = -EINVAL;
  2504. break;
  2505. #endif
  2506. default:
  2507. retval = -EINVAL;
  2508. }
  2509. if (retval > 0)
  2510. retval = -EIO;
  2511. return retval;
  2512. }
  2513. static int
  2514. sisusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  2515. unsigned long arg)
  2516. {
  2517. struct sisusb_usb_data *sisusb;
  2518. struct sisusb_info x;
  2519. struct sisusb_command y;
  2520. int retval = 0;
  2521. u32 __user *argp = (u32 __user *)arg;
  2522. if (!(sisusb = (struct sisusb_usb_data *)file->private_data))
  2523. return -ENODEV;
  2524. mutex_lock(&sisusb->lock);
  2525. /* Sanity check */
  2526. if (!sisusb->present || !sisusb->ready || !sisusb->sisusb_dev) {
  2527. retval = -ENODEV;
  2528. goto err_out;
  2529. }
  2530. switch (cmd) {
  2531. case SISUSB_GET_CONFIG_SIZE:
  2532. if (put_user(sizeof(x), argp))
  2533. retval = -EFAULT;
  2534. break;
  2535. case SISUSB_GET_CONFIG:
  2536. x.sisusb_id = SISUSB_ID;
  2537. x.sisusb_version = SISUSB_VERSION;
  2538. x.sisusb_revision = SISUSB_REVISION;
  2539. x.sisusb_patchlevel = SISUSB_PATCHLEVEL;
  2540. x.sisusb_gfxinit = sisusb->gfxinit;
  2541. x.sisusb_vrambase = SISUSB_PCI_PSEUDO_MEMBASE;
  2542. x.sisusb_mmiobase = SISUSB_PCI_PSEUDO_MMIOBASE;
  2543. x.sisusb_iobase = SISUSB_PCI_PSEUDO_IOPORTBASE;
  2544. x.sisusb_pcibase = SISUSB_PCI_PSEUDO_PCIBASE;
  2545. x.sisusb_vramsize = sisusb->vramsize;
  2546. x.sisusb_minor = sisusb->minor;
  2547. x.sisusb_fbdevactive= 0;
  2548. #ifdef INCL_SISUSB_CON
  2549. x.sisusb_conactive = sisusb->haveconsole ? 1 : 0;
  2550. #else
  2551. x.sisusb_conactive = 0;
  2552. #endif
  2553. if (copy_to_user((void __user *)arg, &x, sizeof(x)))
  2554. retval = -EFAULT;
  2555. break;
  2556. case SISUSB_COMMAND:
  2557. if (copy_from_user(&y, (void __user *)arg, sizeof(y)))
  2558. retval = -EFAULT;
  2559. else
  2560. retval = sisusb_handle_command(sisusb, &y, arg);
  2561. break;
  2562. default:
  2563. retval = -ENOTTY;
  2564. break;
  2565. }
  2566. err_out:
  2567. mutex_unlock(&sisusb->lock);
  2568. return retval;
  2569. }
  2570. #ifdef SISUSB_NEW_CONFIG_COMPAT
  2571. static long
  2572. sisusb_compat_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  2573. {
  2574. long retval;
  2575. switch (cmd) {
  2576. case SISUSB_GET_CONFIG_SIZE:
  2577. case SISUSB_GET_CONFIG:
  2578. case SISUSB_COMMAND:
  2579. lock_kernel();
  2580. retval = sisusb_ioctl(f->f_path.dentry->d_inode, f, cmd, arg);
  2581. unlock_kernel();
  2582. return retval;
  2583. default:
  2584. return -ENOIOCTLCMD;
  2585. }
  2586. }
  2587. #endif
  2588. static const struct file_operations usb_sisusb_fops = {
  2589. .owner = THIS_MODULE,
  2590. .open = sisusb_open,
  2591. .release = sisusb_release,
  2592. .read = sisusb_read,
  2593. .write = sisusb_write,
  2594. .llseek = sisusb_lseek,
  2595. #ifdef SISUSB_NEW_CONFIG_COMPAT
  2596. .compat_ioctl = sisusb_compat_ioctl,
  2597. #endif
  2598. .ioctl = sisusb_ioctl
  2599. };
  2600. static struct usb_class_driver usb_sisusb_class = {
  2601. .name = "sisusbvga%d",
  2602. .fops = &usb_sisusb_fops,
  2603. .minor_base = SISUSB_MINOR
  2604. };
  2605. static int sisusb_probe(struct usb_interface *intf,
  2606. const struct usb_device_id *id)
  2607. {
  2608. struct usb_device *dev = interface_to_usbdev(intf);
  2609. struct sisusb_usb_data *sisusb;
  2610. int retval = 0, i;
  2611. const char *memfail =
  2612. KERN_ERR
  2613. "sisusbvga[%d]: Failed to allocate memory for %s buffer\n";
  2614. printk(KERN_INFO "sisusb: USB2VGA dongle found at address %d\n",
  2615. dev->devnum);
  2616. /* Allocate memory for our private */
  2617. if (!(sisusb = kzalloc(sizeof(*sisusb), GFP_KERNEL))) {
  2618. printk(KERN_ERR
  2619. "sisusb: Failed to allocate memory for private data\n");
  2620. return -ENOMEM;
  2621. }
  2622. kref_init(&sisusb->kref);
  2623. mutex_init(&(sisusb->lock));
  2624. /* Register device */
  2625. if ((retval = usb_register_dev(intf, &usb_sisusb_class))) {
  2626. printk(KERN_ERR
  2627. "sisusb: Failed to get a minor for device %d\n",
  2628. dev->devnum);
  2629. retval = -ENODEV;
  2630. goto error_1;
  2631. }
  2632. sisusb->sisusb_dev = dev;
  2633. sisusb->minor = intf->minor;
  2634. sisusb->vrambase = SISUSB_PCI_MEMBASE;
  2635. sisusb->mmiobase = SISUSB_PCI_MMIOBASE;
  2636. sisusb->mmiosize = SISUSB_PCI_MMIOSIZE;
  2637. sisusb->ioportbase = SISUSB_PCI_IOPORTBASE;
  2638. /* Everything else is zero */
  2639. /* Allocate buffers */
  2640. sisusb->ibufsize = SISUSB_IBUF_SIZE;
  2641. if (!(sisusb->ibuf = usb_buffer_alloc(dev, SISUSB_IBUF_SIZE,
  2642. GFP_KERNEL, &sisusb->transfer_dma_in))) {
  2643. printk(memfail, "input", sisusb->minor);
  2644. retval = -ENOMEM;
  2645. goto error_2;
  2646. }
  2647. sisusb->numobufs = 0;
  2648. sisusb->obufsize = SISUSB_OBUF_SIZE;
  2649. for (i = 0; i < NUMOBUFS; i++) {
  2650. if (!(sisusb->obuf[i] = usb_buffer_alloc(dev, SISUSB_OBUF_SIZE,
  2651. GFP_KERNEL,
  2652. &sisusb->transfer_dma_out[i]))) {
  2653. if (i == 0) {
  2654. printk(memfail, "output", sisusb->minor);
  2655. retval = -ENOMEM;
  2656. goto error_3;
  2657. }
  2658. break;
  2659. } else
  2660. sisusb->numobufs++;
  2661. }
  2662. /* Allocate URBs */
  2663. if (!(sisusb->sisurbin = usb_alloc_urb(0, GFP_KERNEL))) {
  2664. printk(KERN_ERR
  2665. "sisusbvga[%d]: Failed to allocate URBs\n",
  2666. sisusb->minor);
  2667. retval = -ENOMEM;
  2668. goto error_3;
  2669. }
  2670. sisusb->completein = 1;
  2671. for (i = 0; i < sisusb->numobufs; i++) {
  2672. if (!(sisusb->sisurbout[i] = usb_alloc_urb(0, GFP_KERNEL))) {
  2673. printk(KERN_ERR
  2674. "sisusbvga[%d]: Failed to allocate URBs\n",
  2675. sisusb->minor);
  2676. retval = -ENOMEM;
  2677. goto error_4;
  2678. }
  2679. sisusb->urbout_context[i].sisusb = (void *)sisusb;
  2680. sisusb->urbout_context[i].urbindex = i;
  2681. sisusb->urbstatus[i] = 0;
  2682. }
  2683. printk(KERN_INFO "sisusbvga[%d]: Allocated %d output buffers\n",
  2684. sisusb->minor, sisusb->numobufs);
  2685. #ifdef INCL_SISUSB_CON
  2686. /* Allocate our SiS_Pr */
  2687. if (!(sisusb->SiS_Pr = kmalloc(sizeof(struct SiS_Private), GFP_KERNEL))) {
  2688. printk(KERN_ERR
  2689. "sisusbvga[%d]: Failed to allocate SiS_Pr\n",
  2690. sisusb->minor);
  2691. }
  2692. #endif
  2693. /* Do remaining init stuff */
  2694. init_waitqueue_head(&sisusb->wait_q);
  2695. usb_set_intfdata(intf, sisusb);
  2696. usb_get_dev(sisusb->sisusb_dev);
  2697. sisusb->present = 1;
  2698. #ifdef SISUSB_OLD_CONFIG_COMPAT
  2699. {
  2700. int ret;
  2701. /* Our ioctls are all "32/64bit compatible" */
  2702. ret = register_ioctl32_conversion(SISUSB_GET_CONFIG_SIZE, NULL);
  2703. ret |= register_ioctl32_conversion(SISUSB_GET_CONFIG, NULL);
  2704. ret |= register_ioctl32_conversion(SISUSB_COMMAND, NULL);
  2705. if (ret)
  2706. printk(KERN_ERR
  2707. "sisusbvga[%d]: Error registering ioctl32 "
  2708. "translations\n",
  2709. sisusb->minor);
  2710. else
  2711. sisusb->ioctl32registered = 1;
  2712. }
  2713. #endif
  2714. if (dev->speed == USB_SPEED_HIGH) {
  2715. int initscreen = 1;
  2716. #ifdef INCL_SISUSB_CON
  2717. if (sisusb_first_vc > 0 &&
  2718. sisusb_last_vc > 0 &&
  2719. sisusb_first_vc <= sisusb_last_vc &&
  2720. sisusb_last_vc <= MAX_NR_CONSOLES)
  2721. initscreen = 0;
  2722. #endif
  2723. if (sisusb_init_gfxdevice(sisusb, initscreen))
  2724. printk(KERN_ERR
  2725. "sisusbvga[%d]: Failed to early "
  2726. "initialize device\n",
  2727. sisusb->minor);
  2728. } else
  2729. printk(KERN_INFO
  2730. "sisusbvga[%d]: Not attached to USB 2.0 hub, "
  2731. "deferring init\n",
  2732. sisusb->minor);
  2733. sisusb->ready = 1;
  2734. #ifdef SISUSBENDIANTEST
  2735. printk(KERN_DEBUG "sisusb: *** RWTEST ***\n");
  2736. sisusb_testreadwrite(sisusb);
  2737. printk(KERN_DEBUG "sisusb: *** RWTEST END ***\n");
  2738. #endif
  2739. #ifdef INCL_SISUSB_CON
  2740. sisusb_console_init(sisusb, sisusb_first_vc, sisusb_last_vc);
  2741. #endif
  2742. return 0;
  2743. error_4:
  2744. sisusb_free_urbs(sisusb);
  2745. error_3:
  2746. sisusb_free_buffers(sisusb);
  2747. error_2:
  2748. usb_deregister_dev(intf, &usb_sisusb_class);
  2749. error_1:
  2750. kfree(sisusb);
  2751. return retval;
  2752. }
  2753. static void sisusb_disconnect(struct usb_interface *intf)
  2754. {
  2755. struct sisusb_usb_data *sisusb;
  2756. int minor;
  2757. /* This should *not* happen */
  2758. if (!(sisusb = usb_get_intfdata(intf)))
  2759. return;
  2760. #ifdef INCL_SISUSB_CON
  2761. sisusb_console_exit(sisusb);
  2762. #endif
  2763. minor = sisusb->minor;
  2764. usb_deregister_dev(intf, &usb_sisusb_class);
  2765. mutex_lock(&sisusb->lock);
  2766. /* Wait for all URBs to complete and kill them in case (MUST do) */
  2767. if (!sisusb_wait_all_out_complete(sisusb))
  2768. sisusb_kill_all_busy(sisusb);
  2769. usb_set_intfdata(intf, NULL);
  2770. #ifdef SISUSB_OLD_CONFIG_COMPAT
  2771. if (sisusb->ioctl32registered) {
  2772. int ret;
  2773. sisusb->ioctl32registered = 0;
  2774. ret = unregister_ioctl32_conversion(SISUSB_GET_CONFIG_SIZE);
  2775. ret |= unregister_ioctl32_conversion(SISUSB_GET_CONFIG);
  2776. ret |= unregister_ioctl32_conversion(SISUSB_COMMAND);
  2777. if (ret) {
  2778. printk(KERN_ERR
  2779. "sisusbvga[%d]: Error unregistering "
  2780. "ioctl32 translations\n",
  2781. minor);
  2782. }
  2783. }
  2784. #endif
  2785. sisusb->present = 0;
  2786. sisusb->ready = 0;
  2787. mutex_unlock(&sisusb->lock);
  2788. /* decrement our usage count */
  2789. kref_put(&sisusb->kref, sisusb_delete);
  2790. printk(KERN_INFO "sisusbvga[%d]: Disconnected\n", minor);
  2791. }
  2792. static struct usb_device_id sisusb_table [] = {
  2793. { USB_DEVICE(0x0711, 0x0900) },
  2794. { USB_DEVICE(0x0711, 0x0901) },
  2795. { USB_DEVICE(0x0711, 0x0902) },
  2796. { USB_DEVICE(0x182d, 0x021c) },
  2797. { USB_DEVICE(0x182d, 0x0269) },
  2798. { }
  2799. };
  2800. MODULE_DEVICE_TABLE (usb, sisusb_table);
  2801. static struct usb_driver sisusb_driver = {
  2802. .name = "sisusb",
  2803. .probe = sisusb_probe,
  2804. .disconnect = sisusb_disconnect,
  2805. .id_table = sisusb_table,
  2806. };
  2807. static int __init usb_sisusb_init(void)
  2808. {
  2809. int retval;
  2810. #ifdef INCL_SISUSB_CON
  2811. sisusb_init_concode();
  2812. #endif
  2813. if (!(retval = usb_register(&sisusb_driver))) {
  2814. printk(KERN_INFO "sisusb: Driver version %d.%d.%d\n",
  2815. SISUSB_VERSION, SISUSB_REVISION, SISUSB_PATCHLEVEL);
  2816. printk(KERN_INFO
  2817. "sisusb: Copyright (C) 2005 Thomas Winischhofer\n");
  2818. }
  2819. return retval;
  2820. }
  2821. static void __exit usb_sisusb_exit(void)
  2822. {
  2823. usb_deregister(&sisusb_driver);
  2824. }
  2825. module_init(usb_sisusb_init);
  2826. module_exit(usb_sisusb_exit);
  2827. MODULE_AUTHOR("Thomas Winischhofer <thomas@winischhofer.net>");
  2828. MODULE_DESCRIPTION("sisusbvga - Driver for Net2280/SiS315-based USB2VGA dongles");
  2829. MODULE_LICENSE("GPL");