nsc-ircc.c 59 KB

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