nsc-ircc.c 55 KB

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