nsc-ircc.c 59 KB

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