sisusb.c 79 KB

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