nsc-ircc.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  1. /*********************************************************************
  2. *
  3. * Filename: nsc-ircc.c
  4. * Version: 1.0
  5. * Description: Driver for the NSC PC'108 and PC'338 IrDA chipsets
  6. * Status: Stable.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sat Nov 7 21:43:15 1998
  9. * Modified at: Wed Mar 1 11:29:34 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
  13. * Copyright (c) 1998 Lichen Wang, <lwang@actisys.com>
  14. * Copyright (c) 1998 Actisys Corp., www.actisys.com
  15. * Copyright (c) 2000-2004 Jean Tourrilhes <jt@hpl.hp.com>
  16. * All Rights Reserved
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * Neither Dag Brattli nor University of Tromsø admit liability nor
  24. * provide warranty for any of this software. This material is
  25. * provided "AS-IS" and at no charge.
  26. *
  27. * Notice that all functions that needs to access the chip in _any_
  28. * way, must save BSR register on entry, and restore it on exit.
  29. * It is _very_ important to follow this policy!
  30. *
  31. * __u8 bank;
  32. *
  33. * bank = inb(iobase+BSR);
  34. *
  35. * do_your_stuff_here();
  36. *
  37. * outb(bank, iobase+BSR);
  38. *
  39. * If you find bugs in this file, its very likely that the same bug
  40. * will also be in w83977af_ir.c since the implementations are quite
  41. * similar.
  42. *
  43. ********************************************************************/
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/types.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/ioport.h>
  50. #include <linux/delay.h>
  51. #include <linux/slab.h>
  52. #include <linux/init.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/dma-mapping.h>
  55. #include <linux/pnp.h>
  56. #include <linux/platform_device.h>
  57. #include <asm/io.h>
  58. #include <asm/dma.h>
  59. #include <asm/byteorder.h>
  60. #include <net/irda/wrapper.h>
  61. #include <net/irda/irda.h>
  62. #include <net/irda/irda_device.h>
  63. #include "nsc-ircc.h"
  64. #define CHIP_IO_EXTENT 8
  65. #define BROKEN_DONGLE_ID
  66. static char *driver_name = "nsc-ircc";
  67. /* Power Management */
  68. #define NSC_IRCC_DRIVER_NAME "nsc-ircc"
  69. static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state);
  70. static int nsc_ircc_resume(struct platform_device *dev);
  71. static struct platform_driver nsc_ircc_driver = {
  72. .suspend = nsc_ircc_suspend,
  73. .resume = nsc_ircc_resume,
  74. .driver = {
  75. .name = NSC_IRCC_DRIVER_NAME,
  76. },
  77. };
  78. /* Module parameters */
  79. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  80. static int dongle_id;
  81. /* Use BIOS settions by default, but user may supply module parameters */
  82. static unsigned int io[] = { ~0, ~0, ~0, ~0, ~0 };
  83. static unsigned int irq[] = { 0, 0, 0, 0, 0 };
  84. static unsigned int dma[] = { 0, 0, 0, 0, 0 };
  85. static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
  86. static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
  87. static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
  88. static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
  89. static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
  90. static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
  91. #ifdef CONFIG_PNP
  92. static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id);
  93. #endif
  94. /* These are the known NSC chips */
  95. static nsc_chip_t chips[] = {
  96. /* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
  97. { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
  98. nsc_ircc_probe_108, nsc_ircc_init_108 },
  99. { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
  100. nsc_ircc_probe_338, nsc_ircc_init_338 },
  101. /* Contributed by Steffen Pingel - IBM X40 */
  102. { "PC8738x", { 0x164e, 0x4e, 0x2e }, 0x20, 0xf4, 0xff,
  103. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  104. /* Contributed by Jan Frey - IBM A30/A31 */
  105. { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
  106. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  107. /* IBM ThinkPads using PC8738x (T60/X60/Z60) */
  108. { "IBM-PC8738x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
  109. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  110. /* IBM ThinkPads using PC8394T (T43/R52/?) */
  111. { "IBM-PC8394T", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf9, 0xff,
  112. nsc_ircc_probe_39x, nsc_ircc_init_39x },
  113. { NULL }
  114. };
  115. static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL, NULL };
  116. static char *dongle_types[] = {
  117. "Differential serial interface",
  118. "Differential serial interface",
  119. "Reserved",
  120. "Reserved",
  121. "Sharp RY5HD01",
  122. "Reserved",
  123. "Single-ended serial interface",
  124. "Consumer-IR only",
  125. "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
  126. "IBM31T1100 or Temic TFDS6000/TFDS6500",
  127. "Reserved",
  128. "Reserved",
  129. "HP HSDL-1100/HSDL-2100",
  130. "HP HSDL-1100/HSDL-2100",
  131. "Supports SIR Mode only",
  132. "No dongle connected",
  133. };
  134. /* PNP probing */
  135. static chipio_t pnp_info;
  136. static const struct pnp_device_id nsc_ircc_pnp_table[] = {
  137. { .id = "NSC6001", .driver_data = 0 },
  138. { .id = "IBM0071", .driver_data = 0 },
  139. { .id = "HWPC224", .driver_data = 0 },
  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", __FUNCTION__,
  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", __FUNCTION__, 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. __FUNCTION__, 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", __FUNCTION__, 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", __FUNCTION__);
  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", __FUNCTION__);
  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", __FUNCTION__);
  321. return -ENOMEM;
  322. }
  323. self = dev->priv;
  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. __FUNCTION__, 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", __FUNCTION__);
  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", __FUNCTION__);
  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. __FUNCTION__, 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", __FUNCTION__);
  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", __FUNCTION__);
  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", __FUNCTION__);
  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", __FUNCTION__,
  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", __FUNCTION__, 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", __FUNCTION__, 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. __FUNCTION__, 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. __FUNCTION__, 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", __FUNCTION__, 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. /* There don't seem to be any way to get the cfg_base.
  790. * On my box, cfg_base is in the PnP descriptor of the
  791. * motherboard. Oh well... Jean II */
  792. if (pnp_port_valid(dev, 0) &&
  793. !(pnp_port_flags(dev, 0) & IORESOURCE_DISABLED))
  794. pnp_info.fir_base = pnp_port_start(dev, 0);
  795. if (pnp_irq_valid(dev, 0) &&
  796. !(pnp_irq_flags(dev, 0) & IORESOURCE_DISABLED))
  797. pnp_info.irq = pnp_irq(dev, 0);
  798. if (pnp_dma_valid(dev, 0) &&
  799. !(pnp_dma_flags(dev, 0) & IORESOURCE_DISABLED))
  800. pnp_info.dma = pnp_dma(dev, 0);
  801. IRDA_DEBUG(0, "%s() : From PnP, found firbase 0x%03X ; irq %d ; dma %d.\n",
  802. __FUNCTION__, pnp_info.fir_base, pnp_info.irq, pnp_info.dma);
  803. if((pnp_info.fir_base == 0) ||
  804. (pnp_info.irq == -1) || (pnp_info.dma == -1)) {
  805. /* Returning an error will disable the device. Yuck ! */
  806. //return -EINVAL;
  807. pnp_succeeded = 0;
  808. }
  809. return 0;
  810. }
  811. #endif
  812. /*
  813. * Function nsc_ircc_setup (info)
  814. *
  815. * Returns non-negative on success.
  816. *
  817. */
  818. static int nsc_ircc_setup(chipio_t *info)
  819. {
  820. int version;
  821. int iobase = info->fir_base;
  822. /* Read the Module ID */
  823. switch_bank(iobase, BANK3);
  824. version = inb(iobase+MID);
  825. IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
  826. __FUNCTION__, driver_name, version);
  827. /* Should be 0x2? */
  828. if (0x20 != (version & 0xf0)) {
  829. IRDA_ERROR("%s, Wrong chip version %02x\n",
  830. driver_name, version);
  831. return -1;
  832. }
  833. /* Switch to advanced mode */
  834. switch_bank(iobase, BANK2);
  835. outb(ECR1_EXT_SL, iobase+ECR1);
  836. switch_bank(iobase, BANK0);
  837. /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
  838. switch_bank(iobase, BANK0);
  839. outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  840. outb(0x03, iobase+LCR); /* 8 bit word length */
  841. outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
  842. /* Set FIFO size to 32 */
  843. switch_bank(iobase, BANK2);
  844. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  845. /* IRCR2: FEND_MD is not set */
  846. switch_bank(iobase, BANK5);
  847. outb(0x02, iobase+4);
  848. /* Make sure that some defaults are OK */
  849. switch_bank(iobase, BANK6);
  850. outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
  851. outb(0x0a, iobase+1); /* Set MIR pulse width */
  852. outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
  853. outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
  854. /* Enable receive interrupts */
  855. switch_bank(iobase, BANK0);
  856. outb(IER_RXHDL_IE, iobase+IER);
  857. return 0;
  858. }
  859. /*
  860. * Function nsc_ircc_read_dongle_id (void)
  861. *
  862. * Try to read dongle indentification. This procedure needs to be executed
  863. * once after power-on/reset. It also needs to be used whenever you suspect
  864. * that the user may have plugged/unplugged the IrDA Dongle.
  865. */
  866. static int nsc_ircc_read_dongle_id (int iobase)
  867. {
  868. int dongle_id;
  869. __u8 bank;
  870. bank = inb(iobase+BSR);
  871. /* Select Bank 7 */
  872. switch_bank(iobase, BANK7);
  873. /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
  874. outb(0x00, iobase+7);
  875. /* ID0, 1, and 2 are pulled up/down very slowly */
  876. udelay(50);
  877. /* IRCFG1: read the ID bits */
  878. dongle_id = inb(iobase+4) & 0x0f;
  879. #ifdef BROKEN_DONGLE_ID
  880. if (dongle_id == 0x0a)
  881. dongle_id = 0x09;
  882. #endif
  883. /* Go back to bank 0 before returning */
  884. switch_bank(iobase, BANK0);
  885. outb(bank, iobase+BSR);
  886. return dongle_id;
  887. }
  888. /*
  889. * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
  890. *
  891. * This function initializes the dongle for the transceiver that is
  892. * used. This procedure needs to be executed once after
  893. * power-on/reset. It also needs to be used whenever you suspect that
  894. * the dongle is changed.
  895. */
  896. static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
  897. {
  898. int bank;
  899. /* Save current bank */
  900. bank = inb(iobase+BSR);
  901. /* Select Bank 7 */
  902. switch_bank(iobase, BANK7);
  903. /* IRCFG4: set according to dongle_id */
  904. switch (dongle_id) {
  905. case 0x00: /* same as */
  906. case 0x01: /* Differential serial interface */
  907. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  908. __FUNCTION__, dongle_types[dongle_id]);
  909. break;
  910. case 0x02: /* same as */
  911. case 0x03: /* Reserved */
  912. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  913. __FUNCTION__, dongle_types[dongle_id]);
  914. break;
  915. case 0x04: /* Sharp RY5HD01 */
  916. break;
  917. case 0x05: /* Reserved, but this is what the Thinkpad reports */
  918. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  919. __FUNCTION__, dongle_types[dongle_id]);
  920. break;
  921. case 0x06: /* Single-ended serial interface */
  922. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  923. __FUNCTION__, dongle_types[dongle_id]);
  924. break;
  925. case 0x07: /* Consumer-IR only */
  926. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  927. __FUNCTION__, dongle_types[dongle_id]);
  928. break;
  929. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  930. IRDA_DEBUG(0, "%s(), %s\n",
  931. __FUNCTION__, dongle_types[dongle_id]);
  932. break;
  933. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  934. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  935. break;
  936. case 0x0A: /* same as */
  937. case 0x0B: /* Reserved */
  938. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  939. __FUNCTION__, dongle_types[dongle_id]);
  940. break;
  941. case 0x0C: /* same as */
  942. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  943. /*
  944. * Set irsl0 as input, irsl[1-2] as output, and separate
  945. * inputs are used for SIR and MIR/FIR
  946. */
  947. outb(0x48, iobase+7);
  948. break;
  949. case 0x0E: /* Supports SIR Mode only */
  950. outb(0x28, iobase+7); /* Set irsl[0-2] as output */
  951. break;
  952. case 0x0F: /* No dongle connected */
  953. IRDA_DEBUG(0, "%s(), %s\n",
  954. __FUNCTION__, dongle_types[dongle_id]);
  955. switch_bank(iobase, BANK0);
  956. outb(0x62, iobase+MCR);
  957. break;
  958. default:
  959. IRDA_DEBUG(0, "%s(), invalid dongle_id %#x",
  960. __FUNCTION__, dongle_id);
  961. }
  962. /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
  963. outb(0x00, iobase+4);
  964. /* Restore bank register */
  965. outb(bank, iobase+BSR);
  966. } /* set_up_dongle_interface */
  967. /*
  968. * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
  969. *
  970. * Change speed of the attach dongle
  971. *
  972. */
  973. static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
  974. {
  975. __u8 bank;
  976. /* Save current bank */
  977. bank = inb(iobase+BSR);
  978. /* Select Bank 7 */
  979. switch_bank(iobase, BANK7);
  980. /* IRCFG1: set according to dongle_id */
  981. switch (dongle_id) {
  982. case 0x00: /* same as */
  983. case 0x01: /* Differential serial interface */
  984. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  985. __FUNCTION__, dongle_types[dongle_id]);
  986. break;
  987. case 0x02: /* same as */
  988. case 0x03: /* Reserved */
  989. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  990. __FUNCTION__, dongle_types[dongle_id]);
  991. break;
  992. case 0x04: /* Sharp RY5HD01 */
  993. break;
  994. case 0x05: /* Reserved */
  995. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  996. __FUNCTION__, dongle_types[dongle_id]);
  997. break;
  998. case 0x06: /* Single-ended serial interface */
  999. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  1000. __FUNCTION__, dongle_types[dongle_id]);
  1001. break;
  1002. case 0x07: /* Consumer-IR only */
  1003. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  1004. __FUNCTION__, dongle_types[dongle_id]);
  1005. break;
  1006. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  1007. IRDA_DEBUG(0, "%s(), %s\n",
  1008. __FUNCTION__, dongle_types[dongle_id]);
  1009. outb(0x00, iobase+4);
  1010. if (speed > 115200)
  1011. outb(0x01, iobase+4);
  1012. break;
  1013. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  1014. outb(0x01, iobase+4);
  1015. if (speed == 4000000) {
  1016. /* There was a cli() there, but we now are already
  1017. * under spin_lock_irqsave() - JeanII */
  1018. outb(0x81, iobase+4);
  1019. outb(0x80, iobase+4);
  1020. } else
  1021. outb(0x00, iobase+4);
  1022. break;
  1023. case 0x0A: /* same as */
  1024. case 0x0B: /* Reserved */
  1025. IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
  1026. __FUNCTION__, dongle_types[dongle_id]);
  1027. break;
  1028. case 0x0C: /* same as */
  1029. case 0x0D: /* HP HSDL-1100/HSDL-2100 */
  1030. break;
  1031. case 0x0E: /* Supports SIR Mode only */
  1032. break;
  1033. case 0x0F: /* No dongle connected */
  1034. IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
  1035. __FUNCTION__, dongle_types[dongle_id]);
  1036. switch_bank(iobase, BANK0);
  1037. outb(0x62, iobase+MCR);
  1038. break;
  1039. default:
  1040. IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
  1041. }
  1042. /* Restore bank register */
  1043. outb(bank, iobase+BSR);
  1044. }
  1045. /*
  1046. * Function nsc_ircc_change_speed (self, baud)
  1047. *
  1048. * Change the speed of the device
  1049. *
  1050. * This function *must* be called with irq off and spin-lock.
  1051. */
  1052. static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
  1053. {
  1054. struct net_device *dev = self->netdev;
  1055. __u8 mcr = MCR_SIR;
  1056. int iobase;
  1057. __u8 bank;
  1058. __u8 ier; /* Interrupt enable register */
  1059. IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
  1060. IRDA_ASSERT(self != NULL, return 0;);
  1061. iobase = self->io.fir_base;
  1062. /* Update accounting for new speed */
  1063. self->io.speed = speed;
  1064. /* Save current bank */
  1065. bank = inb(iobase+BSR);
  1066. /* Disable interrupts */
  1067. switch_bank(iobase, BANK0);
  1068. outb(0, iobase+IER);
  1069. /* Select Bank 2 */
  1070. switch_bank(iobase, BANK2);
  1071. outb(0x00, iobase+BGDH);
  1072. switch (speed) {
  1073. case 9600: outb(0x0c, iobase+BGDL); break;
  1074. case 19200: outb(0x06, iobase+BGDL); break;
  1075. case 38400: outb(0x03, iobase+BGDL); break;
  1076. case 57600: outb(0x02, iobase+BGDL); break;
  1077. case 115200: outb(0x01, iobase+BGDL); break;
  1078. case 576000:
  1079. switch_bank(iobase, BANK5);
  1080. /* IRCR2: MDRS is set */
  1081. outb(inb(iobase+4) | 0x04, iobase+4);
  1082. mcr = MCR_MIR;
  1083. IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
  1084. break;
  1085. case 1152000:
  1086. mcr = MCR_MIR;
  1087. IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
  1088. break;
  1089. case 4000000:
  1090. mcr = MCR_FIR;
  1091. IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
  1092. break;
  1093. default:
  1094. mcr = MCR_FIR;
  1095. IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n",
  1096. __FUNCTION__, speed);
  1097. break;
  1098. }
  1099. /* Set appropriate speed mode */
  1100. switch_bank(iobase, BANK0);
  1101. outb(mcr | MCR_TX_DFR, iobase+MCR);
  1102. /* Give some hits to the transceiver */
  1103. nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
  1104. /* Set FIFO threshold to TX17, RX16 */
  1105. switch_bank(iobase, BANK0);
  1106. outb(0x00, iobase+FCR);
  1107. outb(FCR_FIFO_EN, iobase+FCR);
  1108. outb(FCR_RXTH| /* Set Rx FIFO threshold */
  1109. FCR_TXTH| /* Set Tx FIFO threshold */
  1110. FCR_TXSR| /* Reset Tx FIFO */
  1111. FCR_RXSR| /* Reset Rx FIFO */
  1112. FCR_FIFO_EN, /* Enable FIFOs */
  1113. iobase+FCR);
  1114. /* Set FIFO size to 32 */
  1115. switch_bank(iobase, BANK2);
  1116. outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
  1117. /* Enable some interrupts so we can receive frames */
  1118. switch_bank(iobase, BANK0);
  1119. if (speed > 115200) {
  1120. /* Install FIR xmit handler */
  1121. dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
  1122. ier = IER_SFIF_IE;
  1123. nsc_ircc_dma_receive(self);
  1124. } else {
  1125. /* Install SIR xmit handler */
  1126. dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
  1127. ier = IER_RXHDL_IE;
  1128. }
  1129. /* Set our current interrupt mask */
  1130. outb(ier, iobase+IER);
  1131. /* Restore BSR */
  1132. outb(bank, iobase+BSR);
  1133. /* Make sure interrupt handlers keep the proper interrupt mask */
  1134. return(ier);
  1135. }
  1136. /*
  1137. * Function nsc_ircc_hard_xmit (skb, dev)
  1138. *
  1139. * Transmit the frame!
  1140. *
  1141. */
  1142. static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
  1143. {
  1144. struct nsc_ircc_cb *self;
  1145. unsigned long flags;
  1146. int iobase;
  1147. __s32 speed;
  1148. __u8 bank;
  1149. self = (struct nsc_ircc_cb *) dev->priv;
  1150. IRDA_ASSERT(self != NULL, return 0;);
  1151. iobase = self->io.fir_base;
  1152. netif_stop_queue(dev);
  1153. /* Make sure tests *& speed change are atomic */
  1154. spin_lock_irqsave(&self->lock, flags);
  1155. /* Check if we need to change the speed */
  1156. speed = irda_get_next_speed(skb);
  1157. if ((speed != self->io.speed) && (speed != -1)) {
  1158. /* Check for empty frame. */
  1159. if (!skb->len) {
  1160. /* If we just sent a frame, we get called before
  1161. * the last bytes get out (because of the SIR FIFO).
  1162. * If this is the case, let interrupt handler change
  1163. * the speed itself... Jean II */
  1164. if (self->io.direction == IO_RECV) {
  1165. nsc_ircc_change_speed(self, speed);
  1166. /* TODO : For SIR->SIR, the next packet
  1167. * may get corrupted - Jean II */
  1168. netif_wake_queue(dev);
  1169. } else {
  1170. self->new_speed = speed;
  1171. /* Queue will be restarted after speed change
  1172. * to make sure packets gets through the
  1173. * proper xmit handler - Jean II */
  1174. }
  1175. dev->trans_start = jiffies;
  1176. spin_unlock_irqrestore(&self->lock, flags);
  1177. dev_kfree_skb(skb);
  1178. return 0;
  1179. } else
  1180. self->new_speed = speed;
  1181. }
  1182. /* Save current bank */
  1183. bank = inb(iobase+BSR);
  1184. self->tx_buff.data = self->tx_buff.head;
  1185. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  1186. self->tx_buff.truesize);
  1187. self->stats.tx_bytes += self->tx_buff.len;
  1188. /* Add interrupt on tx low level (will fire immediately) */
  1189. switch_bank(iobase, BANK0);
  1190. outb(IER_TXLDL_IE, iobase+IER);
  1191. /* Restore bank register */
  1192. outb(bank, iobase+BSR);
  1193. dev->trans_start = jiffies;
  1194. spin_unlock_irqrestore(&self->lock, flags);
  1195. dev_kfree_skb(skb);
  1196. return 0;
  1197. }
  1198. static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
  1199. {
  1200. struct nsc_ircc_cb *self;
  1201. unsigned long flags;
  1202. int iobase;
  1203. __s32 speed;
  1204. __u8 bank;
  1205. int mtt, diff;
  1206. self = (struct nsc_ircc_cb *) dev->priv;
  1207. iobase = self->io.fir_base;
  1208. netif_stop_queue(dev);
  1209. /* Make sure tests *& speed change are atomic */
  1210. spin_lock_irqsave(&self->lock, flags);
  1211. /* Check if we need to change the speed */
  1212. speed = irda_get_next_speed(skb);
  1213. if ((speed != self->io.speed) && (speed != -1)) {
  1214. /* Check for empty frame. */
  1215. if (!skb->len) {
  1216. /* If we are currently transmitting, defer to
  1217. * interrupt handler. - Jean II */
  1218. if(self->tx_fifo.len == 0) {
  1219. nsc_ircc_change_speed(self, speed);
  1220. netif_wake_queue(dev);
  1221. } else {
  1222. self->new_speed = speed;
  1223. /* Keep queue stopped :
  1224. * the speed change operation may change the
  1225. * xmit handler, and we want to make sure
  1226. * the next packet get through the proper
  1227. * Tx path, so block the Tx queue until
  1228. * the speed change has been done.
  1229. * Jean II */
  1230. }
  1231. dev->trans_start = jiffies;
  1232. spin_unlock_irqrestore(&self->lock, flags);
  1233. dev_kfree_skb(skb);
  1234. return 0;
  1235. } else {
  1236. /* Change speed after current frame */
  1237. self->new_speed = speed;
  1238. }
  1239. }
  1240. /* Save current bank */
  1241. bank = inb(iobase+BSR);
  1242. /* Register and copy this frame to DMA memory */
  1243. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  1244. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  1245. self->tx_fifo.tail += skb->len;
  1246. self->stats.tx_bytes += skb->len;
  1247. skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
  1248. skb->len);
  1249. self->tx_fifo.len++;
  1250. self->tx_fifo.free++;
  1251. /* Start transmit only if there is currently no transmit going on */
  1252. if (self->tx_fifo.len == 1) {
  1253. /* Check if we must wait the min turn time or not */
  1254. mtt = irda_get_mtt(skb);
  1255. if (mtt) {
  1256. /* Check how much time we have used already */
  1257. do_gettimeofday(&self->now);
  1258. diff = self->now.tv_usec - self->stamp.tv_usec;
  1259. if (diff < 0)
  1260. diff += 1000000;
  1261. /* Check if the mtt is larger than the time we have
  1262. * already used by all the protocol processing
  1263. */
  1264. if (mtt > diff) {
  1265. mtt -= diff;
  1266. /*
  1267. * Use timer if delay larger than 125 us, and
  1268. * use udelay for smaller values which should
  1269. * be acceptable
  1270. */
  1271. if (mtt > 125) {
  1272. /* Adjust for timer resolution */
  1273. mtt = mtt / 125;
  1274. /* Setup timer */
  1275. switch_bank(iobase, BANK4);
  1276. outb(mtt & 0xff, iobase+TMRL);
  1277. outb((mtt >> 8) & 0x0f, iobase+TMRH);
  1278. /* Start timer */
  1279. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1280. self->io.direction = IO_XMIT;
  1281. /* Enable timer interrupt */
  1282. switch_bank(iobase, BANK0);
  1283. outb(IER_TMR_IE, iobase+IER);
  1284. /* Timer will take care of the rest */
  1285. goto out;
  1286. } else
  1287. udelay(mtt);
  1288. }
  1289. }
  1290. /* Enable DMA interrupt */
  1291. switch_bank(iobase, BANK0);
  1292. outb(IER_DMA_IE, iobase+IER);
  1293. /* Transmit frame */
  1294. nsc_ircc_dma_xmit(self, iobase);
  1295. }
  1296. out:
  1297. /* Not busy transmitting anymore if window is not full,
  1298. * and if we don't need to change speed */
  1299. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
  1300. netif_wake_queue(self->netdev);
  1301. /* Restore bank register */
  1302. outb(bank, iobase+BSR);
  1303. dev->trans_start = jiffies;
  1304. spin_unlock_irqrestore(&self->lock, flags);
  1305. dev_kfree_skb(skb);
  1306. return 0;
  1307. }
  1308. /*
  1309. * Function nsc_ircc_dma_xmit (self, iobase)
  1310. *
  1311. * Transmit data using DMA
  1312. *
  1313. */
  1314. static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
  1315. {
  1316. int bsr;
  1317. /* Save current bank */
  1318. bsr = inb(iobase+BSR);
  1319. /* Disable DMA */
  1320. switch_bank(iobase, BANK0);
  1321. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1322. self->io.direction = IO_XMIT;
  1323. /* Choose transmit DMA channel */
  1324. switch_bank(iobase, BANK2);
  1325. outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1326. irda_setup_dma(self->io.dma,
  1327. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  1328. self->tx_buff.head) + self->tx_buff_dma,
  1329. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  1330. DMA_TX_MODE);
  1331. /* Enable DMA and SIR interaction pulse */
  1332. switch_bank(iobase, BANK0);
  1333. outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
  1334. /* Restore bank register */
  1335. outb(bsr, iobase+BSR);
  1336. }
  1337. /*
  1338. * Function nsc_ircc_pio_xmit (self, iobase)
  1339. *
  1340. * Transmit data using PIO. Returns the number of bytes that actually
  1341. * got transferred
  1342. *
  1343. */
  1344. static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
  1345. {
  1346. int actual = 0;
  1347. __u8 bank;
  1348. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1349. /* Save current bank */
  1350. bank = inb(iobase+BSR);
  1351. switch_bank(iobase, BANK0);
  1352. if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
  1353. IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
  1354. __FUNCTION__);
  1355. /* FIFO may still be filled to the Tx interrupt threshold */
  1356. fifo_size -= 17;
  1357. }
  1358. /* Fill FIFO with current frame */
  1359. while ((fifo_size-- > 0) && (actual < len)) {
  1360. /* Transmit next byte */
  1361. outb(buf[actual++], iobase+TXD);
  1362. }
  1363. IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
  1364. __FUNCTION__, fifo_size, actual, len);
  1365. /* Restore bank */
  1366. outb(bank, iobase+BSR);
  1367. return actual;
  1368. }
  1369. /*
  1370. * Function nsc_ircc_dma_xmit_complete (self)
  1371. *
  1372. * The transfer of a frame in finished. This function will only be called
  1373. * by the interrupt handler
  1374. *
  1375. */
  1376. static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
  1377. {
  1378. int iobase;
  1379. __u8 bank;
  1380. int ret = TRUE;
  1381. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  1382. iobase = self->io.fir_base;
  1383. /* Save current bank */
  1384. bank = inb(iobase+BSR);
  1385. /* Disable DMA */
  1386. switch_bank(iobase, BANK0);
  1387. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1388. /* Check for underrrun! */
  1389. if (inb(iobase+ASCR) & ASCR_TXUR) {
  1390. self->stats.tx_errors++;
  1391. self->stats.tx_fifo_errors++;
  1392. /* Clear bit, by writing 1 into it */
  1393. outb(ASCR_TXUR, iobase+ASCR);
  1394. } else {
  1395. self->stats.tx_packets++;
  1396. }
  1397. /* Finished with this frame, so prepare for next */
  1398. self->tx_fifo.ptr++;
  1399. self->tx_fifo.len--;
  1400. /* Any frames to be sent back-to-back? */
  1401. if (self->tx_fifo.len) {
  1402. nsc_ircc_dma_xmit(self, iobase);
  1403. /* Not finished yet! */
  1404. ret = FALSE;
  1405. } else {
  1406. /* Reset Tx FIFO info */
  1407. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1408. self->tx_fifo.tail = self->tx_buff.head;
  1409. }
  1410. /* Make sure we have room for more frames and
  1411. * that we don't need to change speed */
  1412. if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
  1413. /* Not busy transmitting anymore */
  1414. /* Tell the network layer, that we can accept more frames */
  1415. netif_wake_queue(self->netdev);
  1416. }
  1417. /* Restore bank */
  1418. outb(bank, iobase+BSR);
  1419. return ret;
  1420. }
  1421. /*
  1422. * Function nsc_ircc_dma_receive (self)
  1423. *
  1424. * Get ready for receiving a frame. The device will initiate a DMA
  1425. * if it starts to receive a frame.
  1426. *
  1427. */
  1428. static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
  1429. {
  1430. int iobase;
  1431. __u8 bsr;
  1432. iobase = self->io.fir_base;
  1433. /* Reset Tx FIFO info */
  1434. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1435. self->tx_fifo.tail = self->tx_buff.head;
  1436. /* Save current bank */
  1437. bsr = inb(iobase+BSR);
  1438. /* Disable DMA */
  1439. switch_bank(iobase, BANK0);
  1440. outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
  1441. /* Choose DMA Rx, DMA Fairness, and Advanced mode */
  1442. switch_bank(iobase, BANK2);
  1443. outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
  1444. self->io.direction = IO_RECV;
  1445. self->rx_buff.data = self->rx_buff.head;
  1446. /* Reset Rx FIFO. This will also flush the ST_FIFO */
  1447. switch_bank(iobase, BANK0);
  1448. outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
  1449. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  1450. self->st_fifo.tail = self->st_fifo.head = 0;
  1451. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  1452. DMA_RX_MODE);
  1453. /* Enable DMA */
  1454. switch_bank(iobase, BANK0);
  1455. outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
  1456. /* Restore bank register */
  1457. outb(bsr, iobase+BSR);
  1458. return 0;
  1459. }
  1460. /*
  1461. * Function nsc_ircc_dma_receive_complete (self)
  1462. *
  1463. * Finished with receiving frames
  1464. *
  1465. *
  1466. */
  1467. static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
  1468. {
  1469. struct st_fifo *st_fifo;
  1470. struct sk_buff *skb;
  1471. __u8 status;
  1472. __u8 bank;
  1473. int len;
  1474. st_fifo = &self->st_fifo;
  1475. /* Save current bank */
  1476. bank = inb(iobase+BSR);
  1477. /* Read all entries in status FIFO */
  1478. switch_bank(iobase, BANK5);
  1479. while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
  1480. /* We must empty the status FIFO no matter what */
  1481. len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
  1482. if (st_fifo->tail >= MAX_RX_WINDOW) {
  1483. IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
  1484. continue;
  1485. }
  1486. st_fifo->entries[st_fifo->tail].status = status;
  1487. st_fifo->entries[st_fifo->tail].len = len;
  1488. st_fifo->pending_bytes += len;
  1489. st_fifo->tail++;
  1490. st_fifo->len++;
  1491. }
  1492. /* Try to process all entries in status FIFO */
  1493. while (st_fifo->len > 0) {
  1494. /* Get first entry */
  1495. status = st_fifo->entries[st_fifo->head].status;
  1496. len = st_fifo->entries[st_fifo->head].len;
  1497. st_fifo->pending_bytes -= len;
  1498. st_fifo->head++;
  1499. st_fifo->len--;
  1500. /* Check for errors */
  1501. if (status & FRM_ST_ERR_MSK) {
  1502. if (status & FRM_ST_LOST_FR) {
  1503. /* Add number of lost frames to stats */
  1504. self->stats.rx_errors += len;
  1505. } else {
  1506. /* Skip frame */
  1507. self->stats.rx_errors++;
  1508. self->rx_buff.data += len;
  1509. if (status & FRM_ST_MAX_LEN)
  1510. self->stats.rx_length_errors++;
  1511. if (status & FRM_ST_PHY_ERR)
  1512. self->stats.rx_frame_errors++;
  1513. if (status & FRM_ST_BAD_CRC)
  1514. self->stats.rx_crc_errors++;
  1515. }
  1516. /* The errors below can be reported in both cases */
  1517. if (status & FRM_ST_OVR1)
  1518. self->stats.rx_fifo_errors++;
  1519. if (status & FRM_ST_OVR2)
  1520. self->stats.rx_fifo_errors++;
  1521. } else {
  1522. /*
  1523. * First we must make sure that the frame we
  1524. * want to deliver is all in main memory. If we
  1525. * cannot tell, then we check if the Rx FIFO is
  1526. * empty. If not then we will have to take a nap
  1527. * and try again later.
  1528. */
  1529. if (st_fifo->pending_bytes < self->io.fifo_size) {
  1530. switch_bank(iobase, BANK0);
  1531. if (inb(iobase+LSR) & LSR_RXDA) {
  1532. /* Put this entry back in fifo */
  1533. st_fifo->head--;
  1534. st_fifo->len++;
  1535. st_fifo->pending_bytes += len;
  1536. st_fifo->entries[st_fifo->head].status = status;
  1537. st_fifo->entries[st_fifo->head].len = len;
  1538. /*
  1539. * DMA not finished yet, so try again
  1540. * later, set timer value, resolution
  1541. * 125 us
  1542. */
  1543. switch_bank(iobase, BANK4);
  1544. outb(0x02, iobase+TMRL); /* x 125 us */
  1545. outb(0x00, iobase+TMRH);
  1546. /* Start timer */
  1547. outb(IRCR1_TMR_EN, iobase+IRCR1);
  1548. /* Restore bank register */
  1549. outb(bank, iobase+BSR);
  1550. return FALSE; /* I'll be back! */
  1551. }
  1552. }
  1553. /*
  1554. * Remember the time we received this frame, so we can
  1555. * reduce the min turn time a bit since we will know
  1556. * how much time we have used for protocol processing
  1557. */
  1558. do_gettimeofday(&self->stamp);
  1559. skb = dev_alloc_skb(len+1);
  1560. if (skb == NULL) {
  1561. IRDA_WARNING("%s(), memory squeeze, "
  1562. "dropping frame.\n",
  1563. __FUNCTION__);
  1564. self->stats.rx_dropped++;
  1565. /* Restore bank register */
  1566. outb(bank, iobase+BSR);
  1567. return FALSE;
  1568. }
  1569. /* Make sure IP header gets aligned */
  1570. skb_reserve(skb, 1);
  1571. /* Copy frame without CRC */
  1572. if (self->io.speed < 4000000) {
  1573. skb_put(skb, len-2);
  1574. skb_copy_to_linear_data(skb,
  1575. self->rx_buff.data,
  1576. len - 2);
  1577. } else {
  1578. skb_put(skb, len-4);
  1579. skb_copy_to_linear_data(skb,
  1580. self->rx_buff.data,
  1581. len - 4);
  1582. }
  1583. /* Move to next frame */
  1584. self->rx_buff.data += len;
  1585. self->stats.rx_bytes += len;
  1586. self->stats.rx_packets++;
  1587. skb->dev = self->netdev;
  1588. skb_reset_mac_header(skb);
  1589. skb->protocol = htons(ETH_P_IRDA);
  1590. netif_rx(skb);
  1591. self->netdev->last_rx = jiffies;
  1592. }
  1593. }
  1594. /* Restore bank register */
  1595. outb(bank, iobase+BSR);
  1596. return TRUE;
  1597. }
  1598. /*
  1599. * Function nsc_ircc_pio_receive (self)
  1600. *
  1601. * Receive all data in receiver FIFO
  1602. *
  1603. */
  1604. static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
  1605. {
  1606. __u8 byte;
  1607. int iobase;
  1608. iobase = self->io.fir_base;
  1609. /* Receive all characters in Rx FIFO */
  1610. do {
  1611. byte = inb(iobase+RXD);
  1612. async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
  1613. byte);
  1614. } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
  1615. }
  1616. /*
  1617. * Function nsc_ircc_sir_interrupt (self, eir)
  1618. *
  1619. * Handle SIR interrupt
  1620. *
  1621. */
  1622. static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
  1623. {
  1624. int actual;
  1625. /* Check if transmit FIFO is low on data */
  1626. if (eir & EIR_TXLDL_EV) {
  1627. /* Write data left in transmit buffer */
  1628. actual = nsc_ircc_pio_write(self->io.fir_base,
  1629. self->tx_buff.data,
  1630. self->tx_buff.len,
  1631. self->io.fifo_size);
  1632. self->tx_buff.data += actual;
  1633. self->tx_buff.len -= actual;
  1634. self->io.direction = IO_XMIT;
  1635. /* Check if finished */
  1636. if (self->tx_buff.len > 0)
  1637. self->ier = IER_TXLDL_IE;
  1638. else {
  1639. self->stats.tx_packets++;
  1640. netif_wake_queue(self->netdev);
  1641. self->ier = IER_TXEMP_IE;
  1642. }
  1643. }
  1644. /* Check if transmission has completed */
  1645. if (eir & EIR_TXEMP_EV) {
  1646. /* Turn around and get ready to receive some data */
  1647. self->io.direction = IO_RECV;
  1648. self->ier = IER_RXHDL_IE;
  1649. /* Check if we need to change the speed?
  1650. * Need to be after self->io.direction to avoid race with
  1651. * nsc_ircc_hard_xmit_sir() - Jean II */
  1652. if (self->new_speed) {
  1653. IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
  1654. self->ier = nsc_ircc_change_speed(self,
  1655. self->new_speed);
  1656. self->new_speed = 0;
  1657. netif_wake_queue(self->netdev);
  1658. /* Check if we are going to FIR */
  1659. if (self->io.speed > 115200) {
  1660. /* No need to do anymore SIR stuff */
  1661. return;
  1662. }
  1663. }
  1664. }
  1665. /* Rx FIFO threshold or timeout */
  1666. if (eir & EIR_RXHDL_EV) {
  1667. nsc_ircc_pio_receive(self);
  1668. /* Keep receiving */
  1669. self->ier = IER_RXHDL_IE;
  1670. }
  1671. }
  1672. /*
  1673. * Function nsc_ircc_fir_interrupt (self, eir)
  1674. *
  1675. * Handle MIR/FIR interrupt
  1676. *
  1677. */
  1678. static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
  1679. int eir)
  1680. {
  1681. __u8 bank;
  1682. bank = inb(iobase+BSR);
  1683. /* Status FIFO event*/
  1684. if (eir & EIR_SFIF_EV) {
  1685. /* Check if DMA has finished */
  1686. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1687. /* Wait for next status FIFO interrupt */
  1688. self->ier = IER_SFIF_IE;
  1689. } else {
  1690. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1691. }
  1692. } else if (eir & EIR_TMR_EV) { /* Timer finished */
  1693. /* Disable timer */
  1694. switch_bank(iobase, BANK4);
  1695. outb(0, iobase+IRCR1);
  1696. /* Clear timer event */
  1697. switch_bank(iobase, BANK0);
  1698. outb(ASCR_CTE, iobase+ASCR);
  1699. /* Check if this is a Tx timer interrupt */
  1700. if (self->io.direction == IO_XMIT) {
  1701. nsc_ircc_dma_xmit(self, iobase);
  1702. /* Interrupt on DMA */
  1703. self->ier = IER_DMA_IE;
  1704. } else {
  1705. /* Check (again) if DMA has finished */
  1706. if (nsc_ircc_dma_receive_complete(self, iobase)) {
  1707. self->ier = IER_SFIF_IE;
  1708. } else {
  1709. self->ier = IER_SFIF_IE | IER_TMR_IE;
  1710. }
  1711. }
  1712. } else if (eir & EIR_DMA_EV) {
  1713. /* Finished with all transmissions? */
  1714. if (nsc_ircc_dma_xmit_complete(self)) {
  1715. if(self->new_speed != 0) {
  1716. /* As we stop the Tx queue, the speed change
  1717. * need to be done when the Tx fifo is
  1718. * empty. Ask for a Tx done interrupt */
  1719. self->ier = IER_TXEMP_IE;
  1720. } else {
  1721. /* Check if there are more frames to be
  1722. * transmitted */
  1723. if (irda_device_txqueue_empty(self->netdev)) {
  1724. /* Prepare for receive */
  1725. nsc_ircc_dma_receive(self);
  1726. self->ier = IER_SFIF_IE;
  1727. } else
  1728. IRDA_WARNING("%s(), potential "
  1729. "Tx queue lockup !\n",
  1730. __FUNCTION__);
  1731. }
  1732. } else {
  1733. /* Not finished yet, so interrupt on DMA again */
  1734. self->ier = IER_DMA_IE;
  1735. }
  1736. } else if (eir & EIR_TXEMP_EV) {
  1737. /* The Tx FIFO has totally drained out, so now we can change
  1738. * the speed... - Jean II */
  1739. self->ier = nsc_ircc_change_speed(self, self->new_speed);
  1740. self->new_speed = 0;
  1741. netif_wake_queue(self->netdev);
  1742. /* Note : nsc_ircc_change_speed() restarted Rx fifo */
  1743. }
  1744. outb(bank, iobase+BSR);
  1745. }
  1746. /*
  1747. * Function nsc_ircc_interrupt (irq, dev_id, regs)
  1748. *
  1749. * An interrupt from the chip has arrived. Time to do some work
  1750. *
  1751. */
  1752. static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id)
  1753. {
  1754. struct net_device *dev = dev_id;
  1755. struct nsc_ircc_cb *self;
  1756. __u8 bsr, eir;
  1757. int iobase;
  1758. self = dev->priv;
  1759. spin_lock(&self->lock);
  1760. iobase = self->io.fir_base;
  1761. bsr = inb(iobase+BSR); /* Save current bank */
  1762. switch_bank(iobase, BANK0);
  1763. self->ier = inb(iobase+IER);
  1764. eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
  1765. outb(0, iobase+IER); /* Disable interrupts */
  1766. if (eir) {
  1767. /* Dispatch interrupt handler for the current speed */
  1768. if (self->io.speed > 115200)
  1769. nsc_ircc_fir_interrupt(self, iobase, eir);
  1770. else
  1771. nsc_ircc_sir_interrupt(self, eir);
  1772. }
  1773. outb(self->ier, iobase+IER); /* Restore interrupts */
  1774. outb(bsr, iobase+BSR); /* Restore bank register */
  1775. spin_unlock(&self->lock);
  1776. return IRQ_RETVAL(eir);
  1777. }
  1778. /*
  1779. * Function nsc_ircc_is_receiving (self)
  1780. *
  1781. * Return TRUE is we are currently receiving a frame
  1782. *
  1783. */
  1784. static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
  1785. {
  1786. unsigned long flags;
  1787. int status = FALSE;
  1788. int iobase;
  1789. __u8 bank;
  1790. IRDA_ASSERT(self != NULL, return FALSE;);
  1791. spin_lock_irqsave(&self->lock, flags);
  1792. if (self->io.speed > 115200) {
  1793. iobase = self->io.fir_base;
  1794. /* Check if rx FIFO is not empty */
  1795. bank = inb(iobase+BSR);
  1796. switch_bank(iobase, BANK2);
  1797. if ((inb(iobase+RXFLV) & 0x3f) != 0) {
  1798. /* We are receiving something */
  1799. status = TRUE;
  1800. }
  1801. outb(bank, iobase+BSR);
  1802. } else
  1803. status = (self->rx_buff.state != OUTSIDE_FRAME);
  1804. spin_unlock_irqrestore(&self->lock, flags);
  1805. return status;
  1806. }
  1807. /*
  1808. * Function nsc_ircc_net_open (dev)
  1809. *
  1810. * Start the device
  1811. *
  1812. */
  1813. static int nsc_ircc_net_open(struct net_device *dev)
  1814. {
  1815. struct nsc_ircc_cb *self;
  1816. int iobase;
  1817. char hwname[32];
  1818. __u8 bank;
  1819. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1820. IRDA_ASSERT(dev != NULL, return -1;);
  1821. self = (struct nsc_ircc_cb *) dev->priv;
  1822. IRDA_ASSERT(self != NULL, return 0;);
  1823. iobase = self->io.fir_base;
  1824. if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
  1825. IRDA_WARNING("%s, unable to allocate irq=%d\n",
  1826. driver_name, self->io.irq);
  1827. return -EAGAIN;
  1828. }
  1829. /*
  1830. * Always allocate the DMA channel after the IRQ, and clean up on
  1831. * failure.
  1832. */
  1833. if (request_dma(self->io.dma, dev->name)) {
  1834. IRDA_WARNING("%s, unable to allocate dma=%d\n",
  1835. driver_name, self->io.dma);
  1836. free_irq(self->io.irq, dev);
  1837. return -EAGAIN;
  1838. }
  1839. /* Save current bank */
  1840. bank = inb(iobase+BSR);
  1841. /* turn on interrupts */
  1842. switch_bank(iobase, BANK0);
  1843. outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
  1844. /* Restore bank register */
  1845. outb(bank, iobase+BSR);
  1846. /* Ready to play! */
  1847. netif_start_queue(dev);
  1848. /* Give self a hardware name */
  1849. sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
  1850. /*
  1851. * Open new IrLAP layer instance, now that everything should be
  1852. * initialized properly
  1853. */
  1854. self->irlap = irlap_open(dev, &self->qos, hwname);
  1855. return 0;
  1856. }
  1857. /*
  1858. * Function nsc_ircc_net_close (dev)
  1859. *
  1860. * Stop the device
  1861. *
  1862. */
  1863. static int nsc_ircc_net_close(struct net_device *dev)
  1864. {
  1865. struct nsc_ircc_cb *self;
  1866. int iobase;
  1867. __u8 bank;
  1868. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  1869. IRDA_ASSERT(dev != NULL, return -1;);
  1870. self = (struct nsc_ircc_cb *) dev->priv;
  1871. IRDA_ASSERT(self != NULL, return 0;);
  1872. /* Stop device */
  1873. netif_stop_queue(dev);
  1874. /* Stop and remove instance of IrLAP */
  1875. if (self->irlap)
  1876. irlap_close(self->irlap);
  1877. self->irlap = NULL;
  1878. iobase = self->io.fir_base;
  1879. disable_dma(self->io.dma);
  1880. /* Save current bank */
  1881. bank = inb(iobase+BSR);
  1882. /* Disable interrupts */
  1883. switch_bank(iobase, BANK0);
  1884. outb(0, iobase+IER);
  1885. free_irq(self->io.irq, dev);
  1886. free_dma(self->io.dma);
  1887. /* Restore bank register */
  1888. outb(bank, iobase+BSR);
  1889. return 0;
  1890. }
  1891. /*
  1892. * Function nsc_ircc_net_ioctl (dev, rq, cmd)
  1893. *
  1894. * Process IOCTL commands for this device
  1895. *
  1896. */
  1897. static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1898. {
  1899. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1900. struct nsc_ircc_cb *self;
  1901. unsigned long flags;
  1902. int ret = 0;
  1903. IRDA_ASSERT(dev != NULL, return -1;);
  1904. self = dev->priv;
  1905. IRDA_ASSERT(self != NULL, return -1;);
  1906. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
  1907. switch (cmd) {
  1908. case SIOCSBANDWIDTH: /* Set bandwidth */
  1909. if (!capable(CAP_NET_ADMIN)) {
  1910. ret = -EPERM;
  1911. break;
  1912. }
  1913. spin_lock_irqsave(&self->lock, flags);
  1914. nsc_ircc_change_speed(self, irq->ifr_baudrate);
  1915. spin_unlock_irqrestore(&self->lock, flags);
  1916. break;
  1917. case SIOCSMEDIABUSY: /* Set media busy */
  1918. if (!capable(CAP_NET_ADMIN)) {
  1919. ret = -EPERM;
  1920. break;
  1921. }
  1922. irda_device_set_media_busy(self->netdev, TRUE);
  1923. break;
  1924. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1925. /* This is already protected */
  1926. irq->ifr_receiving = nsc_ircc_is_receiving(self);
  1927. break;
  1928. default:
  1929. ret = -EOPNOTSUPP;
  1930. }
  1931. return ret;
  1932. }
  1933. static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
  1934. {
  1935. struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
  1936. return &self->stats;
  1937. }
  1938. static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state)
  1939. {
  1940. struct nsc_ircc_cb *self = platform_get_drvdata(dev);
  1941. int bank;
  1942. unsigned long flags;
  1943. int iobase = self->io.fir_base;
  1944. if (self->io.suspended)
  1945. return 0;
  1946. IRDA_DEBUG(1, "%s, Suspending\n", driver_name);
  1947. rtnl_lock();
  1948. if (netif_running(self->netdev)) {
  1949. netif_device_detach(self->netdev);
  1950. spin_lock_irqsave(&self->lock, flags);
  1951. /* Save current bank */
  1952. bank = inb(iobase+BSR);
  1953. /* Disable interrupts */
  1954. switch_bank(iobase, BANK0);
  1955. outb(0, iobase+IER);
  1956. /* Restore bank register */
  1957. outb(bank, iobase+BSR);
  1958. spin_unlock_irqrestore(&self->lock, flags);
  1959. free_irq(self->io.irq, self->netdev);
  1960. disable_dma(self->io.dma);
  1961. }
  1962. self->io.suspended = 1;
  1963. rtnl_unlock();
  1964. return 0;
  1965. }
  1966. static int nsc_ircc_resume(struct platform_device *dev)
  1967. {
  1968. struct nsc_ircc_cb *self = platform_get_drvdata(dev);
  1969. unsigned long flags;
  1970. if (!self->io.suspended)
  1971. return 0;
  1972. IRDA_DEBUG(1, "%s, Waking up\n", driver_name);
  1973. rtnl_lock();
  1974. nsc_ircc_setup(&self->io);
  1975. nsc_ircc_init_dongle_interface(self->io.fir_base, self->io.dongle_id);
  1976. if (netif_running(self->netdev)) {
  1977. if (request_irq(self->io.irq, nsc_ircc_interrupt, 0,
  1978. self->netdev->name, self->netdev)) {
  1979. IRDA_WARNING("%s, unable to allocate irq=%d\n",
  1980. driver_name, self->io.irq);
  1981. /*
  1982. * Don't fail resume process, just kill this
  1983. * network interface
  1984. */
  1985. unregister_netdevice(self->netdev);
  1986. } else {
  1987. spin_lock_irqsave(&self->lock, flags);
  1988. nsc_ircc_change_speed(self, self->io.speed);
  1989. spin_unlock_irqrestore(&self->lock, flags);
  1990. netif_device_attach(self->netdev);
  1991. }
  1992. } else {
  1993. spin_lock_irqsave(&self->lock, flags);
  1994. nsc_ircc_change_speed(self, 9600);
  1995. spin_unlock_irqrestore(&self->lock, flags);
  1996. }
  1997. self->io.suspended = 0;
  1998. rtnl_unlock();
  1999. return 0;
  2000. }
  2001. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  2002. MODULE_DESCRIPTION("NSC IrDA Device Driver");
  2003. MODULE_LICENSE("GPL");
  2004. module_param(qos_mtt_bits, int, 0);
  2005. MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
  2006. module_param_array(io, int, NULL, 0);
  2007. MODULE_PARM_DESC(io, "Base I/O addresses");
  2008. module_param_array(irq, int, NULL, 0);
  2009. MODULE_PARM_DESC(irq, "IRQ lines");
  2010. module_param_array(dma, int, NULL, 0);
  2011. MODULE_PARM_DESC(dma, "DMA channels");
  2012. module_param(dongle_id, int, 0);
  2013. MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
  2014. module_init(nsc_ircc_init);
  2015. module_exit(nsc_ircc_cleanup);