ali-ircc.c 57 KB

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