sisusb.c 73 KB

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