via-ircc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  1. /********************************************************************
  2. Filename: via-ircc.c
  3. Version: 1.0
  4. Description: Driver for the VIA VT8231/VT8233 IrDA chipsets
  5. Author: VIA Technologies,inc
  6. Date : 08/06/2003
  7. Copyright (c) 1998-2003 VIA Technologies, Inc.
  8. This program is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free Software
  10. Foundation; either version 2, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTIES OR REPRESENTATIONS; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. See the GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License along with
  16. this program; if not, see <http://www.gnu.org/licenses/>.
  17. F01 Oct/02/02: Modify code for V0.11(move out back to back transfer)
  18. F02 Oct/28/02: Add SB device ID for 3147 and 3177.
  19. Comment :
  20. jul/09/2002 : only implement two kind of dongle currently.
  21. Oct/02/2002 : work on VT8231 and VT8233 .
  22. Aug/06/2003 : change driver format to pci driver .
  23. 2004-02-16: <sda@bdit.de>
  24. - Removed unneeded 'legacy' pci stuff.
  25. - Make sure SIR mode is set (hw_init()) before calling mode-dependent stuff.
  26. - On speed change from core, don't send SIR frame with new speed.
  27. Use current speed and change speeds later.
  28. - Make module-param dongle_id actually work.
  29. - New dongle_id 17 (0x11): TDFS4500. Single-ended SIR only.
  30. Tested with home-grown PCB on EPIA boards.
  31. - Code cleanup.
  32. ********************************************************************/
  33. #include <linux/module.h>
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/ioport.h>
  39. #include <linux/delay.h>
  40. #include <linux/init.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/rtnetlink.h>
  43. #include <linux/pci.h>
  44. #include <linux/dma-mapping.h>
  45. #include <linux/gfp.h>
  46. #include <asm/io.h>
  47. #include <asm/dma.h>
  48. #include <asm/byteorder.h>
  49. #include <linux/pm.h>
  50. #include <net/irda/wrapper.h>
  51. #include <net/irda/irda.h>
  52. #include <net/irda/irda_device.h>
  53. #include "via-ircc.h"
  54. #define VIA_MODULE_NAME "via-ircc"
  55. #define CHIP_IO_EXTENT 0x40
  56. static char *driver_name = VIA_MODULE_NAME;
  57. /* Module parameters */
  58. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  59. static int dongle_id = 0; /* default: probe */
  60. /* We can't guess the type of connected dongle, user *must* supply it. */
  61. module_param(dongle_id, int, 0);
  62. /* Some prototypes */
  63. static int via_ircc_open(struct pci_dev *pdev, chipio_t *info,
  64. unsigned int id);
  65. static int via_ircc_dma_receive(struct via_ircc_cb *self);
  66. static int via_ircc_dma_receive_complete(struct via_ircc_cb *self,
  67. int iobase);
  68. static netdev_tx_t via_ircc_hard_xmit_sir(struct sk_buff *skb,
  69. struct net_device *dev);
  70. static netdev_tx_t via_ircc_hard_xmit_fir(struct sk_buff *skb,
  71. struct net_device *dev);
  72. static void via_hw_init(struct via_ircc_cb *self);
  73. static void via_ircc_change_speed(struct via_ircc_cb *self, __u32 baud);
  74. static irqreturn_t via_ircc_interrupt(int irq, void *dev_id);
  75. static int via_ircc_is_receiving(struct via_ircc_cb *self);
  76. static int via_ircc_read_dongle_id(int iobase);
  77. static int via_ircc_net_open(struct net_device *dev);
  78. static int via_ircc_net_close(struct net_device *dev);
  79. static int via_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq,
  80. int cmd);
  81. static void via_ircc_change_dongle_speed(int iobase, int speed,
  82. int dongle_id);
  83. static int RxTimerHandler(struct via_ircc_cb *self, int iobase);
  84. static void hwreset(struct via_ircc_cb *self);
  85. static int via_ircc_dma_xmit(struct via_ircc_cb *self, u16 iobase);
  86. static int upload_rxdata(struct via_ircc_cb *self, int iobase);
  87. static int via_init_one(struct pci_dev *pcidev, const struct pci_device_id *id);
  88. static void via_remove_one(struct pci_dev *pdev);
  89. /* FIXME : Should use udelay() instead, even if we are x86 only - Jean II */
  90. static void iodelay(int udelay)
  91. {
  92. u8 data;
  93. int i;
  94. for (i = 0; i < udelay; i++) {
  95. data = inb(0x80);
  96. }
  97. }
  98. static const struct pci_device_id via_pci_tbl[] = {
  99. { PCI_VENDOR_ID_VIA, 0x8231, PCI_ANY_ID, PCI_ANY_ID,0,0,0 },
  100. { PCI_VENDOR_ID_VIA, 0x3109, PCI_ANY_ID, PCI_ANY_ID,0,0,1 },
  101. { PCI_VENDOR_ID_VIA, 0x3074, PCI_ANY_ID, PCI_ANY_ID,0,0,2 },
  102. { PCI_VENDOR_ID_VIA, 0x3147, PCI_ANY_ID, PCI_ANY_ID,0,0,3 },
  103. { PCI_VENDOR_ID_VIA, 0x3177, PCI_ANY_ID, PCI_ANY_ID,0,0,4 },
  104. { 0, }
  105. };
  106. MODULE_DEVICE_TABLE(pci,via_pci_tbl);
  107. static struct pci_driver via_driver = {
  108. .name = VIA_MODULE_NAME,
  109. .id_table = via_pci_tbl,
  110. .probe = via_init_one,
  111. .remove = via_remove_one,
  112. };
  113. /*
  114. * Function via_ircc_init ()
  115. *
  116. * Initialize chip. Just find out chip type and resource.
  117. */
  118. static int __init via_ircc_init(void)
  119. {
  120. int rc;
  121. rc = pci_register_driver(&via_driver);
  122. if (rc < 0) {
  123. pr_debug("%s(): error rc = %d, returning -ENODEV...\n",
  124. __func__, rc);
  125. return -ENODEV;
  126. }
  127. return 0;
  128. }
  129. static int via_init_one(struct pci_dev *pcidev, const struct pci_device_id *id)
  130. {
  131. int rc;
  132. u8 temp,oldPCI_40,oldPCI_44,bTmp,bTmp1;
  133. u16 Chipset,FirDRQ1,FirDRQ0,FirIRQ,FirIOBase;
  134. chipio_t info;
  135. pr_debug("%s(): Device ID=(0X%X)\n", __func__, id->device);
  136. rc = pci_enable_device (pcidev);
  137. if (rc) {
  138. pr_debug("%s(): error rc = %d\n", __func__, rc);
  139. return -ENODEV;
  140. }
  141. // South Bridge exist
  142. if ( ReadLPCReg(0x20) != 0x3C )
  143. Chipset=0x3096;
  144. else
  145. Chipset=0x3076;
  146. if (Chipset==0x3076) {
  147. pr_debug("%s(): Chipset = 3076\n", __func__);
  148. WriteLPCReg(7,0x0c );
  149. temp=ReadLPCReg(0x30);//check if BIOS Enable Fir
  150. if((temp&0x01)==1) { // BIOS close or no FIR
  151. WriteLPCReg(0x1d, 0x82 );
  152. WriteLPCReg(0x23,0x18);
  153. temp=ReadLPCReg(0xF0);
  154. if((temp&0x01)==0) {
  155. temp=(ReadLPCReg(0x74)&0x03); //DMA
  156. FirDRQ0=temp + 4;
  157. temp=(ReadLPCReg(0x74)&0x0C) >> 2;
  158. FirDRQ1=temp + 4;
  159. } else {
  160. temp=(ReadLPCReg(0x74)&0x0C) >> 2; //DMA
  161. FirDRQ0=temp + 4;
  162. FirDRQ1=FirDRQ0;
  163. }
  164. FirIRQ=(ReadLPCReg(0x70)&0x0f); //IRQ
  165. FirIOBase=ReadLPCReg(0x60 ) << 8; //IO Space :high byte
  166. FirIOBase=FirIOBase| ReadLPCReg(0x61) ; //low byte
  167. FirIOBase=FirIOBase ;
  168. info.fir_base=FirIOBase;
  169. info.irq=FirIRQ;
  170. info.dma=FirDRQ1;
  171. info.dma2=FirDRQ0;
  172. pci_read_config_byte(pcidev,0x40,&bTmp);
  173. pci_write_config_byte(pcidev,0x40,((bTmp | 0x08) & 0xfe));
  174. pci_read_config_byte(pcidev,0x42,&bTmp);
  175. pci_write_config_byte(pcidev,0x42,(bTmp | 0xf0));
  176. pci_write_config_byte(pcidev,0x5a,0xc0);
  177. WriteLPCReg(0x28, 0x70 );
  178. rc = via_ircc_open(pcidev, &info, 0x3076);
  179. } else
  180. rc = -ENODEV; //IR not turn on
  181. } else { //Not VT1211
  182. pr_debug("%s(): Chipset = 3096\n", __func__);
  183. pci_read_config_byte(pcidev,0x67,&bTmp);//check if BIOS Enable Fir
  184. if((bTmp&0x01)==1) { // BIOS enable FIR
  185. //Enable Double DMA clock
  186. pci_read_config_byte(pcidev,0x42,&oldPCI_40);
  187. pci_write_config_byte(pcidev,0x42,oldPCI_40 | 0x80);
  188. pci_read_config_byte(pcidev,0x40,&oldPCI_40);
  189. pci_write_config_byte(pcidev,0x40,oldPCI_40 & 0xf7);
  190. pci_read_config_byte(pcidev,0x44,&oldPCI_44);
  191. pci_write_config_byte(pcidev,0x44,0x4e);
  192. //---------- read configuration from Function0 of south bridge
  193. if((bTmp&0x02)==0) {
  194. pci_read_config_byte(pcidev,0x44,&bTmp1); //DMA
  195. FirDRQ0 = (bTmp1 & 0x30) >> 4;
  196. pci_read_config_byte(pcidev,0x44,&bTmp1);
  197. FirDRQ1 = (bTmp1 & 0xc0) >> 6;
  198. } else {
  199. pci_read_config_byte(pcidev,0x44,&bTmp1); //DMA
  200. FirDRQ0 = (bTmp1 & 0x30) >> 4 ;
  201. FirDRQ1=0;
  202. }
  203. pci_read_config_byte(pcidev,0x47,&bTmp1); //IRQ
  204. FirIRQ = bTmp1 & 0x0f;
  205. pci_read_config_byte(pcidev,0x69,&bTmp);
  206. FirIOBase = bTmp << 8;//hight byte
  207. pci_read_config_byte(pcidev,0x68,&bTmp);
  208. FirIOBase = (FirIOBase | bTmp ) & 0xfff0;
  209. //-------------------------
  210. info.fir_base=FirIOBase;
  211. info.irq=FirIRQ;
  212. info.dma=FirDRQ1;
  213. info.dma2=FirDRQ0;
  214. rc = via_ircc_open(pcidev, &info, 0x3096);
  215. } else
  216. rc = -ENODEV; //IR not turn on !!!!!
  217. }//Not VT1211
  218. pr_debug("%s(): End - rc = %d\n", __func__, rc);
  219. return rc;
  220. }
  221. static void __exit via_ircc_cleanup(void)
  222. {
  223. /* Cleanup all instances of the driver */
  224. pci_unregister_driver (&via_driver);
  225. }
  226. static const struct net_device_ops via_ircc_sir_ops = {
  227. .ndo_start_xmit = via_ircc_hard_xmit_sir,
  228. .ndo_open = via_ircc_net_open,
  229. .ndo_stop = via_ircc_net_close,
  230. .ndo_do_ioctl = via_ircc_net_ioctl,
  231. };
  232. static const struct net_device_ops via_ircc_fir_ops = {
  233. .ndo_start_xmit = via_ircc_hard_xmit_fir,
  234. .ndo_open = via_ircc_net_open,
  235. .ndo_stop = via_ircc_net_close,
  236. .ndo_do_ioctl = via_ircc_net_ioctl,
  237. };
  238. /*
  239. * Function via_ircc_open(pdev, iobase, irq)
  240. *
  241. * Open driver instance
  242. *
  243. */
  244. static int via_ircc_open(struct pci_dev *pdev, chipio_t *info, unsigned int id)
  245. {
  246. struct net_device *dev;
  247. struct via_ircc_cb *self;
  248. int err;
  249. /* Allocate new instance of the driver */
  250. dev = alloc_irdadev(sizeof(struct via_ircc_cb));
  251. if (dev == NULL)
  252. return -ENOMEM;
  253. self = netdev_priv(dev);
  254. self->netdev = dev;
  255. spin_lock_init(&self->lock);
  256. pci_set_drvdata(pdev, self);
  257. /* Initialize Resource */
  258. self->io.cfg_base = info->cfg_base;
  259. self->io.fir_base = info->fir_base;
  260. self->io.irq = info->irq;
  261. self->io.fir_ext = CHIP_IO_EXTENT;
  262. self->io.dma = info->dma;
  263. self->io.dma2 = info->dma2;
  264. self->io.fifo_size = 32;
  265. self->chip_id = id;
  266. self->st_fifo.len = 0;
  267. self->RxDataReady = 0;
  268. /* Reserve the ioports that we need */
  269. if (!request_region(self->io.fir_base, self->io.fir_ext, driver_name)) {
  270. pr_debug("%s(), can't get iobase of 0x%03x\n",
  271. __func__, self->io.fir_base);
  272. err = -ENODEV;
  273. goto err_out1;
  274. }
  275. /* Initialize QoS for this device */
  276. irda_init_max_qos_capabilies(&self->qos);
  277. /* Check if user has supplied the dongle id or not */
  278. if (!dongle_id)
  279. dongle_id = via_ircc_read_dongle_id(self->io.fir_base);
  280. self->io.dongle_id = dongle_id;
  281. /* The only value we must override it the baudrate */
  282. /* Maximum speeds and capabilities are dongle-dependent. */
  283. switch( self->io.dongle_id ){
  284. case 0x0d:
  285. self->qos.baud_rate.bits =
  286. IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200 |
  287. IR_576000 | IR_1152000 | (IR_4000000 << 8);
  288. break;
  289. default:
  290. self->qos.baud_rate.bits =
  291. IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  292. break;
  293. }
  294. /* Following was used for testing:
  295. *
  296. * self->qos.baud_rate.bits = IR_9600;
  297. *
  298. * Is is no good, as it prohibits (error-prone) speed-changes.
  299. */
  300. self->qos.min_turn_time.bits = qos_mtt_bits;
  301. irda_qos_bits_to_value(&self->qos);
  302. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  303. self->rx_buff.truesize = 14384 + 2048;
  304. self->tx_buff.truesize = 14384 + 2048;
  305. /* Allocate memory if needed */
  306. self->rx_buff.head =
  307. dma_zalloc_coherent(&pdev->dev, self->rx_buff.truesize,
  308. &self->rx_buff_dma, GFP_KERNEL);
  309. if (self->rx_buff.head == NULL) {
  310. err = -ENOMEM;
  311. goto err_out2;
  312. }
  313. self->tx_buff.head =
  314. dma_zalloc_coherent(&pdev->dev, self->tx_buff.truesize,
  315. &self->tx_buff_dma, GFP_KERNEL);
  316. if (self->tx_buff.head == NULL) {
  317. err = -ENOMEM;
  318. goto err_out3;
  319. }
  320. self->rx_buff.in_frame = FALSE;
  321. self->rx_buff.state = OUTSIDE_FRAME;
  322. self->tx_buff.data = self->tx_buff.head;
  323. self->rx_buff.data = self->rx_buff.head;
  324. /* Reset Tx queue info */
  325. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  326. self->tx_fifo.tail = self->tx_buff.head;
  327. /* Override the network functions we need to use */
  328. dev->netdev_ops = &via_ircc_sir_ops;
  329. err = register_netdev(dev);
  330. if (err)
  331. goto err_out4;
  332. net_info_ratelimited("IrDA: Registered device %s (via-ircc)\n",
  333. dev->name);
  334. /* Initialise the hardware..
  335. */
  336. self->io.speed = 9600;
  337. via_hw_init(self);
  338. return 0;
  339. err_out4:
  340. dma_free_coherent(&pdev->dev, self->tx_buff.truesize,
  341. self->tx_buff.head, self->tx_buff_dma);
  342. err_out3:
  343. dma_free_coherent(&pdev->dev, self->rx_buff.truesize,
  344. self->rx_buff.head, self->rx_buff_dma);
  345. err_out2:
  346. release_region(self->io.fir_base, self->io.fir_ext);
  347. err_out1:
  348. free_netdev(dev);
  349. return err;
  350. }
  351. /*
  352. * Function via_remove_one(pdev)
  353. *
  354. * Close driver instance
  355. *
  356. */
  357. static void via_remove_one(struct pci_dev *pdev)
  358. {
  359. struct via_ircc_cb *self = pci_get_drvdata(pdev);
  360. int iobase;
  361. iobase = self->io.fir_base;
  362. ResetChip(iobase, 5); //hardware reset.
  363. /* Remove netdevice */
  364. unregister_netdev(self->netdev);
  365. /* Release the PORT that this driver is using */
  366. pr_debug("%s(), Releasing Region %03x\n",
  367. __func__, self->io.fir_base);
  368. release_region(self->io.fir_base, self->io.fir_ext);
  369. if (self->tx_buff.head)
  370. dma_free_coherent(&pdev->dev, self->tx_buff.truesize,
  371. self->tx_buff.head, self->tx_buff_dma);
  372. if (self->rx_buff.head)
  373. dma_free_coherent(&pdev->dev, self->rx_buff.truesize,
  374. self->rx_buff.head, self->rx_buff_dma);
  375. free_netdev(self->netdev);
  376. pci_disable_device(pdev);
  377. }
  378. /*
  379. * Function via_hw_init(self)
  380. *
  381. * Returns non-negative on success.
  382. *
  383. * Formerly via_ircc_setup
  384. */
  385. static void via_hw_init(struct via_ircc_cb *self)
  386. {
  387. int iobase = self->io.fir_base;
  388. SetMaxRxPacketSize(iobase, 0x0fff); //set to max:4095
  389. // FIFO Init
  390. EnRXFIFOReadyInt(iobase, OFF);
  391. EnRXFIFOHalfLevelInt(iobase, OFF);
  392. EnTXFIFOHalfLevelInt(iobase, OFF);
  393. EnTXFIFOUnderrunEOMInt(iobase, ON);
  394. EnTXFIFOReadyInt(iobase, OFF);
  395. InvertTX(iobase, OFF);
  396. InvertRX(iobase, OFF);
  397. if (ReadLPCReg(0x20) == 0x3c)
  398. WriteLPCReg(0xF0, 0); // for VT1211
  399. /* Int Init */
  400. EnRXSpecInt(iobase, ON);
  401. /* The following is basically hwreset */
  402. /* If this is the case, why not just call hwreset() ? Jean II */
  403. ResetChip(iobase, 5);
  404. EnableDMA(iobase, OFF);
  405. EnableTX(iobase, OFF);
  406. EnableRX(iobase, OFF);
  407. EnRXDMA(iobase, OFF);
  408. EnTXDMA(iobase, OFF);
  409. RXStart(iobase, OFF);
  410. TXStart(iobase, OFF);
  411. InitCard(iobase);
  412. CommonInit(iobase);
  413. SIRFilter(iobase, ON);
  414. SetSIR(iobase, ON);
  415. CRC16(iobase, ON);
  416. EnTXCRC(iobase, 0);
  417. WriteReg(iobase, I_ST_CT_0, 0x00);
  418. SetBaudRate(iobase, 9600);
  419. SetPulseWidth(iobase, 12);
  420. SetSendPreambleCount(iobase, 0);
  421. self->io.speed = 9600;
  422. self->st_fifo.len = 0;
  423. via_ircc_change_dongle_speed(iobase, self->io.speed,
  424. self->io.dongle_id);
  425. WriteReg(iobase, I_ST_CT_0, 0x80);
  426. }
  427. /*
  428. * Function via_ircc_read_dongle_id (void)
  429. *
  430. */
  431. static int via_ircc_read_dongle_id(int iobase)
  432. {
  433. net_err_ratelimited("via-ircc: dongle probing not supported, please specify dongle_id module parameter\n");
  434. return 9; /* Default to IBM */
  435. }
  436. /*
  437. * Function via_ircc_change_dongle_speed (iobase, speed, dongle_id)
  438. * Change speed of the attach dongle
  439. * only implement two type of dongle currently.
  440. */
  441. static void via_ircc_change_dongle_speed(int iobase, int speed,
  442. int dongle_id)
  443. {
  444. u8 mode = 0;
  445. /* speed is unused, as we use IsSIROn()/IsMIROn() */
  446. speed = speed;
  447. pr_debug("%s(): change_dongle_speed to %d for 0x%x, %d\n",
  448. __func__, speed, iobase, dongle_id);
  449. switch (dongle_id) {
  450. /* Note: The dongle_id's listed here are derived from
  451. * nsc-ircc.c */
  452. case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
  453. UseOneRX(iobase, ON); // use one RX pin RX1,RX2
  454. InvertTX(iobase, OFF);
  455. InvertRX(iobase, OFF);
  456. EnRX2(iobase, ON); //sir to rx2
  457. EnGPIOtoRX2(iobase, OFF);
  458. if (IsSIROn(iobase)) { //sir
  459. // Mode select Off
  460. SlowIRRXLowActive(iobase, ON);
  461. udelay(1000);
  462. SlowIRRXLowActive(iobase, OFF);
  463. } else {
  464. if (IsMIROn(iobase)) { //mir
  465. // Mode select On
  466. SlowIRRXLowActive(iobase, OFF);
  467. udelay(20);
  468. } else { // fir
  469. if (IsFIROn(iobase)) { //fir
  470. // Mode select On
  471. SlowIRRXLowActive(iobase, OFF);
  472. udelay(20);
  473. }
  474. }
  475. }
  476. break;
  477. case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
  478. UseOneRX(iobase, ON); //use ONE RX....RX1
  479. InvertTX(iobase, OFF);
  480. InvertRX(iobase, OFF); // invert RX pin
  481. EnRX2(iobase, ON);
  482. EnGPIOtoRX2(iobase, OFF);
  483. if (IsSIROn(iobase)) { //sir
  484. // Mode select On
  485. SlowIRRXLowActive(iobase, ON);
  486. udelay(20);
  487. // Mode select Off
  488. SlowIRRXLowActive(iobase, OFF);
  489. }
  490. if (IsMIROn(iobase)) { //mir
  491. // Mode select On
  492. SlowIRRXLowActive(iobase, OFF);
  493. udelay(20);
  494. // Mode select Off
  495. SlowIRRXLowActive(iobase, ON);
  496. } else { // fir
  497. if (IsFIROn(iobase)) { //fir
  498. // Mode select On
  499. SlowIRRXLowActive(iobase, OFF);
  500. // TX On
  501. WriteTX(iobase, ON);
  502. udelay(20);
  503. // Mode select OFF
  504. SlowIRRXLowActive(iobase, ON);
  505. udelay(20);
  506. // TX Off
  507. WriteTX(iobase, OFF);
  508. }
  509. }
  510. break;
  511. case 0x0d:
  512. UseOneRX(iobase, OFF); // use two RX pin RX1,RX2
  513. InvertTX(iobase, OFF);
  514. InvertRX(iobase, OFF);
  515. SlowIRRXLowActive(iobase, OFF);
  516. if (IsSIROn(iobase)) { //sir
  517. EnGPIOtoRX2(iobase, OFF);
  518. WriteGIO(iobase, OFF);
  519. EnRX2(iobase, OFF); //sir to rx2
  520. } else { // fir mir
  521. EnGPIOtoRX2(iobase, OFF);
  522. WriteGIO(iobase, OFF);
  523. EnRX2(iobase, OFF); //fir to rx
  524. }
  525. break;
  526. case 0x11: /* Temic TFDS4500 */
  527. pr_debug("%s: Temic TFDS4500: One RX pin, TX normal, RX inverted\n",
  528. __func__);
  529. UseOneRX(iobase, ON); //use ONE RX....RX1
  530. InvertTX(iobase, OFF);
  531. InvertRX(iobase, ON); // invert RX pin
  532. EnRX2(iobase, ON); //sir to rx2
  533. EnGPIOtoRX2(iobase, OFF);
  534. if( IsSIROn(iobase) ){ //sir
  535. // Mode select On
  536. SlowIRRXLowActive(iobase, ON);
  537. udelay(20);
  538. // Mode select Off
  539. SlowIRRXLowActive(iobase, OFF);
  540. } else{
  541. pr_debug("%s: Warning: TFDS4500 not running in SIR mode !\n",
  542. __func__);
  543. }
  544. break;
  545. case 0x0ff: /* Vishay */
  546. if (IsSIROn(iobase))
  547. mode = 0;
  548. else if (IsMIROn(iobase))
  549. mode = 1;
  550. else if (IsFIROn(iobase))
  551. mode = 2;
  552. else if (IsVFIROn(iobase))
  553. mode = 5; //VFIR-16
  554. SI_SetMode(iobase, mode);
  555. break;
  556. default:
  557. net_err_ratelimited("%s: Error: dongle_id %d unsupported !\n",
  558. __func__, dongle_id);
  559. }
  560. }
  561. /*
  562. * Function via_ircc_change_speed (self, baud)
  563. *
  564. * Change the speed of the device
  565. *
  566. */
  567. static void via_ircc_change_speed(struct via_ircc_cb *self, __u32 speed)
  568. {
  569. struct net_device *dev = self->netdev;
  570. u16 iobase;
  571. u8 value = 0, bTmp;
  572. iobase = self->io.fir_base;
  573. /* Update accounting for new speed */
  574. self->io.speed = speed;
  575. pr_debug("%s: change_speed to %d bps.\n", __func__, speed);
  576. WriteReg(iobase, I_ST_CT_0, 0x0);
  577. /* Controller mode sellection */
  578. switch (speed) {
  579. case 2400:
  580. case 9600:
  581. case 19200:
  582. case 38400:
  583. case 57600:
  584. case 115200:
  585. value = (115200/speed)-1;
  586. SetSIR(iobase, ON);
  587. CRC16(iobase, ON);
  588. break;
  589. case 576000:
  590. /* FIXME: this can't be right, as it's the same as 115200,
  591. * and 576000 is MIR, not SIR. */
  592. value = 0;
  593. SetSIR(iobase, ON);
  594. CRC16(iobase, ON);
  595. break;
  596. case 1152000:
  597. value = 0;
  598. SetMIR(iobase, ON);
  599. /* FIXME: CRC ??? */
  600. break;
  601. case 4000000:
  602. value = 0;
  603. SetFIR(iobase, ON);
  604. SetPulseWidth(iobase, 0);
  605. SetSendPreambleCount(iobase, 14);
  606. CRC16(iobase, OFF);
  607. EnTXCRC(iobase, ON);
  608. break;
  609. case 16000000:
  610. value = 0;
  611. SetVFIR(iobase, ON);
  612. /* FIXME: CRC ??? */
  613. break;
  614. default:
  615. value = 0;
  616. break;
  617. }
  618. /* Set baudrate to 0x19[2..7] */
  619. bTmp = (ReadReg(iobase, I_CF_H_1) & 0x03);
  620. bTmp |= value << 2;
  621. WriteReg(iobase, I_CF_H_1, bTmp);
  622. /* Some dongles may need to be informed about speed changes. */
  623. via_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
  624. /* Set FIFO size to 64 */
  625. SetFIFO(iobase, 64);
  626. /* Enable IR */
  627. WriteReg(iobase, I_ST_CT_0, 0x80);
  628. // EnTXFIFOHalfLevelInt(iobase,ON);
  629. /* Enable some interrupts so we can receive frames */
  630. //EnAllInt(iobase,ON);
  631. if (IsSIROn(iobase)) {
  632. SIRFilter(iobase, ON);
  633. SIRRecvAny(iobase, ON);
  634. } else {
  635. SIRFilter(iobase, OFF);
  636. SIRRecvAny(iobase, OFF);
  637. }
  638. if (speed > 115200) {
  639. /* Install FIR xmit handler */
  640. dev->netdev_ops = &via_ircc_fir_ops;
  641. via_ircc_dma_receive(self);
  642. } else {
  643. /* Install SIR xmit handler */
  644. dev->netdev_ops = &via_ircc_sir_ops;
  645. }
  646. netif_wake_queue(dev);
  647. }
  648. /*
  649. * Function via_ircc_hard_xmit (skb, dev)
  650. *
  651. * Transmit the frame!
  652. *
  653. */
  654. static netdev_tx_t via_ircc_hard_xmit_sir(struct sk_buff *skb,
  655. struct net_device *dev)
  656. {
  657. struct via_ircc_cb *self;
  658. unsigned long flags;
  659. u16 iobase;
  660. __u32 speed;
  661. self = netdev_priv(dev);
  662. IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
  663. iobase = self->io.fir_base;
  664. netif_stop_queue(dev);
  665. /* Check if we need to change the speed */
  666. speed = irda_get_next_speed(skb);
  667. if ((speed != self->io.speed) && (speed != -1)) {
  668. /* Check for empty frame */
  669. if (!skb->len) {
  670. via_ircc_change_speed(self, speed);
  671. dev->trans_start = jiffies;
  672. dev_kfree_skb(skb);
  673. return NETDEV_TX_OK;
  674. } else
  675. self->new_speed = speed;
  676. }
  677. InitCard(iobase);
  678. CommonInit(iobase);
  679. SIRFilter(iobase, ON);
  680. SetSIR(iobase, ON);
  681. CRC16(iobase, ON);
  682. EnTXCRC(iobase, 0);
  683. WriteReg(iobase, I_ST_CT_0, 0x00);
  684. spin_lock_irqsave(&self->lock, flags);
  685. self->tx_buff.data = self->tx_buff.head;
  686. self->tx_buff.len =
  687. async_wrap_skb(skb, self->tx_buff.data,
  688. self->tx_buff.truesize);
  689. dev->stats.tx_bytes += self->tx_buff.len;
  690. /* Send this frame with old speed */
  691. SetBaudRate(iobase, self->io.speed);
  692. SetPulseWidth(iobase, 12);
  693. SetSendPreambleCount(iobase, 0);
  694. WriteReg(iobase, I_ST_CT_0, 0x80);
  695. EnableTX(iobase, ON);
  696. EnableRX(iobase, OFF);
  697. ResetChip(iobase, 0);
  698. ResetChip(iobase, 1);
  699. ResetChip(iobase, 2);
  700. ResetChip(iobase, 3);
  701. ResetChip(iobase, 4);
  702. EnAllInt(iobase, ON);
  703. EnTXDMA(iobase, ON);
  704. EnRXDMA(iobase, OFF);
  705. irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
  706. DMA_TX_MODE);
  707. SetSendByte(iobase, self->tx_buff.len);
  708. RXStart(iobase, OFF);
  709. TXStart(iobase, ON);
  710. dev->trans_start = jiffies;
  711. spin_unlock_irqrestore(&self->lock, flags);
  712. dev_kfree_skb(skb);
  713. return NETDEV_TX_OK;
  714. }
  715. static netdev_tx_t via_ircc_hard_xmit_fir(struct sk_buff *skb,
  716. struct net_device *dev)
  717. {
  718. struct via_ircc_cb *self;
  719. u16 iobase;
  720. __u32 speed;
  721. unsigned long flags;
  722. self = netdev_priv(dev);
  723. iobase = self->io.fir_base;
  724. if (self->st_fifo.len)
  725. return NETDEV_TX_OK;
  726. if (self->chip_id == 0x3076)
  727. iodelay(1500);
  728. else
  729. udelay(1500);
  730. netif_stop_queue(dev);
  731. speed = irda_get_next_speed(skb);
  732. if ((speed != self->io.speed) && (speed != -1)) {
  733. if (!skb->len) {
  734. via_ircc_change_speed(self, speed);
  735. dev->trans_start = jiffies;
  736. dev_kfree_skb(skb);
  737. return NETDEV_TX_OK;
  738. } else
  739. self->new_speed = speed;
  740. }
  741. spin_lock_irqsave(&self->lock, flags);
  742. self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
  743. self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
  744. self->tx_fifo.tail += skb->len;
  745. dev->stats.tx_bytes += skb->len;
  746. skb_copy_from_linear_data(skb,
  747. self->tx_fifo.queue[self->tx_fifo.free].start, skb->len);
  748. self->tx_fifo.len++;
  749. self->tx_fifo.free++;
  750. //F01 if (self->tx_fifo.len == 1) {
  751. via_ircc_dma_xmit(self, iobase);
  752. //F01 }
  753. //F01 if (self->tx_fifo.free < (MAX_TX_WINDOW -1 )) netif_wake_queue(self->netdev);
  754. dev->trans_start = jiffies;
  755. dev_kfree_skb(skb);
  756. spin_unlock_irqrestore(&self->lock, flags);
  757. return NETDEV_TX_OK;
  758. }
  759. static int via_ircc_dma_xmit(struct via_ircc_cb *self, u16 iobase)
  760. {
  761. EnTXDMA(iobase, OFF);
  762. self->io.direction = IO_XMIT;
  763. EnPhys(iobase, ON);
  764. EnableTX(iobase, ON);
  765. EnableRX(iobase, OFF);
  766. ResetChip(iobase, 0);
  767. ResetChip(iobase, 1);
  768. ResetChip(iobase, 2);
  769. ResetChip(iobase, 3);
  770. ResetChip(iobase, 4);
  771. EnAllInt(iobase, ON);
  772. EnTXDMA(iobase, ON);
  773. EnRXDMA(iobase, OFF);
  774. irda_setup_dma(self->io.dma,
  775. ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
  776. self->tx_buff.head) + self->tx_buff_dma,
  777. self->tx_fifo.queue[self->tx_fifo.ptr].len, DMA_TX_MODE);
  778. pr_debug("%s: tx_fifo.ptr=%x,len=%x,tx_fifo.len=%x..\n",
  779. __func__, self->tx_fifo.ptr,
  780. self->tx_fifo.queue[self->tx_fifo.ptr].len,
  781. self->tx_fifo.len);
  782. SetSendByte(iobase, self->tx_fifo.queue[self->tx_fifo.ptr].len);
  783. RXStart(iobase, OFF);
  784. TXStart(iobase, ON);
  785. return 0;
  786. }
  787. /*
  788. * Function via_ircc_dma_xmit_complete (self)
  789. *
  790. * The transfer of a frame in finished. This function will only be called
  791. * by the interrupt handler
  792. *
  793. */
  794. static int via_ircc_dma_xmit_complete(struct via_ircc_cb *self)
  795. {
  796. int iobase;
  797. u8 Tx_status;
  798. iobase = self->io.fir_base;
  799. /* Disable DMA */
  800. // DisableDmaChannel(self->io.dma);
  801. /* Check for underrun! */
  802. /* Clear bit, by writing 1 into it */
  803. Tx_status = GetTXStatus(iobase);
  804. if (Tx_status & 0x08) {
  805. self->netdev->stats.tx_errors++;
  806. self->netdev->stats.tx_fifo_errors++;
  807. hwreset(self);
  808. /* how to clear underrun? */
  809. } else {
  810. self->netdev->stats.tx_packets++;
  811. ResetChip(iobase, 3);
  812. ResetChip(iobase, 4);
  813. }
  814. /* Check if we need to change the speed */
  815. if (self->new_speed) {
  816. via_ircc_change_speed(self, self->new_speed);
  817. self->new_speed = 0;
  818. }
  819. /* Finished with this frame, so prepare for next */
  820. if (IsFIROn(iobase)) {
  821. if (self->tx_fifo.len) {
  822. self->tx_fifo.len--;
  823. self->tx_fifo.ptr++;
  824. }
  825. }
  826. pr_debug("%s: tx_fifo.len=%x ,tx_fifo.ptr=%x,tx_fifo.free=%x...\n",
  827. __func__,
  828. self->tx_fifo.len, self->tx_fifo.ptr, self->tx_fifo.free);
  829. /* F01_S
  830. // Any frames to be sent back-to-back?
  831. if (self->tx_fifo.len) {
  832. // Not finished yet!
  833. via_ircc_dma_xmit(self, iobase);
  834. ret = FALSE;
  835. } else {
  836. F01_E*/
  837. // Reset Tx FIFO info
  838. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  839. self->tx_fifo.tail = self->tx_buff.head;
  840. //F01 }
  841. // Make sure we have room for more frames
  842. //F01 if (self->tx_fifo.free < (MAX_TX_WINDOW -1 )) {
  843. // Not busy transmitting anymore
  844. // Tell the network layer, that we can accept more frames
  845. netif_wake_queue(self->netdev);
  846. //F01 }
  847. return TRUE;
  848. }
  849. /*
  850. * Function via_ircc_dma_receive (self)
  851. *
  852. * Set configuration for receive a frame.
  853. *
  854. */
  855. static int via_ircc_dma_receive(struct via_ircc_cb *self)
  856. {
  857. int iobase;
  858. iobase = self->io.fir_base;
  859. self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
  860. self->tx_fifo.tail = self->tx_buff.head;
  861. self->RxDataReady = 0;
  862. self->io.direction = IO_RECV;
  863. self->rx_buff.data = self->rx_buff.head;
  864. self->st_fifo.len = self->st_fifo.pending_bytes = 0;
  865. self->st_fifo.tail = self->st_fifo.head = 0;
  866. EnPhys(iobase, ON);
  867. EnableTX(iobase, OFF);
  868. EnableRX(iobase, ON);
  869. ResetChip(iobase, 0);
  870. ResetChip(iobase, 1);
  871. ResetChip(iobase, 2);
  872. ResetChip(iobase, 3);
  873. ResetChip(iobase, 4);
  874. EnAllInt(iobase, ON);
  875. EnTXDMA(iobase, OFF);
  876. EnRXDMA(iobase, ON);
  877. irda_setup_dma(self->io.dma2, self->rx_buff_dma,
  878. self->rx_buff.truesize, DMA_RX_MODE);
  879. TXStart(iobase, OFF);
  880. RXStart(iobase, ON);
  881. return 0;
  882. }
  883. /*
  884. * Function via_ircc_dma_receive_complete (self)
  885. *
  886. * Controller Finished with receiving frames,
  887. * and this routine is call by ISR
  888. *
  889. */
  890. static int via_ircc_dma_receive_complete(struct via_ircc_cb *self,
  891. int iobase)
  892. {
  893. struct st_fifo *st_fifo;
  894. struct sk_buff *skb;
  895. int len, i;
  896. u8 status = 0;
  897. iobase = self->io.fir_base;
  898. st_fifo = &self->st_fifo;
  899. if (self->io.speed < 4000000) { //Speed below FIR
  900. len = GetRecvByte(iobase, self);
  901. skb = dev_alloc_skb(len + 1);
  902. if (skb == NULL)
  903. return FALSE;
  904. // Make sure IP header gets aligned
  905. skb_reserve(skb, 1);
  906. skb_put(skb, len - 2);
  907. if (self->chip_id == 0x3076) {
  908. for (i = 0; i < len - 2; i++)
  909. skb->data[i] = self->rx_buff.data[i * 2];
  910. } else {
  911. if (self->chip_id == 0x3096) {
  912. for (i = 0; i < len - 2; i++)
  913. skb->data[i] =
  914. self->rx_buff.data[i];
  915. }
  916. }
  917. // Move to next frame
  918. self->rx_buff.data += len;
  919. self->netdev->stats.rx_bytes += len;
  920. self->netdev->stats.rx_packets++;
  921. skb->dev = self->netdev;
  922. skb_reset_mac_header(skb);
  923. skb->protocol = htons(ETH_P_IRDA);
  924. netif_rx(skb);
  925. return TRUE;
  926. }
  927. else { //FIR mode
  928. len = GetRecvByte(iobase, self);
  929. if (len == 0)
  930. return TRUE; //interrupt only, data maybe move by RxT
  931. if (((len - 4) < 2) || ((len - 4) > 2048)) {
  932. pr_debug("%s(): Trouble:len=%x,CurCount=%x,LastCount=%x\n",
  933. __func__, len, RxCurCount(iobase, self),
  934. self->RxLastCount);
  935. hwreset(self);
  936. return FALSE;
  937. }
  938. pr_debug("%s(): fifo.len=%x,len=%x,CurCount=%x..\n",
  939. __func__,
  940. st_fifo->len, len - 4, RxCurCount(iobase, self));
  941. st_fifo->entries[st_fifo->tail].status = status;
  942. st_fifo->entries[st_fifo->tail].len = len;
  943. st_fifo->pending_bytes += len;
  944. st_fifo->tail++;
  945. st_fifo->len++;
  946. if (st_fifo->tail > MAX_RX_WINDOW)
  947. st_fifo->tail = 0;
  948. self->RxDataReady = 0;
  949. // It maybe have MAX_RX_WINDOW package receive by
  950. // receive_complete before Timer IRQ
  951. /* F01_S
  952. if (st_fifo->len < (MAX_RX_WINDOW+2 )) {
  953. RXStart(iobase,ON);
  954. SetTimer(iobase,4);
  955. }
  956. else {
  957. F01_E */
  958. EnableRX(iobase, OFF);
  959. EnRXDMA(iobase, OFF);
  960. RXStart(iobase, OFF);
  961. //F01_S
  962. // Put this entry back in fifo
  963. if (st_fifo->head > MAX_RX_WINDOW)
  964. st_fifo->head = 0;
  965. status = st_fifo->entries[st_fifo->head].status;
  966. len = st_fifo->entries[st_fifo->head].len;
  967. st_fifo->head++;
  968. st_fifo->len--;
  969. skb = dev_alloc_skb(len + 1 - 4);
  970. /*
  971. * if frame size, data ptr, or skb ptr are wrong, then get next
  972. * entry.
  973. */
  974. if ((skb == NULL) || (skb->data == NULL) ||
  975. (self->rx_buff.data == NULL) || (len < 6)) {
  976. self->netdev->stats.rx_dropped++;
  977. kfree_skb(skb);
  978. return TRUE;
  979. }
  980. skb_reserve(skb, 1);
  981. skb_put(skb, len - 4);
  982. skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4);
  983. pr_debug("%s(): len=%x.rx_buff=%p\n", __func__,
  984. len - 4, self->rx_buff.data);
  985. // Move to next frame
  986. self->rx_buff.data += len;
  987. self->netdev->stats.rx_bytes += len;
  988. self->netdev->stats.rx_packets++;
  989. skb->dev = self->netdev;
  990. skb_reset_mac_header(skb);
  991. skb->protocol = htons(ETH_P_IRDA);
  992. netif_rx(skb);
  993. //F01_E
  994. } //FIR
  995. return TRUE;
  996. }
  997. /*
  998. * if frame is received , but no INT ,then use this routine to upload frame.
  999. */
  1000. static int upload_rxdata(struct via_ircc_cb *self, int iobase)
  1001. {
  1002. struct sk_buff *skb;
  1003. int len;
  1004. struct st_fifo *st_fifo;
  1005. st_fifo = &self->st_fifo;
  1006. len = GetRecvByte(iobase, self);
  1007. pr_debug("%s(): len=%x\n", __func__, len);
  1008. if ((len - 4) < 2) {
  1009. self->netdev->stats.rx_dropped++;
  1010. return FALSE;
  1011. }
  1012. skb = dev_alloc_skb(len + 1);
  1013. if (skb == NULL) {
  1014. self->netdev->stats.rx_dropped++;
  1015. return FALSE;
  1016. }
  1017. skb_reserve(skb, 1);
  1018. skb_put(skb, len - 4 + 1);
  1019. skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4 + 1);
  1020. st_fifo->tail++;
  1021. st_fifo->len++;
  1022. if (st_fifo->tail > MAX_RX_WINDOW)
  1023. st_fifo->tail = 0;
  1024. // Move to next frame
  1025. self->rx_buff.data += len;
  1026. self->netdev->stats.rx_bytes += len;
  1027. self->netdev->stats.rx_packets++;
  1028. skb->dev = self->netdev;
  1029. skb_reset_mac_header(skb);
  1030. skb->protocol = htons(ETH_P_IRDA);
  1031. netif_rx(skb);
  1032. if (st_fifo->len < (MAX_RX_WINDOW + 2)) {
  1033. RXStart(iobase, ON);
  1034. } else {
  1035. EnableRX(iobase, OFF);
  1036. EnRXDMA(iobase, OFF);
  1037. RXStart(iobase, OFF);
  1038. }
  1039. return TRUE;
  1040. }
  1041. /*
  1042. * Implement back to back receive , use this routine to upload data.
  1043. */
  1044. static int RxTimerHandler(struct via_ircc_cb *self, int iobase)
  1045. {
  1046. struct st_fifo *st_fifo;
  1047. struct sk_buff *skb;
  1048. int len;
  1049. u8 status;
  1050. st_fifo = &self->st_fifo;
  1051. if (CkRxRecv(iobase, self)) {
  1052. // if still receiving ,then return ,don't upload frame
  1053. self->RetryCount = 0;
  1054. SetTimer(iobase, 20);
  1055. self->RxDataReady++;
  1056. return FALSE;
  1057. } else
  1058. self->RetryCount++;
  1059. if ((self->RetryCount >= 1) ||
  1060. ((st_fifo->pending_bytes + 2048) > self->rx_buff.truesize) ||
  1061. (st_fifo->len >= (MAX_RX_WINDOW))) {
  1062. while (st_fifo->len > 0) { //upload frame
  1063. // Put this entry back in fifo
  1064. if (st_fifo->head > MAX_RX_WINDOW)
  1065. st_fifo->head = 0;
  1066. status = st_fifo->entries[st_fifo->head].status;
  1067. len = st_fifo->entries[st_fifo->head].len;
  1068. st_fifo->head++;
  1069. st_fifo->len--;
  1070. skb = dev_alloc_skb(len + 1 - 4);
  1071. /*
  1072. * if frame size, data ptr, or skb ptr are wrong,
  1073. * then get next entry.
  1074. */
  1075. if ((skb == NULL) || (skb->data == NULL) ||
  1076. (self->rx_buff.data == NULL) || (len < 6)) {
  1077. self->netdev->stats.rx_dropped++;
  1078. continue;
  1079. }
  1080. skb_reserve(skb, 1);
  1081. skb_put(skb, len - 4);
  1082. skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4);
  1083. pr_debug("%s(): len=%x.head=%x\n", __func__,
  1084. len - 4, st_fifo->head);
  1085. // Move to next frame
  1086. self->rx_buff.data += len;
  1087. self->netdev->stats.rx_bytes += len;
  1088. self->netdev->stats.rx_packets++;
  1089. skb->dev = self->netdev;
  1090. skb_reset_mac_header(skb);
  1091. skb->protocol = htons(ETH_P_IRDA);
  1092. netif_rx(skb);
  1093. } //while
  1094. self->RetryCount = 0;
  1095. pr_debug("%s(): End of upload HostStatus=%x,RxStatus=%x\n",
  1096. __func__, GetHostStatus(iobase), GetRXStatus(iobase));
  1097. /*
  1098. * if frame is receive complete at this routine ,then upload
  1099. * frame.
  1100. */
  1101. if ((GetRXStatus(iobase) & 0x10) &&
  1102. (RxCurCount(iobase, self) != self->RxLastCount)) {
  1103. upload_rxdata(self, iobase);
  1104. if (irda_device_txqueue_empty(self->netdev))
  1105. via_ircc_dma_receive(self);
  1106. }
  1107. } // timer detect complete
  1108. else
  1109. SetTimer(iobase, 4);
  1110. return TRUE;
  1111. }
  1112. /*
  1113. * Function via_ircc_interrupt (irq, dev_id)
  1114. *
  1115. * An interrupt from the chip has arrived. Time to do some work
  1116. *
  1117. */
  1118. static irqreturn_t via_ircc_interrupt(int dummy, void *dev_id)
  1119. {
  1120. struct net_device *dev = dev_id;
  1121. struct via_ircc_cb *self = netdev_priv(dev);
  1122. int iobase;
  1123. u8 iHostIntType, iRxIntType, iTxIntType;
  1124. iobase = self->io.fir_base;
  1125. spin_lock(&self->lock);
  1126. iHostIntType = GetHostStatus(iobase);
  1127. pr_debug("%s(): iHostIntType %02x: %s %s %s %02x\n",
  1128. __func__, iHostIntType,
  1129. (iHostIntType & 0x40) ? "Timer" : "",
  1130. (iHostIntType & 0x20) ? "Tx" : "",
  1131. (iHostIntType & 0x10) ? "Rx" : "",
  1132. (iHostIntType & 0x0e) >> 1);
  1133. if ((iHostIntType & 0x40) != 0) { //Timer Event
  1134. self->EventFlag.TimeOut++;
  1135. ClearTimerInt(iobase, 1);
  1136. if (self->io.direction == IO_XMIT) {
  1137. via_ircc_dma_xmit(self, iobase);
  1138. }
  1139. if (self->io.direction == IO_RECV) {
  1140. /*
  1141. * frame ready hold too long, must reset.
  1142. */
  1143. if (self->RxDataReady > 30) {
  1144. hwreset(self);
  1145. if (irda_device_txqueue_empty(self->netdev)) {
  1146. via_ircc_dma_receive(self);
  1147. }
  1148. } else { // call this to upload frame.
  1149. RxTimerHandler(self, iobase);
  1150. }
  1151. } //RECV
  1152. } //Timer Event
  1153. if ((iHostIntType & 0x20) != 0) { //Tx Event
  1154. iTxIntType = GetTXStatus(iobase);
  1155. pr_debug("%s(): iTxIntType %02x: %s %s %s %s\n",
  1156. __func__, iTxIntType,
  1157. (iTxIntType & 0x08) ? "FIFO underr." : "",
  1158. (iTxIntType & 0x04) ? "EOM" : "",
  1159. (iTxIntType & 0x02) ? "FIFO ready" : "",
  1160. (iTxIntType & 0x01) ? "Early EOM" : "");
  1161. if (iTxIntType & 0x4) {
  1162. self->EventFlag.EOMessage++; // read and will auto clean
  1163. if (via_ircc_dma_xmit_complete(self)) {
  1164. if (irda_device_txqueue_empty
  1165. (self->netdev)) {
  1166. via_ircc_dma_receive(self);
  1167. }
  1168. } else {
  1169. self->EventFlag.Unknown++;
  1170. }
  1171. } //EOP
  1172. } //Tx Event
  1173. //----------------------------------------
  1174. if ((iHostIntType & 0x10) != 0) { //Rx Event
  1175. /* Check if DMA has finished */
  1176. iRxIntType = GetRXStatus(iobase);
  1177. pr_debug("%s(): iRxIntType %02x: %s %s %s %s %s %s %s\n",
  1178. __func__, iRxIntType,
  1179. (iRxIntType & 0x80) ? "PHY err." : "",
  1180. (iRxIntType & 0x40) ? "CRC err" : "",
  1181. (iRxIntType & 0x20) ? "FIFO overr." : "",
  1182. (iRxIntType & 0x10) ? "EOF" : "",
  1183. (iRxIntType & 0x08) ? "RxData" : "",
  1184. (iRxIntType & 0x02) ? "RxMaxLen" : "",
  1185. (iRxIntType & 0x01) ? "SIR bad" : "");
  1186. if (!iRxIntType)
  1187. pr_debug("%s(): RxIRQ =0\n", __func__);
  1188. if (iRxIntType & 0x10) {
  1189. if (via_ircc_dma_receive_complete(self, iobase)) {
  1190. //F01 if(!(IsFIROn(iobase))) via_ircc_dma_receive(self);
  1191. via_ircc_dma_receive(self);
  1192. }
  1193. } // No ERR
  1194. else { //ERR
  1195. pr_debug("%s(): RxIRQ ERR:iRxIntType=%x,HostIntType=%x,CurCount=%x,RxLastCount=%x_____\n",
  1196. __func__, iRxIntType, iHostIntType,
  1197. RxCurCount(iobase, self), self->RxLastCount);
  1198. if (iRxIntType & 0x20) { //FIFO OverRun ERR
  1199. ResetChip(iobase, 0);
  1200. ResetChip(iobase, 1);
  1201. } else { //PHY,CRC ERR
  1202. if (iRxIntType != 0x08)
  1203. hwreset(self); //F01
  1204. }
  1205. via_ircc_dma_receive(self);
  1206. } //ERR
  1207. } //Rx Event
  1208. spin_unlock(&self->lock);
  1209. return IRQ_RETVAL(iHostIntType);
  1210. }
  1211. static void hwreset(struct via_ircc_cb *self)
  1212. {
  1213. int iobase;
  1214. iobase = self->io.fir_base;
  1215. ResetChip(iobase, 5);
  1216. EnableDMA(iobase, OFF);
  1217. EnableTX(iobase, OFF);
  1218. EnableRX(iobase, OFF);
  1219. EnRXDMA(iobase, OFF);
  1220. EnTXDMA(iobase, OFF);
  1221. RXStart(iobase, OFF);
  1222. TXStart(iobase, OFF);
  1223. InitCard(iobase);
  1224. CommonInit(iobase);
  1225. SIRFilter(iobase, ON);
  1226. SetSIR(iobase, ON);
  1227. CRC16(iobase, ON);
  1228. EnTXCRC(iobase, 0);
  1229. WriteReg(iobase, I_ST_CT_0, 0x00);
  1230. SetBaudRate(iobase, 9600);
  1231. SetPulseWidth(iobase, 12);
  1232. SetSendPreambleCount(iobase, 0);
  1233. WriteReg(iobase, I_ST_CT_0, 0x80);
  1234. /* Restore speed. */
  1235. via_ircc_change_speed(self, self->io.speed);
  1236. self->st_fifo.len = 0;
  1237. }
  1238. /*
  1239. * Function via_ircc_is_receiving (self)
  1240. *
  1241. * Return TRUE is we are currently receiving a frame
  1242. *
  1243. */
  1244. static int via_ircc_is_receiving(struct via_ircc_cb *self)
  1245. {
  1246. int status = FALSE;
  1247. int iobase;
  1248. IRDA_ASSERT(self != NULL, return FALSE;);
  1249. iobase = self->io.fir_base;
  1250. if (CkRxRecv(iobase, self))
  1251. status = TRUE;
  1252. pr_debug("%s(): status=%x....\n", __func__, status);
  1253. return status;
  1254. }
  1255. /*
  1256. * Function via_ircc_net_open (dev)
  1257. *
  1258. * Start the device
  1259. *
  1260. */
  1261. static int via_ircc_net_open(struct net_device *dev)
  1262. {
  1263. struct via_ircc_cb *self;
  1264. int iobase;
  1265. char hwname[32];
  1266. IRDA_ASSERT(dev != NULL, return -1;);
  1267. self = netdev_priv(dev);
  1268. dev->stats.rx_packets = 0;
  1269. IRDA_ASSERT(self != NULL, return 0;);
  1270. iobase = self->io.fir_base;
  1271. if (request_irq(self->io.irq, via_ircc_interrupt, 0, dev->name, dev)) {
  1272. net_warn_ratelimited("%s, unable to allocate irq=%d\n",
  1273. driver_name, self->io.irq);
  1274. return -EAGAIN;
  1275. }
  1276. /*
  1277. * Always allocate the DMA channel after the IRQ, and clean up on
  1278. * failure.
  1279. */
  1280. if (request_dma(self->io.dma, dev->name)) {
  1281. net_warn_ratelimited("%s, unable to allocate dma=%d\n",
  1282. driver_name, self->io.dma);
  1283. free_irq(self->io.irq, dev);
  1284. return -EAGAIN;
  1285. }
  1286. if (self->io.dma2 != self->io.dma) {
  1287. if (request_dma(self->io.dma2, dev->name)) {
  1288. net_warn_ratelimited("%s, unable to allocate dma2=%d\n",
  1289. driver_name, self->io.dma2);
  1290. free_irq(self->io.irq, dev);
  1291. free_dma(self->io.dma);
  1292. return -EAGAIN;
  1293. }
  1294. }
  1295. /* turn on interrupts */
  1296. EnAllInt(iobase, ON);
  1297. EnInternalLoop(iobase, OFF);
  1298. EnExternalLoop(iobase, OFF);
  1299. /* */
  1300. via_ircc_dma_receive(self);
  1301. /* Ready to play! */
  1302. netif_start_queue(dev);
  1303. /*
  1304. * Open new IrLAP layer instance, now that everything should be
  1305. * initialized properly
  1306. */
  1307. sprintf(hwname, "VIA @ 0x%x", iobase);
  1308. self->irlap = irlap_open(dev, &self->qos, hwname);
  1309. self->RxLastCount = 0;
  1310. return 0;
  1311. }
  1312. /*
  1313. * Function via_ircc_net_close (dev)
  1314. *
  1315. * Stop the device
  1316. *
  1317. */
  1318. static int via_ircc_net_close(struct net_device *dev)
  1319. {
  1320. struct via_ircc_cb *self;
  1321. int iobase;
  1322. IRDA_ASSERT(dev != NULL, return -1;);
  1323. self = netdev_priv(dev);
  1324. IRDA_ASSERT(self != NULL, return 0;);
  1325. /* Stop device */
  1326. netif_stop_queue(dev);
  1327. /* Stop and remove instance of IrLAP */
  1328. if (self->irlap)
  1329. irlap_close(self->irlap);
  1330. self->irlap = NULL;
  1331. iobase = self->io.fir_base;
  1332. EnTXDMA(iobase, OFF);
  1333. EnRXDMA(iobase, OFF);
  1334. DisableDmaChannel(self->io.dma);
  1335. /* Disable interrupts */
  1336. EnAllInt(iobase, OFF);
  1337. free_irq(self->io.irq, dev);
  1338. free_dma(self->io.dma);
  1339. if (self->io.dma2 != self->io.dma)
  1340. free_dma(self->io.dma2);
  1341. return 0;
  1342. }
  1343. /*
  1344. * Function via_ircc_net_ioctl (dev, rq, cmd)
  1345. *
  1346. * Process IOCTL commands for this device
  1347. *
  1348. */
  1349. static int via_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq,
  1350. int cmd)
  1351. {
  1352. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1353. struct via_ircc_cb *self;
  1354. unsigned long flags;
  1355. int ret = 0;
  1356. IRDA_ASSERT(dev != NULL, return -1;);
  1357. self = netdev_priv(dev);
  1358. IRDA_ASSERT(self != NULL, return -1;);
  1359. pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name,
  1360. cmd);
  1361. /* Disable interrupts & save flags */
  1362. spin_lock_irqsave(&self->lock, flags);
  1363. switch (cmd) {
  1364. case SIOCSBANDWIDTH: /* Set bandwidth */
  1365. if (!capable(CAP_NET_ADMIN)) {
  1366. ret = -EPERM;
  1367. goto out;
  1368. }
  1369. via_ircc_change_speed(self, irq->ifr_baudrate);
  1370. break;
  1371. case SIOCSMEDIABUSY: /* Set media busy */
  1372. if (!capable(CAP_NET_ADMIN)) {
  1373. ret = -EPERM;
  1374. goto out;
  1375. }
  1376. irda_device_set_media_busy(self->netdev, TRUE);
  1377. break;
  1378. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1379. irq->ifr_receiving = via_ircc_is_receiving(self);
  1380. break;
  1381. default:
  1382. ret = -EOPNOTSUPP;
  1383. }
  1384. out:
  1385. spin_unlock_irqrestore(&self->lock, flags);
  1386. return ret;
  1387. }
  1388. MODULE_AUTHOR("VIA Technologies,inc");
  1389. MODULE_DESCRIPTION("VIA IrDA Device Driver");
  1390. MODULE_LICENSE("GPL");
  1391. module_init(via_ircc_init);
  1392. module_exit(via_ircc_cleanup);