nsc-ircc.c 59 KB

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