nsc-ircc.c 59 KB

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