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