sisusb.c 75 KB

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