sisusb.c 77 KB

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