sisusb.c 76 KB

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