nsc-ircc.c 59 KB

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