ali-ircc.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*********************************************************************
  2. *
  3. * Filename: ali-ircc.h
  4. * Version: 0.5
  5. * Description: Driver for the ALI M1535D and M1543C FIR Controller
  6. * Status: Experimental.
  7. * Author: Benjamin Kong <benjamin_kong@ali.com.tw>
  8. * Created at: 2000/10/16 03:46PM
  9. * Modified at: 2001/1/3 02:55PM
  10. * Modified by: Benjamin Kong <benjamin_kong@ali.com.tw>
  11. * Modified at: 2003/11/6 and support for ALi south-bridge chipsets M1563
  12. * Modified by: Clear Zhang <clear_zhang@ali.com.tw>
  13. *
  14. * Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
  15. * All Rights Reserved
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. ********************************************************************/
  23. #include <linux/module.h>
  24. #include <linux/gfp.h>
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/ioport.h>
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/rtnetlink.h>
  34. #include <linux/serial_reg.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/platform_device.h>
  37. #include <asm/io.h>
  38. #include <asm/dma.h>
  39. #include <asm/byteorder.h>
  40. #include <net/irda/wrapper.h>
  41. #include <net/irda/irda.h>
  42. #include <net/irda/irda_device.h>
  43. #include "ali-ircc.h"
  44. #define CHIP_IO_EXTENT 8
  45. #define BROKEN_DONGLE_ID
  46. #define ALI_IRCC_DRIVER_NAME "ali-ircc"
  47. /* Power Management */
  48. static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
  49. static int ali_ircc_resume(struct platform_device *dev);
  50. static struct platform_driver ali_ircc_driver = {
  51. .suspend = ali_ircc_suspend,
  52. .resume = ali_ircc_resume,
  53. .driver = {
  54. .name = ALI_IRCC_DRIVER_NAME,
  55. },
  56. };
  57. /* Module parameters */
  58. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  59. /* Use BIOS settions by default, but user may supply module parameters */
  60. static unsigned int io[] = { ~0, ~0, ~0, ~0 };
  61. static unsigned int irq[] = { 0, 0, 0, 0 };
  62. static unsigned int dma[] = { 0, 0, 0, 0 };
  63. static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
  64. static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
  65. static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
  66. /* These are the currently known ALi south-bridge chipsets, the only one difference
  67. * is that M1543C doesn't support HP HDSL-3600
  68. */
  69. static ali_chip_t chips[] =
  70. {
  71. { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
  72. { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
  73. { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
  74. { NULL }
  75. };
  76. /* Max 4 instances for now */
  77. static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
  78. /* Dongle Types */
  79. static char *dongle_types[] = {
  80. "TFDS6000",
  81. "HP HSDL-3600",
  82. "HP HSDL-1100",
  83. "No dongle connected",
  84. };
  85. /* Some prototypes */
  86. static int ali_ircc_open(int i, chipio_t *info);
  87. static int ali_ircc_close(struct ali_ircc_cb *self);
  88. static int ali_ircc_setup(chipio_t *info);
  89. static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
  90. static int ali_ircc_net_open(struct net_device *dev);
  91. static int ali_ircc_net_close(struct net_device *dev);
  92. static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  93. static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
  94. /* SIR function */
  95. static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
  96. struct net_device *dev);
  97. static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
  98. static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
  99. static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
  100. static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
  101. static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
  102. /* FIR function */
  103. static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
  104. struct net_device *dev);
  105. static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
  106. static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
  107. static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
  108. static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
  109. static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
  110. static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
  111. /* My Function */
  112. static int ali_ircc_read_dongle_id (int i, chipio_t *info);
  113. static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
  114. /* ALi chip function */
  115. static void SIR2FIR(int iobase);
  116. static void FIR2SIR(int iobase);
  117. static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
  118. /*
  119. * Function ali_ircc_init ()
  120. *
  121. * Initialize chip. Find out whay kinds of chips we are dealing with
  122. * and their configuration registers address
  123. */
  124. static int __init ali_ircc_init(void)
  125. {
  126. ali_chip_t *chip;
  127. chipio_t info;
  128. int ret;
  129. int cfg, cfg_base;
  130. int reg, revision;
  131. int i = 0;
  132. ret = platform_driver_register(&ali_ircc_driver);
  133. if (ret) {
  134. net_err_ratelimited("%s, Can't register driver!\n",
  135. ALI_IRCC_DRIVER_NAME);
  136. return ret;
  137. }
  138. ret = -ENODEV;
  139. /* Probe for all the ALi chipsets we know about */
  140. for (chip= chips; chip->name; chip++, i++)
  141. {
  142. pr_debug("%s(), Probing for %s ...\n", __func__, chip->name);
  143. /* Try all config registers for this chip */
  144. for (cfg=0; cfg<2; cfg++)
  145. {
  146. cfg_base = chip->cfg[cfg];
  147. if (!cfg_base)
  148. continue;
  149. memset(&info, 0, sizeof(chipio_t));
  150. info.cfg_base = cfg_base;
  151. info.fir_base = io[i];
  152. info.dma = dma[i];
  153. info.irq = irq[i];
  154. /* Enter Configuration */
  155. outb(chip->entr1, cfg_base);
  156. outb(chip->entr2, cfg_base);
  157. /* Select Logical Device 5 Registers (UART2) */
  158. outb(0x07, cfg_base);
  159. outb(0x05, cfg_base+1);
  160. /* Read Chip Identification Register */
  161. outb(chip->cid_index, cfg_base);
  162. reg = inb(cfg_base+1);
  163. if (reg == chip->cid_value)
  164. {
  165. pr_debug("%s(), Chip found at 0x%03x\n",
  166. __func__, cfg_base);
  167. outb(0x1F, cfg_base);
  168. revision = inb(cfg_base+1);
  169. pr_debug("%s(), Found %s chip, revision=%d\n",
  170. __func__, chip->name, revision);
  171. /*
  172. * If the user supplies the base address, then
  173. * we init the chip, if not we probe the values
  174. * set by the BIOS
  175. */
  176. if (io[i] < 2000)
  177. {
  178. chip->init(chip, &info);
  179. }
  180. else
  181. {
  182. chip->probe(chip, &info);
  183. }
  184. if (ali_ircc_open(i, &info) == 0)
  185. ret = 0;
  186. i++;
  187. }
  188. else
  189. {
  190. pr_debug("%s(), No %s chip at 0x%03x\n",
  191. __func__, chip->name, cfg_base);
  192. }
  193. /* Exit configuration */
  194. outb(0xbb, cfg_base);
  195. }
  196. }
  197. if (ret)
  198. platform_driver_unregister(&ali_ircc_driver);
  199. return ret;
  200. }
  201. /*
  202. * Function ali_ircc_cleanup ()
  203. *
  204. * Close all configured chips
  205. *
  206. */
  207. static void __exit ali_ircc_cleanup(void)
  208. {
  209. int i;
  210. for (i=0; i < ARRAY_SIZE(dev_self); i++) {
  211. if (dev_self[i])
  212. ali_ircc_close(dev_self[i]);
  213. }
  214. platform_driver_unregister(&ali_ircc_driver);
  215. }
  216. static const struct net_device_ops ali_ircc_sir_ops = {
  217. .ndo_open = ali_ircc_net_open,
  218. .ndo_stop = ali_ircc_net_close,
  219. .ndo_start_xmit = ali_ircc_sir_hard_xmit,
  220. .ndo_do_ioctl = ali_ircc_net_ioctl,
  221. };
  222. static const struct net_device_ops ali_ircc_fir_ops = {
  223. .ndo_open = ali_ircc_net_open,
  224. .ndo_stop = ali_ircc_net_close,
  225. .ndo_start_xmit = ali_ircc_fir_hard_xmit,
  226. .ndo_do_ioctl = ali_ircc_net_ioctl,
  227. };
  228. /*
  229. * Function ali_ircc_open (int i, chipio_t *inf)
  230. *
  231. * Open driver instance
  232. *
  233. */
  234. static int ali_ircc_open(int i, chipio_t *info)
  235. {
  236. struct net_device *dev;
  237. struct ali_ircc_cb *self;
  238. int dongle_id;
  239. int err;
  240. if (i >= ARRAY_SIZE(dev_self)) {
  241. net_err_ratelimited("%s(), maximum number of supported chips reached!\n",
  242. __func__);
  243. return -ENOMEM;
  244. }
  245. /* Set FIR FIFO and DMA Threshold */
  246. if ((ali_ircc_setup(info)) == -1)
  247. return -1;
  248. dev = alloc_irdadev(sizeof(*self));
  249. if (dev == NULL) {
  250. net_err_ratelimited("%s(), can't allocate memory for control block!\n",
  251. __func__);
  252. return -ENOMEM;
  253. }
  254. self = netdev_priv(dev);
  255. self->netdev = dev;
  256. spin_lock_init(&self->lock);
  257. /* Need to store self somewhere */
  258. dev_self[i] = self;
  259. self->index = i;
  260. /* Initialize IO */
  261. self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
  262. self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
  263. self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
  264. self->io.irq = info->irq;
  265. self->io.fir_ext = CHIP_IO_EXTENT;
  266. self->io.dma = info->dma;
  267. self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
  268. /* Reserve the ioports that we need */
  269. if (!request_region(self->io.fir_base, self->io.fir_ext,
  270. ALI_IRCC_DRIVER_NAME)) {
  271. net_warn_ratelimited("%s(), can't get iobase of 0x%03x\n",
  272. __func__, self->io.fir_base);
  273. err = -ENODEV;
  274. goto err_out1;
  275. }
  276. /* Initialize QoS for this device */
  277. irda_init_max_qos_capabilies(&self->qos);
  278. /* The only value we must override it the baudrate */
  279. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  280. IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
  281. self->qos.min_turn_time.bits = qos_mtt_bits;
  282. irda_qos_bits_to_value(&self->qos);
  283. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  284. self->rx_buff.truesize = 14384;
  285. self->tx_buff.truesize = 14384;
  286. /* Allocate memory if needed */
  287. self->rx_buff.head =
  288. dma_zalloc_coherent(NULL, self->rx_buff.truesize,
  289. &self->rx_buff_dma, GFP_KERNEL);
  290. if (self->rx_buff.head == NULL) {
  291. err = -ENOMEM;
  292. goto err_out2;
  293. }
  294. self->tx_buff.head =
  295. dma_zalloc_coherent(NULL, self->tx_buff.truesize,
  296. &self->tx_buff_dma, GFP_KERNEL);
  297. if (self->tx_buff.head == NULL) {
  298. err = -ENOMEM;
  299. goto err_out3;
  300. }
  301. self->rx_buff.in_frame = FALSE;
  302. self->rx_buff.state = OUTSIDE_FRAME;
  303. self->tx_buff.data = self->tx_buff.head;
  304. self->rx_buff.data = self->rx_buff.head;
  305. /* Reset Tx queue info */
  306. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  307. self->tx_fifo.tail = self->tx_buff.head;
  308. /* Override the network functions we need to use */
  309. dev->netdev_ops = &ali_ircc_sir_ops;
  310. err = register_netdev(dev);
  311. if (err) {
  312. net_err_ratelimited("%s(), register_netdev() failed!\n",
  313. __func__);
  314. goto err_out4;
  315. }
  316. net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
  317. /* Check dongle id */
  318. dongle_id = ali_ircc_read_dongle_id(i, info);
  319. net_info_ratelimited("%s(), %s, Found dongle: %s\n",
  320. __func__, ALI_IRCC_DRIVER_NAME,
  321. dongle_types[dongle_id]);
  322. self->io.dongle_id = dongle_id;
  323. return 0;
  324. err_out4:
  325. dma_free_coherent(NULL, self->tx_buff.truesize,
  326. self->tx_buff.head, self->tx_buff_dma);
  327. err_out3:
  328. dma_free_coherent(NULL, self->rx_buff.truesize,
  329. self->rx_buff.head, self->rx_buff_dma);
  330. err_out2:
  331. release_region(self->io.fir_base, self->io.fir_ext);
  332. err_out1:
  333. dev_self[i] = NULL;
  334. free_netdev(dev);
  335. return err;
  336. }
  337. /*
  338. * Function ali_ircc_close (self)
  339. *
  340. * Close driver instance
  341. *
  342. */
  343. static int __exit ali_ircc_close(struct ali_ircc_cb *self)
  344. {
  345. int iobase;
  346. IRDA_ASSERT(self != NULL, return -1;);
  347. iobase = self->io.fir_base;
  348. /* Remove netdevice */
  349. unregister_netdev(self->netdev);
  350. /* Release the PORT that this driver is using */
  351. pr_debug("%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
  352. release_region(self->io.fir_base, self->io.fir_ext);
  353. if (self->tx_buff.head)
  354. dma_free_coherent(NULL, self->tx_buff.truesize,
  355. self->tx_buff.head, self->tx_buff_dma);
  356. if (self->rx_buff.head)
  357. dma_free_coherent(NULL, self->rx_buff.truesize,
  358. self->rx_buff.head, self->rx_buff_dma);
  359. dev_self[self->index] = NULL;
  360. free_netdev(self->netdev);
  361. return 0;
  362. }
  363. /*
  364. * Function ali_ircc_init_43 (chip, info)
  365. *
  366. * Initialize the ALi M1543 chip.
  367. */
  368. static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
  369. {
  370. /* All controller information like I/O address, DMA channel, IRQ
  371. * are set by BIOS
  372. */
  373. return 0;
  374. }
  375. /*
  376. * Function ali_ircc_init_53 (chip, info)
  377. *
  378. * Initialize the ALi M1535 chip.
  379. */
  380. static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
  381. {
  382. /* All controller information like I/O address, DMA channel, IRQ
  383. * are set by BIOS
  384. */
  385. return 0;
  386. }
  387. /*
  388. * Function ali_ircc_probe_53 (chip, info)
  389. *
  390. * Probes for the ALi M1535D or M1535
  391. */
  392. static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
  393. {
  394. int cfg_base = info->cfg_base;
  395. int hi, low, reg;
  396. /* Enter Configuration */
  397. outb(chip->entr1, cfg_base);
  398. outb(chip->entr2, cfg_base);
  399. /* Select Logical Device 5 Registers (UART2) */
  400. outb(0x07, cfg_base);
  401. outb(0x05, cfg_base+1);
  402. /* Read address control register */
  403. outb(0x60, cfg_base);
  404. hi = inb(cfg_base+1);
  405. outb(0x61, cfg_base);
  406. low = inb(cfg_base+1);
  407. info->fir_base = (hi<<8) + low;
  408. info->sir_base = info->fir_base;
  409. pr_debug("%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
  410. /* Read IRQ control register */
  411. outb(0x70, cfg_base);
  412. reg = inb(cfg_base+1);
  413. info->irq = reg & 0x0f;
  414. pr_debug("%s(), probing irq=%d\n", __func__, info->irq);
  415. /* Read DMA channel */
  416. outb(0x74, cfg_base);
  417. reg = inb(cfg_base+1);
  418. info->dma = reg & 0x07;
  419. if(info->dma == 0x04)
  420. net_warn_ratelimited("%s(), No DMA channel assigned !\n",
  421. __func__);
  422. else
  423. pr_debug("%s(), probing dma=%d\n", __func__, info->dma);
  424. /* Read Enabled Status */
  425. outb(0x30, cfg_base);
  426. reg = inb(cfg_base+1);
  427. info->enabled = (reg & 0x80) && (reg & 0x01);
  428. pr_debug("%s(), probing enabled=%d\n", __func__, info->enabled);
  429. /* Read Power Status */
  430. outb(0x22, cfg_base);
  431. reg = inb(cfg_base+1);
  432. info->suspended = (reg & 0x20);
  433. pr_debug("%s(), probing suspended=%d\n", __func__, info->suspended);
  434. /* Exit configuration */
  435. outb(0xbb, cfg_base);
  436. return 0;
  437. }
  438. /*
  439. * Function ali_ircc_setup (info)
  440. *
  441. * Set FIR FIFO and DMA Threshold
  442. * Returns non-negative on success.
  443. *
  444. */
  445. static int ali_ircc_setup(chipio_t *info)
  446. {
  447. unsigned char tmp;
  448. int version;
  449. int iobase = info->fir_base;
  450. /* Locking comments :
  451. * Most operations here need to be protected. We are called before
  452. * the device instance is created in ali_ircc_open(), therefore
  453. * nobody can bother us - Jean II */
  454. /* Switch to FIR space */
  455. SIR2FIR(iobase);
  456. /* Master Reset */
  457. outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
  458. /* Read FIR ID Version Register */
  459. switch_bank(iobase, BANK3);
  460. version = inb(iobase+FIR_ID_VR);
  461. /* Should be 0x00 in the M1535/M1535D */
  462. if(version != 0x00)
  463. {
  464. net_err_ratelimited("%s, Wrong chip version %02x\n",
  465. ALI_IRCC_DRIVER_NAME, version);
  466. return -1;
  467. }
  468. /* Set FIR FIFO Threshold Register */
  469. switch_bank(iobase, BANK1);
  470. outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
  471. /* Set FIR DMA Threshold Register */
  472. outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
  473. /* CRC enable */
  474. switch_bank(iobase, BANK2);
  475. outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
  476. /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
  477. /* Switch to Bank 0 */
  478. switch_bank(iobase, BANK0);
  479. tmp = inb(iobase+FIR_LCR_B);
  480. tmp &=~0x20; // disable SIP
  481. tmp |= 0x80; // these two steps make RX mode
  482. tmp &= 0xbf;
  483. outb(tmp, iobase+FIR_LCR_B);
  484. /* Disable Interrupt */
  485. outb(0x00, iobase+FIR_IER);
  486. /* Switch to SIR space */
  487. FIR2SIR(iobase);
  488. net_info_ratelimited("%s, driver loaded (Benjamin Kong)\n",
  489. ALI_IRCC_DRIVER_NAME);
  490. /* Enable receive interrupts */
  491. // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
  492. // Turn on the interrupts in ali_ircc_net_open
  493. return 0;
  494. }
  495. /*
  496. * Function ali_ircc_read_dongle_id (int index, info)
  497. *
  498. * Try to read dongle identification. This procedure needs to be executed
  499. * once after power-on/reset. It also needs to be used whenever you suspect
  500. * that the user may have plugged/unplugged the IrDA Dongle.
  501. */
  502. static int ali_ircc_read_dongle_id (int i, chipio_t *info)
  503. {
  504. int dongle_id, reg;
  505. int cfg_base = info->cfg_base;
  506. /* Enter Configuration */
  507. outb(chips[i].entr1, cfg_base);
  508. outb(chips[i].entr2, cfg_base);
  509. /* Select Logical Device 5 Registers (UART2) */
  510. outb(0x07, cfg_base);
  511. outb(0x05, cfg_base+1);
  512. /* Read Dongle ID */
  513. outb(0xf0, cfg_base);
  514. reg = inb(cfg_base+1);
  515. dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
  516. pr_debug("%s(), probing dongle_id=%d, dongle_types=%s\n",
  517. __func__, dongle_id, dongle_types[dongle_id]);
  518. /* Exit configuration */
  519. outb(0xbb, cfg_base);
  520. return dongle_id;
  521. }
  522. /*
  523. * Function ali_ircc_interrupt (irq, dev_id, regs)
  524. *
  525. * An interrupt from the chip has arrived. Time to do some work
  526. *
  527. */
  528. static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
  529. {
  530. struct net_device *dev = dev_id;
  531. struct ali_ircc_cb *self;
  532. int ret;
  533. self = netdev_priv(dev);
  534. spin_lock(&self->lock);
  535. /* Dispatch interrupt handler for the current speed */
  536. if (self->io.speed > 115200)
  537. ret = ali_ircc_fir_interrupt(self);
  538. else
  539. ret = ali_ircc_sir_interrupt(self);
  540. spin_unlock(&self->lock);
  541. return ret;
  542. }
  543. /*
  544. * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
  545. *
  546. * Handle MIR/FIR interrupt
  547. *
  548. */
  549. static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
  550. {
  551. __u8 eir, OldMessageCount;
  552. int iobase, tmp;
  553. iobase = self->io.fir_base;
  554. switch_bank(iobase, BANK0);
  555. self->InterruptID = inb(iobase+FIR_IIR);
  556. self->BusStatus = inb(iobase+FIR_BSR);
  557. OldMessageCount = (self->LineStatus + 1) & 0x07;
  558. self->LineStatus = inb(iobase+FIR_LSR);
  559. //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
  560. eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
  561. pr_debug("%s(), self->InterruptID = %x\n", __func__, self->InterruptID);
  562. pr_debug("%s(), self->LineStatus = %x\n", __func__, self->LineStatus);
  563. pr_debug("%s(), self->ier = %x\n", __func__, self->ier);
  564. pr_debug("%s(), eir = %x\n", __func__, eir);
  565. /* Disable interrupts */
  566. SetCOMInterrupts(self, FALSE);
  567. /* Tx or Rx Interrupt */
  568. if (eir & IIR_EOM)
  569. {
  570. if (self->io.direction == IO_XMIT) /* TX */
  571. {
  572. pr_debug("%s(), ******* IIR_EOM (Tx) *******\n",
  573. __func__);
  574. if(ali_ircc_dma_xmit_complete(self))
  575. {
  576. if (irda_device_txqueue_empty(self->netdev))
  577. {
  578. /* Prepare for receive */
  579. ali_ircc_dma_receive(self);
  580. self->ier = IER_EOM;
  581. }
  582. }
  583. else
  584. {
  585. self->ier = IER_EOM;
  586. }
  587. }
  588. else /* RX */
  589. {
  590. pr_debug("%s(), ******* IIR_EOM (Rx) *******\n",
  591. __func__);
  592. if(OldMessageCount > ((self->LineStatus+1) & 0x07))
  593. {
  594. self->rcvFramesOverflow = TRUE;
  595. pr_debug("%s(), ******* self->rcvFramesOverflow = TRUE ********\n",
  596. __func__);
  597. }
  598. if (ali_ircc_dma_receive_complete(self))
  599. {
  600. pr_debug("%s(), ******* receive complete ********\n",
  601. __func__);
  602. self->ier = IER_EOM;
  603. }
  604. else
  605. {
  606. pr_debug("%s(), ******* Not receive complete ********\n",
  607. __func__);
  608. self->ier = IER_EOM | IER_TIMER;
  609. }
  610. }
  611. }
  612. /* Timer Interrupt */
  613. else if (eir & IIR_TIMER)
  614. {
  615. if(OldMessageCount > ((self->LineStatus+1) & 0x07))
  616. {
  617. self->rcvFramesOverflow = TRUE;
  618. pr_debug("%s(), ******* self->rcvFramesOverflow = TRUE *******\n",
  619. __func__);
  620. }
  621. /* Disable Timer */
  622. switch_bank(iobase, BANK1);
  623. tmp = inb(iobase+FIR_CR);
  624. outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
  625. /* Check if this is a Tx timer interrupt */
  626. if (self->io.direction == IO_XMIT)
  627. {
  628. ali_ircc_dma_xmit(self);
  629. /* Interrupt on EOM */
  630. self->ier = IER_EOM;
  631. }
  632. else /* Rx */
  633. {
  634. if(ali_ircc_dma_receive_complete(self))
  635. {
  636. self->ier = IER_EOM;
  637. }
  638. else
  639. {
  640. self->ier = IER_EOM | IER_TIMER;
  641. }
  642. }
  643. }
  644. /* Restore Interrupt */
  645. SetCOMInterrupts(self, TRUE);
  646. return IRQ_RETVAL(eir);
  647. }
  648. /*
  649. * Function ali_ircc_sir_interrupt (irq, self, eir)
  650. *
  651. * Handle SIR interrupt
  652. *
  653. */
  654. static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
  655. {
  656. int iobase;
  657. int iir, lsr;
  658. iobase = self->io.sir_base;
  659. iir = inb(iobase+UART_IIR) & UART_IIR_ID;
  660. if (iir) {
  661. /* Clear interrupt */
  662. lsr = inb(iobase+UART_LSR);
  663. pr_debug("%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
  664. __func__, iir, lsr, iobase);
  665. switch (iir)
  666. {
  667. case UART_IIR_RLSI:
  668. pr_debug("%s(), RLSI\n", __func__);
  669. break;
  670. case UART_IIR_RDI:
  671. /* Receive interrupt */
  672. ali_ircc_sir_receive(self);
  673. break;
  674. case UART_IIR_THRI:
  675. if (lsr & UART_LSR_THRE)
  676. {
  677. /* Transmitter ready for data */
  678. ali_ircc_sir_write_wakeup(self);
  679. }
  680. break;
  681. default:
  682. pr_debug("%s(), unhandled IIR=%#x\n",
  683. __func__, iir);
  684. break;
  685. }
  686. }
  687. return IRQ_RETVAL(iir);
  688. }
  689. /*
  690. * Function ali_ircc_sir_receive (self)
  691. *
  692. * Receive one frame from the infrared port
  693. *
  694. */
  695. static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
  696. {
  697. int boguscount = 0;
  698. int iobase;
  699. IRDA_ASSERT(self != NULL, return;);
  700. iobase = self->io.sir_base;
  701. /*
  702. * Receive all characters in Rx FIFO, unwrap and unstuff them.
  703. * async_unwrap_char will deliver all found frames
  704. */
  705. do {
  706. async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
  707. inb(iobase+UART_RX));
  708. /* Make sure we don't stay here too long */
  709. if (boguscount++ > 32) {
  710. pr_debug("%s(), breaking!\n", __func__);
  711. break;
  712. }
  713. } while (inb(iobase+UART_LSR) & UART_LSR_DR);
  714. }
  715. /*
  716. * Function ali_ircc_sir_write_wakeup (tty)
  717. *
  718. * Called by the driver when there's room for more data. If we have
  719. * more packets to send, we send them here.
  720. *
  721. */
  722. static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
  723. {
  724. int actual = 0;
  725. int iobase;
  726. IRDA_ASSERT(self != NULL, return;);
  727. iobase = self->io.sir_base;
  728. /* Finished with frame? */
  729. if (self->tx_buff.len > 0)
  730. {
  731. /* Write data left in transmit buffer */
  732. actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
  733. self->tx_buff.data, self->tx_buff.len);
  734. self->tx_buff.data += actual;
  735. self->tx_buff.len -= actual;
  736. }
  737. else
  738. {
  739. if (self->new_speed)
  740. {
  741. /* We must wait until all data are gone */
  742. while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
  743. pr_debug("%s(), UART_LSR_THRE\n", __func__);
  744. pr_debug("%s(), Changing speed! self->new_speed = %d\n",
  745. __func__, self->new_speed);
  746. ali_ircc_change_speed(self, self->new_speed);
  747. self->new_speed = 0;
  748. // benjamin 2000/11/10 06:32PM
  749. if (self->io.speed > 115200)
  750. {
  751. pr_debug("%s(), ali_ircc_change_speed from UART_LSR_TEMT\n",
  752. __func__);
  753. self->ier = IER_EOM;
  754. // SetCOMInterrupts(self, TRUE);
  755. return;
  756. }
  757. }
  758. else
  759. {
  760. netif_wake_queue(self->netdev);
  761. }
  762. self->netdev->stats.tx_packets++;
  763. /* Turn on receive interrupts */
  764. outb(UART_IER_RDI, iobase+UART_IER);
  765. }
  766. }
  767. static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
  768. {
  769. struct net_device *dev = self->netdev;
  770. int iobase;
  771. pr_debug("%s(), setting speed = %d\n", __func__, baud);
  772. /* This function *must* be called with irq off and spin-lock.
  773. * - Jean II */
  774. iobase = self->io.fir_base;
  775. SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
  776. /* Go to MIR, FIR Speed */
  777. if (baud > 115200)
  778. {
  779. ali_ircc_fir_change_speed(self, baud);
  780. /* Install FIR xmit handler*/
  781. dev->netdev_ops = &ali_ircc_fir_ops;
  782. /* Enable Interuupt */
  783. self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
  784. /* Be ready for incoming frames */
  785. ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
  786. }
  787. /* Go to SIR Speed */
  788. else
  789. {
  790. ali_ircc_sir_change_speed(self, baud);
  791. /* Install SIR xmit handler*/
  792. dev->netdev_ops = &ali_ircc_sir_ops;
  793. }
  794. SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
  795. netif_wake_queue(self->netdev);
  796. }
  797. static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
  798. {
  799. int iobase;
  800. struct ali_ircc_cb *self = priv;
  801. struct net_device *dev;
  802. IRDA_ASSERT(self != NULL, return;);
  803. dev = self->netdev;
  804. iobase = self->io.fir_base;
  805. pr_debug("%s(), self->io.speed = %d, change to speed = %d\n",
  806. __func__, self->io.speed, baud);
  807. /* Come from SIR speed */
  808. if(self->io.speed <=115200)
  809. {
  810. SIR2FIR(iobase);
  811. }
  812. /* Update accounting for new speed */
  813. self->io.speed = baud;
  814. // Set Dongle Speed mode
  815. ali_ircc_change_dongle_speed(self, baud);
  816. }
  817. /*
  818. * Function ali_sir_change_speed (self, speed)
  819. *
  820. * Set speed of IrDA port to specified baudrate
  821. *
  822. */
  823. static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
  824. {
  825. struct ali_ircc_cb *self = priv;
  826. unsigned long flags;
  827. int iobase;
  828. int fcr; /* FIFO control reg */
  829. int lcr; /* Line control reg */
  830. int divisor;
  831. pr_debug("%s(), Setting speed to: %d\n", __func__, speed);
  832. IRDA_ASSERT(self != NULL, return;);
  833. iobase = self->io.sir_base;
  834. /* Come from MIR or FIR speed */
  835. if(self->io.speed >115200)
  836. {
  837. // Set Dongle Speed mode first
  838. ali_ircc_change_dongle_speed(self, speed);
  839. FIR2SIR(iobase);
  840. }
  841. // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
  842. inb(iobase+UART_LSR);
  843. inb(iobase+UART_SCR);
  844. /* Update accounting for new speed */
  845. self->io.speed = speed;
  846. spin_lock_irqsave(&self->lock, flags);
  847. divisor = 115200/speed;
  848. fcr = UART_FCR_ENABLE_FIFO;
  849. /*
  850. * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
  851. * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
  852. * about this timeout since it will always be fast enough.
  853. */
  854. if (self->io.speed < 38400)
  855. fcr |= UART_FCR_TRIGGER_1;
  856. else
  857. fcr |= UART_FCR_TRIGGER_14;
  858. /* IrDA ports use 8N1 */
  859. lcr = UART_LCR_WLEN8;
  860. outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
  861. outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
  862. outb(divisor >> 8, iobase+UART_DLM);
  863. outb(lcr, iobase+UART_LCR); /* Set 8N1 */
  864. outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
  865. /* without this, the connection will be broken after come back from FIR speed,
  866. but with this, the SIR connection is harder to established */
  867. outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
  868. spin_unlock_irqrestore(&self->lock, flags);
  869. }
  870. static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
  871. {
  872. struct ali_ircc_cb *self = priv;
  873. int iobase,dongle_id;
  874. int tmp = 0;
  875. iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
  876. dongle_id = self->io.dongle_id;
  877. /* We are already locked, no need to do it again */
  878. pr_debug("%s(), Set Speed for %s , Speed = %d\n",
  879. __func__, dongle_types[dongle_id], speed);
  880. switch_bank(iobase, BANK2);
  881. tmp = inb(iobase+FIR_IRDA_CR);
  882. /* IBM type dongle */
  883. if(dongle_id == 0)
  884. {
  885. if(speed == 4000000)
  886. {
  887. // __ __
  888. // SD/MODE __| |__ __
  889. // __ __
  890. // IRTX __ __| |__
  891. // T1 T2 T3 T4 T5
  892. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  893. tmp |= IRDA_CR_CRC; // CRC=1
  894. switch_bank(iobase, BANK2);
  895. outb(tmp, iobase+FIR_IRDA_CR);
  896. // T1 -> SD/MODE:0 IRTX:0
  897. tmp &= ~0x09;
  898. tmp |= 0x02;
  899. outb(tmp, iobase+FIR_IRDA_CR);
  900. udelay(2);
  901. // T2 -> SD/MODE:1 IRTX:0
  902. tmp &= ~0x01;
  903. tmp |= 0x0a;
  904. outb(tmp, iobase+FIR_IRDA_CR);
  905. udelay(2);
  906. // T3 -> SD/MODE:1 IRTX:1
  907. tmp |= 0x0b;
  908. outb(tmp, iobase+FIR_IRDA_CR);
  909. udelay(2);
  910. // T4 -> SD/MODE:0 IRTX:1
  911. tmp &= ~0x08;
  912. tmp |= 0x03;
  913. outb(tmp, iobase+FIR_IRDA_CR);
  914. udelay(2);
  915. // T5 -> SD/MODE:0 IRTX:0
  916. tmp &= ~0x09;
  917. tmp |= 0x02;
  918. outb(tmp, iobase+FIR_IRDA_CR);
  919. udelay(2);
  920. // reset -> Normal TX output Signal
  921. outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
  922. }
  923. else /* speed <=1152000 */
  924. {
  925. // __
  926. // SD/MODE __| |__
  927. //
  928. // IRTX ________
  929. // T1 T2 T3
  930. /* MIR 115200, 57600 */
  931. if (speed==1152000)
  932. {
  933. tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
  934. }
  935. else
  936. {
  937. tmp &=~0x80; //HDLC 0.576Mbps
  938. tmp |= 0x20; //HDLC=1,
  939. }
  940. tmp |= IRDA_CR_CRC; // CRC=1
  941. switch_bank(iobase, BANK2);
  942. outb(tmp, iobase+FIR_IRDA_CR);
  943. /* MIR 115200, 57600 */
  944. //switch_bank(iobase, BANK2);
  945. // T1 -> SD/MODE:0 IRTX:0
  946. tmp &= ~0x09;
  947. tmp |= 0x02;
  948. outb(tmp, iobase+FIR_IRDA_CR);
  949. udelay(2);
  950. // T2 -> SD/MODE:1 IRTX:0
  951. tmp &= ~0x01;
  952. tmp |= 0x0a;
  953. outb(tmp, iobase+FIR_IRDA_CR);
  954. // T3 -> SD/MODE:0 IRTX:0
  955. tmp &= ~0x09;
  956. tmp |= 0x02;
  957. outb(tmp, iobase+FIR_IRDA_CR);
  958. udelay(2);
  959. // reset -> Normal TX output Signal
  960. outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
  961. }
  962. }
  963. else if (dongle_id == 1) /* HP HDSL-3600 */
  964. {
  965. switch(speed)
  966. {
  967. case 4000000:
  968. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  969. break;
  970. case 1152000:
  971. tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
  972. break;
  973. case 576000:
  974. tmp &=~0x80; // HDLC 0.576Mbps
  975. tmp |= 0x20; // HDLC=1,
  976. break;
  977. }
  978. tmp |= IRDA_CR_CRC; // CRC=1
  979. switch_bank(iobase, BANK2);
  980. outb(tmp, iobase+FIR_IRDA_CR);
  981. }
  982. else /* HP HDSL-1100 */
  983. {
  984. if(speed <= 115200) /* SIR */
  985. {
  986. tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
  987. switch_bank(iobase, BANK2);
  988. outb(tmp, iobase+FIR_IRDA_CR);
  989. }
  990. else /* MIR FIR */
  991. {
  992. switch(speed)
  993. {
  994. case 4000000:
  995. tmp &= ~IRDA_CR_HDLC; // HDLC=0
  996. break;
  997. case 1152000:
  998. tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
  999. break;
  1000. case 576000:
  1001. tmp &=~0x80; // HDLC 0.576Mbps
  1002. tmp |= 0x20; // HDLC=1,
  1003. break;
  1004. }
  1005. tmp |= IRDA_CR_CRC; // CRC=1
  1006. tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
  1007. switch_bank(iobase, BANK2);
  1008. outb(tmp, iobase+FIR_IRDA_CR);
  1009. }
  1010. }
  1011. switch_bank(iobase, BANK0);
  1012. }
  1013. /*
  1014. * Function ali_ircc_sir_write (driver)
  1015. *
  1016. * Fill Tx FIFO with transmit data
  1017. *
  1018. */
  1019. static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
  1020. {
  1021. int actual = 0;
  1022. /* Tx FIFO should be empty! */
  1023. if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
  1024. pr_debug("%s(), failed, fifo not empty!\n", __func__);
  1025. return 0;
  1026. }
  1027. /* Fill FIFO with current frame */
  1028. while ((fifo_size-- > 0) && (actual < len)) {
  1029. /* Transmit next byte */
  1030. outb(buf[actual], iobase+UART_TX);
  1031. actual++;
  1032. }
  1033. return actual;
  1034. }
  1035. /*
  1036. * Function ali_ircc_net_open (dev)
  1037. *
  1038. * Start the device
  1039. *
  1040. */
  1041. static int ali_ircc_net_open(struct net_device *dev)
  1042. {
  1043. struct ali_ircc_cb *self;
  1044. int iobase;
  1045. char hwname[32];
  1046. IRDA_ASSERT(dev != NULL, return -1;);
  1047. self = netdev_priv(dev);
  1048. IRDA_ASSERT(self != NULL, return 0;);
  1049. iobase = self->io.fir_base;
  1050. /* Request IRQ and install Interrupt Handler */
  1051. if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
  1052. {
  1053. net_warn_ratelimited("%s, unable to allocate irq=%d\n",
  1054. ALI_IRCC_DRIVER_NAME, self->io.irq);
  1055. return -EAGAIN;
  1056. }
  1057. /*
  1058. * Always allocate the DMA channel after the IRQ, and clean up on
  1059. * failure.
  1060. */
  1061. if (request_dma(self->io.dma, dev->name)) {
  1062. net_warn_ratelimited("%s, unable to allocate dma=%d\n",
  1063. ALI_IRCC_DRIVER_NAME, self->io.dma);
  1064. free_irq(self->io.irq, dev);
  1065. return -EAGAIN;
  1066. }
  1067. /* Turn on interrups */
  1068. outb(UART_IER_RDI , iobase+UART_IER);
  1069. /* Ready to play! */
  1070. netif_start_queue(dev); //benjamin by irport
  1071. /* Give self a hardware name */
  1072. sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
  1073. /*
  1074. * Open new IrLAP layer instance, now that everything should be
  1075. * initialized properly
  1076. */
  1077. self->irlap = irlap_open(dev, &self->qos, hwname);
  1078. return 0;
  1079. }
  1080. /*
  1081. * Function ali_ircc_net_close (dev)
  1082. *
  1083. * Stop the device
  1084. *
  1085. */
  1086. static int ali_ircc_net_close(struct net_device *dev)
  1087. {
  1088. struct ali_ircc_cb *self;
  1089. //int iobase;
  1090. IRDA_ASSERT(dev != NULL, return -1;);
  1091. self = netdev_priv(dev);
  1092. IRDA_ASSERT(self != NULL, return 0;);
  1093. /* Stop device */
  1094. netif_stop_queue(dev);
  1095. /* Stop and remove instance of IrLAP */
  1096. if (self->irlap)
  1097. irlap_close(self->irlap);
  1098. self->irlap = NULL;
  1099. disable_dma(self->io.dma);
  1100. /* Disable interrupts */
  1101. SetCOMInterrupts(self, FALSE);
  1102. free_irq(self->io.irq, dev);
  1103. free_dma(self->io.dma);
  1104. return 0;
  1105. }
  1106. /*
  1107. * Function ali_ircc_fir_hard_xmit (skb, dev)
  1108. *
  1109. * Transmit the frame
  1110. *
  1111. */
  1112. static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
  1113. struct net_device *dev)
  1114. {
  1115. struct ali_ircc_cb *self;
  1116. unsigned long flags;
  1117. int iobase;
  1118. __u32 speed;
  1119. int mtt, diff;
  1120. self = netdev_priv(dev);
  1121. iobase = self->io.fir_base;
  1122. netif_stop_queue(dev);
  1123. /* Make sure tests *& speed change are atomic */
  1124. spin_lock_irqsave(&self->lock, flags);
  1125. /* Note : you should make sure that speed changes are not going
  1126. * to corrupt any outgoing frame. Look at nsc-ircc for the gory
  1127. * details - Jean II */
  1128. /* Check if we need to change the speed */
  1129. speed = irda_get_next_speed(skb);
  1130. if ((speed != self->io.speed) && (speed != -1)) {
  1131. /* Check for empty frame */
  1132. if (!skb->len) {
  1133. ali_ircc_change_speed(self, speed);
  1134. dev->trans_start = jiffies;
  1135. spin_unlock_irqrestore(&self->lock, flags);
  1136. dev_kfree_skb(skb);
  1137. return NETDEV_TX_OK;
  1138. } else
  1139. self->new_speed = speed;
  1140. }
  1141. /* Register and copy this frame to DMA memory */
  1142. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  1143. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  1144. self->tx_fifo.tail += skb->len;
  1145. dev->stats.tx_bytes += skb->len;
  1146. skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
  1147. skb->len);
  1148. self->tx_fifo.len++;
  1149. self->tx_fifo.free++;
  1150. /* Start transmit only if there is currently no transmit going on */
  1151. if (self->tx_fifo.len == 1)
  1152. {
  1153. /* Check if we must wait the min turn time or not */
  1154. mtt = irda_get_mtt(skb);
  1155. if (mtt)
  1156. {
  1157. /* Check how much time we have used already */
  1158. do_gettimeofday(&self->now);
  1159. diff = self->now.tv_usec - self->stamp.tv_usec;
  1160. /* self->stamp is set from ali_ircc_dma_receive_complete() */
  1161. pr_debug("%s(), ******* diff = %d *******\n",
  1162. __func__, diff);
  1163. if (diff < 0)
  1164. diff += 1000000;
  1165. /* Check if the mtt is larger than the time we have
  1166. * already used by all the protocol processing
  1167. */
  1168. if (mtt > diff)
  1169. {
  1170. mtt -= diff;
  1171. /*
  1172. * Use timer if delay larger than 1000 us, and
  1173. * use udelay for smaller values which should
  1174. * be acceptable
  1175. */
  1176. if (mtt > 500)
  1177. {
  1178. /* Adjust for timer resolution */
  1179. mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
  1180. pr_debug("%s(), ************** mtt = %d ***********\n",
  1181. __func__, mtt);
  1182. /* Setup timer */
  1183. if (mtt == 1) /* 500 us */
  1184. {
  1185. switch_bank(iobase, BANK1);
  1186. outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
  1187. }
  1188. else if (mtt == 2) /* 1 ms */
  1189. {
  1190. switch_bank(iobase, BANK1);
  1191. outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
  1192. }
  1193. else /* > 2ms -> 4ms */
  1194. {
  1195. switch_bank(iobase, BANK1);
  1196. outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
  1197. }
  1198. /* Start timer */
  1199. outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
  1200. self->io.direction = IO_XMIT;
  1201. /* Enable timer interrupt */
  1202. self->ier = IER_TIMER;
  1203. SetCOMInterrupts(self, TRUE);
  1204. /* Timer will take care of the rest */
  1205. goto out;
  1206. }
  1207. else
  1208. udelay(mtt);
  1209. } // if (if (mtt > diff)
  1210. }// if (mtt)
  1211. /* Enable EOM interrupt */
  1212. self->ier = IER_EOM;
  1213. SetCOMInterrupts(self, TRUE);
  1214. /* Transmit frame */
  1215. ali_ircc_dma_xmit(self);
  1216. } // if (self->tx_fifo.len == 1)
  1217. out:
  1218. /* Not busy transmitting anymore if window is not full */
  1219. if (self->tx_fifo.free < MAX_TX_WINDOW)
  1220. netif_wake_queue(self->netdev);
  1221. /* Restore bank register */
  1222. switch_bank(iobase, BANK0);
  1223. dev->trans_start = jiffies;
  1224. spin_unlock_irqrestore(&self->lock, flags);
  1225. dev_kfree_skb(skb);
  1226. return NETDEV_TX_OK;
  1227. }
  1228. static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
  1229. {
  1230. int iobase, tmp;
  1231. unsigned char FIFO_OPTI, Hi, Lo;
  1232. iobase = self->io.fir_base;
  1233. /* FIFO threshold , this method comes from NDIS5 code */
  1234. if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
  1235. FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
  1236. else
  1237. FIFO_OPTI = TX_FIFO_Threshold;
  1238. /* Disable DMA */
  1239. switch_bank(iobase, BANK1);
  1240. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1241. self->io.direction = IO_XMIT;
  1242. irda_setup_dma(self->io.dma,
  1243. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  1244. self->tx_buff.head) + self->tx_buff_dma,
  1245. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  1246. DMA_TX_MODE);
  1247. /* Reset Tx FIFO */
  1248. switch_bank(iobase, BANK0);
  1249. outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
  1250. /* Set Tx FIFO threshold */
  1251. if (self->fifo_opti_buf!=FIFO_OPTI)
  1252. {
  1253. switch_bank(iobase, BANK1);
  1254. outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
  1255. self->fifo_opti_buf=FIFO_OPTI;
  1256. }
  1257. /* Set Tx DMA threshold */
  1258. switch_bank(iobase, BANK1);
  1259. outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
  1260. /* Set max Tx frame size */
  1261. Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
  1262. Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
  1263. switch_bank(iobase, BANK2);
  1264. outb(Hi, iobase+FIR_TX_DSR_HI);
  1265. outb(Lo, iobase+FIR_TX_DSR_LO);
  1266. /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
  1267. switch_bank(iobase, BANK0);
  1268. tmp = inb(iobase+FIR_LCR_B);
  1269. tmp &= ~0x20; // Disable SIP
  1270. outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
  1271. pr_debug("%s(), *** Change to TX mode: FIR_LCR_B = 0x%x ***\n",
  1272. __func__, inb(iobase + FIR_LCR_B));
  1273. outb(0, iobase+FIR_LSR);
  1274. /* Enable DMA and Burst Mode */
  1275. switch_bank(iobase, BANK1);
  1276. outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
  1277. switch_bank(iobase, BANK0);
  1278. }
  1279. static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
  1280. {
  1281. int iobase;
  1282. int ret = TRUE;
  1283. iobase = self->io.fir_base;
  1284. /* Disable DMA */
  1285. switch_bank(iobase, BANK1);
  1286. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1287. /* Check for underrun! */
  1288. switch_bank(iobase, BANK0);
  1289. if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
  1290. {
  1291. net_err_ratelimited("%s(), ********* LSR_FRAME_ABORT *********\n",
  1292. __func__);
  1293. self->netdev->stats.tx_errors++;
  1294. self->netdev->stats.tx_fifo_errors++;
  1295. }
  1296. else
  1297. {
  1298. self->netdev->stats.tx_packets++;
  1299. }
  1300. /* Check if we need to change the speed */
  1301. if (self->new_speed)
  1302. {
  1303. ali_ircc_change_speed(self, self->new_speed);
  1304. self->new_speed = 0;
  1305. }
  1306. /* Finished with this frame, so prepare for next */
  1307. self->tx_fifo.ptr++;
  1308. self->tx_fifo.len--;
  1309. /* Any frames to be sent back-to-back? */
  1310. if (self->tx_fifo.len)
  1311. {
  1312. ali_ircc_dma_xmit(self);
  1313. /* Not finished yet! */
  1314. ret = FALSE;
  1315. }
  1316. else
  1317. { /* Reset Tx FIFO info */
  1318. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1319. self->tx_fifo.tail = self->tx_buff.head;
  1320. }
  1321. /* Make sure we have room for more frames */
  1322. if (self->tx_fifo.free < MAX_TX_WINDOW) {
  1323. /* Not busy transmitting anymore */
  1324. /* Tell the network layer, that we can accept more frames */
  1325. netif_wake_queue(self->netdev);
  1326. }
  1327. switch_bank(iobase, BANK0);
  1328. return ret;
  1329. }
  1330. /*
  1331. * Function ali_ircc_dma_receive (self)
  1332. *
  1333. * Get ready for receiving a frame. The device will initiate a DMA
  1334. * if it starts to receive a frame.
  1335. *
  1336. */
  1337. static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
  1338. {
  1339. int iobase, tmp;
  1340. iobase = self->io.fir_base;
  1341. /* Reset Tx FIFO info */
  1342. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  1343. self->tx_fifo.tail = self->tx_buff.head;
  1344. /* Disable DMA */
  1345. switch_bank(iobase, BANK1);
  1346. outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
  1347. /* Reset Message Count */
  1348. switch_bank(iobase, BANK0);
  1349. outb(0x07, iobase+FIR_LSR);
  1350. self->rcvFramesOverflow = FALSE;
  1351. self->LineStatus = inb(iobase+FIR_LSR) ;
  1352. /* Reset Rx FIFO info */
  1353. self->io.direction = IO_RECV;
  1354. self->rx_buff.data = self->rx_buff.head;
  1355. /* Reset Rx FIFO */
  1356. // switch_bank(iobase, BANK0);
  1357. outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
  1358. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  1359. self->st_fifo.tail = self->st_fifo.head = 0;
  1360. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  1361. DMA_RX_MODE);
  1362. /* Set Receive Mode,Brick Wall */
  1363. //switch_bank(iobase, BANK0);
  1364. tmp = inb(iobase+FIR_LCR_B);
  1365. outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
  1366. pr_debug("%s(), *** Change To RX mode: FIR_LCR_B = 0x%x ***\n",
  1367. __func__, inb(iobase + FIR_LCR_B));
  1368. /* Set Rx Threshold */
  1369. switch_bank(iobase, BANK1);
  1370. outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
  1371. outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
  1372. /* Enable DMA and Burst Mode */
  1373. // switch_bank(iobase, BANK1);
  1374. outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
  1375. switch_bank(iobase, BANK0);
  1376. return 0;
  1377. }
  1378. static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
  1379. {
  1380. struct st_fifo *st_fifo;
  1381. struct sk_buff *skb;
  1382. __u8 status, MessageCount;
  1383. int len, i, iobase, val;
  1384. st_fifo = &self->st_fifo;
  1385. iobase = self->io.fir_base;
  1386. switch_bank(iobase, BANK0);
  1387. MessageCount = inb(iobase+ FIR_LSR)&0x07;
  1388. if (MessageCount > 0)
  1389. pr_debug("%s(), Message count = %d\n", __func__, MessageCount);
  1390. for (i=0; i<=MessageCount; i++)
  1391. {
  1392. /* Bank 0 */
  1393. switch_bank(iobase, BANK0);
  1394. status = inb(iobase+FIR_LSR);
  1395. switch_bank(iobase, BANK2);
  1396. len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
  1397. len = len << 8;
  1398. len |= inb(iobase+FIR_RX_DSR_LO);
  1399. pr_debug("%s(), RX Length = 0x%.2x,\n", __func__ , len);
  1400. pr_debug("%s(), RX Status = 0x%.2x,\n", __func__ , status);
  1401. if (st_fifo->tail >= MAX_RX_WINDOW) {
  1402. pr_debug("%s(), window is full!\n", __func__);
  1403. continue;
  1404. }
  1405. st_fifo->entries[st_fifo->tail].status = status;
  1406. st_fifo->entries[st_fifo->tail].len = len;
  1407. st_fifo->pending_bytes += len;
  1408. st_fifo->tail++;
  1409. st_fifo->len++;
  1410. }
  1411. for (i=0; i<=MessageCount; i++)
  1412. {
  1413. /* Get first entry */
  1414. status = st_fifo->entries[st_fifo->head].status;
  1415. len = st_fifo->entries[st_fifo->head].len;
  1416. st_fifo->pending_bytes -= len;
  1417. st_fifo->head++;
  1418. st_fifo->len--;
  1419. /* Check for errors */
  1420. if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
  1421. {
  1422. pr_debug("%s(), ************* RX Errors ************\n",
  1423. __func__);
  1424. /* Skip frame */
  1425. self->netdev->stats.rx_errors++;
  1426. self->rx_buff.data += len;
  1427. if (status & LSR_FIFO_UR)
  1428. {
  1429. self->netdev->stats.rx_frame_errors++;
  1430. pr_debug("%s(), ************* FIFO Errors ************\n",
  1431. __func__);
  1432. }
  1433. if (status & LSR_FRAME_ERROR)
  1434. {
  1435. self->netdev->stats.rx_frame_errors++;
  1436. pr_debug("%s(), ************* FRAME Errors ************\n",
  1437. __func__);
  1438. }
  1439. if (status & LSR_CRC_ERROR)
  1440. {
  1441. self->netdev->stats.rx_crc_errors++;
  1442. pr_debug("%s(), ************* CRC Errors ************\n",
  1443. __func__);
  1444. }
  1445. if(self->rcvFramesOverflow)
  1446. {
  1447. self->netdev->stats.rx_frame_errors++;
  1448. pr_debug("%s(), ************* Overran DMA buffer ************\n",
  1449. __func__);
  1450. }
  1451. if(len == 0)
  1452. {
  1453. self->netdev->stats.rx_frame_errors++;
  1454. pr_debug("%s(), ********** Receive Frame Size = 0 *********\n",
  1455. __func__);
  1456. }
  1457. }
  1458. else
  1459. {
  1460. if (st_fifo->pending_bytes < 32)
  1461. {
  1462. switch_bank(iobase, BANK0);
  1463. val = inb(iobase+FIR_BSR);
  1464. if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
  1465. {
  1466. pr_debug("%s(), ************* BSR_FIFO_NOT_EMPTY ************\n",
  1467. __func__);
  1468. /* Put this entry back in fifo */
  1469. st_fifo->head--;
  1470. st_fifo->len++;
  1471. st_fifo->pending_bytes += len;
  1472. st_fifo->entries[st_fifo->head].status = status;
  1473. st_fifo->entries[st_fifo->head].len = len;
  1474. /*
  1475. * DMA not finished yet, so try again
  1476. * later, set timer value, resolution
  1477. * 500 us
  1478. */
  1479. switch_bank(iobase, BANK1);
  1480. outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
  1481. /* Enable Timer */
  1482. outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
  1483. return FALSE; /* I'll be back! */
  1484. }
  1485. }
  1486. /*
  1487. * Remember the time we received this frame, so we can
  1488. * reduce the min turn time a bit since we will know
  1489. * how much time we have used for protocol processing
  1490. */
  1491. do_gettimeofday(&self->stamp);
  1492. skb = dev_alloc_skb(len+1);
  1493. if (skb == NULL)
  1494. {
  1495. self->netdev->stats.rx_dropped++;
  1496. return FALSE;
  1497. }
  1498. /* Make sure IP header gets aligned */
  1499. skb_reserve(skb, 1);
  1500. /* Copy frame without CRC, CRC is removed by hardware*/
  1501. skb_put(skb, len);
  1502. skb_copy_to_linear_data(skb, self->rx_buff.data, len);
  1503. /* Move to next frame */
  1504. self->rx_buff.data += len;
  1505. self->netdev->stats.rx_bytes += len;
  1506. self->netdev->stats.rx_packets++;
  1507. skb->dev = self->netdev;
  1508. skb_reset_mac_header(skb);
  1509. skb->protocol = htons(ETH_P_IRDA);
  1510. netif_rx(skb);
  1511. }
  1512. }
  1513. switch_bank(iobase, BANK0);
  1514. return TRUE;
  1515. }
  1516. /*
  1517. * Function ali_ircc_sir_hard_xmit (skb, dev)
  1518. *
  1519. * Transmit the frame!
  1520. *
  1521. */
  1522. static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
  1523. struct net_device *dev)
  1524. {
  1525. struct ali_ircc_cb *self;
  1526. unsigned long flags;
  1527. int iobase;
  1528. __u32 speed;
  1529. IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
  1530. self = netdev_priv(dev);
  1531. IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
  1532. iobase = self->io.sir_base;
  1533. netif_stop_queue(dev);
  1534. /* Make sure tests *& speed change are atomic */
  1535. spin_lock_irqsave(&self->lock, flags);
  1536. /* Note : you should make sure that speed changes are not going
  1537. * to corrupt any outgoing frame. Look at nsc-ircc for the gory
  1538. * details - Jean II */
  1539. /* Check if we need to change the speed */
  1540. speed = irda_get_next_speed(skb);
  1541. if ((speed != self->io.speed) && (speed != -1)) {
  1542. /* Check for empty frame */
  1543. if (!skb->len) {
  1544. ali_ircc_change_speed(self, speed);
  1545. dev->trans_start = jiffies;
  1546. spin_unlock_irqrestore(&self->lock, flags);
  1547. dev_kfree_skb(skb);
  1548. return NETDEV_TX_OK;
  1549. } else
  1550. self->new_speed = speed;
  1551. }
  1552. /* Init tx buffer */
  1553. self->tx_buff.data = self->tx_buff.head;
  1554. /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
  1555. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  1556. self->tx_buff.truesize);
  1557. self->netdev->stats.tx_bytes += self->tx_buff.len;
  1558. /* Turn on transmit finished interrupt. Will fire immediately! */
  1559. outb(UART_IER_THRI, iobase+UART_IER);
  1560. dev->trans_start = jiffies;
  1561. spin_unlock_irqrestore(&self->lock, flags);
  1562. dev_kfree_skb(skb);
  1563. return NETDEV_TX_OK;
  1564. }
  1565. /*
  1566. * Function ali_ircc_net_ioctl (dev, rq, cmd)
  1567. *
  1568. * Process IOCTL commands for this device
  1569. *
  1570. */
  1571. static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1572. {
  1573. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1574. struct ali_ircc_cb *self;
  1575. unsigned long flags;
  1576. int ret = 0;
  1577. IRDA_ASSERT(dev != NULL, return -1;);
  1578. self = netdev_priv(dev);
  1579. IRDA_ASSERT(self != NULL, return -1;);
  1580. pr_debug("%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
  1581. switch (cmd) {
  1582. case SIOCSBANDWIDTH: /* Set bandwidth */
  1583. pr_debug("%s(), SIOCSBANDWIDTH\n", __func__);
  1584. /*
  1585. * This function will also be used by IrLAP to change the
  1586. * speed, so we still must allow for speed change within
  1587. * interrupt context.
  1588. */
  1589. if (!in_interrupt() && !capable(CAP_NET_ADMIN))
  1590. return -EPERM;
  1591. spin_lock_irqsave(&self->lock, flags);
  1592. ali_ircc_change_speed(self, irq->ifr_baudrate);
  1593. spin_unlock_irqrestore(&self->lock, flags);
  1594. break;
  1595. case SIOCSMEDIABUSY: /* Set media busy */
  1596. pr_debug("%s(), SIOCSMEDIABUSY\n", __func__);
  1597. if (!capable(CAP_NET_ADMIN))
  1598. return -EPERM;
  1599. irda_device_set_media_busy(self->netdev, TRUE);
  1600. break;
  1601. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1602. pr_debug("%s(), SIOCGRECEIVING\n", __func__);
  1603. /* This is protected */
  1604. irq->ifr_receiving = ali_ircc_is_receiving(self);
  1605. break;
  1606. default:
  1607. ret = -EOPNOTSUPP;
  1608. }
  1609. return ret;
  1610. }
  1611. /*
  1612. * Function ali_ircc_is_receiving (self)
  1613. *
  1614. * Return TRUE is we are currently receiving a frame
  1615. *
  1616. */
  1617. static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
  1618. {
  1619. unsigned long flags;
  1620. int status = FALSE;
  1621. int iobase;
  1622. IRDA_ASSERT(self != NULL, return FALSE;);
  1623. spin_lock_irqsave(&self->lock, flags);
  1624. if (self->io.speed > 115200)
  1625. {
  1626. iobase = self->io.fir_base;
  1627. switch_bank(iobase, BANK1);
  1628. if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
  1629. {
  1630. /* We are receiving something */
  1631. pr_debug("%s(), We are receiving something\n",
  1632. __func__);
  1633. status = TRUE;
  1634. }
  1635. switch_bank(iobase, BANK0);
  1636. }
  1637. else
  1638. {
  1639. status = (self->rx_buff.state != OUTSIDE_FRAME);
  1640. }
  1641. spin_unlock_irqrestore(&self->lock, flags);
  1642. return status;
  1643. }
  1644. static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
  1645. {
  1646. struct ali_ircc_cb *self = platform_get_drvdata(dev);
  1647. net_info_ratelimited("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
  1648. if (self->io.suspended)
  1649. return 0;
  1650. ali_ircc_net_close(self->netdev);
  1651. self->io.suspended = 1;
  1652. return 0;
  1653. }
  1654. static int ali_ircc_resume(struct platform_device *dev)
  1655. {
  1656. struct ali_ircc_cb *self = platform_get_drvdata(dev);
  1657. if (!self->io.suspended)
  1658. return 0;
  1659. ali_ircc_net_open(self->netdev);
  1660. net_info_ratelimited("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
  1661. self->io.suspended = 0;
  1662. return 0;
  1663. }
  1664. /* ALi Chip Function */
  1665. static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
  1666. {
  1667. unsigned char newMask;
  1668. int iobase = self->io.fir_base; /* or sir_base */
  1669. pr_debug("%s(), -------- Start -------- ( Enable = %d )\n",
  1670. __func__, enable);
  1671. /* Enable the interrupt which we wish to */
  1672. if (enable){
  1673. if (self->io.direction == IO_XMIT)
  1674. {
  1675. if (self->io.speed > 115200) /* FIR, MIR */
  1676. {
  1677. newMask = self->ier;
  1678. }
  1679. else /* SIR */
  1680. {
  1681. newMask = UART_IER_THRI | UART_IER_RDI;
  1682. }
  1683. }
  1684. else {
  1685. if (self->io.speed > 115200) /* FIR, MIR */
  1686. {
  1687. newMask = self->ier;
  1688. }
  1689. else /* SIR */
  1690. {
  1691. newMask = UART_IER_RDI;
  1692. }
  1693. }
  1694. }
  1695. else /* Disable all the interrupts */
  1696. {
  1697. newMask = 0x00;
  1698. }
  1699. //SIR and FIR has different registers
  1700. if (self->io.speed > 115200)
  1701. {
  1702. switch_bank(iobase, BANK0);
  1703. outb(newMask, iobase+FIR_IER);
  1704. }
  1705. else
  1706. outb(newMask, iobase+UART_IER);
  1707. }
  1708. static void SIR2FIR(int iobase)
  1709. {
  1710. //unsigned char tmp;
  1711. /* Already protected (change_speed() or setup()), no need to lock.
  1712. * Jean II */
  1713. outb(0x28, iobase+UART_MCR);
  1714. outb(0x68, iobase+UART_MCR);
  1715. outb(0x88, iobase+UART_MCR);
  1716. outb(0x60, iobase+FIR_MCR); /* Master Reset */
  1717. outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
  1718. //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
  1719. //tmp |= 0x20;
  1720. //outb(tmp, iobase+FIR_LCR_B);
  1721. }
  1722. static void FIR2SIR(int iobase)
  1723. {
  1724. unsigned char val;
  1725. /* Already protected (change_speed() or setup()), no need to lock.
  1726. * Jean II */
  1727. outb(0x20, iobase+FIR_MCR); /* IRQ to low */
  1728. outb(0x00, iobase+UART_IER);
  1729. outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
  1730. outb(0x00, iobase+UART_FCR);
  1731. outb(0x07, iobase+UART_FCR);
  1732. val = inb(iobase+UART_RX);
  1733. val = inb(iobase+UART_LSR);
  1734. val = inb(iobase+UART_MSR);
  1735. }
  1736. MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
  1737. MODULE_DESCRIPTION("ALi FIR Controller Driver");
  1738. MODULE_LICENSE("GPL");
  1739. MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
  1740. module_param_array(io, int, NULL, 0);
  1741. MODULE_PARM_DESC(io, "Base I/O addresses");
  1742. module_param_array(irq, int, NULL, 0);
  1743. MODULE_PARM_DESC(irq, "IRQ lines");
  1744. module_param_array(dma, int, NULL, 0);
  1745. MODULE_PARM_DESC(dma, "DMA channels");
  1746. module_init(ali_ircc_init);
  1747. module_exit(ali_ircc_cleanup);