ali-ircc.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /*********************************************************************
  2. *
  3. * Filename: ali-ircc.h
  4. * Version: 0.5
  5. * Description: Driver for the ALI M1535D and M1543C FIR Controller
  6. * Status: Experimental.
  7. * Author: Benjamin Kong <benjamin_kong@ali.com.tw>
  8. * Created at: 2000/10/16 03:46PM
  9. * Modified at: 2001/1/3 02:55PM
  10. * Modified by: Benjamin Kong <benjamin_kong@ali.com.tw>
  11. * Modified at: 2003/11/6 and support for ALi south-bridge chipsets M1563
  12. * Modified by: Clear Zhang <clear_zhang@ali.com.tw>
  13. *
  14. * Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
  15. * All Rights Reserved
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. ********************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/types.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/ioport.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/platform_device.h>
  36. #include <asm/io.h>
  37. #include <asm/dma.h>
  38. #include <asm/byteorder.h>
  39. #include <net/irda/wrapper.h>
  40. #include <net/irda/irda.h>
  41. #include <net/irda/irda_device.h>
  42. #include "ali-ircc.h"
  43. #define CHIP_IO_EXTENT 8
  44. #define BROKEN_DONGLE_ID
  45. #define ALI_IRCC_DRIVER_NAME "ali-ircc"
  46. /* Power Management */
  47. static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
  48. static int ali_ircc_resume(struct platform_device *dev);
  49. static struct platform_driver ali_ircc_driver = {
  50. .suspend = ali_ircc_suspend,
  51. .resume = ali_ircc_resume,
  52. .driver = {
  53. .name = ALI_IRCC_DRIVER_NAME,
  54. },
  55. };
  56. /* Module parameters */
  57. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  58. /* Use BIOS settions by default, but user may supply module parameters */
  59. static unsigned int io[] = { ~0, ~0, ~0, ~0 };
  60. static unsigned int irq[] = { 0, 0, 0, 0 };
  61. static unsigned int dma[] = { 0, 0, 0, 0 };
  62. static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
  63. static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
  64. static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
  65. /* These are the currently known ALi sourth-bridge chipsets, the only one difference
  66. * is that M1543C doesn't support HP HDSL-3600
  67. */
  68. static ali_chip_t chips[] =
  69. {
  70. { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
  71. { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
  72. { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
  73. { NULL }
  74. };
  75. /* Max 4 instances for now */
  76. static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
  77. /* Dongle Types */
  78. static char *dongle_types[] = {
  79. "TFDS6000",
  80. "HP HSDL-3600",
  81. "HP HSDL-1100",
  82. "No dongle connected",
  83. };
  84. /* Some prototypes */
  85. static int ali_ircc_open(int i, chipio_t *info);
  86. static int ali_ircc_close(struct ali_ircc_cb *self);
  87. static int ali_ircc_setup(chipio_t *info);
  88. static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
  89. static int ali_ircc_net_open(struct net_device *dev);
  90. static int ali_ircc_net_close(struct net_device *dev);
  91. static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  92. static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
  93. static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev);
  94. /* SIR function */
  95. static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  96. static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
  97. static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
  98. static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
  99. static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
  100. static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
  101. /* FIR function */
  102. static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
  103. static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
  104. static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
  105. static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
  106. static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
  107. static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
  108. static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
  109. /* My Function */
  110. static int ali_ircc_read_dongle_id (int i, chipio_t *info);
  111. static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
  112. /* ALi chip function */
  113. static void SIR2FIR(int iobase);
  114. static void FIR2SIR(int iobase);
  115. static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
  116. /*
  117. * Function ali_ircc_init ()
  118. *
  119. * Initialize chip. Find out whay kinds of chips we are dealing with
  120. * and their configuation registers address
  121. */
  122. static int __init ali_ircc_init(void)
  123. {
  124. ali_chip_t *chip;
  125. chipio_t info;
  126. int ret = -ENODEV;
  127. int cfg, cfg_base;
  128. int reg, revision;
  129. int i = 0;
  130. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  131. ret = platform_driver_register(&ali_ircc_driver);
  132. if (ret) {
  133. IRDA_ERROR("%s, Can't register driver!\n",
  134. ALI_IRCC_DRIVER_NAME);
  135. return ret;
  136. }
  137. /* Probe for all the ALi chipsets we know about */
  138. for (chip= chips; chip->name; chip++, i++)
  139. {
  140. IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__, chip->name);
  141. /* Try all config registers for this chip */
  142. for (cfg=0; cfg<2; cfg++)
  143. {
  144. cfg_base = chip->cfg[cfg];
  145. if (!cfg_base)
  146. continue;
  147. memset(&info, 0, sizeof(chipio_t));
  148. info.cfg_base = cfg_base;
  149. info.fir_base = io[i];
  150. info.dma = dma[i];
  151. info.irq = irq[i];
  152. /* Enter Configuration */
  153. outb(chip->entr1, cfg_base);
  154. outb(chip->entr2, cfg_base);
  155. /* Select Logical Device 5 Registers (UART2) */
  156. outb(0x07, cfg_base);
  157. outb(0x05, cfg_base+1);
  158. /* Read Chip Identification Register */
  159. outb(chip->cid_index, cfg_base);
  160. reg = inb(cfg_base+1);
  161. if (reg == chip->cid_value)
  162. {
  163. IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __FUNCTION__, cfg_base);
  164. outb(0x1F, cfg_base);
  165. revision = inb(cfg_base+1);
  166. IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __FUNCTION__,
  167. chip->name, revision);
  168. /*
  169. * If the user supplies the base address, then
  170. * we init the chip, if not we probe the values
  171. * set by the BIOS
  172. */
  173. if (io[i] < 2000)
  174. {
  175. chip->init(chip, &info);
  176. }
  177. else
  178. {
  179. chip->probe(chip, &info);
  180. }
  181. if (ali_ircc_open(i, &info) == 0)
  182. ret = 0;
  183. i++;
  184. }
  185. else
  186. {
  187. IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __FUNCTION__, chip->name, cfg_base);
  188. }
  189. /* Exit configuration */
  190. outb(0xbb, cfg_base);
  191. }
  192. }
  193. IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
  194. if (ret)
  195. platform_driver_unregister(&ali_ircc_driver);
  196. return ret;
  197. }
  198. /*
  199. * Function ali_ircc_cleanup ()
  200. *
  201. * Close all configured chips
  202. *
  203. */
  204. static void __exit ali_ircc_cleanup(void)
  205. {
  206. int i;
  207. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  208. for (i=0; i < 4; i++) {
  209. if (dev_self[i])
  210. ali_ircc_close(dev_self[i]);
  211. }
  212. platform_driver_unregister(&ali_ircc_driver);
  213. IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
  214. }
  215. /*
  216. * Function ali_ircc_open (int i, chipio_t *inf)
  217. *
  218. * Open driver instance
  219. *
  220. */
  221. static int ali_ircc_open(int i, chipio_t *info)
  222. {
  223. struct net_device *dev;
  224. struct ali_ircc_cb *self;
  225. int dongle_id;
  226. int err;
  227. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  228. /* Set FIR FIFO and DMA Threshold */
  229. if ((ali_ircc_setup(info)) == -1)
  230. return -1;
  231. dev = alloc_irdadev(sizeof(*self));
  232. if (dev == NULL) {
  233. IRDA_ERROR("%s(), can't allocate memory for control block!\n",
  234. __FUNCTION__);
  235. return -ENOMEM;
  236. }
  237. self = dev->priv;
  238. self->netdev = dev;
  239. spin_lock_init(&self->lock);
  240. /* Need to store self somewhere */
  241. dev_self[i] = self;
  242. self->index = i;
  243. /* Initialize IO */
  244. self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
  245. self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
  246. self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
  247. self->io.irq = info->irq;
  248. self->io.fir_ext = CHIP_IO_EXTENT;
  249. self->io.dma = info->dma;
  250. self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
  251. /* Reserve the ioports that we need */
  252. if (!request_region(self->io.fir_base, self->io.fir_ext,
  253. ALI_IRCC_DRIVER_NAME)) {
  254. IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __FUNCTION__,
  255. self->io.fir_base);
  256. err = -ENODEV;
  257. goto err_out1;
  258. }
  259. /* Initialize QoS for this device */
  260. irda_init_max_qos_capabilies(&self->qos);
  261. /* The only value we must override it the baudrate */
  262. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  263. IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
  264. self->qos.min_turn_time.bits = qos_mtt_bits;
  265. irda_qos_bits_to_value(&self->qos);
  266. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  267. self->rx_buff.truesize = 14384;
  268. self->tx_buff.truesize = 14384;
  269. /* Allocate memory if needed */
  270. self->rx_buff.head =
  271. dma_alloc_coherent(NULL, self->rx_buff.truesize,
  272. &self->rx_buff_dma, GFP_KERNEL);
  273. if (self->rx_buff.head == NULL) {
  274. err = -ENOMEM;
  275. goto err_out2;
  276. }
  277. memset(self->rx_buff.head, 0, self->rx_buff.truesize);
  278. self->tx_buff.head =
  279. dma_alloc_coherent(NULL, self->tx_buff.truesize,
  280. &self->tx_buff_dma, GFP_KERNEL);
  281. if (self->tx_buff.head == NULL) {
  282. err = -ENOMEM;
  283. goto err_out3;
  284. }
  285. memset(self->tx_buff.head, 0, self->tx_buff.truesize);
  286. self->rx_buff.in_frame = FALSE;
  287. self->rx_buff.state = OUTSIDE_FRAME;
  288. self->tx_buff.data = self->tx_buff.head;
  289. self->rx_buff.data = self->rx_buff.head;
  290. /* Reset Tx queue info */
  291. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  292. self->tx_fifo.tail = self->tx_buff.head;
  293. /* Keep track of module usage */
  294. SET_MODULE_OWNER(dev);
  295. /* Override the network functions we need to use */
  296. dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
  297. dev->open = ali_ircc_net_open;
  298. dev->stop = ali_ircc_net_close;
  299. dev->do_ioctl = ali_ircc_net_ioctl;
  300. dev->get_stats = ali_ircc_net_get_stats;
  301. err = register_netdev(dev);
  302. if (err) {
  303. IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
  304. goto err_out4;
  305. }
  306. IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
  307. /* Check dongle id */
  308. dongle_id = ali_ircc_read_dongle_id(i, info);
  309. IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __FUNCTION__,
  310. ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
  311. self->io.dongle_id = dongle_id;
  312. IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
  313. return 0;
  314. err_out4:
  315. dma_free_coherent(NULL, self->tx_buff.truesize,
  316. self->tx_buff.head, self->tx_buff_dma);
  317. err_out3:
  318. dma_free_coherent(NULL, self->rx_buff.truesize,
  319. self->rx_buff.head, self->rx_buff_dma);
  320. err_out2:
  321. release_region(self->io.fir_base, self->io.fir_ext);
  322. err_out1:
  323. dev_self[i] = NULL;
  324. free_netdev(dev);
  325. return err;
  326. }
  327. /*
  328. * Function ali_ircc_close (self)
  329. *
  330. * Close driver instance
  331. *
  332. */
  333. static int __exit ali_ircc_close(struct ali_ircc_cb *self)
  334. {
  335. int iobase;
  336. IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  337. IRDA_ASSERT(self != NULL, return -1;);
  338. iobase = self->io.fir_base;
  339. /* Remove netdevice */
  340. unregister_netdev(self->netdev);
  341. /* Release the PORT that this driver is using */
  342. IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __FUNCTION__, self->io.fir_base);
  343. release_region(self->io.fir_base, self->io.fir_ext);
  344. if (self->tx_buff.head)
  345. dma_free_coherent(NULL, self->tx_buff.truesize,
  346. self->tx_buff.head, self->tx_buff_dma);
  347. if (self->rx_buff.head)
  348. dma_free_coherent(NULL, self->rx_buff.truesize,
  349. self->rx_buff.head, self->rx_buff_dma);
  350. dev_self[self->index] = NULL;
  351. free_netdev(self->netdev);
  352. IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
  353. return 0;
  354. }
  355. /*
  356. * Function ali_ircc_init_43 (chip, info)
  357. *
  358. * Initialize the ALi M1543 chip.
  359. */
  360. static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
  361. {
  362. /* All controller information like I/O address, DMA channel, IRQ
  363. * are set by BIOS
  364. */
  365. return 0;
  366. }
  367. /*
  368. * Function ali_ircc_init_53 (chip, info)
  369. *
  370. * Initialize the ALi M1535 chip.
  371. */
  372. static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
  373. {
  374. /* All controller information like I/O address, DMA channel, IRQ
  375. * are set by BIOS
  376. */
  377. return 0;
  378. }
  379. /*
  380. * Function ali_ircc_probe_53 (chip, info)
  381. *
  382. * Probes for the ALi M1535D or M1535
  383. */
  384. static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
  385. {
  386. int cfg_base = info->cfg_base;
  387. int hi, low, reg;
  388. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  389. /* Enter Configuration */
  390. outb(chip->entr1, cfg_base);
  391. outb(chip->entr2, cfg_base);
  392. /* Select Logical Device 5 Registers (UART2) */
  393. outb(0x07, cfg_base);
  394. outb(0x05, cfg_base+1);
  395. /* Read address control register */
  396. outb(0x60, cfg_base);
  397. hi = inb(cfg_base+1);
  398. outb(0x61, cfg_base);
  399. low = inb(cfg_base+1);
  400. info->fir_base = (hi<<8) + low;
  401. info->sir_base = info->fir_base;
  402. IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__, info->fir_base);
  403. /* Read IRQ control register */
  404. outb(0x70, cfg_base);
  405. reg = inb(cfg_base+1);
  406. info->irq = reg & 0x0f;
  407. IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
  408. /* Read DMA channel */
  409. outb(0x74, cfg_base);
  410. reg = inb(cfg_base+1);
  411. info->dma = reg & 0x07;
  412. if(info->dma == 0x04)
  413. IRDA_WARNING("%s(), No DMA channel assigned !\n", __FUNCTION__);
  414. else
  415. IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
  416. /* Read Enabled Status */
  417. outb(0x30, cfg_base);
  418. reg = inb(cfg_base+1);
  419. info->enabled = (reg & 0x80) && (reg & 0x01);
  420. IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __FUNCTION__, info->enabled);
  421. /* Read Power Status */
  422. outb(0x22, cfg_base);
  423. reg = inb(cfg_base+1);
  424. info->suspended = (reg & 0x20);
  425. IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __FUNCTION__, info->suspended);
  426. /* Exit configuration */
  427. outb(0xbb, cfg_base);
  428. IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
  429. return 0;
  430. }
  431. /*
  432. * Function ali_ircc_setup (info)
  433. *
  434. * Set FIR FIFO and DMA Threshold
  435. * Returns non-negative on success.
  436. *
  437. */
  438. static int ali_ircc_setup(chipio_t *info)
  439. {
  440. unsigned char tmp;
  441. int version;
  442. int iobase = info->fir_base;
  443. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  444. /* Locking comments :
  445. * Most operations here need to be protected. We are called before
  446. * the device instance is created in ali_ircc_open(), therefore
  447. * nobody can bother us - Jean II */
  448. /* Switch to FIR space */
  449. SIR2FIR(iobase);
  450. /* Master Reset */
  451. outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
  452. /* Read FIR ID Version Register */
  453. switch_bank(iobase, BANK3);
  454. version = inb(iobase+FIR_ID_VR);
  455. /* Should be 0x00 in the M1535/M1535D */
  456. if(version != 0x00)
  457. {
  458. IRDA_ERROR("%s, Wrong chip version %02x\n",
  459. ALI_IRCC_DRIVER_NAME, version);
  460. return -1;
  461. }
  462. /* Set FIR FIFO Threshold Register */
  463. switch_bank(iobase, BANK1);
  464. outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
  465. /* Set FIR DMA Threshold Register */
  466. outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
  467. /* CRC enable */
  468. switch_bank(iobase, BANK2);
  469. outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
  470. /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
  471. /* Switch to Bank 0 */
  472. switch_bank(iobase, BANK0);
  473. tmp = inb(iobase+FIR_LCR_B);
  474. tmp &=~0x20; // disable SIP
  475. tmp |= 0x80; // these two steps make RX mode
  476. tmp &= 0xbf;
  477. outb(tmp, iobase+FIR_LCR_B);
  478. /* Disable Interrupt */
  479. outb(0x00, iobase+FIR_IER);
  480. /* Switch to SIR space */
  481. FIR2SIR(iobase);
  482. IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
  483. ALI_IRCC_DRIVER_NAME);
  484. /* Enable receive interrupts */
  485. // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
  486. // Turn on the interrupts in ali_ircc_net_open
  487. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
  488. return 0;
  489. }
  490. /*
  491. * Function ali_ircc_read_dongle_id (int index, info)
  492. *
  493. * Try to read dongle indentification. This procedure needs to be executed
  494. * once after power-on/reset. It also needs to be used whenever you suspect
  495. * that the user may have plugged/unplugged the IrDA Dongle.
  496. */
  497. static int ali_ircc_read_dongle_id (int i, chipio_t *info)
  498. {
  499. int dongle_id, reg;
  500. int cfg_base = info->cfg_base;
  501. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  502. /* Enter Configuration */
  503. outb(chips[i].entr1, cfg_base);
  504. outb(chips[i].entr2, cfg_base);
  505. /* Select Logical Device 5 Registers (UART2) */
  506. outb(0x07, cfg_base);
  507. outb(0x05, cfg_base+1);
  508. /* Read Dongle ID */
  509. outb(0xf0, cfg_base);
  510. reg = inb(cfg_base+1);
  511. dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
  512. IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __FUNCTION__,
  513. dongle_id, dongle_types[dongle_id]);
  514. /* Exit configuration */
  515. outb(0xbb, cfg_base);
  516. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
  517. return dongle_id;
  518. }
  519. /*
  520. * Function ali_ircc_interrupt (irq, dev_id, regs)
  521. *
  522. * An interrupt from the chip has arrived. Time to do some work
  523. *
  524. */
  525. static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id,
  526. struct pt_regs *regs)
  527. {
  528. struct net_device *dev = (struct net_device *) dev_id;
  529. struct ali_ircc_cb *self;
  530. int ret;
  531. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  532. if (!dev) {
  533. IRDA_WARNING("%s: irq %d for unknown device.\n",
  534. ALI_IRCC_DRIVER_NAME, irq);
  535. return IRQ_NONE;
  536. }
  537. self = (struct ali_ircc_cb *) dev->priv;
  538. spin_lock(&self->lock);
  539. /* Dispatch interrupt handler for the current speed */
  540. if (self->io.speed > 115200)
  541. ret = ali_ircc_fir_interrupt(self);
  542. else
  543. ret = ali_ircc_sir_interrupt(self);
  544. spin_unlock(&self->lock);
  545. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
  546. return ret;
  547. }
  548. /*
  549. * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
  550. *
  551. * Handle MIR/FIR interrupt
  552. *
  553. */
  554. static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
  555. {
  556. __u8 eir, OldMessageCount;
  557. int iobase, tmp;
  558. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  559. iobase = self->io.fir_base;
  560. switch_bank(iobase, BANK0);
  561. self->InterruptID = inb(iobase+FIR_IIR);
  562. self->BusStatus = inb(iobase+FIR_BSR);
  563. OldMessageCount = (self->LineStatus + 1) & 0x07;
  564. self->LineStatus = inb(iobase+FIR_LSR);
  565. //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
  566. eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
  567. IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __FUNCTION__,self->InterruptID);
  568. IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __FUNCTION__,self->LineStatus);
  569. IRDA_DEBUG(1, "%s(), self->ier = %x\n", __FUNCTION__,self->ier);
  570. IRDA_DEBUG(1, "%s(), eir = %x\n", __FUNCTION__,eir);
  571. /* Disable interrupts */
  572. SetCOMInterrupts(self, FALSE);
  573. /* Tx or Rx Interrupt */
  574. if (eir & IIR_EOM)
  575. {
  576. if (self->io.direction == IO_XMIT) /* TX */
  577. {
  578. IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __FUNCTION__);
  579. if(ali_ircc_dma_xmit_complete(self))
  580. {
  581. if (irda_device_txqueue_empty(self->netdev))
  582. {
  583. /* Prepare for receive */
  584. ali_ircc_dma_receive(self);
  585. self->ier = IER_EOM;
  586. }
  587. }
  588. else
  589. {
  590. self->ier = IER_EOM;
  591. }
  592. }
  593. else /* RX */
  594. {
  595. IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __FUNCTION__);
  596. if(OldMessageCount > ((self->LineStatus+1) & 0x07))
  597. {
  598. self->rcvFramesOverflow = TRUE;
  599. IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __FUNCTION__);
  600. }
  601. if (ali_ircc_dma_receive_complete(self))
  602. {
  603. IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __FUNCTION__);
  604. self->ier = IER_EOM;
  605. }
  606. else
  607. {
  608. IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __FUNCTION__);
  609. self->ier = IER_EOM | IER_TIMER;
  610. }
  611. }
  612. }
  613. /* Timer Interrupt */
  614. else if (eir & IIR_TIMER)
  615. {
  616. if(OldMessageCount > ((self->LineStatus+1) & 0x07))
  617. {
  618. self->rcvFramesOverflow = TRUE;
  619. IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __FUNCTION__);
  620. }
  621. /* Disable Timer */
  622. switch_bank(iobase, BANK1);
  623. tmp = inb(iobase+FIR_CR);
  624. outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
  625. /* Check if this is a Tx timer interrupt */
  626. if (self->io.direction == IO_XMIT)
  627. {
  628. ali_ircc_dma_xmit(self);
  629. /* Interrupt on EOM */
  630. self->ier = IER_EOM;
  631. }
  632. else /* Rx */
  633. {
  634. if(ali_ircc_dma_receive_complete(self))
  635. {
  636. self->ier = IER_EOM;
  637. }
  638. else
  639. {
  640. self->ier = IER_EOM | IER_TIMER;
  641. }
  642. }
  643. }
  644. /* Restore Interrupt */
  645. SetCOMInterrupts(self, TRUE);
  646. IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __FUNCTION__);
  647. return IRQ_RETVAL(eir);
  648. }
  649. /*
  650. * Function ali_ircc_sir_interrupt (irq, self, eir)
  651. *
  652. * Handle SIR interrupt
  653. *
  654. */
  655. static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
  656. {
  657. int iobase;
  658. int iir, lsr;
  659. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  660. iobase = self->io.sir_base;
  661. iir = inb(iobase+UART_IIR) & UART_IIR_ID;
  662. if (iir) {
  663. /* Clear interrupt */
  664. lsr = inb(iobase+UART_LSR);
  665. IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__,
  666. iir, lsr, iobase);
  667. switch (iir)
  668. {
  669. case UART_IIR_RLSI:
  670. IRDA_DEBUG(2, "%s(), RLSI\n", __FUNCTION__);
  671. break;
  672. case UART_IIR_RDI:
  673. /* Receive interrupt */
  674. ali_ircc_sir_receive(self);
  675. break;
  676. case UART_IIR_THRI:
  677. if (lsr & UART_LSR_THRE)
  678. {
  679. /* Transmitter ready for data */
  680. ali_ircc_sir_write_wakeup(self);
  681. }
  682. break;
  683. default:
  684. IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __FUNCTION__, iir);
  685. break;
  686. }
  687. }
  688. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
  689. return IRQ_RETVAL(iir);
  690. }
  691. /*
  692. * Function ali_ircc_sir_receive (self)
  693. *
  694. * Receive one frame from the infrared port
  695. *
  696. */
  697. static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
  698. {
  699. int boguscount = 0;
  700. int iobase;
  701. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
  702. IRDA_ASSERT(self != NULL, return;);
  703. iobase = self->io.sir_base;
  704. /*
  705. * Receive all characters in Rx FIFO, unwrap and unstuff them.
  706. * async_unwrap_char will deliver all found frames
  707. */
  708. do {
  709. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  710. inb(iobase+UART_RX));
  711. /* Make sure we don't stay here too long */
  712. if (boguscount++ > 32) {
  713. IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
  714. break;
  715. }
  716. } while (inb(iobase+UART_LSR) & UART_LSR_DR);
  717. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  718. }
  719. /*
  720. * Function ali_ircc_sir_write_wakeup (tty)
  721. *
  722. * Called by the driver when there's room for more data. If we have
  723. * more packets to send, we send them here.
  724. *
  725. */
  726. static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
  727. {
  728. int actual = 0;
  729. int iobase;
  730. IRDA_ASSERT(self != NULL, return;);
  731. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  732. iobase = self->io.sir_base;
  733. /* Finished with frame? */
  734. if (self->tx_buff.len > 0)
  735. {
  736. /* Write data left in transmit buffer */
  737. actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
  738. self->tx_buff.data, self->tx_buff.len);
  739. self->tx_buff.data += actual;
  740. self->tx_buff.len -= actual;
  741. }
  742. else
  743. {
  744. if (self->new_speed)
  745. {
  746. /* We must wait until all data are gone */
  747. while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
  748. IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __FUNCTION__ );
  749. IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __FUNCTION__ , self->new_speed);
  750. ali_ircc_change_speed(self, self->new_speed);
  751. self->new_speed = 0;
  752. // benjamin 2000/11/10 06:32PM
  753. if (self->io.speed > 115200)
  754. {
  755. IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __FUNCTION__ );
  756. self->ier = IER_EOM;
  757. // SetCOMInterrupts(self, TRUE);
  758. return;
  759. }
  760. }
  761. else
  762. {
  763. netif_wake_queue(self->netdev);
  764. }
  765. self->stats.tx_packets++;
  766. /* Turn on receive interrupts */
  767. outb(UART_IER_RDI, iobase+UART_IER);
  768. }
  769. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  770. }
  771. static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
  772. {
  773. struct net_device *dev = self->netdev;
  774. int iobase;
  775. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  776. IRDA_DEBUG(2, "%s(), setting speed = %d \n", __FUNCTION__ , baud);
  777. /* This function *must* be called with irq off and spin-lock.
  778. * - Jean II */
  779. iobase = self->io.fir_base;
  780. SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
  781. /* Go to MIR, FIR Speed */
  782. if (baud > 115200)
  783. {
  784. ali_ircc_fir_change_speed(self, baud);
  785. /* Install FIR xmit handler*/
  786. dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
  787. /* Enable Interuupt */
  788. self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
  789. /* Be ready for incomming frames */
  790. ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
  791. }
  792. /* Go to SIR Speed */
  793. else
  794. {
  795. ali_ircc_sir_change_speed(self, baud);
  796. /* Install SIR xmit handler*/
  797. dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
  798. }
  799. SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
  800. netif_wake_queue(self->netdev);
  801. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  802. }
  803. static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
  804. {
  805. int iobase;
  806. struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
  807. struct net_device *dev;
  808. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  809. IRDA_ASSERT(self != NULL, return;);
  810. dev = self->netdev;
  811. iobase = self->io.fir_base;
  812. IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __FUNCTION__ ,self->io.speed,baud);
  813. /* Come from SIR speed */
  814. if(self->io.speed <=115200)
  815. {
  816. SIR2FIR(iobase);
  817. }
  818. /* Update accounting for new speed */
  819. self->io.speed = baud;
  820. // Set Dongle Speed mode
  821. ali_ircc_change_dongle_speed(self, baud);
  822. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  823. }
  824. /*
  825. * Function ali_sir_change_speed (self, speed)
  826. *
  827. * Set speed of IrDA port to specified baudrate
  828. *
  829. */
  830. static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
  831. {
  832. struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
  833. unsigned long flags;
  834. int iobase;
  835. int fcr; /* FIFO control reg */
  836. int lcr; /* Line control reg */
  837. int divisor;
  838. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  839. IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __FUNCTION__ , speed);
  840. IRDA_ASSERT(self != NULL, return;);
  841. iobase = self->io.sir_base;
  842. /* Come from MIR or FIR speed */
  843. if(self->io.speed >115200)
  844. {
  845. // Set Dongle Speed mode first
  846. ali_ircc_change_dongle_speed(self, speed);
  847. FIR2SIR(iobase);
  848. }
  849. // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
  850. inb(iobase+UART_LSR);
  851. inb(iobase+UART_SCR);
  852. /* Update accounting for new speed */
  853. self->io.speed = speed;
  854. spin_lock_irqsave(&self->lock, flags);
  855. divisor = 115200/speed;
  856. fcr = UART_FCR_ENABLE_FIFO;
  857. /*
  858. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  859. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  860. * about this timeout since it will always be fast enough.
  861. */
  862. if (self->io.speed < 38400)
  863. fcr |= UART_FCR_TRIGGER_1;
  864. else
  865. fcr |= UART_FCR_TRIGGER_14;
  866. /* IrDA ports use 8N1 */
  867. lcr = UART_LCR_WLEN8;
  868. outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
  869. outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
  870. outb(divisor >> 8, iobase+UART_DLM);
  871. outb(lcr, iobase+UART_LCR); /* Set 8N1 */
  872. outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
  873. /* without this, the conection will be broken after come back from FIR speed,
  874. but with this, the SIR connection is harder to established */
  875. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
  876. spin_unlock_irqrestore(&self->lock, flags);
  877. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  878. }
  879. static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
  880. {
  881. struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
  882. int iobase,dongle_id;
  883. int tmp = 0;
  884. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  885. iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
  886. dongle_id = self->io.dongle_id;
  887. /* We are already locked, no need to do it again */
  888. IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __FUNCTION__ , dongle_types[dongle_id], speed);
  889. switch_bank(iobase, BANK2);
  890. tmp = inb(iobase+FIR_IRDA_CR);
  891. /* IBM type dongle */
  892. if(dongle_id == 0)
  893. {
  894. if(speed == 4000000)
  895. {
  896. // __ __
  897. // SD/MODE __| |__ __
  898. // __ __
  899. // IRTX __ __| |__
  900. // T1 T2 T3 T4 T5
  901. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  902. tmp |= IRDA_CR_CRC; // CRC=1
  903. switch_bank(iobase, BANK2);
  904. outb(tmp, iobase+FIR_IRDA_CR);
  905. // T1 -> SD/MODE:0 IRTX:0
  906. tmp &= ~0x09;
  907. tmp |= 0x02;
  908. outb(tmp, iobase+FIR_IRDA_CR);
  909. udelay(2);
  910. // T2 -> SD/MODE:1 IRTX:0
  911. tmp &= ~0x01;
  912. tmp |= 0x0a;
  913. outb(tmp, iobase+FIR_IRDA_CR);
  914. udelay(2);
  915. // T3 -> SD/MODE:1 IRTX:1
  916. tmp |= 0x0b;
  917. outb(tmp, iobase+FIR_IRDA_CR);
  918. udelay(2);
  919. // T4 -> SD/MODE:0 IRTX:1
  920. tmp &= ~0x08;
  921. tmp |= 0x03;
  922. outb(tmp, iobase+FIR_IRDA_CR);
  923. udelay(2);
  924. // T5 -> SD/MODE:0 IRTX:0
  925. tmp &= ~0x09;
  926. tmp |= 0x02;
  927. outb(tmp, iobase+FIR_IRDA_CR);
  928. udelay(2);
  929. // reset -> Normal TX output Signal
  930. outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
  931. }
  932. else /* speed <=1152000 */
  933. {
  934. // __
  935. // SD/MODE __| |__
  936. //
  937. // IRTX ________
  938. // T1 T2 T3
  939. /* MIR 115200, 57600 */
  940. if (speed==1152000)
  941. {
  942. tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
  943. }
  944. else
  945. {
  946. tmp &=~0x80; //HDLC 0.576Mbps
  947. tmp |= 0x20; //HDLC=1,
  948. }
  949. tmp |= IRDA_CR_CRC; // CRC=1
  950. switch_bank(iobase, BANK2);
  951. outb(tmp, iobase+FIR_IRDA_CR);
  952. /* MIR 115200, 57600 */
  953. //switch_bank(iobase, BANK2);
  954. // T1 -> SD/MODE:0 IRTX:0
  955. tmp &= ~0x09;
  956. tmp |= 0x02;
  957. outb(tmp, iobase+FIR_IRDA_CR);
  958. udelay(2);
  959. // T2 -> SD/MODE:1 IRTX:0
  960. tmp &= ~0x01;
  961. tmp |= 0x0a;
  962. outb(tmp, iobase+FIR_IRDA_CR);
  963. // T3 -> SD/MODE:0 IRTX:0
  964. tmp &= ~0x09;
  965. tmp |= 0x02;
  966. outb(tmp, iobase+FIR_IRDA_CR);
  967. udelay(2);
  968. // reset -> Normal TX output Signal
  969. outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
  970. }
  971. }
  972. else if (dongle_id == 1) /* HP HDSL-3600 */
  973. {
  974. switch(speed)
  975. {
  976. case 4000000:
  977. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  978. break;
  979. case 1152000:
  980. tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
  981. break;
  982. case 576000:
  983. tmp &=~0x80; // HDLC 0.576Mbps
  984. tmp |= 0x20; // HDLC=1,
  985. break;
  986. }
  987. tmp |= IRDA_CR_CRC; // CRC=1
  988. switch_bank(iobase, BANK2);
  989. outb(tmp, iobase+FIR_IRDA_CR);
  990. }
  991. else /* HP HDSL-1100 */
  992. {
  993. if(speed <= 115200) /* SIR */
  994. {
  995. tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
  996. switch_bank(iobase, BANK2);
  997. outb(tmp, iobase+FIR_IRDA_CR);
  998. }
  999. else /* MIR FIR */
  1000. {
  1001. switch(speed)
  1002. {
  1003. case 4000000:
  1004. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  1005. break;
  1006. case 1152000:
  1007. tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
  1008. break;
  1009. case 576000:
  1010. tmp &=~0x80; // HDLC 0.576Mbps
  1011. tmp |= 0x20; // HDLC=1,
  1012. break;
  1013. }
  1014. tmp |= IRDA_CR_CRC; // CRC=1
  1015. tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
  1016. switch_bank(iobase, BANK2);
  1017. outb(tmp, iobase+FIR_IRDA_CR);
  1018. }
  1019. }
  1020. switch_bank(iobase, BANK0);
  1021. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1022. }
  1023. /*
  1024. * Function ali_ircc_sir_write (driver)
  1025. *
  1026. * Fill Tx FIFO with transmit data
  1027. *
  1028. */
  1029. static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
  1030. {
  1031. int actual = 0;
  1032. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1033. /* Tx FIFO should be empty! */
  1034. if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
  1035. IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __FUNCTION__ );
  1036. return 0;
  1037. }
  1038. /* Fill FIFO with current frame */
  1039. while ((fifo_size-- > 0) && (actual < len)) {
  1040. /* Transmit next byte */
  1041. outb(buf[actual], iobase+UART_TX);
  1042. actual++;
  1043. }
  1044. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1045. return actual;
  1046. }
  1047. /*
  1048. * Function ali_ircc_net_open (dev)
  1049. *
  1050. * Start the device
  1051. *
  1052. */
  1053. static int ali_ircc_net_open(struct net_device *dev)
  1054. {
  1055. struct ali_ircc_cb *self;
  1056. int iobase;
  1057. char hwname[32];
  1058. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1059. IRDA_ASSERT(dev != NULL, return -1;);
  1060. self = (struct ali_ircc_cb *) dev->priv;
  1061. IRDA_ASSERT(self != NULL, return 0;);
  1062. iobase = self->io.fir_base;
  1063. /* Request IRQ and install Interrupt Handler */
  1064. if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
  1065. {
  1066. IRDA_WARNING("%s, unable to allocate irq=%d\n",
  1067. ALI_IRCC_DRIVER_NAME,
  1068. self->io.irq);
  1069. return -EAGAIN;
  1070. }
  1071. /*
  1072. * Always allocate the DMA channel after the IRQ, and clean up on
  1073. * failure.
  1074. */
  1075. if (request_dma(self->io.dma, dev->name)) {
  1076. IRDA_WARNING("%s, unable to allocate dma=%d\n",
  1077. ALI_IRCC_DRIVER_NAME,
  1078. self->io.dma);
  1079. free_irq(self->io.irq, self);
  1080. return -EAGAIN;
  1081. }
  1082. /* Turn on interrups */
  1083. outb(UART_IER_RDI , iobase+UART_IER);
  1084. /* Ready to play! */
  1085. netif_start_queue(dev); //benjamin by irport
  1086. /* Give self a hardware name */
  1087. sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
  1088. /*
  1089. * Open new IrLAP layer instance, now that everything should be
  1090. * initialized properly
  1091. */
  1092. self->irlap = irlap_open(dev, &self->qos, hwname);
  1093. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1094. return 0;
  1095. }
  1096. /*
  1097. * Function ali_ircc_net_close (dev)
  1098. *
  1099. * Stop the device
  1100. *
  1101. */
  1102. static int ali_ircc_net_close(struct net_device *dev)
  1103. {
  1104. struct ali_ircc_cb *self;
  1105. //int iobase;
  1106. IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1107. IRDA_ASSERT(dev != NULL, return -1;);
  1108. self = (struct ali_ircc_cb *) dev->priv;
  1109. IRDA_ASSERT(self != NULL, return 0;);
  1110. /* Stop device */
  1111. netif_stop_queue(dev);
  1112. /* Stop and remove instance of IrLAP */
  1113. if (self->irlap)
  1114. irlap_close(self->irlap);
  1115. self->irlap = NULL;
  1116. disable_dma(self->io.dma);
  1117. /* Disable interrupts */
  1118. SetCOMInterrupts(self, FALSE);
  1119. free_irq(self->io.irq, dev);
  1120. free_dma(self->io.dma);
  1121. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1122. return 0;
  1123. }
  1124. /*
  1125. * Function ali_ircc_fir_hard_xmit (skb, dev)
  1126. *
  1127. * Transmit the frame
  1128. *
  1129. */
  1130. static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  1131. {
  1132. struct ali_ircc_cb *self;
  1133. unsigned long flags;
  1134. int iobase;
  1135. __u32 speed;
  1136. int mtt, diff;
  1137. IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1138. self = (struct ali_ircc_cb *) dev->priv;
  1139. iobase = self->io.fir_base;
  1140. netif_stop_queue(dev);
  1141. /* Make sure tests *& speed change are atomic */
  1142. spin_lock_irqsave(&self->lock, flags);
  1143. /* Note : you should make sure that speed changes are not going
  1144. * to corrupt any outgoing frame. Look at nsc-ircc for the gory
  1145. * details - Jean II */
  1146. /* Check if we need to change the speed */
  1147. speed = irda_get_next_speed(skb);
  1148. if ((speed != self->io.speed) && (speed != -1)) {
  1149. /* Check for empty frame */
  1150. if (!skb->len) {
  1151. ali_ircc_change_speed(self, speed);
  1152. dev->trans_start = jiffies;
  1153. spin_unlock_irqrestore(&self->lock, flags);
  1154. dev_kfree_skb(skb);
  1155. return 0;
  1156. } else
  1157. self->new_speed = speed;
  1158. }
  1159. /* Register and copy this frame to DMA memory */
  1160. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  1161. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  1162. self->tx_fifo.tail += skb->len;
  1163. self->stats.tx_bytes += skb->len;
  1164. memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
  1165. skb->len);
  1166. self->tx_fifo.len++;
  1167. self->tx_fifo.free++;
  1168. /* Start transmit only if there is currently no transmit going on */
  1169. if (self->tx_fifo.len == 1)
  1170. {
  1171. /* Check if we must wait the min turn time or not */
  1172. mtt = irda_get_mtt(skb);
  1173. if (mtt)
  1174. {
  1175. /* Check how much time we have used already */
  1176. do_gettimeofday(&self->now);
  1177. diff = self->now.tv_usec - self->stamp.tv_usec;
  1178. /* self->stamp is set from ali_ircc_dma_receive_complete() */
  1179. IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __FUNCTION__ , diff);
  1180. if (diff < 0)
  1181. diff += 1000000;
  1182. /* Check if the mtt is larger than the time we have
  1183. * already used by all the protocol processing
  1184. */
  1185. if (mtt > diff)
  1186. {
  1187. mtt -= diff;
  1188. /*
  1189. * Use timer if delay larger than 1000 us, and
  1190. * use udelay for smaller values which should
  1191. * be acceptable
  1192. */
  1193. if (mtt > 500)
  1194. {
  1195. /* Adjust for timer resolution */
  1196. mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
  1197. IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __FUNCTION__ , mtt);
  1198. /* Setup timer */
  1199. if (mtt == 1) /* 500 us */
  1200. {
  1201. switch_bank(iobase, BANK1);
  1202. outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
  1203. }
  1204. else if (mtt == 2) /* 1 ms */
  1205. {
  1206. switch_bank(iobase, BANK1);
  1207. outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
  1208. }
  1209. else /* > 2ms -> 4ms */
  1210. {
  1211. switch_bank(iobase, BANK1);
  1212. outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
  1213. }
  1214. /* Start timer */
  1215. outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
  1216. self->io.direction = IO_XMIT;
  1217. /* Enable timer interrupt */
  1218. self->ier = IER_TIMER;
  1219. SetCOMInterrupts(self, TRUE);
  1220. /* Timer will take care of the rest */
  1221. goto out;
  1222. }
  1223. else
  1224. udelay(mtt);
  1225. } // if (if (mtt > diff)
  1226. }// if (mtt)
  1227. /* Enable EOM interrupt */
  1228. self->ier = IER_EOM;
  1229. SetCOMInterrupts(self, TRUE);
  1230. /* Transmit frame */
  1231. ali_ircc_dma_xmit(self);
  1232. } // if (self->tx_fifo.len == 1)
  1233. out:
  1234. /* Not busy transmitting anymore if window is not full */
  1235. if (self->tx_fifo.free < MAX_TX_WINDOW)
  1236. netif_wake_queue(self->netdev);
  1237. /* Restore bank register */
  1238. switch_bank(iobase, BANK0);
  1239. dev->trans_start = jiffies;
  1240. spin_unlock_irqrestore(&self->lock, flags);
  1241. dev_kfree_skb(skb);
  1242. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1243. return 0;
  1244. }
  1245. static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
  1246. {
  1247. int iobase, tmp;
  1248. unsigned char FIFO_OPTI, Hi, Lo;
  1249. IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1250. iobase = self->io.fir_base;
  1251. /* FIFO threshold , this method comes from NDIS5 code */
  1252. if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
  1253. FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
  1254. else
  1255. FIFO_OPTI = TX_FIFO_Threshold;
  1256. /* Disable DMA */
  1257. switch_bank(iobase, BANK1);
  1258. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1259. self->io.direction = IO_XMIT;
  1260. irda_setup_dma(self->io.dma,
  1261. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  1262. self->tx_buff.head) + self->tx_buff_dma,
  1263. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  1264. DMA_TX_MODE);
  1265. /* Reset Tx FIFO */
  1266. switch_bank(iobase, BANK0);
  1267. outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
  1268. /* Set Tx FIFO threshold */
  1269. if (self->fifo_opti_buf!=FIFO_OPTI)
  1270. {
  1271. switch_bank(iobase, BANK1);
  1272. outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
  1273. self->fifo_opti_buf=FIFO_OPTI;
  1274. }
  1275. /* Set Tx DMA threshold */
  1276. switch_bank(iobase, BANK1);
  1277. outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
  1278. /* Set max Tx frame size */
  1279. Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
  1280. Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
  1281. switch_bank(iobase, BANK2);
  1282. outb(Hi, iobase+FIR_TX_DSR_HI);
  1283. outb(Lo, iobase+FIR_TX_DSR_LO);
  1284. /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
  1285. switch_bank(iobase, BANK0);
  1286. tmp = inb(iobase+FIR_LCR_B);
  1287. tmp &= ~0x20; // Disable SIP
  1288. outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
  1289. IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
  1290. outb(0, iobase+FIR_LSR);
  1291. /* Enable DMA and Burst Mode */
  1292. switch_bank(iobase, BANK1);
  1293. outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
  1294. switch_bank(iobase, BANK0);
  1295. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1296. }
  1297. static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
  1298. {
  1299. int iobase;
  1300. int ret = TRUE;
  1301. IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1302. iobase = self->io.fir_base;
  1303. /* Disable DMA */
  1304. switch_bank(iobase, BANK1);
  1305. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1306. /* Check for underrun! */
  1307. switch_bank(iobase, BANK0);
  1308. if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
  1309. {
  1310. IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __FUNCTION__);
  1311. self->stats.tx_errors++;
  1312. self->stats.tx_fifo_errors++;
  1313. }
  1314. else
  1315. {
  1316. self->stats.tx_packets++;
  1317. }
  1318. /* Check if we need to change the speed */
  1319. if (self->new_speed)
  1320. {
  1321. ali_ircc_change_speed(self, self->new_speed);
  1322. self->new_speed = 0;
  1323. }
  1324. /* Finished with this frame, so prepare for next */
  1325. self->tx_fifo.ptr++;
  1326. self->tx_fifo.len--;
  1327. /* Any frames to be sent back-to-back? */
  1328. if (self->tx_fifo.len)
  1329. {
  1330. ali_ircc_dma_xmit(self);
  1331. /* Not finished yet! */
  1332. ret = FALSE;
  1333. }
  1334. else
  1335. { /* Reset Tx FIFO info */
  1336. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1337. self->tx_fifo.tail = self->tx_buff.head;
  1338. }
  1339. /* Make sure we have room for more frames */
  1340. if (self->tx_fifo.free < MAX_TX_WINDOW) {
  1341. /* Not busy transmitting anymore */
  1342. /* Tell the network layer, that we can accept more frames */
  1343. netif_wake_queue(self->netdev);
  1344. }
  1345. switch_bank(iobase, BANK0);
  1346. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1347. return ret;
  1348. }
  1349. /*
  1350. * Function ali_ircc_dma_receive (self)
  1351. *
  1352. * Get ready for receiving a frame. The device will initiate a DMA
  1353. * if it starts to receive a frame.
  1354. *
  1355. */
  1356. static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
  1357. {
  1358. int iobase, tmp;
  1359. IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1360. iobase = self->io.fir_base;
  1361. /* Reset Tx FIFO info */
  1362. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1363. self->tx_fifo.tail = self->tx_buff.head;
  1364. /* Disable DMA */
  1365. switch_bank(iobase, BANK1);
  1366. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1367. /* Reset Message Count */
  1368. switch_bank(iobase, BANK0);
  1369. outb(0x07, iobase+FIR_LSR);
  1370. self->rcvFramesOverflow = FALSE;
  1371. self->LineStatus = inb(iobase+FIR_LSR) ;
  1372. /* Reset Rx FIFO info */
  1373. self->io.direction = IO_RECV;
  1374. self->rx_buff.data = self->rx_buff.head;
  1375. /* Reset Rx FIFO */
  1376. // switch_bank(iobase, BANK0);
  1377. outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
  1378. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  1379. self->st_fifo.tail = self->st_fifo.head = 0;
  1380. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  1381. DMA_RX_MODE);
  1382. /* Set Receive Mode,Brick Wall */
  1383. //switch_bank(iobase, BANK0);
  1384. tmp = inb(iobase+FIR_LCR_B);
  1385. outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
  1386. IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
  1387. /* Set Rx Threshold */
  1388. switch_bank(iobase, BANK1);
  1389. outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
  1390. outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
  1391. /* Enable DMA and Burst Mode */
  1392. // switch_bank(iobase, BANK1);
  1393. outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
  1394. switch_bank(iobase, BANK0);
  1395. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1396. return 0;
  1397. }
  1398. static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
  1399. {
  1400. struct st_fifo *st_fifo;
  1401. struct sk_buff *skb;
  1402. __u8 status, MessageCount;
  1403. int len, i, iobase, val;
  1404. IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1405. st_fifo = &self->st_fifo;
  1406. iobase = self->io.fir_base;
  1407. switch_bank(iobase, BANK0);
  1408. MessageCount = inb(iobase+ FIR_LSR)&0x07;
  1409. if (MessageCount > 0)
  1410. IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __FUNCTION__ , MessageCount);
  1411. for (i=0; i<=MessageCount; i++)
  1412. {
  1413. /* Bank 0 */
  1414. switch_bank(iobase, BANK0);
  1415. status = inb(iobase+FIR_LSR);
  1416. switch_bank(iobase, BANK2);
  1417. len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
  1418. len = len << 8;
  1419. len |= inb(iobase+FIR_RX_DSR_LO);
  1420. IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __FUNCTION__ , len);
  1421. IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __FUNCTION__ , status);
  1422. if (st_fifo->tail >= MAX_RX_WINDOW) {
  1423. IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__ );
  1424. continue;
  1425. }
  1426. st_fifo->entries[st_fifo->tail].status = status;
  1427. st_fifo->entries[st_fifo->tail].len = len;
  1428. st_fifo->pending_bytes += len;
  1429. st_fifo->tail++;
  1430. st_fifo->len++;
  1431. }
  1432. for (i=0; i<=MessageCount; i++)
  1433. {
  1434. /* Get first entry */
  1435. status = st_fifo->entries[st_fifo->head].status;
  1436. len = st_fifo->entries[st_fifo->head].len;
  1437. st_fifo->pending_bytes -= len;
  1438. st_fifo->head++;
  1439. st_fifo->len--;
  1440. /* Check for errors */
  1441. if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
  1442. {
  1443. IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __FUNCTION__ );
  1444. /* Skip frame */
  1445. self->stats.rx_errors++;
  1446. self->rx_buff.data += len;
  1447. if (status & LSR_FIFO_UR)
  1448. {
  1449. self->stats.rx_frame_errors++;
  1450. IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __FUNCTION__ );
  1451. }
  1452. if (status & LSR_FRAME_ERROR)
  1453. {
  1454. self->stats.rx_frame_errors++;
  1455. IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __FUNCTION__ );
  1456. }
  1457. if (status & LSR_CRC_ERROR)
  1458. {
  1459. self->stats.rx_crc_errors++;
  1460. IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __FUNCTION__ );
  1461. }
  1462. if(self->rcvFramesOverflow)
  1463. {
  1464. self->stats.rx_frame_errors++;
  1465. IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __FUNCTION__ );
  1466. }
  1467. if(len == 0)
  1468. {
  1469. self->stats.rx_frame_errors++;
  1470. IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __FUNCTION__ );
  1471. }
  1472. }
  1473. else
  1474. {
  1475. if (st_fifo->pending_bytes < 32)
  1476. {
  1477. switch_bank(iobase, BANK0);
  1478. val = inb(iobase+FIR_BSR);
  1479. if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
  1480. {
  1481. IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __FUNCTION__ );
  1482. /* Put this entry back in fifo */
  1483. st_fifo->head--;
  1484. st_fifo->len++;
  1485. st_fifo->pending_bytes += len;
  1486. st_fifo->entries[st_fifo->head].status = status;
  1487. st_fifo->entries[st_fifo->head].len = len;
  1488. /*
  1489. * DMA not finished yet, so try again
  1490. * later, set timer value, resolution
  1491. * 500 us
  1492. */
  1493. switch_bank(iobase, BANK1);
  1494. outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
  1495. /* Enable Timer */
  1496. outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
  1497. return FALSE; /* I'll be back! */
  1498. }
  1499. }
  1500. /*
  1501. * Remember the time we received this frame, so we can
  1502. * reduce the min turn time a bit since we will know
  1503. * how much time we have used for protocol processing
  1504. */
  1505. do_gettimeofday(&self->stamp);
  1506. skb = dev_alloc_skb(len+1);
  1507. if (skb == NULL)
  1508. {
  1509. IRDA_WARNING("%s(), memory squeeze, "
  1510. "dropping frame.\n",
  1511. __FUNCTION__);
  1512. self->stats.rx_dropped++;
  1513. return FALSE;
  1514. }
  1515. /* Make sure IP header gets aligned */
  1516. skb_reserve(skb, 1);
  1517. /* Copy frame without CRC, CRC is removed by hardware*/
  1518. skb_put(skb, len);
  1519. memcpy(skb->data, self->rx_buff.data, len);
  1520. /* Move to next frame */
  1521. self->rx_buff.data += len;
  1522. self->stats.rx_bytes += len;
  1523. self->stats.rx_packets++;
  1524. skb->dev = self->netdev;
  1525. skb->mac.raw = skb->data;
  1526. skb->protocol = htons(ETH_P_IRDA);
  1527. netif_rx(skb);
  1528. self->netdev->last_rx = jiffies;
  1529. }
  1530. }
  1531. switch_bank(iobase, BANK0);
  1532. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1533. return TRUE;
  1534. }
  1535. /*
  1536. * Function ali_ircc_sir_hard_xmit (skb, dev)
  1537. *
  1538. * Transmit the frame!
  1539. *
  1540. */
  1541. static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  1542. {
  1543. struct ali_ircc_cb *self;
  1544. unsigned long flags;
  1545. int iobase;
  1546. __u32 speed;
  1547. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1548. IRDA_ASSERT(dev != NULL, return 0;);
  1549. self = (struct ali_ircc_cb *) dev->priv;
  1550. IRDA_ASSERT(self != NULL, return 0;);
  1551. iobase = self->io.sir_base;
  1552. netif_stop_queue(dev);
  1553. /* Make sure tests *& speed change are atomic */
  1554. spin_lock_irqsave(&self->lock, flags);
  1555. /* Note : you should make sure that speed changes are not going
  1556. * to corrupt any outgoing frame. Look at nsc-ircc for the gory
  1557. * details - Jean II */
  1558. /* Check if we need to change the speed */
  1559. speed = irda_get_next_speed(skb);
  1560. if ((speed != self->io.speed) && (speed != -1)) {
  1561. /* Check for empty frame */
  1562. if (!skb->len) {
  1563. ali_ircc_change_speed(self, speed);
  1564. dev->trans_start = jiffies;
  1565. spin_unlock_irqrestore(&self->lock, flags);
  1566. dev_kfree_skb(skb);
  1567. return 0;
  1568. } else
  1569. self->new_speed = speed;
  1570. }
  1571. /* Init tx buffer */
  1572. self->tx_buff.data = self->tx_buff.head;
  1573. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  1574. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  1575. self->tx_buff.truesize);
  1576. self->stats.tx_bytes += self->tx_buff.len;
  1577. /* Turn on transmit finished interrupt. Will fire immediately! */
  1578. outb(UART_IER_THRI, iobase+UART_IER);
  1579. dev->trans_start = jiffies;
  1580. spin_unlock_irqrestore(&self->lock, flags);
  1581. dev_kfree_skb(skb);
  1582. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1583. return 0;
  1584. }
  1585. /*
  1586. * Function ali_ircc_net_ioctl (dev, rq, cmd)
  1587. *
  1588. * Process IOCTL commands for this device
  1589. *
  1590. */
  1591. static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1592. {
  1593. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1594. struct ali_ircc_cb *self;
  1595. unsigned long flags;
  1596. int ret = 0;
  1597. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1598. IRDA_ASSERT(dev != NULL, return -1;);
  1599. self = dev->priv;
  1600. IRDA_ASSERT(self != NULL, return -1;);
  1601. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
  1602. switch (cmd) {
  1603. case SIOCSBANDWIDTH: /* Set bandwidth */
  1604. IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __FUNCTION__ );
  1605. /*
  1606. * This function will also be used by IrLAP to change the
  1607. * speed, so we still must allow for speed change within
  1608. * interrupt context.
  1609. */
  1610. if (!in_interrupt() && !capable(CAP_NET_ADMIN))
  1611. return -EPERM;
  1612. spin_lock_irqsave(&self->lock, flags);
  1613. ali_ircc_change_speed(self, irq->ifr_baudrate);
  1614. spin_unlock_irqrestore(&self->lock, flags);
  1615. break;
  1616. case SIOCSMEDIABUSY: /* Set media busy */
  1617. IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __FUNCTION__ );
  1618. if (!capable(CAP_NET_ADMIN))
  1619. return -EPERM;
  1620. irda_device_set_media_busy(self->netdev, TRUE);
  1621. break;
  1622. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1623. IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __FUNCTION__ );
  1624. /* This is protected */
  1625. irq->ifr_receiving = ali_ircc_is_receiving(self);
  1626. break;
  1627. default:
  1628. ret = -EOPNOTSUPP;
  1629. }
  1630. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1631. return ret;
  1632. }
  1633. /*
  1634. * Function ali_ircc_is_receiving (self)
  1635. *
  1636. * Return TRUE is we are currently receiving a frame
  1637. *
  1638. */
  1639. static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
  1640. {
  1641. unsigned long flags;
  1642. int status = FALSE;
  1643. int iobase;
  1644. IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
  1645. IRDA_ASSERT(self != NULL, return FALSE;);
  1646. spin_lock_irqsave(&self->lock, flags);
  1647. if (self->io.speed > 115200)
  1648. {
  1649. iobase = self->io.fir_base;
  1650. switch_bank(iobase, BANK1);
  1651. if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
  1652. {
  1653. /* We are receiving something */
  1654. IRDA_DEBUG(1, "%s(), We are receiving something\n", __FUNCTION__ );
  1655. status = TRUE;
  1656. }
  1657. switch_bank(iobase, BANK0);
  1658. }
  1659. else
  1660. {
  1661. status = (self->rx_buff.state != OUTSIDE_FRAME);
  1662. }
  1663. spin_unlock_irqrestore(&self->lock, flags);
  1664. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1665. return status;
  1666. }
  1667. static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev)
  1668. {
  1669. struct ali_ircc_cb *self = (struct ali_ircc_cb *) dev->priv;
  1670. IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1671. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1672. return &self->stats;
  1673. }
  1674. static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
  1675. {
  1676. struct ali_ircc_cb *self = platform_get_drvdata(dev);
  1677. IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
  1678. if (self->io.suspended)
  1679. return 0;
  1680. ali_ircc_net_close(self->netdev);
  1681. self->io.suspended = 1;
  1682. return 0;
  1683. }
  1684. static int ali_ircc_resume(struct platform_device *dev)
  1685. {
  1686. struct ali_ircc_cb *self = platform_get_drvdata(dev);
  1687. if (!self->io.suspended)
  1688. return 0;
  1689. ali_ircc_net_open(self->netdev);
  1690. IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
  1691. self->io.suspended = 0;
  1692. return 0;
  1693. }
  1694. /* ALi Chip Function */
  1695. static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
  1696. {
  1697. unsigned char newMask;
  1698. int iobase = self->io.fir_base; /* or sir_base */
  1699. IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __FUNCTION__ , enable);
  1700. /* Enable the interrupt which we wish to */
  1701. if (enable){
  1702. if (self->io.direction == IO_XMIT)
  1703. {
  1704. if (self->io.speed > 115200) /* FIR, MIR */
  1705. {
  1706. newMask = self->ier;
  1707. }
  1708. else /* SIR */
  1709. {
  1710. newMask = UART_IER_THRI | UART_IER_RDI;
  1711. }
  1712. }
  1713. else {
  1714. if (self->io.speed > 115200) /* FIR, MIR */
  1715. {
  1716. newMask = self->ier;
  1717. }
  1718. else /* SIR */
  1719. {
  1720. newMask = UART_IER_RDI;
  1721. }
  1722. }
  1723. }
  1724. else /* Disable all the interrupts */
  1725. {
  1726. newMask = 0x00;
  1727. }
  1728. //SIR and FIR has different registers
  1729. if (self->io.speed > 115200)
  1730. {
  1731. switch_bank(iobase, BANK0);
  1732. outb(newMask, iobase+FIR_IER);
  1733. }
  1734. else
  1735. outb(newMask, iobase+UART_IER);
  1736. IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1737. }
  1738. static void SIR2FIR(int iobase)
  1739. {
  1740. //unsigned char tmp;
  1741. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1742. /* Already protected (change_speed() or setup()), no need to lock.
  1743. * Jean II */
  1744. outb(0x28, iobase+UART_MCR);
  1745. outb(0x68, iobase+UART_MCR);
  1746. outb(0x88, iobase+UART_MCR);
  1747. outb(0x60, iobase+FIR_MCR); /* Master Reset */
  1748. outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
  1749. //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
  1750. //tmp |= 0x20;
  1751. //outb(tmp, iobase+FIR_LCR_B);
  1752. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1753. }
  1754. static void FIR2SIR(int iobase)
  1755. {
  1756. unsigned char val;
  1757. IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
  1758. /* Already protected (change_speed() or setup()), no need to lock.
  1759. * Jean II */
  1760. outb(0x20, iobase+FIR_MCR); /* IRQ to low */
  1761. outb(0x00, iobase+UART_IER);
  1762. outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
  1763. outb(0x00, iobase+UART_FCR);
  1764. outb(0x07, iobase+UART_FCR);
  1765. val = inb(iobase+UART_RX);
  1766. val = inb(iobase+UART_LSR);
  1767. val = inb(iobase+UART_MSR);
  1768. IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
  1769. }
  1770. MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
  1771. MODULE_DESCRIPTION("ALi FIR Controller Driver");
  1772. MODULE_LICENSE("GPL");
  1773. module_param_array(io, int, NULL, 0);
  1774. MODULE_PARM_DESC(io, "Base I/O addresses");
  1775. module_param_array(irq, int, NULL, 0);
  1776. MODULE_PARM_DESC(irq, "IRQ lines");
  1777. module_param_array(dma, int, NULL, 0);
  1778. MODULE_PARM_DESC(dma, "DMA channels");
  1779. module_init(ali_ircc_init);
  1780. module_exit(ali_ircc_cleanup);