nsc-ircc.c 58 KB

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