sisusb.c 76 KB

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