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