ali-ircc.c 57 KB

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