sisusb.c 76 KB

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