nsc-ircc.c 59 KB

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