ali-ircc.c 51 KB

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