nsc-ircc.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  1. /*********************************************************************
  2. *
  3. * Filename: nsc-ircc.c
  4. * Version: 1.0
  5. * Description: Driver for the NSC PC'108 and PC'338 IrDA chipsets
  6. * Status: Stable.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Nov 7 21:43:15 1998
  9. * Modified at: Wed Mar 1 11:29:34 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
  13. * Copyright (c) 1998 Lichen Wang, <lwang@actisys.com>
  14. * Copyright (c) 1998 Actisys Corp., www.actisys.com
  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. * Neither Dag Brattli nor University of Tromsø admit liability nor
  23. * provide warranty for any of this software. This material is
  24. * provided "AS-IS" and at no charge.
  25. *
  26. * Notice that all functions that needs to access the chip in _any_
  27. * way, must save BSR register on entry, and restore it on exit.
  28. * It is _very_ important to follow this policy!
  29. *
  30. * __u8 bank;
  31. *
  32. * bank = inb(iobase+BSR);
  33. *
  34. * do_your_stuff_here();
  35. *
  36. * outb(bank, iobase+BSR);
  37. *
  38. * If you find bugs in this file, its very likely that the same bug
  39. * will also be in w83977af_ir.c since the implementations are quite
  40. * similar.
  41. *
  42. ********************************************************************/
  43. #include <linux/module.h>
  44. #include <linux/kernel.h>
  45. #include <linux/types.h>
  46. #include <linux/skbuff.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/ioport.h>
  49. #include <linux/delay.h>
  50. #include <linux/slab.h>
  51. #include <linux/init.h>
  52. #include <linux/rtnetlink.h>
  53. #include <linux/dma-mapping.h>
  54. #include <asm/io.h>
  55. #include <asm/dma.h>
  56. #include <asm/byteorder.h>
  57. #include <linux/pm.h>
  58. #include <net/irda/wrapper.h>
  59. #include <net/irda/irda.h>
  60. #include <net/irda/irda_device.h>
  61. #include "nsc-ircc.h"
  62. #define CHIP_IO_EXTENT 8
  63. #define BROKEN_DONGLE_ID
  64. static char *driver_name = "nsc-ircc";
  65. /* Module parameters */
  66. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  67. static int dongle_id;
  68. /* Use BIOS settions by default, but user may supply module parameters */
  69. static unsigned int io[] = { ~0, ~0, ~0, ~0 };
  70. static unsigned int irq[] = { 0, 0, 0, 0, 0 };
  71. static unsigned int dma[] = { 0, 0, 0, 0, 0 };
  72. static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
  73. static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
  74. static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
  75. static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
  76. static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
  77. static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
  78. /* These are the known NSC chips */
  79. static nsc_chip_t chips[] = {
  80. /* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
  81. { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
  82. nsc_ircc_probe_108, nsc_ircc_init_108 },
  83. { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
  84. nsc_ircc_probe_338, nsc_ircc_init_338 },
  85. /* Contributed by Steffen Pingel - IBM X40 */
  86. { "PC8738x", { 0x164e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
  87. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  88. /* Contributed by Jan Frey - IBM A30/A31 */
  89. { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
  90. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  91. { NULL }
  92. };
  93. /* Max 4 instances for now */
  94. static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
  95. static char *dongle_types[] = {
  96. "Differential serial interface",
  97. "Differential serial interface",
  98. "Reserved",
  99. "Reserved",
  100. "Sharp RY5HD01",
  101. "Reserved",
  102. "Single-ended serial interface",
  103. "Consumer-IR only",
  104. "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
  105. "IBM31T1100 or Temic TFDS6000/TFDS6500",
  106. "Reserved",
  107. "Reserved",
  108. "HP HSDL-1100/HSDL-2100",
  109. "HP HSDL-1100/HSDL-2100",
  110. "Supports SIR Mode only",
  111. "No dongle connected",
  112. };
  113. /* Some prototypes */
  114. static int nsc_ircc_open(int i, chipio_t *info);
  115. static int nsc_ircc_close(struct nsc_ircc_cb *self);
  116. static int nsc_ircc_setup(chipio_t *info);
  117. static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
  118. static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self);
  119. static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
  120. static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev);
  121. static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev);
  122. static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
  123. static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
  124. static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
  125. static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
  126. static int nsc_ircc_read_dongle_id (int iobase);
  127. static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
  128. static int nsc_ircc_net_open(struct net_device *dev);
  129. static int nsc_ircc_net_close(struct net_device *dev);
  130. static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  131. static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev);
  132. static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
  133. /*
  134. * Function nsc_ircc_init ()
  135. *
  136. * Initialize chip. Just try to find out how many chips we are dealing with
  137. * and where they are
  138. */
  139. static int __init nsc_ircc_init(void)
  140. {
  141. chipio_t info;
  142. nsc_chip_t *chip;
  143. int ret = -ENODEV;
  144. int cfg_base;
  145. int cfg, id;
  146. int reg;
  147. int i = 0;
  148. /* Probe for all the NSC chipsets we know about */
  149. for (chip=chips; chip->name ; chip++) {
  150. IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__,
  151. chip->name);
  152. /* Try all config registers for this chip */
  153. for (cfg=0; cfg<3; cfg++) {
  154. cfg_base = chip->cfg[cfg];
  155. if (!cfg_base)
  156. continue;
  157. memset(&info, 0, sizeof(chipio_t));
  158. info.cfg_base = cfg_base;
  159. info.fir_base = io[i];
  160. info.dma = dma[i];
  161. info.irq = irq[i];
  162. /* Read index register */
  163. reg = inb(cfg_base);
  164. if (reg == 0xff) {
  165. IRDA_DEBUG(2, "%s() no chip at 0x%03x\n", __FUNCTION__, cfg_base);
  166. continue;
  167. }
  168. /* Read chip identification register */
  169. outb(chip->cid_index, cfg_base);
  170. id = inb(cfg_base+1);
  171. if ((id & chip->cid_mask) == chip->cid_value) {
  172. IRDA_DEBUG(2, "%s() Found %s chip, revision=%d\n",
  173. __FUNCTION__, chip->name, id & ~chip->cid_mask);
  174. /*
  175. * If the user supplies the base address, then
  176. * we init the chip, if not we probe the values
  177. * set by the BIOS
  178. */
  179. if (io[i] < 0x2000) {
  180. chip->init(chip, &info);
  181. } else
  182. chip->probe(chip, &info);
  183. if (nsc_ircc_open(i, &info) == 0)
  184. ret = 0;
  185. i++;
  186. } else {
  187. IRDA_DEBUG(2, "%s(), Wrong chip id=0x%02x\n", __FUNCTION__, id);
  188. }
  189. }
  190. }
  191. return ret;
  192. }
  193. /*
  194. * Function nsc_ircc_cleanup ()
  195. *
  196. * Close all configured chips
  197. *
  198. */
  199. static void __exit nsc_ircc_cleanup(void)
  200. {
  201. int i;
  202. pm_unregister_all(nsc_ircc_pmproc);
  203. for (i=0; i < 4; i++) {
  204. if (dev_self[i])
  205. nsc_ircc_close(dev_self[i]);
  206. }
  207. }
  208. /*
  209. * Function nsc_ircc_open (iobase, irq)
  210. *
  211. * Open driver instance
  212. *
  213. */
  214. static int __init nsc_ircc_open(int i, chipio_t *info)
  215. {
  216. struct net_device *dev;
  217. struct nsc_ircc_cb *self;
  218. struct pm_dev *pmdev;
  219. void *ret;
  220. int err;
  221. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  222. IRDA_MESSAGE("%s, Found chip at base=0x%03x\n", driver_name,
  223. info->cfg_base);
  224. if ((nsc_ircc_setup(info)) == -1)
  225. return -1;
  226. IRDA_MESSAGE("%s, driver loaded (Dag Brattli)\n", driver_name);
  227. dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
  228. if (dev == NULL) {
  229. IRDA_ERROR("%s(), can't allocate memory for "
  230. "control block!\n", __FUNCTION__);
  231. return -ENOMEM;
  232. }
  233. self = dev->priv;
  234. self->netdev = dev;
  235. spin_lock_init(&self->lock);
  236. /* Need to store self somewhere */
  237. dev_self[i] = self;
  238. self->index = i;
  239. /* Initialize IO */
  240. self->io.cfg_base = info->cfg_base;
  241. self->io.fir_base = info->fir_base;
  242. self->io.irq = info->irq;
  243. self->io.fir_ext = CHIP_IO_EXTENT;
  244. self->io.dma = info->dma;
  245. self->io.fifo_size = 32;
  246. /* Reserve the ioports that we need */
  247. ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
  248. if (!ret) {
  249. IRDA_WARNING("%s(), can't get iobase of 0x%03x\n",
  250. __FUNCTION__, self->io.fir_base);
  251. err = -ENODEV;
  252. goto out1;
  253. }
  254. /* Initialize QoS for this device */
  255. irda_init_max_qos_capabilies(&self->qos);
  256. /* The only value we must override it the baudrate */
  257. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  258. IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
  259. self->qos.min_turn_time.bits = qos_mtt_bits;
  260. irda_qos_bits_to_value(&self->qos);
  261. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  262. self->rx_buff.truesize = 14384;
  263. self->tx_buff.truesize = 14384;
  264. /* Allocate memory if needed */
  265. self->rx_buff.head =
  266. dma_alloc_coherent(NULL, self->rx_buff.truesize,
  267. &self->rx_buff_dma, GFP_KERNEL);
  268. if (self->rx_buff.head == NULL) {
  269. err = -ENOMEM;
  270. goto out2;
  271. }
  272. memset(self->rx_buff.head, 0, self->rx_buff.truesize);
  273. self->tx_buff.head =
  274. dma_alloc_coherent(NULL, self->tx_buff.truesize,
  275. &self->tx_buff_dma, GFP_KERNEL);
  276. if (self->tx_buff.head == NULL) {
  277. err = -ENOMEM;
  278. goto out3;
  279. }
  280. memset(self->tx_buff.head, 0, self->tx_buff.truesize);
  281. self->rx_buff.in_frame = FALSE;
  282. self->rx_buff.state = OUTSIDE_FRAME;
  283. self->tx_buff.data = self->tx_buff.head;
  284. self->rx_buff.data = self->rx_buff.head;
  285. /* Reset Tx queue info */
  286. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  287. self->tx_fifo.tail = self->tx_buff.head;
  288. /* Override the network functions we need to use */
  289. SET_MODULE_OWNER(dev);
  290. dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
  291. dev->open = nsc_ircc_net_open;
  292. dev->stop = nsc_ircc_net_close;
  293. dev->do_ioctl = nsc_ircc_net_ioctl;
  294. dev->get_stats = nsc_ircc_net_get_stats;
  295. err = register_netdev(dev);
  296. if (err) {
  297. IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
  298. goto out4;
  299. }
  300. IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
  301. /* Check if user has supplied a valid dongle id or not */
  302. if ((dongle_id <= 0) ||
  303. (dongle_id >= (sizeof(dongle_types) / sizeof(dongle_types[0]))) ) {
  304. dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
  305. IRDA_MESSAGE("%s, Found dongle: %s\n", driver_name,
  306. dongle_types[dongle_id]);
  307. } else {
  308. IRDA_MESSAGE("%s, Using dongle: %s\n", driver_name,
  309. dongle_types[dongle_id]);
  310. }
  311. self->io.dongle_id = dongle_id;
  312. nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
  313. pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, nsc_ircc_pmproc);
  314. if (pmdev)
  315. pmdev->data = self;
  316. return 0;
  317. out4:
  318. dma_free_coherent(NULL, self->tx_buff.truesize,
  319. self->tx_buff.head, self->tx_buff_dma);
  320. out3:
  321. dma_free_coherent(NULL, self->rx_buff.truesize,
  322. self->rx_buff.head, self->rx_buff_dma);
  323. out2:
  324. release_region(self->io.fir_base, self->io.fir_ext);
  325. out1:
  326. free_netdev(dev);
  327. dev_self[i] = NULL;
  328. return err;
  329. }
  330. /*
  331. * Function nsc_ircc_close (self)
  332. *
  333. * Close driver instance
  334. *
  335. */
  336. static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
  337. {
  338. int iobase;
  339. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  340. IRDA_ASSERT(self != NULL, return -1;);
  341. iobase = self->io.fir_base;
  342. /* Remove netdevice */
  343. unregister_netdev(self->netdev);
  344. /* Release the PORT that this driver is using */
  345. IRDA_DEBUG(4, "%s(), Releasing Region %03x\n",
  346. __FUNCTION__, self->io.fir_base);
  347. release_region(self->io.fir_base, self->io.fir_ext);
  348. if (self->tx_buff.head)
  349. dma_free_coherent(NULL, self->tx_buff.truesize,
  350. self->tx_buff.head, self->tx_buff_dma);
  351. if (self->rx_buff.head)
  352. dma_free_coherent(NULL, self->rx_buff.truesize,
  353. self->rx_buff.head, self->rx_buff_dma);
  354. dev_self[self->index] = NULL;
  355. free_netdev(self->netdev);
  356. return 0;
  357. }
  358. /*
  359. * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
  360. *
  361. * Initialize the NSC '108 chip
  362. *
  363. */
  364. static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
  365. {
  366. int cfg_base = info->cfg_base;
  367. __u8 temp=0;
  368. outb(2, cfg_base); /* Mode Control Register (MCTL) */
  369. outb(0x00, cfg_base+1); /* Disable device */
  370. /* Base Address and Interrupt Control Register (BAIC) */
  371. outb(CFG_108_BAIC, cfg_base);
  372. switch (info->fir_base) {
  373. case 0x3e8: outb(0x14, cfg_base+1); break;
  374. case 0x2e8: outb(0x15, cfg_base+1); break;
  375. case 0x3f8: outb(0x16, cfg_base+1); break;
  376. case 0x2f8: outb(0x17, cfg_base+1); break;
  377. default: IRDA_ERROR("%s(), invalid base_address", __FUNCTION__);
  378. }
  379. /* Control Signal Routing Register (CSRT) */
  380. switch (info->irq) {
  381. case 3: temp = 0x01; break;
  382. case 4: temp = 0x02; break;
  383. case 5: temp = 0x03; break;
  384. case 7: temp = 0x04; break;
  385. case 9: temp = 0x05; break;
  386. case 11: temp = 0x06; break;
  387. case 15: temp = 0x07; break;
  388. default: IRDA_ERROR("%s(), invalid irq", __FUNCTION__);
  389. }
  390. outb(CFG_108_CSRT, cfg_base);
  391. switch (info->dma) {
  392. case 0: outb(0x08+temp, cfg_base+1); break;
  393. case 1: outb(0x10+temp, cfg_base+1); break;
  394. case 3: outb(0x18+temp, cfg_base+1); break;
  395. default: IRDA_ERROR("%s(), invalid dma", __FUNCTION__);
  396. }
  397. outb(CFG_108_MCTL, cfg_base); /* Mode Control Register (MCTL) */
  398. outb(0x03, cfg_base+1); /* Enable device */
  399. return 0;
  400. }
  401. /*
  402. * Function nsc_ircc_probe_108 (chip, info)
  403. *
  404. *
  405. *
  406. */
  407. static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info)
  408. {
  409. int cfg_base = info->cfg_base;
  410. int reg;
  411. /* Read address and interrupt control register (BAIC) */
  412. outb(CFG_108_BAIC, cfg_base);
  413. reg = inb(cfg_base+1);
  414. switch (reg & 0x03) {
  415. case 0:
  416. info->fir_base = 0x3e8;
  417. break;
  418. case 1:
  419. info->fir_base = 0x2e8;
  420. break;
  421. case 2:
  422. info->fir_base = 0x3f8;
  423. break;
  424. case 3:
  425. info->fir_base = 0x2f8;
  426. break;
  427. }
  428. info->sir_base = info->fir_base;
  429. IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__,
  430. info->fir_base);
  431. /* Read control signals routing register (CSRT) */
  432. outb(CFG_108_CSRT, cfg_base);
  433. reg = inb(cfg_base+1);
  434. switch (reg & 0x07) {
  435. case 0:
  436. info->irq = -1;
  437. break;
  438. case 1:
  439. info->irq = 3;
  440. break;
  441. case 2:
  442. info->irq = 4;
  443. break;
  444. case 3:
  445. info->irq = 5;
  446. break;
  447. case 4:
  448. info->irq = 7;
  449. break;
  450. case 5:
  451. info->irq = 9;
  452. break;
  453. case 6:
  454. info->irq = 11;
  455. break;
  456. case 7:
  457. info->irq = 15;
  458. break;
  459. }
  460. IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
  461. /* Currently we only read Rx DMA but it will also be used for Tx */
  462. switch ((reg >> 3) & 0x03) {
  463. case 0:
  464. info->dma = -1;
  465. break;
  466. case 1:
  467. info->dma = 0;
  468. break;
  469. case 2:
  470. info->dma = 1;
  471. break;
  472. case 3:
  473. info->dma = 3;
  474. break;
  475. }
  476. IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
  477. /* Read mode control register (MCTL) */
  478. outb(CFG_108_MCTL, cfg_base);
  479. reg = inb(cfg_base+1);
  480. info->enabled = reg & 0x01;
  481. info->suspended = !((reg >> 1) & 0x01);
  482. return 0;
  483. }
  484. /*
  485. * Function nsc_ircc_init_338 (chip, info)
  486. *
  487. * Initialize the NSC '338 chip. Remember that the 87338 needs two
  488. * consecutive writes to the data registers while CPU interrupts are
  489. * disabled. The 97338 does not require this, but shouldn't be any
  490. * harm if we do it anyway.
  491. */
  492. static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info)
  493. {
  494. /* No init yet */
  495. return 0;
  496. }
  497. /*
  498. * Function nsc_ircc_probe_338 (chip, info)
  499. *
  500. *
  501. *
  502. */
  503. static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info)
  504. {
  505. int cfg_base = info->cfg_base;
  506. int reg, com = 0;
  507. int pnp;
  508. /* Read funtion enable register (FER) */
  509. outb(CFG_338_FER, cfg_base);
  510. reg = inb(cfg_base+1);
  511. info->enabled = (reg >> 2) & 0x01;
  512. /* Check if we are in Legacy or PnP mode */
  513. outb(CFG_338_PNP0, cfg_base);
  514. reg = inb(cfg_base+1);
  515. pnp = (reg >> 3) & 0x01;
  516. if (pnp) {
  517. IRDA_DEBUG(2, "(), Chip is in PnP mode\n");
  518. outb(0x46, cfg_base);
  519. reg = (inb(cfg_base+1) & 0xfe) << 2;
  520. outb(0x47, cfg_base);
  521. reg |= ((inb(cfg_base+1) & 0xfc) << 8);
  522. info->fir_base = reg;
  523. } else {
  524. /* Read function address register (FAR) */
  525. outb(CFG_338_FAR, cfg_base);
  526. reg = inb(cfg_base+1);
  527. switch ((reg >> 4) & 0x03) {
  528. case 0:
  529. info->fir_base = 0x3f8;
  530. break;
  531. case 1:
  532. info->fir_base = 0x2f8;
  533. break;
  534. case 2:
  535. com = 3;
  536. break;
  537. case 3:
  538. com = 4;
  539. break;
  540. }
  541. if (com) {
  542. switch ((reg >> 6) & 0x03) {
  543. case 0:
  544. if (com == 3)
  545. info->fir_base = 0x3e8;
  546. else
  547. info->fir_base = 0x2e8;
  548. break;
  549. case 1:
  550. if (com == 3)
  551. info->fir_base = 0x338;
  552. else
  553. info->fir_base = 0x238;
  554. break;
  555. case 2:
  556. if (com == 3)
  557. info->fir_base = 0x2e8;
  558. else
  559. info->fir_base = 0x2e0;
  560. break;
  561. case 3:
  562. if (com == 3)
  563. info->fir_base = 0x220;
  564. else
  565. info->fir_base = 0x228;
  566. break;
  567. }
  568. }
  569. }
  570. info->sir_base = info->fir_base;
  571. /* Read PnP register 1 (PNP1) */
  572. outb(CFG_338_PNP1, cfg_base);
  573. reg = inb(cfg_base+1);
  574. info->irq = reg >> 4;
  575. /* Read PnP register 3 (PNP3) */
  576. outb(CFG_338_PNP3, cfg_base);
  577. reg = inb(cfg_base+1);
  578. info->dma = (reg & 0x07) - 1;
  579. /* Read power and test register (PTR) */
  580. outb(CFG_338_PTR, cfg_base);
  581. reg = inb(cfg_base+1);
  582. info->suspended = reg & 0x01;
  583. return 0;
  584. }
  585. /*
  586. * Function nsc_ircc_init_39x (chip, info)
  587. *
  588. * Now that we know it's a '39x (see probe below), we need to
  589. * configure it so we can use it.
  590. *
  591. * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
  592. * the configuration of the different functionality (serial, parallel,
  593. * floppy...) are each in a different bank (Logical Device Number).
  594. * The base address, irq and dma configuration registers are common
  595. * to all functionalities (index 0x30 to 0x7F).
  596. * There is only one configuration register specific to the
  597. * serial port, CFG_39X_SPC.
  598. * JeanII
  599. *
  600. * Note : this code was written by Jan Frey <janfrey@web.de>
  601. */
  602. static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info)
  603. {
  604. int cfg_base = info->cfg_base;
  605. int enabled;
  606. /* User is shure about his config... accept it. */
  607. IRDA_DEBUG(2, "%s(): nsc_ircc_init_39x (user settings): "
  608. "io=0x%04x, irq=%d, dma=%d\n",
  609. __FUNCTION__, info->fir_base, info->irq, info->dma);
  610. /* Access bank for SP2 */
  611. outb(CFG_39X_LDN, cfg_base);
  612. outb(0x02, cfg_base+1);
  613. /* Configure SP2 */
  614. /* We want to enable the device if not enabled */
  615. outb(CFG_39X_ACT, cfg_base);
  616. enabled = inb(cfg_base+1) & 0x01;
  617. if (!enabled) {
  618. /* Enable the device */
  619. outb(CFG_39X_SIOCF1, cfg_base);
  620. outb(0x01, cfg_base+1);
  621. /* May want to update info->enabled. Jean II */
  622. }
  623. /* Enable UART bank switching (bit 7) ; Sets the chip to normal
  624. * power mode (wake up from sleep mode) (bit 1) */
  625. outb(CFG_39X_SPC, cfg_base);
  626. outb(0x82, cfg_base+1);
  627. return 0;
  628. }
  629. /*
  630. * Function nsc_ircc_probe_39x (chip, info)
  631. *
  632. * Test if we really have a '39x chip at the given address
  633. *
  634. * Note : this code was written by Jan Frey <janfrey@web.de>
  635. */
  636. static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info)
  637. {
  638. int cfg_base = info->cfg_base;
  639. int reg1, reg2, irq, irqt, dma1, dma2;
  640. int enabled, susp;
  641. IRDA_DEBUG(2, "%s(), nsc_ircc_probe_39x, base=%d\n",
  642. __FUNCTION__, cfg_base);
  643. /* This function should be executed with irq off to avoid
  644. * another driver messing with the Super I/O bank - Jean II */
  645. /* Access bank for SP2 */
  646. outb(CFG_39X_LDN, cfg_base);
  647. outb(0x02, cfg_base+1);
  648. /* Read infos about SP2 ; store in info struct */
  649. outb(CFG_39X_BASEH, cfg_base);
  650. reg1 = inb(cfg_base+1);
  651. outb(CFG_39X_BASEL, cfg_base);
  652. reg2 = inb(cfg_base+1);
  653. info->fir_base = (reg1 << 8) | reg2;
  654. outb(CFG_39X_IRQNUM, cfg_base);
  655. irq = inb(cfg_base+1);
  656. outb(CFG_39X_IRQSEL, cfg_base);
  657. irqt = inb(cfg_base+1);
  658. info->irq = irq;
  659. outb(CFG_39X_DMA0, cfg_base);
  660. dma1 = inb(cfg_base+1);
  661. outb(CFG_39X_DMA1, cfg_base);
  662. dma2 = inb(cfg_base+1);
  663. info->dma = dma1 -1;
  664. outb(CFG_39X_ACT, cfg_base);
  665. info->enabled = enabled = inb(cfg_base+1) & 0x01;
  666. outb(CFG_39X_SPC, cfg_base);
  667. susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
  668. IRDA_DEBUG(2, "%s(): io=0x%02x%02x, irq=%d (type %d), rxdma=%d, txdma=%d, enabled=%d (suspended=%d)\n", __FUNCTION__, reg1,reg2,irq,irqt,dma1,dma2,enabled,susp);
  669. /* Configure SP2 */
  670. /* We want to enable the device if not enabled */
  671. outb(CFG_39X_ACT, cfg_base);
  672. enabled = inb(cfg_base+1) & 0x01;
  673. if (!enabled) {
  674. /* Enable the device */
  675. outb(CFG_39X_SIOCF1, cfg_base);
  676. outb(0x01, cfg_base+1);
  677. /* May want to update info->enabled. Jean II */
  678. }
  679. /* Enable UART bank switching (bit 7) ; Sets the chip to normal
  680. * power mode (wake up from sleep mode) (bit 1) */
  681. outb(CFG_39X_SPC, cfg_base);
  682. outb(0x82, cfg_base+1);
  683. return 0;
  684. }
  685. /*
  686. * Function nsc_ircc_setup (info)
  687. *
  688. * Returns non-negative on success.
  689. *
  690. */
  691. static int nsc_ircc_setup(chipio_t *info)
  692. {
  693. int version;
  694. int iobase = info->fir_base;
  695. /* Read the Module ID */
  696. switch_bank(iobase, BANK3);
  697. version = inb(iobase+MID);
  698. IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
  699. __FUNCTION__, driver_name, version);
  700. /* Should be 0x2? */
  701. if (0x20 != (version & 0xf0)) {
  702. IRDA_ERROR("%s, Wrong chip version %02x\n",
  703. driver_name, version);
  704. return -1;
  705. }
  706. /* Switch to advanced mode */
  707. switch_bank(iobase, BANK2);
  708. outb(ECR1_EXT_SL, iobase+ECR1);
  709. switch_bank(iobase, BANK0);
  710. /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
  711. switch_bank(iobase, BANK0);
  712. outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  713. outb(0x03, iobase+LCR); /* 8 bit word length */
  714. outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
  715. /* Set FIFO size to 32 */
  716. switch_bank(iobase, BANK2);
  717. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  718. /* IRCR2: FEND_MD is not set */
  719. switch_bank(iobase, BANK5);
  720. outb(0x02, iobase+4);
  721. /* Make sure that some defaults are OK */
  722. switch_bank(iobase, BANK6);
  723. outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
  724. outb(0x0a, iobase+1); /* Set MIR pulse width */
  725. outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
  726. outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
  727. /* Enable receive interrupts */
  728. switch_bank(iobase, BANK0);
  729. outb(IER_RXHDL_IE, iobase+IER);
  730. return 0;
  731. }
  732. /*
  733. * Function nsc_ircc_read_dongle_id (void)
  734. *
  735. * Try to read dongle indentification. This procedure needs to be executed
  736. * once after power-on/reset. It also needs to be used whenever you suspect
  737. * that the user may have plugged/unplugged the IrDA Dongle.
  738. */
  739. static int nsc_ircc_read_dongle_id (int iobase)
  740. {
  741. int dongle_id;
  742. __u8 bank;
  743. bank = inb(iobase+BSR);
  744. /* Select Bank 7 */
  745. switch_bank(iobase, BANK7);
  746. /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
  747. outb(0x00, iobase+7);
  748. /* ID0, 1, and 2 are pulled up/down very slowly */
  749. udelay(50);
  750. /* IRCFG1: read the ID bits */
  751. dongle_id = inb(iobase+4) & 0x0f;
  752. #ifdef BROKEN_DONGLE_ID
  753. if (dongle_id == 0x0a)
  754. dongle_id = 0x09;
  755. #endif
  756. /* Go back to bank 0 before returning */
  757. switch_bank(iobase, BANK0);
  758. outb(bank, iobase+BSR);
  759. return dongle_id;
  760. }
  761. /*
  762. * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
  763. *
  764. * This function initializes the dongle for the transceiver that is
  765. * used. This procedure needs to be executed once after
  766. * power-on/reset. It also needs to be used whenever you suspect that
  767. * the dongle is changed.
  768. */
  769. static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
  770. {
  771. int bank;
  772. /* Save current bank */
  773. bank = inb(iobase+BSR);
  774. /* Select Bank 7 */
  775. switch_bank(iobase, BANK7);
  776. /* IRCFG4: set according to dongle_id */
  777. switch (dongle_id) {
  778. case 0x00: /* same as */
  779. case 0x01: /* Differential serial interface */
  780. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  781. __FUNCTION__, dongle_types[dongle_id]);
  782. break;
  783. case 0x02: /* same as */
  784. case 0x03: /* Reserved */
  785. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  786. __FUNCTION__, dongle_types[dongle_id]);
  787. break;
  788. case 0x04: /* Sharp RY5HD01 */
  789. break;
  790. case 0x05: /* Reserved, but this is what the Thinkpad reports */
  791. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  792. __FUNCTION__, dongle_types[dongle_id]);
  793. break;
  794. case 0x06: /* Single-ended serial interface */
  795. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  796. __FUNCTION__, dongle_types[dongle_id]);
  797. break;
  798. case 0x07: /* Consumer-IR only */
  799. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  800. __FUNCTION__, dongle_types[dongle_id]);
  801. break;
  802. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  803. IRDA_DEBUG(0, "%s(), %s\n",
  804. __FUNCTION__, dongle_types[dongle_id]);
  805. break;
  806. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  807. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  808. break;
  809. case 0x0A: /* same as */
  810. case 0x0B: /* Reserved */
  811. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  812. __FUNCTION__, dongle_types[dongle_id]);
  813. break;
  814. case 0x0C: /* same as */
  815. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  816. /*
  817. * Set irsl0 as input, irsl[1-2] as output, and separate
  818. * inputs are used for SIR and MIR/FIR
  819. */
  820. outb(0x48, iobase+7);
  821. break;
  822. case 0x0E: /* Supports SIR Mode only */
  823. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  824. break;
  825. case 0x0F: /* No dongle connected */
  826. IRDA_DEBUG(0, "%s(), %s\n",
  827. __FUNCTION__, dongle_types[dongle_id]);
  828. switch_bank(iobase, BANK0);
  829. outb(0x62, iobase+MCR);
  830. break;
  831. default:
  832. IRDA_DEBUG(0, "%s(), invalid dongle_id %#x",
  833. __FUNCTION__, dongle_id);
  834. }
  835. /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
  836. outb(0x00, iobase+4);
  837. /* Restore bank register */
  838. outb(bank, iobase+BSR);
  839. } /* set_up_dongle_interface */
  840. /*
  841. * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
  842. *
  843. * Change speed of the attach dongle
  844. *
  845. */
  846. static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
  847. {
  848. __u8 bank;
  849. /* Save current bank */
  850. bank = inb(iobase+BSR);
  851. /* Select Bank 7 */
  852. switch_bank(iobase, BANK7);
  853. /* IRCFG1: set according to dongle_id */
  854. switch (dongle_id) {
  855. case 0x00: /* same as */
  856. case 0x01: /* Differential serial interface */
  857. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  858. __FUNCTION__, dongle_types[dongle_id]);
  859. break;
  860. case 0x02: /* same as */
  861. case 0x03: /* Reserved */
  862. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  863. __FUNCTION__, dongle_types[dongle_id]);
  864. break;
  865. case 0x04: /* Sharp RY5HD01 */
  866. break;
  867. case 0x05: /* Reserved */
  868. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  869. __FUNCTION__, dongle_types[dongle_id]);
  870. break;
  871. case 0x06: /* Single-ended serial interface */
  872. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  873. __FUNCTION__, dongle_types[dongle_id]);
  874. break;
  875. case 0x07: /* Consumer-IR only */
  876. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  877. __FUNCTION__, dongle_types[dongle_id]);
  878. break;
  879. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  880. IRDA_DEBUG(0, "%s(), %s\n",
  881. __FUNCTION__, dongle_types[dongle_id]);
  882. outb(0x00, iobase+4);
  883. if (speed > 115200)
  884. outb(0x01, iobase+4);
  885. break;
  886. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  887. outb(0x01, iobase+4);
  888. if (speed == 4000000) {
  889. /* There was a cli() there, but we now are already
  890. * under spin_lock_irqsave() - JeanII */
  891. outb(0x81, iobase+4);
  892. outb(0x80, iobase+4);
  893. } else
  894. outb(0x00, iobase+4);
  895. break;
  896. case 0x0A: /* same as */
  897. case 0x0B: /* Reserved */
  898. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  899. __FUNCTION__, dongle_types[dongle_id]);
  900. break;
  901. case 0x0C: /* same as */
  902. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  903. break;
  904. case 0x0E: /* Supports SIR Mode only */
  905. break;
  906. case 0x0F: /* No dongle connected */
  907. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  908. __FUNCTION__, dongle_types[dongle_id]);
  909. switch_bank(iobase, BANK0);
  910. outb(0x62, iobase+MCR);
  911. break;
  912. default:
  913. IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
  914. }
  915. /* Restore bank register */
  916. outb(bank, iobase+BSR);
  917. }
  918. /*
  919. * Function nsc_ircc_change_speed (self, baud)
  920. *
  921. * Change the speed of the device
  922. *
  923. * This function *must* be called with irq off and spin-lock.
  924. */
  925. static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
  926. {
  927. struct net_device *dev = self->netdev;
  928. __u8 mcr = MCR_SIR;
  929. int iobase;
  930. __u8 bank;
  931. __u8 ier; /* Interrupt enable register */
  932. IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
  933. IRDA_ASSERT(self != NULL, return 0;);
  934. iobase = self->io.fir_base;
  935. /* Update accounting for new speed */
  936. self->io.speed = speed;
  937. /* Save current bank */
  938. bank = inb(iobase+BSR);
  939. /* Disable interrupts */
  940. switch_bank(iobase, BANK0);
  941. outb(0, iobase+IER);
  942. /* Select Bank 2 */
  943. switch_bank(iobase, BANK2);
  944. outb(0x00, iobase+BGDH);
  945. switch (speed) {
  946. case 9600: outb(0x0c, iobase+BGDL); break;
  947. case 19200: outb(0x06, iobase+BGDL); break;
  948. case 38400: outb(0x03, iobase+BGDL); break;
  949. case 57600: outb(0x02, iobase+BGDL); break;
  950. case 115200: outb(0x01, iobase+BGDL); break;
  951. case 576000:
  952. switch_bank(iobase, BANK5);
  953. /* IRCR2: MDRS is set */
  954. outb(inb(iobase+4) | 0x04, iobase+4);
  955. mcr = MCR_MIR;
  956. IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
  957. break;
  958. case 1152000:
  959. mcr = MCR_MIR;
  960. IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
  961. break;
  962. case 4000000:
  963. mcr = MCR_FIR;
  964. IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
  965. break;
  966. default:
  967. mcr = MCR_FIR;
  968. IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n",
  969. __FUNCTION__, speed);
  970. break;
  971. }
  972. /* Set appropriate speed mode */
  973. switch_bank(iobase, BANK0);
  974. outb(mcr | MCR_TX_DFR, iobase+MCR);
  975. /* Give some hits to the transceiver */
  976. nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
  977. /* Set FIFO threshold to TX17, RX16 */
  978. switch_bank(iobase, BANK0);
  979. outb(0x00, iobase+FCR);
  980. outb(FCR_FIFO_EN, iobase+FCR);
  981. outb(FCR_RXTH| /* Set Rx FIFO threshold */
  982. FCR_TXTH| /* Set Tx FIFO threshold */
  983. FCR_TXSR| /* Reset Tx FIFO */
  984. FCR_RXSR| /* Reset Rx FIFO */
  985. FCR_FIFO_EN, /* Enable FIFOs */
  986. iobase+FCR);
  987. /* Set FIFO size to 32 */
  988. switch_bank(iobase, BANK2);
  989. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  990. /* Enable some interrupts so we can receive frames */
  991. switch_bank(iobase, BANK0);
  992. if (speed > 115200) {
  993. /* Install FIR xmit handler */
  994. dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
  995. ier = IER_SFIF_IE;
  996. nsc_ircc_dma_receive(self);
  997. } else {
  998. /* Install SIR xmit handler */
  999. dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
  1000. ier = IER_RXHDL_IE;
  1001. }
  1002. /* Set our current interrupt mask */
  1003. outb(ier, iobase+IER);
  1004. /* Restore BSR */
  1005. outb(bank, iobase+BSR);
  1006. /* Make sure interrupt handlers keep the proper interrupt mask */
  1007. return(ier);
  1008. }
  1009. /*
  1010. * Function nsc_ircc_hard_xmit (skb, dev)
  1011. *
  1012. * Transmit the frame!
  1013. *
  1014. */
  1015. static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
  1016. {
  1017. struct nsc_ircc_cb *self;
  1018. unsigned long flags;
  1019. int iobase;
  1020. __s32 speed;
  1021. __u8 bank;
  1022. self = (struct nsc_ircc_cb *) dev->priv;
  1023. IRDA_ASSERT(self != NULL, return 0;);
  1024. iobase = self->io.fir_base;
  1025. netif_stop_queue(dev);
  1026. /* Make sure tests *& speed change are atomic */
  1027. spin_lock_irqsave(&self->lock, flags);
  1028. /* Check if we need to change the speed */
  1029. speed = irda_get_next_speed(skb);
  1030. if ((speed != self->io.speed) && (speed != -1)) {
  1031. /* Check for empty frame. */
  1032. if (!skb->len) {
  1033. /* If we just sent a frame, we get called before
  1034. * the last bytes get out (because of the SIR FIFO).
  1035. * If this is the case, let interrupt handler change
  1036. * the speed itself... Jean II */
  1037. if (self->io.direction == IO_RECV) {
  1038. nsc_ircc_change_speed(self, speed);
  1039. /* TODO : For SIR->SIR, the next packet
  1040. * may get corrupted - Jean II */
  1041. netif_wake_queue(dev);
  1042. } else {
  1043. self->new_speed = speed;
  1044. /* Queue will be restarted after speed change
  1045. * to make sure packets gets through the
  1046. * proper xmit handler - Jean II */
  1047. }
  1048. dev->trans_start = jiffies;
  1049. spin_unlock_irqrestore(&self->lock, flags);
  1050. dev_kfree_skb(skb);
  1051. return 0;
  1052. } else
  1053. self->new_speed = speed;
  1054. }
  1055. /* Save current bank */
  1056. bank = inb(iobase+BSR);
  1057. self->tx_buff.data = self->tx_buff.head;
  1058. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  1059. self->tx_buff.truesize);
  1060. self->stats.tx_bytes += self->tx_buff.len;
  1061. /* Add interrupt on tx low level (will fire immediately) */
  1062. switch_bank(iobase, BANK0);
  1063. outb(IER_TXLDL_IE, iobase+IER);
  1064. /* Restore bank register */
  1065. outb(bank, iobase+BSR);
  1066. dev->trans_start = jiffies;
  1067. spin_unlock_irqrestore(&self->lock, flags);
  1068. dev_kfree_skb(skb);
  1069. return 0;
  1070. }
  1071. static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
  1072. {
  1073. struct nsc_ircc_cb *self;
  1074. unsigned long flags;
  1075. int iobase;
  1076. __s32 speed;
  1077. __u8 bank;
  1078. int mtt, diff;
  1079. self = (struct nsc_ircc_cb *) dev->priv;
  1080. iobase = self->io.fir_base;
  1081. netif_stop_queue(dev);
  1082. /* Make sure tests *& speed change are atomic */
  1083. spin_lock_irqsave(&self->lock, flags);
  1084. /* Check if we need to change the speed */
  1085. speed = irda_get_next_speed(skb);
  1086. if ((speed != self->io.speed) && (speed != -1)) {
  1087. /* Check for empty frame. */
  1088. if (!skb->len) {
  1089. /* If we are currently transmitting, defer to
  1090. * interrupt handler. - Jean II */
  1091. if(self->tx_fifo.len == 0) {
  1092. nsc_ircc_change_speed(self, speed);
  1093. netif_wake_queue(dev);
  1094. } else {
  1095. self->new_speed = speed;
  1096. /* Keep queue stopped :
  1097. * the speed change operation may change the
  1098. * xmit handler, and we want to make sure
  1099. * the next packet get through the proper
  1100. * Tx path, so block the Tx queue until
  1101. * the speed change has been done.
  1102. * Jean II */
  1103. }
  1104. dev->trans_start = jiffies;
  1105. spin_unlock_irqrestore(&self->lock, flags);
  1106. dev_kfree_skb(skb);
  1107. return 0;
  1108. } else {
  1109. /* Change speed after current frame */
  1110. self->new_speed = speed;
  1111. }
  1112. }
  1113. /* Save current bank */
  1114. bank = inb(iobase+BSR);
  1115. /* Register and copy this frame to DMA memory */
  1116. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  1117. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  1118. self->tx_fifo.tail += skb->len;
  1119. self->stats.tx_bytes += skb->len;
  1120. memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
  1121. skb->len);
  1122. self->tx_fifo.len++;
  1123. self->tx_fifo.free++;
  1124. /* Start transmit only if there is currently no transmit going on */
  1125. if (self->tx_fifo.len == 1) {
  1126. /* Check if we must wait the min turn time or not */
  1127. mtt = irda_get_mtt(skb);
  1128. if (mtt) {
  1129. /* Check how much time we have used already */
  1130. do_gettimeofday(&self->now);
  1131. diff = self->now.tv_usec - self->stamp.tv_usec;
  1132. if (diff < 0)
  1133. diff += 1000000;
  1134. /* Check if the mtt is larger than the time we have
  1135. * already used by all the protocol processing
  1136. */
  1137. if (mtt > diff) {
  1138. mtt -= diff;
  1139. /*
  1140. * Use timer if delay larger than 125 us, and
  1141. * use udelay for smaller values which should
  1142. * be acceptable
  1143. */
  1144. if (mtt > 125) {
  1145. /* Adjust for timer resolution */
  1146. mtt = mtt / 125;
  1147. /* Setup timer */
  1148. switch_bank(iobase, BANK4);
  1149. outb(mtt & 0xff, iobase+TMRL);
  1150. outb((mtt >> 8) & 0x0f, iobase+TMRH);
  1151. /* Start timer */
  1152. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1153. self->io.direction = IO_XMIT;
  1154. /* Enable timer interrupt */
  1155. switch_bank(iobase, BANK0);
  1156. outb(IER_TMR_IE, iobase+IER);
  1157. /* Timer will take care of the rest */
  1158. goto out;
  1159. } else
  1160. udelay(mtt);
  1161. }
  1162. }
  1163. /* Enable DMA interrupt */
  1164. switch_bank(iobase, BANK0);
  1165. outb(IER_DMA_IE, iobase+IER);
  1166. /* Transmit frame */
  1167. nsc_ircc_dma_xmit(self, iobase);
  1168. }
  1169. out:
  1170. /* Not busy transmitting anymore if window is not full,
  1171. * and if we don't need to change speed */
  1172. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
  1173. netif_wake_queue(self->netdev);
  1174. /* Restore bank register */
  1175. outb(bank, iobase+BSR);
  1176. dev->trans_start = jiffies;
  1177. spin_unlock_irqrestore(&self->lock, flags);
  1178. dev_kfree_skb(skb);
  1179. return 0;
  1180. }
  1181. /*
  1182. * Function nsc_ircc_dma_xmit (self, iobase)
  1183. *
  1184. * Transmit data using DMA
  1185. *
  1186. */
  1187. static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
  1188. {
  1189. int bsr;
  1190. /* Save current bank */
  1191. bsr = inb(iobase+BSR);
  1192. /* Disable DMA */
  1193. switch_bank(iobase, BANK0);
  1194. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1195. self->io.direction = IO_XMIT;
  1196. /* Choose transmit DMA channel */
  1197. switch_bank(iobase, BANK2);
  1198. outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1199. irda_setup_dma(self->io.dma,
  1200. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  1201. self->tx_buff.head) + self->tx_buff_dma,
  1202. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  1203. DMA_TX_MODE);
  1204. /* Enable DMA and SIR interaction pulse */
  1205. switch_bank(iobase, BANK0);
  1206. outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
  1207. /* Restore bank register */
  1208. outb(bsr, iobase+BSR);
  1209. }
  1210. /*
  1211. * Function nsc_ircc_pio_xmit (self, iobase)
  1212. *
  1213. * Transmit data using PIO. Returns the number of bytes that actually
  1214. * got transferred
  1215. *
  1216. */
  1217. static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
  1218. {
  1219. int actual = 0;
  1220. __u8 bank;
  1221. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1222. /* Save current bank */
  1223. bank = inb(iobase+BSR);
  1224. switch_bank(iobase, BANK0);
  1225. if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
  1226. IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
  1227. __FUNCTION__);
  1228. /* FIFO may still be filled to the Tx interrupt threshold */
  1229. fifo_size -= 17;
  1230. }
  1231. /* Fill FIFO with current frame */
  1232. while ((fifo_size-- > 0) && (actual < len)) {
  1233. /* Transmit next byte */
  1234. outb(buf[actual++], iobase+TXD);
  1235. }
  1236. IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
  1237. __FUNCTION__, fifo_size, actual, len);
  1238. /* Restore bank */
  1239. outb(bank, iobase+BSR);
  1240. return actual;
  1241. }
  1242. /*
  1243. * Function nsc_ircc_dma_xmit_complete (self)
  1244. *
  1245. * The transfer of a frame in finished. This function will only be called
  1246. * by the interrupt handler
  1247. *
  1248. */
  1249. static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
  1250. {
  1251. int iobase;
  1252. __u8 bank;
  1253. int ret = TRUE;
  1254. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  1255. iobase = self->io.fir_base;
  1256. /* Save current bank */
  1257. bank = inb(iobase+BSR);
  1258. /* Disable DMA */
  1259. switch_bank(iobase, BANK0);
  1260. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1261. /* Check for underrrun! */
  1262. if (inb(iobase+ASCR) & ASCR_TXUR) {
  1263. self->stats.tx_errors++;
  1264. self->stats.tx_fifo_errors++;
  1265. /* Clear bit, by writing 1 into it */
  1266. outb(ASCR_TXUR, iobase+ASCR);
  1267. } else {
  1268. self->stats.tx_packets++;
  1269. }
  1270. /* Finished with this frame, so prepare for next */
  1271. self->tx_fifo.ptr++;
  1272. self->tx_fifo.len--;
  1273. /* Any frames to be sent back-to-back? */
  1274. if (self->tx_fifo.len) {
  1275. nsc_ircc_dma_xmit(self, iobase);
  1276. /* Not finished yet! */
  1277. ret = FALSE;
  1278. } else {
  1279. /* Reset Tx FIFO info */
  1280. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1281. self->tx_fifo.tail = self->tx_buff.head;
  1282. }
  1283. /* Make sure we have room for more frames and
  1284. * that we don't need to change speed */
  1285. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
  1286. /* Not busy transmitting anymore */
  1287. /* Tell the network layer, that we can accept more frames */
  1288. netif_wake_queue(self->netdev);
  1289. }
  1290. /* Restore bank */
  1291. outb(bank, iobase+BSR);
  1292. return ret;
  1293. }
  1294. /*
  1295. * Function nsc_ircc_dma_receive (self)
  1296. *
  1297. * Get ready for receiving a frame. The device will initiate a DMA
  1298. * if it starts to receive a frame.
  1299. *
  1300. */
  1301. static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
  1302. {
  1303. int iobase;
  1304. __u8 bsr;
  1305. iobase = self->io.fir_base;
  1306. /* Reset Tx FIFO info */
  1307. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1308. self->tx_fifo.tail = self->tx_buff.head;
  1309. /* Save current bank */
  1310. bsr = inb(iobase+BSR);
  1311. /* Disable DMA */
  1312. switch_bank(iobase, BANK0);
  1313. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1314. /* Choose DMA Rx, DMA Fairness, and Advanced mode */
  1315. switch_bank(iobase, BANK2);
  1316. outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1317. self->io.direction = IO_RECV;
  1318. self->rx_buff.data = self->rx_buff.head;
  1319. /* Reset Rx FIFO. This will also flush the ST_FIFO */
  1320. switch_bank(iobase, BANK0);
  1321. outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  1322. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  1323. self->st_fifo.tail = self->st_fifo.head = 0;
  1324. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  1325. DMA_RX_MODE);
  1326. /* Enable DMA */
  1327. switch_bank(iobase, BANK0);
  1328. outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
  1329. /* Restore bank register */
  1330. outb(bsr, iobase+BSR);
  1331. return 0;
  1332. }
  1333. /*
  1334. * Function nsc_ircc_dma_receive_complete (self)
  1335. *
  1336. * Finished with receiving frames
  1337. *
  1338. *
  1339. */
  1340. static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
  1341. {
  1342. struct st_fifo *st_fifo;
  1343. struct sk_buff *skb;
  1344. __u8 status;
  1345. __u8 bank;
  1346. int len;
  1347. st_fifo = &self->st_fifo;
  1348. /* Save current bank */
  1349. bank = inb(iobase+BSR);
  1350. /* Read all entries in status FIFO */
  1351. switch_bank(iobase, BANK5);
  1352. while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
  1353. /* We must empty the status FIFO no matter what */
  1354. len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
  1355. if (st_fifo->tail >= MAX_RX_WINDOW) {
  1356. IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
  1357. continue;
  1358. }
  1359. st_fifo->entries[st_fifo->tail].status = status;
  1360. st_fifo->entries[st_fifo->tail].len = len;
  1361. st_fifo->pending_bytes += len;
  1362. st_fifo->tail++;
  1363. st_fifo->len++;
  1364. }
  1365. /* Try to process all entries in status FIFO */
  1366. while (st_fifo->len > 0) {
  1367. /* Get first entry */
  1368. status = st_fifo->entries[st_fifo->head].status;
  1369. len = st_fifo->entries[st_fifo->head].len;
  1370. st_fifo->pending_bytes -= len;
  1371. st_fifo->head++;
  1372. st_fifo->len--;
  1373. /* Check for errors */
  1374. if (status & FRM_ST_ERR_MSK) {
  1375. if (status & FRM_ST_LOST_FR) {
  1376. /* Add number of lost frames to stats */
  1377. self->stats.rx_errors += len;
  1378. } else {
  1379. /* Skip frame */
  1380. self->stats.rx_errors++;
  1381. self->rx_buff.data += len;
  1382. if (status & FRM_ST_MAX_LEN)
  1383. self->stats.rx_length_errors++;
  1384. if (status & FRM_ST_PHY_ERR)
  1385. self->stats.rx_frame_errors++;
  1386. if (status & FRM_ST_BAD_CRC)
  1387. self->stats.rx_crc_errors++;
  1388. }
  1389. /* The errors below can be reported in both cases */
  1390. if (status & FRM_ST_OVR1)
  1391. self->stats.rx_fifo_errors++;
  1392. if (status & FRM_ST_OVR2)
  1393. self->stats.rx_fifo_errors++;
  1394. } else {
  1395. /*
  1396. * First we must make sure that the frame we
  1397. * want to deliver is all in main memory. If we
  1398. * cannot tell, then we check if the Rx FIFO is
  1399. * empty. If not then we will have to take a nap
  1400. * and try again later.
  1401. */
  1402. if (st_fifo->pending_bytes < self->io.fifo_size) {
  1403. switch_bank(iobase, BANK0);
  1404. if (inb(iobase+LSR) & LSR_RXDA) {
  1405. /* Put this entry back in fifo */
  1406. st_fifo->head--;
  1407. st_fifo->len++;
  1408. st_fifo->pending_bytes += len;
  1409. st_fifo->entries[st_fifo->head].status = status;
  1410. st_fifo->entries[st_fifo->head].len = len;
  1411. /*
  1412. * DMA not finished yet, so try again
  1413. * later, set timer value, resolution
  1414. * 125 us
  1415. */
  1416. switch_bank(iobase, BANK4);
  1417. outb(0x02, iobase+TMRL); /* x 125 us */
  1418. outb(0x00, iobase+TMRH);
  1419. /* Start timer */
  1420. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1421. /* Restore bank register */
  1422. outb(bank, iobase+BSR);
  1423. return FALSE; /* I'll be back! */
  1424. }
  1425. }
  1426. /*
  1427. * Remember the time we received this frame, so we can
  1428. * reduce the min turn time a bit since we will know
  1429. * how much time we have used for protocol processing
  1430. */
  1431. do_gettimeofday(&self->stamp);
  1432. skb = dev_alloc_skb(len+1);
  1433. if (skb == NULL) {
  1434. IRDA_WARNING("%s(), memory squeeze, "
  1435. "dropping frame.\n",
  1436. __FUNCTION__);
  1437. self->stats.rx_dropped++;
  1438. /* Restore bank register */
  1439. outb(bank, iobase+BSR);
  1440. return FALSE;
  1441. }
  1442. /* Make sure IP header gets aligned */
  1443. skb_reserve(skb, 1);
  1444. /* Copy frame without CRC */
  1445. if (self->io.speed < 4000000) {
  1446. skb_put(skb, len-2);
  1447. memcpy(skb->data, self->rx_buff.data, len-2);
  1448. } else {
  1449. skb_put(skb, len-4);
  1450. memcpy(skb->data, self->rx_buff.data, len-4);
  1451. }
  1452. /* Move to next frame */
  1453. self->rx_buff.data += len;
  1454. self->stats.rx_bytes += len;
  1455. self->stats.rx_packets++;
  1456. skb->dev = self->netdev;
  1457. skb->mac.raw = skb->data;
  1458. skb->protocol = htons(ETH_P_IRDA);
  1459. netif_rx(skb);
  1460. self->netdev->last_rx = jiffies;
  1461. }
  1462. }
  1463. /* Restore bank register */
  1464. outb(bank, iobase+BSR);
  1465. return TRUE;
  1466. }
  1467. /*
  1468. * Function nsc_ircc_pio_receive (self)
  1469. *
  1470. * Receive all data in receiver FIFO
  1471. *
  1472. */
  1473. static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
  1474. {
  1475. __u8 byte;
  1476. int iobase;
  1477. iobase = self->io.fir_base;
  1478. /* Receive all characters in Rx FIFO */
  1479. do {
  1480. byte = inb(iobase+RXD);
  1481. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  1482. byte);
  1483. } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
  1484. }
  1485. /*
  1486. * Function nsc_ircc_sir_interrupt (self, eir)
  1487. *
  1488. * Handle SIR interrupt
  1489. *
  1490. */
  1491. static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
  1492. {
  1493. int actual;
  1494. /* Check if transmit FIFO is low on data */
  1495. if (eir & EIR_TXLDL_EV) {
  1496. /* Write data left in transmit buffer */
  1497. actual = nsc_ircc_pio_write(self->io.fir_base,
  1498. self->tx_buff.data,
  1499. self->tx_buff.len,
  1500. self->io.fifo_size);
  1501. self->tx_buff.data += actual;
  1502. self->tx_buff.len -= actual;
  1503. self->io.direction = IO_XMIT;
  1504. /* Check if finished */
  1505. if (self->tx_buff.len > 0)
  1506. self->ier = IER_TXLDL_IE;
  1507. else {
  1508. self->stats.tx_packets++;
  1509. netif_wake_queue(self->netdev);
  1510. self->ier = IER_TXEMP_IE;
  1511. }
  1512. }
  1513. /* Check if transmission has completed */
  1514. if (eir & EIR_TXEMP_EV) {
  1515. /* Turn around and get ready to receive some data */
  1516. self->io.direction = IO_RECV;
  1517. self->ier = IER_RXHDL_IE;
  1518. /* Check if we need to change the speed?
  1519. * Need to be after self->io.direction to avoid race with
  1520. * nsc_ircc_hard_xmit_sir() - Jean II */
  1521. if (self->new_speed) {
  1522. IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
  1523. self->ier = nsc_ircc_change_speed(self,
  1524. self->new_speed);
  1525. self->new_speed = 0;
  1526. netif_wake_queue(self->netdev);
  1527. /* Check if we are going to FIR */
  1528. if (self->io.speed > 115200) {
  1529. /* No need to do anymore SIR stuff */
  1530. return;
  1531. }
  1532. }
  1533. }
  1534. /* Rx FIFO threshold or timeout */
  1535. if (eir & EIR_RXHDL_EV) {
  1536. nsc_ircc_pio_receive(self);
  1537. /* Keep receiving */
  1538. self->ier = IER_RXHDL_IE;
  1539. }
  1540. }
  1541. /*
  1542. * Function nsc_ircc_fir_interrupt (self, eir)
  1543. *
  1544. * Handle MIR/FIR interrupt
  1545. *
  1546. */
  1547. static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
  1548. int eir)
  1549. {
  1550. __u8 bank;
  1551. bank = inb(iobase+BSR);
  1552. /* Status FIFO event*/
  1553. if (eir & EIR_SFIF_EV) {
  1554. /* Check if DMA has finished */
  1555. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1556. /* Wait for next status FIFO interrupt */
  1557. self->ier = IER_SFIF_IE;
  1558. } else {
  1559. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1560. }
  1561. } else if (eir & EIR_TMR_EV) { /* Timer finished */
  1562. /* Disable timer */
  1563. switch_bank(iobase, BANK4);
  1564. outb(0, iobase+IRCR1);
  1565. /* Clear timer event */
  1566. switch_bank(iobase, BANK0);
  1567. outb(ASCR_CTE, iobase+ASCR);
  1568. /* Check if this is a Tx timer interrupt */
  1569. if (self->io.direction == IO_XMIT) {
  1570. nsc_ircc_dma_xmit(self, iobase);
  1571. /* Interrupt on DMA */
  1572. self->ier = IER_DMA_IE;
  1573. } else {
  1574. /* Check (again) if DMA has finished */
  1575. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1576. self->ier = IER_SFIF_IE;
  1577. } else {
  1578. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1579. }
  1580. }
  1581. } else if (eir & EIR_DMA_EV) {
  1582. /* Finished with all transmissions? */
  1583. if (nsc_ircc_dma_xmit_complete(self)) {
  1584. if(self->new_speed != 0) {
  1585. /* As we stop the Tx queue, the speed change
  1586. * need to be done when the Tx fifo is
  1587. * empty. Ask for a Tx done interrupt */
  1588. self->ier = IER_TXEMP_IE;
  1589. } else {
  1590. /* Check if there are more frames to be
  1591. * transmitted */
  1592. if (irda_device_txqueue_empty(self->netdev)) {
  1593. /* Prepare for receive */
  1594. nsc_ircc_dma_receive(self);
  1595. self->ier = IER_SFIF_IE;
  1596. } else
  1597. IRDA_WARNING("%s(), potential "
  1598. "Tx queue lockup !\n",
  1599. __FUNCTION__);
  1600. }
  1601. } else {
  1602. /* Not finished yet, so interrupt on DMA again */
  1603. self->ier = IER_DMA_IE;
  1604. }
  1605. } else if (eir & EIR_TXEMP_EV) {
  1606. /* The Tx FIFO has totally drained out, so now we can change
  1607. * the speed... - Jean II */
  1608. self->ier = nsc_ircc_change_speed(self, self->new_speed);
  1609. self->new_speed = 0;
  1610. netif_wake_queue(self->netdev);
  1611. /* Note : nsc_ircc_change_speed() restarted Rx fifo */
  1612. }
  1613. outb(bank, iobase+BSR);
  1614. }
  1615. /*
  1616. * Function nsc_ircc_interrupt (irq, dev_id, regs)
  1617. *
  1618. * An interrupt from the chip has arrived. Time to do some work
  1619. *
  1620. */
  1621. static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id,
  1622. struct pt_regs *regs)
  1623. {
  1624. struct net_device *dev = (struct net_device *) dev_id;
  1625. struct nsc_ircc_cb *self;
  1626. __u8 bsr, eir;
  1627. int iobase;
  1628. if (!dev) {
  1629. IRDA_WARNING("%s: irq %d for unknown device.\n",
  1630. driver_name, irq);
  1631. return IRQ_NONE;
  1632. }
  1633. self = (struct nsc_ircc_cb *) dev->priv;
  1634. spin_lock(&self->lock);
  1635. iobase = self->io.fir_base;
  1636. bsr = inb(iobase+BSR); /* Save current bank */
  1637. switch_bank(iobase, BANK0);
  1638. self->ier = inb(iobase+IER);
  1639. eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
  1640. outb(0, iobase+IER); /* Disable interrupts */
  1641. if (eir) {
  1642. /* Dispatch interrupt handler for the current speed */
  1643. if (self->io.speed > 115200)
  1644. nsc_ircc_fir_interrupt(self, iobase, eir);
  1645. else
  1646. nsc_ircc_sir_interrupt(self, eir);
  1647. }
  1648. outb(self->ier, iobase+IER); /* Restore interrupts */
  1649. outb(bsr, iobase+BSR); /* Restore bank register */
  1650. spin_unlock(&self->lock);
  1651. return IRQ_RETVAL(eir);
  1652. }
  1653. /*
  1654. * Function nsc_ircc_is_receiving (self)
  1655. *
  1656. * Return TRUE is we are currently receiving a frame
  1657. *
  1658. */
  1659. static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
  1660. {
  1661. unsigned long flags;
  1662. int status = FALSE;
  1663. int iobase;
  1664. __u8 bank;
  1665. IRDA_ASSERT(self != NULL, return FALSE;);
  1666. spin_lock_irqsave(&self->lock, flags);
  1667. if (self->io.speed > 115200) {
  1668. iobase = self->io.fir_base;
  1669. /* Check if rx FIFO is not empty */
  1670. bank = inb(iobase+BSR);
  1671. switch_bank(iobase, BANK2);
  1672. if ((inb(iobase+RXFLV) & 0x3f) != 0) {
  1673. /* We are receiving something */
  1674. status = TRUE;
  1675. }
  1676. outb(bank, iobase+BSR);
  1677. } else
  1678. status = (self->rx_buff.state != OUTSIDE_FRAME);
  1679. spin_unlock_irqrestore(&self->lock, flags);
  1680. return status;
  1681. }
  1682. /*
  1683. * Function nsc_ircc_net_open (dev)
  1684. *
  1685. * Start the device
  1686. *
  1687. */
  1688. static int nsc_ircc_net_open(struct net_device *dev)
  1689. {
  1690. struct nsc_ircc_cb *self;
  1691. int iobase;
  1692. char hwname[32];
  1693. __u8 bank;
  1694. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1695. IRDA_ASSERT(dev != NULL, return -1;);
  1696. self = (struct nsc_ircc_cb *) dev->priv;
  1697. IRDA_ASSERT(self != NULL, return 0;);
  1698. iobase = self->io.fir_base;
  1699. if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
  1700. IRDA_WARNING("%s, unable to allocate irq=%d\n",
  1701. driver_name, self->io.irq);
  1702. return -EAGAIN;
  1703. }
  1704. /*
  1705. * Always allocate the DMA channel after the IRQ, and clean up on
  1706. * failure.
  1707. */
  1708. if (request_dma(self->io.dma, dev->name)) {
  1709. IRDA_WARNING("%s, unable to allocate dma=%d\n",
  1710. driver_name, self->io.dma);
  1711. free_irq(self->io.irq, dev);
  1712. return -EAGAIN;
  1713. }
  1714. /* Save current bank */
  1715. bank = inb(iobase+BSR);
  1716. /* turn on interrupts */
  1717. switch_bank(iobase, BANK0);
  1718. outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
  1719. /* Restore bank register */
  1720. outb(bank, iobase+BSR);
  1721. /* Ready to play! */
  1722. netif_start_queue(dev);
  1723. /* Give self a hardware name */
  1724. sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
  1725. /*
  1726. * Open new IrLAP layer instance, now that everything should be
  1727. * initialized properly
  1728. */
  1729. self->irlap = irlap_open(dev, &self->qos, hwname);
  1730. return 0;
  1731. }
  1732. /*
  1733. * Function nsc_ircc_net_close (dev)
  1734. *
  1735. * Stop the device
  1736. *
  1737. */
  1738. static int nsc_ircc_net_close(struct net_device *dev)
  1739. {
  1740. struct nsc_ircc_cb *self;
  1741. int iobase;
  1742. __u8 bank;
  1743. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1744. IRDA_ASSERT(dev != NULL, return -1;);
  1745. self = (struct nsc_ircc_cb *) dev->priv;
  1746. IRDA_ASSERT(self != NULL, return 0;);
  1747. /* Stop device */
  1748. netif_stop_queue(dev);
  1749. /* Stop and remove instance of IrLAP */
  1750. if (self->irlap)
  1751. irlap_close(self->irlap);
  1752. self->irlap = NULL;
  1753. iobase = self->io.fir_base;
  1754. disable_dma(self->io.dma);
  1755. /* Save current bank */
  1756. bank = inb(iobase+BSR);
  1757. /* Disable interrupts */
  1758. switch_bank(iobase, BANK0);
  1759. outb(0, iobase+IER);
  1760. free_irq(self->io.irq, dev);
  1761. free_dma(self->io.dma);
  1762. /* Restore bank register */
  1763. outb(bank, iobase+BSR);
  1764. return 0;
  1765. }
  1766. /*
  1767. * Function nsc_ircc_net_ioctl (dev, rq, cmd)
  1768. *
  1769. * Process IOCTL commands for this device
  1770. *
  1771. */
  1772. static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1773. {
  1774. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1775. struct nsc_ircc_cb *self;
  1776. unsigned long flags;
  1777. int ret = 0;
  1778. IRDA_ASSERT(dev != NULL, return -1;);
  1779. self = dev->priv;
  1780. IRDA_ASSERT(self != NULL, return -1;);
  1781. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
  1782. switch (cmd) {
  1783. case SIOCSBANDWIDTH: /* Set bandwidth */
  1784. if (!capable(CAP_NET_ADMIN)) {
  1785. ret = -EPERM;
  1786. break;
  1787. }
  1788. spin_lock_irqsave(&self->lock, flags);
  1789. nsc_ircc_change_speed(self, irq->ifr_baudrate);
  1790. spin_unlock_irqrestore(&self->lock, flags);
  1791. break;
  1792. case SIOCSMEDIABUSY: /* Set media busy */
  1793. if (!capable(CAP_NET_ADMIN)) {
  1794. ret = -EPERM;
  1795. break;
  1796. }
  1797. irda_device_set_media_busy(self->netdev, TRUE);
  1798. break;
  1799. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1800. /* This is already protected */
  1801. irq->ifr_receiving = nsc_ircc_is_receiving(self);
  1802. break;
  1803. default:
  1804. ret = -EOPNOTSUPP;
  1805. }
  1806. return ret;
  1807. }
  1808. static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
  1809. {
  1810. struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
  1811. return &self->stats;
  1812. }
  1813. static void nsc_ircc_suspend(struct nsc_ircc_cb *self)
  1814. {
  1815. IRDA_MESSAGE("%s, Suspending\n", driver_name);
  1816. if (self->io.suspended)
  1817. return;
  1818. nsc_ircc_net_close(self->netdev);
  1819. self->io.suspended = 1;
  1820. }
  1821. static void nsc_ircc_wakeup(struct nsc_ircc_cb *self)
  1822. {
  1823. if (!self->io.suspended)
  1824. return;
  1825. nsc_ircc_setup(&self->io);
  1826. nsc_ircc_net_open(self->netdev);
  1827. IRDA_MESSAGE("%s, Waking up\n", driver_name);
  1828. self->io.suspended = 0;
  1829. }
  1830. static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
  1831. {
  1832. struct nsc_ircc_cb *self = (struct nsc_ircc_cb*) dev->data;
  1833. if (self) {
  1834. switch (rqst) {
  1835. case PM_SUSPEND:
  1836. nsc_ircc_suspend(self);
  1837. break;
  1838. case PM_RESUME:
  1839. nsc_ircc_wakeup(self);
  1840. break;
  1841. }
  1842. }
  1843. return 0;
  1844. }
  1845. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  1846. MODULE_DESCRIPTION("NSC IrDA Device Driver");
  1847. MODULE_LICENSE("GPL");
  1848. module_param(qos_mtt_bits, int, 0);
  1849. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  1850. module_param_array(io, int, NULL, 0);
  1851. MODULE_PARM_DESC(io, "Base I/O addresses");
  1852. module_param_array(irq, int, NULL, 0);
  1853. MODULE_PARM_DESC(irq, "IRQ lines");
  1854. module_param_array(dma, int, NULL, 0);
  1855. MODULE_PARM_DESC(dma, "DMA channels");
  1856. module_param(dongle_id, int, 0);
  1857. MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
  1858. module_init(nsc_ircc_init);
  1859. module_exit(nsc_ircc_cleanup);