sisusb.c 81 KB

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