sisusb.c 81 KB

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