sisusb.c 77 KB

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