nsc-ircc.c 59 KB

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