ali-ircc.c 57 KB

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