sisusb.c 82 KB

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