ali-ircc.c 56 KB

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