vlsi_ir.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /*********************************************************************
  2. *
  3. * vlsi_ir.c: VLSI82C147 PCI IrDA controller driver for Linux
  4. *
  5. * Copyright (c) 2001-2003 Martin Diehl
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. ********************************************************************/
  23. #include <linux/config.h>
  24. #include <linux/module.h>
  25. #define DRIVER_NAME "vlsi_ir"
  26. #define DRIVER_VERSION "v0.5"
  27. #define DRIVER_DESCRIPTION "IrDA SIR/MIR/FIR driver for VLSI 82C147"
  28. #define DRIVER_AUTHOR "Martin Diehl <info@mdiehl.de>"
  29. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  30. MODULE_AUTHOR(DRIVER_AUTHOR);
  31. MODULE_LICENSE("GPL");
  32. /********************************************************/
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/pci.h>
  36. #include <linux/slab.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/skbuff.h>
  39. #include <linux/delay.h>
  40. #include <linux/time.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/smp_lock.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/byteorder.h>
  46. #include <net/irda/irda.h>
  47. #include <net/irda/irda_device.h>
  48. #include <net/irda/wrapper.h>
  49. #include <net/irda/crc.h>
  50. #include "vlsi_ir.h"
  51. /********************************************************/
  52. static /* const */ char drivername[] = DRIVER_NAME;
  53. static struct pci_device_id vlsi_irda_table [] = {
  54. {
  55. .class = PCI_CLASS_WIRELESS_IRDA << 8,
  56. .class_mask = PCI_CLASS_SUBCLASS_MASK << 8,
  57. .vendor = PCI_VENDOR_ID_VLSI,
  58. .device = PCI_DEVICE_ID_VLSI_82C147,
  59. .subvendor = PCI_ANY_ID,
  60. .subdevice = PCI_ANY_ID,
  61. },
  62. { /* all zeroes */ }
  63. };
  64. MODULE_DEVICE_TABLE(pci, vlsi_irda_table);
  65. /********************************************************/
  66. /* clksrc: which clock source to be used
  67. * 0: auto - try PLL, fallback to 40MHz XCLK
  68. * 1: on-chip 48MHz PLL
  69. * 2: external 48MHz XCLK
  70. * 3: external 40MHz XCLK (HP OB-800)
  71. */
  72. static int clksrc = 0; /* default is 0(auto) */
  73. module_param(clksrc, int, 0);
  74. MODULE_PARM_DESC(clksrc, "clock input source selection");
  75. /* ringsize: size of the tx and rx descriptor rings
  76. * independent for tx and rx
  77. * specify as ringsize=tx[,rx]
  78. * allowed values: 4, 8, 16, 32, 64
  79. * Due to the IrDA 1.x max. allowed window size=7,
  80. * there should be no gain when using rings larger than 8
  81. */
  82. static int ringsize[] = {8,8}; /* default is tx=8 / rx=8 */
  83. module_param_array(ringsize, int, NULL, 0);
  84. MODULE_PARM_DESC(ringsize, "TX, RX ring descriptor size");
  85. /* sirpulse: tuning of the SIR pulse width within IrPHY 1.3 limits
  86. * 0: very short, 1.5us (exception: 6us at 2.4 kbaud)
  87. * 1: nominal 3/16 bittime width
  88. * note: IrDA compliant peer devices should be happy regardless
  89. * which one is used. Primary goal is to save some power
  90. * on the sender's side - at 9.6kbaud for example the short
  91. * pulse width saves more than 90% of the transmitted IR power.
  92. */
  93. static int sirpulse = 1; /* default is 3/16 bittime */
  94. module_param(sirpulse, int, 0);
  95. MODULE_PARM_DESC(sirpulse, "SIR pulse width tuning");
  96. /* qos_mtt_bits: encoded min-turn-time value we require the peer device
  97. * to use before transmitting to us. "Type 1" (per-station)
  98. * bitfield according to IrLAP definition (section 6.6.8)
  99. * Don't know which transceiver is used by my OB800 - the
  100. * pretty common HP HDLS-1100 requires 1 msec - so lets use this.
  101. */
  102. static int qos_mtt_bits = 0x07; /* default is 1 ms or more */
  103. module_param(qos_mtt_bits, int, 0);
  104. MODULE_PARM_DESC(qos_mtt_bits, "IrLAP bitfield representing min-turn-time");
  105. /********************************************************/
  106. static void vlsi_reg_debug(unsigned iobase, const char *s)
  107. {
  108. int i;
  109. printk(KERN_DEBUG "%s: ", s);
  110. for (i = 0; i < 0x20; i++)
  111. printk("%02x", (unsigned)inb((iobase+i)));
  112. printk("\n");
  113. }
  114. static void vlsi_ring_debug(struct vlsi_ring *r)
  115. {
  116. struct ring_descr *rd;
  117. unsigned i;
  118. printk(KERN_DEBUG "%s - ring %p / size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
  119. __FUNCTION__, r, r->size, r->mask, r->len, r->dir, r->rd[0].hw);
  120. printk(KERN_DEBUG "%s - head = %d / tail = %d\n", __FUNCTION__,
  121. atomic_read(&r->head) & r->mask, atomic_read(&r->tail) & r->mask);
  122. for (i = 0; i < r->size; i++) {
  123. rd = &r->rd[i];
  124. printk(KERN_DEBUG "%s - ring descr %u: ", __FUNCTION__, i);
  125. printk("skb=%p data=%p hw=%p\n", rd->skb, rd->buf, rd->hw);
  126. printk(KERN_DEBUG "%s - hw: status=%02x count=%u addr=0x%08x\n",
  127. __FUNCTION__, (unsigned) rd_get_status(rd),
  128. (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
  129. }
  130. }
  131. /********************************************************/
  132. /* needed regardless of CONFIG_PROC_FS */
  133. static struct proc_dir_entry *vlsi_proc_root = NULL;
  134. #ifdef CONFIG_PROC_FS
  135. static void vlsi_proc_pdev(struct seq_file *seq, struct pci_dev *pdev)
  136. {
  137. unsigned iobase = pci_resource_start(pdev, 0);
  138. unsigned i;
  139. seq_printf(seq, "\n%s (vid/did: %04x/%04x)\n",
  140. PCIDEV_NAME(pdev), (int)pdev->vendor, (int)pdev->device);
  141. seq_printf(seq, "pci-power-state: %u\n", (unsigned) pdev->current_state);
  142. seq_printf(seq, "resources: irq=%u / io=0x%04x / dma_mask=0x%016Lx\n",
  143. pdev->irq, (unsigned)pci_resource_start(pdev, 0), (unsigned long long)pdev->dma_mask);
  144. seq_printf(seq, "hw registers: ");
  145. for (i = 0; i < 0x20; i++)
  146. seq_printf(seq, "%02x", (unsigned)inb((iobase+i)));
  147. seq_printf(seq, "\n");
  148. }
  149. static void vlsi_proc_ndev(struct seq_file *seq, struct net_device *ndev)
  150. {
  151. vlsi_irda_dev_t *idev = ndev->priv;
  152. u8 byte;
  153. u16 word;
  154. unsigned delta1, delta2;
  155. struct timeval now;
  156. unsigned iobase = ndev->base_addr;
  157. seq_printf(seq, "\n%s link state: %s / %s / %s / %s\n", ndev->name,
  158. netif_device_present(ndev) ? "attached" : "detached",
  159. netif_running(ndev) ? "running" : "not running",
  160. netif_carrier_ok(ndev) ? "carrier ok" : "no carrier",
  161. netif_queue_stopped(ndev) ? "queue stopped" : "queue running");
  162. if (!netif_running(ndev))
  163. return;
  164. seq_printf(seq, "\nhw-state:\n");
  165. pci_read_config_byte(idev->pdev, VLSI_PCI_IRMISC, &byte);
  166. seq_printf(seq, "IRMISC:%s%s%s uart%s",
  167. (byte&IRMISC_IRRAIL) ? " irrail" : "",
  168. (byte&IRMISC_IRPD) ? " irpd" : "",
  169. (byte&IRMISC_UARTTST) ? " uarttest" : "",
  170. (byte&IRMISC_UARTEN) ? "@" : " disabled\n");
  171. if (byte&IRMISC_UARTEN) {
  172. seq_printf(seq, "0x%s\n",
  173. (byte&2) ? ((byte&1) ? "3e8" : "2e8")
  174. : ((byte&1) ? "3f8" : "2f8"));
  175. }
  176. pci_read_config_byte(idev->pdev, VLSI_PCI_CLKCTL, &byte);
  177. seq_printf(seq, "CLKCTL: PLL %s%s%s / clock %s / wakeup %s\n",
  178. (byte&CLKCTL_PD_INV) ? "powered" : "down",
  179. (byte&CLKCTL_LOCK) ? " locked" : "",
  180. (byte&CLKCTL_EXTCLK) ? ((byte&CLKCTL_XCKSEL)?" / 40 MHz XCLK":" / 48 MHz XCLK") : "",
  181. (byte&CLKCTL_CLKSTP) ? "stopped" : "running",
  182. (byte&CLKCTL_WAKE) ? "enabled" : "disabled");
  183. pci_read_config_byte(idev->pdev, VLSI_PCI_MSTRPAGE, &byte);
  184. seq_printf(seq, "MSTRPAGE: 0x%02x\n", (unsigned)byte);
  185. byte = inb(iobase+VLSI_PIO_IRINTR);
  186. seq_printf(seq, "IRINTR:%s%s%s%s%s%s%s%s\n",
  187. (byte&IRINTR_ACTEN) ? " ACTEN" : "",
  188. (byte&IRINTR_RPKTEN) ? " RPKTEN" : "",
  189. (byte&IRINTR_TPKTEN) ? " TPKTEN" : "",
  190. (byte&IRINTR_OE_EN) ? " OE_EN" : "",
  191. (byte&IRINTR_ACTIVITY) ? " ACTIVITY" : "",
  192. (byte&IRINTR_RPKTINT) ? " RPKTINT" : "",
  193. (byte&IRINTR_TPKTINT) ? " TPKTINT" : "",
  194. (byte&IRINTR_OE_INT) ? " OE_INT" : "");
  195. word = inw(iobase+VLSI_PIO_RINGPTR);
  196. seq_printf(seq, "RINGPTR: rx=%u / tx=%u\n", RINGPTR_GET_RX(word), RINGPTR_GET_TX(word));
  197. word = inw(iobase+VLSI_PIO_RINGBASE);
  198. seq_printf(seq, "RINGBASE: busmap=0x%08x\n",
  199. ((unsigned)word << 10)|(MSTRPAGE_VALUE<<24));
  200. word = inw(iobase+VLSI_PIO_RINGSIZE);
  201. seq_printf(seq, "RINGSIZE: rx=%u / tx=%u\n", RINGSIZE_TO_RXSIZE(word),
  202. RINGSIZE_TO_TXSIZE(word));
  203. word = inw(iobase+VLSI_PIO_IRCFG);
  204. seq_printf(seq, "IRCFG:%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
  205. (word&IRCFG_LOOP) ? " LOOP" : "",
  206. (word&IRCFG_ENTX) ? " ENTX" : "",
  207. (word&IRCFG_ENRX) ? " ENRX" : "",
  208. (word&IRCFG_MSTR) ? " MSTR" : "",
  209. (word&IRCFG_RXANY) ? " RXANY" : "",
  210. (word&IRCFG_CRC16) ? " CRC16" : "",
  211. (word&IRCFG_FIR) ? " FIR" : "",
  212. (word&IRCFG_MIR) ? " MIR" : "",
  213. (word&IRCFG_SIR) ? " SIR" : "",
  214. (word&IRCFG_SIRFILT) ? " SIRFILT" : "",
  215. (word&IRCFG_SIRTEST) ? " SIRTEST" : "",
  216. (word&IRCFG_TXPOL) ? " TXPOL" : "",
  217. (word&IRCFG_RXPOL) ? " RXPOL" : "");
  218. word = inw(iobase+VLSI_PIO_IRENABLE);
  219. seq_printf(seq, "IRENABLE:%s%s%s%s%s%s%s%s\n",
  220. (word&IRENABLE_PHYANDCLOCK) ? " PHYANDCLOCK" : "",
  221. (word&IRENABLE_CFGER) ? " CFGERR" : "",
  222. (word&IRENABLE_FIR_ON) ? " FIR_ON" : "",
  223. (word&IRENABLE_MIR_ON) ? " MIR_ON" : "",
  224. (word&IRENABLE_SIR_ON) ? " SIR_ON" : "",
  225. (word&IRENABLE_ENTXST) ? " ENTXST" : "",
  226. (word&IRENABLE_ENRXST) ? " ENRXST" : "",
  227. (word&IRENABLE_CRC16_ON) ? " CRC16_ON" : "");
  228. word = inw(iobase+VLSI_PIO_PHYCTL);
  229. seq_printf(seq, "PHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
  230. (unsigned)PHYCTL_TO_BAUD(word),
  231. (unsigned)PHYCTL_TO_PLSWID(word),
  232. (unsigned)PHYCTL_TO_PREAMB(word));
  233. word = inw(iobase+VLSI_PIO_NPHYCTL);
  234. seq_printf(seq, "NPHYCTL: baud-divisor=%u / pulsewidth=%u / preamble=%u\n",
  235. (unsigned)PHYCTL_TO_BAUD(word),
  236. (unsigned)PHYCTL_TO_PLSWID(word),
  237. (unsigned)PHYCTL_TO_PREAMB(word));
  238. word = inw(iobase+VLSI_PIO_MAXPKT);
  239. seq_printf(seq, "MAXPKT: max. rx packet size = %u\n", word);
  240. word = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  241. seq_printf(seq, "RCVBCNT: rx-fifo filling level = %u\n", word);
  242. seq_printf(seq, "\nsw-state:\n");
  243. seq_printf(seq, "IrPHY setup: %d baud - %s encoding\n", idev->baud,
  244. (idev->mode==IFF_SIR)?"SIR":((idev->mode==IFF_MIR)?"MIR":"FIR"));
  245. do_gettimeofday(&now);
  246. if (now.tv_usec >= idev->last_rx.tv_usec) {
  247. delta2 = now.tv_usec - idev->last_rx.tv_usec;
  248. delta1 = 0;
  249. }
  250. else {
  251. delta2 = 1000000 + now.tv_usec - idev->last_rx.tv_usec;
  252. delta1 = 1;
  253. }
  254. seq_printf(seq, "last rx: %lu.%06u sec\n",
  255. now.tv_sec - idev->last_rx.tv_sec - delta1, delta2);
  256. seq_printf(seq, "RX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu",
  257. idev->stats.rx_packets, idev->stats.rx_bytes, idev->stats.rx_errors,
  258. idev->stats.rx_dropped);
  259. seq_printf(seq, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
  260. idev->stats.rx_over_errors, idev->stats.rx_length_errors,
  261. idev->stats.rx_frame_errors, idev->stats.rx_crc_errors);
  262. seq_printf(seq, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
  263. idev->stats.tx_packets, idev->stats.tx_bytes, idev->stats.tx_errors,
  264. idev->stats.tx_dropped, idev->stats.tx_fifo_errors);
  265. }
  266. static void vlsi_proc_ring(struct seq_file *seq, struct vlsi_ring *r)
  267. {
  268. struct ring_descr *rd;
  269. unsigned i, j;
  270. int h, t;
  271. seq_printf(seq, "size %u / mask 0x%04x / len %u / dir %d / hw %p\n",
  272. r->size, r->mask, r->len, r->dir, r->rd[0].hw);
  273. h = atomic_read(&r->head) & r->mask;
  274. t = atomic_read(&r->tail) & r->mask;
  275. seq_printf(seq, "head = %d / tail = %d ", h, t);
  276. if (h == t)
  277. seq_printf(seq, "(empty)\n");
  278. else {
  279. if (((t+1)&r->mask) == h)
  280. seq_printf(seq, "(full)\n");
  281. else
  282. seq_printf(seq, "(level = %d)\n", ((unsigned)(t-h) & r->mask));
  283. rd = &r->rd[h];
  284. j = (unsigned) rd_get_count(rd);
  285. seq_printf(seq, "current: rd = %d / status = %02x / len = %u\n",
  286. h, (unsigned)rd_get_status(rd), j);
  287. if (j > 0) {
  288. seq_printf(seq, " data:");
  289. if (j > 20)
  290. j = 20;
  291. for (i = 0; i < j; i++)
  292. seq_printf(seq, " %02x", (unsigned)((unsigned char *)rd->buf)[i]);
  293. seq_printf(seq, "\n");
  294. }
  295. }
  296. for (i = 0; i < r->size; i++) {
  297. rd = &r->rd[i];
  298. seq_printf(seq, "> ring descr %u: ", i);
  299. seq_printf(seq, "skb=%p data=%p hw=%p\n", rd->skb, rd->buf, rd->hw);
  300. seq_printf(seq, " hw: status=%02x count=%u busaddr=0x%08x\n",
  301. (unsigned) rd_get_status(rd),
  302. (unsigned) rd_get_count(rd), (unsigned) rd_get_addr(rd));
  303. }
  304. }
  305. static int vlsi_seq_show(struct seq_file *seq, void *v)
  306. {
  307. struct net_device *ndev = seq->private;
  308. vlsi_irda_dev_t *idev = ndev->priv;
  309. unsigned long flags;
  310. seq_printf(seq, "\n%s %s\n\n", DRIVER_NAME, DRIVER_VERSION);
  311. seq_printf(seq, "clksrc: %s\n",
  312. (clksrc>=2) ? ((clksrc==3)?"40MHz XCLK":"48MHz XCLK")
  313. : ((clksrc==1)?"48MHz PLL":"autodetect"));
  314. seq_printf(seq, "ringsize: tx=%d / rx=%d\n",
  315. ringsize[0], ringsize[1]);
  316. seq_printf(seq, "sirpulse: %s\n", (sirpulse)?"3/16 bittime":"short");
  317. seq_printf(seq, "qos_mtt_bits: 0x%02x\n", (unsigned)qos_mtt_bits);
  318. spin_lock_irqsave(&idev->lock, flags);
  319. if (idev->pdev != NULL) {
  320. vlsi_proc_pdev(seq, idev->pdev);
  321. if (idev->pdev->current_state == 0)
  322. vlsi_proc_ndev(seq, ndev);
  323. else
  324. seq_printf(seq, "\nPCI controller down - resume_ok = %d\n",
  325. idev->resume_ok);
  326. if (netif_running(ndev) && idev->rx_ring && idev->tx_ring) {
  327. seq_printf(seq, "\n--------- RX ring -----------\n\n");
  328. vlsi_proc_ring(seq, idev->rx_ring);
  329. seq_printf(seq, "\n--------- TX ring -----------\n\n");
  330. vlsi_proc_ring(seq, idev->tx_ring);
  331. }
  332. }
  333. seq_printf(seq, "\n");
  334. spin_unlock_irqrestore(&idev->lock, flags);
  335. return 0;
  336. }
  337. static int vlsi_seq_open(struct inode *inode, struct file *file)
  338. {
  339. return single_open(file, vlsi_seq_show, PDE(inode)->data);
  340. }
  341. static struct file_operations vlsi_proc_fops = {
  342. .owner = THIS_MODULE,
  343. .open = vlsi_seq_open,
  344. .read = seq_read,
  345. .llseek = seq_lseek,
  346. .release = single_release,
  347. };
  348. #define VLSI_PROC_FOPS (&vlsi_proc_fops)
  349. #else /* CONFIG_PROC_FS */
  350. #define VLSI_PROC_FOPS NULL
  351. #endif
  352. /********************************************************/
  353. static struct vlsi_ring *vlsi_alloc_ring(struct pci_dev *pdev, struct ring_descr_hw *hwmap,
  354. unsigned size, unsigned len, int dir)
  355. {
  356. struct vlsi_ring *r;
  357. struct ring_descr *rd;
  358. unsigned i, j;
  359. dma_addr_t busaddr;
  360. if (!size || ((size-1)&size)!=0) /* must be >0 and power of 2 */
  361. return NULL;
  362. r = kmalloc(sizeof(*r) + size * sizeof(struct ring_descr), GFP_KERNEL);
  363. if (!r)
  364. return NULL;
  365. memset(r, 0, sizeof(*r));
  366. r->pdev = pdev;
  367. r->dir = dir;
  368. r->len = len;
  369. r->rd = (struct ring_descr *)(r+1);
  370. r->mask = size - 1;
  371. r->size = size;
  372. atomic_set(&r->head, 0);
  373. atomic_set(&r->tail, 0);
  374. for (i = 0; i < size; i++) {
  375. rd = r->rd + i;
  376. memset(rd, 0, sizeof(*rd));
  377. rd->hw = hwmap + i;
  378. rd->buf = kmalloc(len, GFP_KERNEL|GFP_DMA);
  379. if (rd->buf == NULL
  380. || !(busaddr = pci_map_single(pdev, rd->buf, len, dir))) {
  381. if (rd->buf) {
  382. IRDA_ERROR("%s: failed to create PCI-MAP for %p",
  383. __FUNCTION__, rd->buf);
  384. kfree(rd->buf);
  385. rd->buf = NULL;
  386. }
  387. for (j = 0; j < i; j++) {
  388. rd = r->rd + j;
  389. busaddr = rd_get_addr(rd);
  390. rd_set_addr_status(rd, 0, 0);
  391. if (busaddr)
  392. pci_unmap_single(pdev, busaddr, len, dir);
  393. kfree(rd->buf);
  394. rd->buf = NULL;
  395. }
  396. kfree(r);
  397. return NULL;
  398. }
  399. rd_set_addr_status(rd, busaddr, 0);
  400. /* initially, the dma buffer is owned by the CPU */
  401. rd->skb = NULL;
  402. }
  403. return r;
  404. }
  405. static int vlsi_free_ring(struct vlsi_ring *r)
  406. {
  407. struct ring_descr *rd;
  408. unsigned i;
  409. dma_addr_t busaddr;
  410. for (i = 0; i < r->size; i++) {
  411. rd = r->rd + i;
  412. if (rd->skb)
  413. dev_kfree_skb_any(rd->skb);
  414. busaddr = rd_get_addr(rd);
  415. rd_set_addr_status(rd, 0, 0);
  416. if (busaddr)
  417. pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
  418. kfree(rd->buf);
  419. }
  420. kfree(r);
  421. return 0;
  422. }
  423. static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
  424. {
  425. char *ringarea;
  426. struct ring_descr_hw *hwmap;
  427. idev->virtaddr = NULL;
  428. idev->busaddr = 0;
  429. ringarea = pci_alloc_consistent(idev->pdev, HW_RING_AREA_SIZE, &idev->busaddr);
  430. if (!ringarea) {
  431. IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
  432. __FUNCTION__);
  433. goto out;
  434. }
  435. memset(ringarea, 0, HW_RING_AREA_SIZE);
  436. hwmap = (struct ring_descr_hw *)ringarea;
  437. idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
  438. XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
  439. if (idev->rx_ring == NULL)
  440. goto out_unmap;
  441. hwmap += MAX_RING_DESCR;
  442. idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
  443. XFER_BUF_SIZE, PCI_DMA_TODEVICE);
  444. if (idev->tx_ring == NULL)
  445. goto out_free_rx;
  446. idev->virtaddr = ringarea;
  447. return 0;
  448. out_free_rx:
  449. vlsi_free_ring(idev->rx_ring);
  450. out_unmap:
  451. idev->rx_ring = idev->tx_ring = NULL;
  452. pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
  453. idev->busaddr = 0;
  454. out:
  455. return -ENOMEM;
  456. }
  457. static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
  458. {
  459. vlsi_free_ring(idev->rx_ring);
  460. vlsi_free_ring(idev->tx_ring);
  461. idev->rx_ring = idev->tx_ring = NULL;
  462. if (idev->busaddr)
  463. pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
  464. idev->virtaddr = NULL;
  465. idev->busaddr = 0;
  466. return 0;
  467. }
  468. /********************************************************/
  469. static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
  470. {
  471. u16 status;
  472. int crclen, len = 0;
  473. struct sk_buff *skb;
  474. int ret = 0;
  475. struct net_device *ndev = (struct net_device *)pci_get_drvdata(r->pdev);
  476. vlsi_irda_dev_t *idev = ndev->priv;
  477. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  478. /* dma buffer now owned by the CPU */
  479. status = rd_get_status(rd);
  480. if (status & RD_RX_ERROR) {
  481. if (status & RD_RX_OVER)
  482. ret |= VLSI_RX_OVER;
  483. if (status & RD_RX_LENGTH)
  484. ret |= VLSI_RX_LENGTH;
  485. if (status & RD_RX_PHYERR)
  486. ret |= VLSI_RX_FRAME;
  487. if (status & RD_RX_CRCERR)
  488. ret |= VLSI_RX_CRC;
  489. goto done;
  490. }
  491. len = rd_get_count(rd);
  492. crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
  493. len -= crclen; /* remove trailing CRC */
  494. if (len <= 0) {
  495. IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __FUNCTION__, len);
  496. ret |= VLSI_RX_DROP;
  497. goto done;
  498. }
  499. if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
  500. /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
  501. * endian-adjustment there just in place will dirty a cache line
  502. * which belongs to the map and thus we must be sure it will
  503. * get flushed before giving the buffer back to hardware.
  504. * vlsi_fill_rx() will do this anyway - but here we rely on.
  505. */
  506. le16_to_cpus(rd->buf+len);
  507. if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
  508. IRDA_DEBUG(0, "%s: crc error\n", __FUNCTION__);
  509. ret |= VLSI_RX_CRC;
  510. goto done;
  511. }
  512. }
  513. if (!rd->skb) {
  514. IRDA_WARNING("%s: rx packet lost\n", __FUNCTION__);
  515. ret |= VLSI_RX_DROP;
  516. goto done;
  517. }
  518. skb = rd->skb;
  519. rd->skb = NULL;
  520. skb->dev = ndev;
  521. memcpy(skb_put(skb,len), rd->buf, len);
  522. skb->mac.raw = skb->data;
  523. if (in_interrupt())
  524. netif_rx(skb);
  525. else
  526. netif_rx_ni(skb);
  527. ndev->last_rx = jiffies;
  528. done:
  529. rd_set_status(rd, 0);
  530. rd_set_count(rd, 0);
  531. /* buffer still owned by CPU */
  532. return (ret) ? -ret : len;
  533. }
  534. static void vlsi_fill_rx(struct vlsi_ring *r)
  535. {
  536. struct ring_descr *rd;
  537. for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
  538. if (rd_is_active(rd)) {
  539. IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
  540. __FUNCTION__);
  541. vlsi_ring_debug(r);
  542. break;
  543. }
  544. if (!rd->skb) {
  545. rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
  546. if (rd->skb) {
  547. skb_reserve(rd->skb,1);
  548. rd->skb->protocol = htons(ETH_P_IRDA);
  549. }
  550. else
  551. break; /* probably not worth logging? */
  552. }
  553. /* give dma buffer back to busmaster */
  554. pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
  555. rd_activate(rd);
  556. }
  557. }
  558. static void vlsi_rx_interrupt(struct net_device *ndev)
  559. {
  560. vlsi_irda_dev_t *idev = ndev->priv;
  561. struct vlsi_ring *r = idev->rx_ring;
  562. struct ring_descr *rd;
  563. int ret;
  564. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  565. if (rd_is_active(rd))
  566. break;
  567. ret = vlsi_process_rx(r, rd);
  568. if (ret < 0) {
  569. ret = -ret;
  570. idev->stats.rx_errors++;
  571. if (ret & VLSI_RX_DROP)
  572. idev->stats.rx_dropped++;
  573. if (ret & VLSI_RX_OVER)
  574. idev->stats.rx_over_errors++;
  575. if (ret & VLSI_RX_LENGTH)
  576. idev->stats.rx_length_errors++;
  577. if (ret & VLSI_RX_FRAME)
  578. idev->stats.rx_frame_errors++;
  579. if (ret & VLSI_RX_CRC)
  580. idev->stats.rx_crc_errors++;
  581. }
  582. else if (ret > 0) {
  583. idev->stats.rx_packets++;
  584. idev->stats.rx_bytes += ret;
  585. }
  586. }
  587. do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
  588. vlsi_fill_rx(r);
  589. if (ring_first(r) == NULL) {
  590. /* we are in big trouble, if this should ever happen */
  591. IRDA_ERROR("%s: rx ring exhausted!\n", __FUNCTION__);
  592. vlsi_ring_debug(r);
  593. }
  594. else
  595. outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
  596. }
  597. /* caller must have stopped the controller from busmastering */
  598. static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
  599. {
  600. struct vlsi_ring *r = idev->rx_ring;
  601. struct ring_descr *rd;
  602. int ret;
  603. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  604. ret = 0;
  605. if (rd_is_active(rd)) {
  606. rd_set_status(rd, 0);
  607. if (rd_get_count(rd)) {
  608. IRDA_DEBUG(0, "%s - dropping rx packet\n", __FUNCTION__);
  609. ret = -VLSI_RX_DROP;
  610. }
  611. rd_set_count(rd, 0);
  612. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  613. if (rd->skb) {
  614. dev_kfree_skb_any(rd->skb);
  615. rd->skb = NULL;
  616. }
  617. }
  618. else
  619. ret = vlsi_process_rx(r, rd);
  620. if (ret < 0) {
  621. ret = -ret;
  622. idev->stats.rx_errors++;
  623. if (ret & VLSI_RX_DROP)
  624. idev->stats.rx_dropped++;
  625. if (ret & VLSI_RX_OVER)
  626. idev->stats.rx_over_errors++;
  627. if (ret & VLSI_RX_LENGTH)
  628. idev->stats.rx_length_errors++;
  629. if (ret & VLSI_RX_FRAME)
  630. idev->stats.rx_frame_errors++;
  631. if (ret & VLSI_RX_CRC)
  632. idev->stats.rx_crc_errors++;
  633. }
  634. else if (ret > 0) {
  635. idev->stats.rx_packets++;
  636. idev->stats.rx_bytes += ret;
  637. }
  638. }
  639. }
  640. /********************************************************/
  641. static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
  642. {
  643. u16 status;
  644. int len;
  645. int ret;
  646. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  647. /* dma buffer now owned by the CPU */
  648. status = rd_get_status(rd);
  649. if (status & RD_TX_UNDRN)
  650. ret = VLSI_TX_FIFO;
  651. else
  652. ret = 0;
  653. rd_set_status(rd, 0);
  654. if (rd->skb) {
  655. len = rd->skb->len;
  656. dev_kfree_skb_any(rd->skb);
  657. rd->skb = NULL;
  658. }
  659. else /* tx-skb already freed? - should never happen */
  660. len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
  661. rd_set_count(rd, 0);
  662. /* dma buffer still owned by the CPU */
  663. return (ret) ? -ret : len;
  664. }
  665. static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
  666. {
  667. u16 nphyctl;
  668. u16 config;
  669. unsigned mode;
  670. int ret;
  671. int baudrate;
  672. int fifocnt;
  673. baudrate = idev->new_baud;
  674. IRDA_DEBUG(2, "%s: %d -> %d\n", __FUNCTION__, idev->baud, idev->new_baud);
  675. if (baudrate == 4000000) {
  676. mode = IFF_FIR;
  677. config = IRCFG_FIR;
  678. nphyctl = PHYCTL_FIR;
  679. }
  680. else if (baudrate == 1152000) {
  681. mode = IFF_MIR;
  682. config = IRCFG_MIR | IRCFG_CRC16;
  683. nphyctl = PHYCTL_MIR(clksrc==3);
  684. }
  685. else {
  686. mode = IFF_SIR;
  687. config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
  688. switch(baudrate) {
  689. default:
  690. IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
  691. __FUNCTION__, baudrate);
  692. baudrate = 9600;
  693. /* fallthru */
  694. case 2400:
  695. case 9600:
  696. case 19200:
  697. case 38400:
  698. case 57600:
  699. case 115200:
  700. nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
  701. break;
  702. }
  703. }
  704. config |= IRCFG_MSTR | IRCFG_ENRX;
  705. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  706. if (fifocnt != 0) {
  707. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
  708. }
  709. outw(0, iobase+VLSI_PIO_IRENABLE);
  710. outw(config, iobase+VLSI_PIO_IRCFG);
  711. outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
  712. wmb();
  713. outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
  714. mb();
  715. udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
  716. /* read back settings for validation */
  717. config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
  718. if (mode == IFF_FIR)
  719. config ^= IRENABLE_FIR_ON;
  720. else if (mode == IFF_MIR)
  721. config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
  722. else
  723. config ^= IRENABLE_SIR_ON;
  724. if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
  725. IRDA_WARNING("%s: failed to set %s mode!\n", __FUNCTION__,
  726. (mode==IFF_SIR)?"SIR":((mode==IFF_MIR)?"MIR":"FIR"));
  727. ret = -1;
  728. }
  729. else {
  730. if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
  731. IRDA_WARNING("%s: failed to apply baudrate %d\n",
  732. __FUNCTION__, baudrate);
  733. ret = -1;
  734. }
  735. else {
  736. idev->mode = mode;
  737. idev->baud = baudrate;
  738. idev->new_baud = 0;
  739. ret = 0;
  740. }
  741. }
  742. if (ret)
  743. vlsi_reg_debug(iobase,__FUNCTION__);
  744. return ret;
  745. }
  746. static int vlsi_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  747. {
  748. vlsi_irda_dev_t *idev = ndev->priv;
  749. struct vlsi_ring *r = idev->tx_ring;
  750. struct ring_descr *rd;
  751. unsigned long flags;
  752. unsigned iobase = ndev->base_addr;
  753. u8 status;
  754. u16 config;
  755. int mtt;
  756. int len, speed;
  757. struct timeval now, ready;
  758. char *msg = NULL;
  759. speed = irda_get_next_speed(skb);
  760. spin_lock_irqsave(&idev->lock, flags);
  761. if (speed != -1 && speed != idev->baud) {
  762. netif_stop_queue(ndev);
  763. idev->new_baud = speed;
  764. status = RD_TX_CLRENTX; /* stop tx-ring after this frame */
  765. }
  766. else
  767. status = 0;
  768. if (skb->len == 0) {
  769. /* handle zero packets - should be speed change */
  770. if (status == 0) {
  771. msg = "bogus zero-length packet";
  772. goto drop_unlock;
  773. }
  774. /* due to the completely asynch tx operation we might have
  775. * IrLAP racing with the hardware here, f.e. if the controller
  776. * is just sending the last packet with current speed while
  777. * the LAP is already switching the speed using synchronous
  778. * len=0 packet. Immediate execution would lead to hw lockup
  779. * requiring a powercycle to reset. Good candidate to trigger
  780. * this is the final UA:RSP packet after receiving a DISC:CMD
  781. * when getting the LAP down.
  782. * Note that we are not protected by the queue_stop approach
  783. * because the final UA:RSP arrives _without_ request to apply
  784. * new-speed-after-this-packet - hence the driver doesn't know
  785. * this was the last packet and doesn't stop the queue. So the
  786. * forced switch to default speed from LAP gets through as fast
  787. * as only some 10 usec later while the UA:RSP is still processed
  788. * by the hardware and we would get screwed.
  789. */
  790. if (ring_first(idev->tx_ring) == NULL) {
  791. /* no race - tx-ring already empty */
  792. vlsi_set_baud(idev, iobase);
  793. netif_wake_queue(ndev);
  794. }
  795. else
  796. ;
  797. /* keep the speed change pending like it would
  798. * for any len>0 packet. tx completion interrupt
  799. * will apply it when the tx ring becomes empty.
  800. */
  801. spin_unlock_irqrestore(&idev->lock, flags);
  802. dev_kfree_skb_any(skb);
  803. return 0;
  804. }
  805. /* sanity checks - simply drop the packet */
  806. rd = ring_last(r);
  807. if (!rd) {
  808. msg = "ring full, but queue wasn't stopped";
  809. goto drop_unlock;
  810. }
  811. if (rd_is_active(rd)) {
  812. msg = "entry still owned by hw";
  813. goto drop_unlock;
  814. }
  815. if (!rd->buf) {
  816. msg = "tx ring entry without pci buffer";
  817. goto drop_unlock;
  818. }
  819. if (rd->skb) {
  820. msg = "ring entry with old skb still attached";
  821. goto drop_unlock;
  822. }
  823. /* no need for serialization or interrupt disable during mtt */
  824. spin_unlock_irqrestore(&idev->lock, flags);
  825. if ((mtt = irda_get_mtt(skb)) > 0) {
  826. ready.tv_usec = idev->last_rx.tv_usec + mtt;
  827. ready.tv_sec = idev->last_rx.tv_sec;
  828. if (ready.tv_usec >= 1000000) {
  829. ready.tv_usec -= 1000000;
  830. ready.tv_sec++; /* IrLAP 1.1: mtt always < 1 sec */
  831. }
  832. for(;;) {
  833. do_gettimeofday(&now);
  834. if (now.tv_sec > ready.tv_sec
  835. || (now.tv_sec==ready.tv_sec && now.tv_usec>=ready.tv_usec))
  836. break;
  837. udelay(100);
  838. /* must not sleep here - we are called under xmit_lock! */
  839. }
  840. }
  841. /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
  842. * after subsequent tx-completion
  843. */
  844. if (idev->mode == IFF_SIR) {
  845. status |= RD_TX_DISCRC; /* no hw-crc creation */
  846. len = async_wrap_skb(skb, rd->buf, r->len);
  847. /* Some rare worst case situation in SIR mode might lead to
  848. * potential buffer overflow. The wrapper detects this, returns
  849. * with a shortened frame (without FCS/EOF) but doesn't provide
  850. * any error indication about the invalid packet which we are
  851. * going to transmit.
  852. * Therefore we log if the buffer got filled to the point, where the
  853. * wrapper would abort, i.e. when there are less than 5 bytes left to
  854. * allow appending the FCS/EOF.
  855. */
  856. if (len >= r->len-5)
  857. IRDA_WARNING("%s: possible buffer overflow with SIR wrapping!\n",
  858. __FUNCTION__);
  859. }
  860. else {
  861. /* hw deals with MIR/FIR mode wrapping */
  862. status |= RD_TX_PULSE; /* send 2 us highspeed indication pulse */
  863. len = skb->len;
  864. if (len > r->len) {
  865. msg = "frame exceeds tx buffer length";
  866. goto drop;
  867. }
  868. else
  869. memcpy(rd->buf, skb->data, len);
  870. }
  871. rd->skb = skb; /* remember skb for tx-complete stats */
  872. rd_set_count(rd, len);
  873. rd_set_status(rd, status); /* not yet active! */
  874. /* give dma buffer back to busmaster-hw (flush caches to make
  875. * CPU-driven changes visible from the pci bus).
  876. */
  877. pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
  878. /* Switching to TX mode here races with the controller
  879. * which may stop TX at any time when fetching an inactive descriptor
  880. * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
  881. * _after_ the new descriptor was activated on the ring. This ensures
  882. * we will either find TX already stopped or we can be sure, there
  883. * will be a TX-complete interrupt even if the chip stopped doing
  884. * TX just after we found it still running. The ISR will then find
  885. * the non-empty ring and restart TX processing. The enclosing
  886. * spinlock provides the correct serialization to prevent race with isr.
  887. */
  888. spin_lock_irqsave(&idev->lock,flags);
  889. rd_activate(rd);
  890. if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
  891. int fifocnt;
  892. fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  893. if (fifocnt != 0) {
  894. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
  895. }
  896. config = inw(iobase+VLSI_PIO_IRCFG);
  897. mb();
  898. outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
  899. wmb();
  900. outw(0, iobase+VLSI_PIO_PROMPT);
  901. }
  902. ndev->trans_start = jiffies;
  903. if (ring_put(r) == NULL) {
  904. netif_stop_queue(ndev);
  905. IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __FUNCTION__);
  906. }
  907. spin_unlock_irqrestore(&idev->lock, flags);
  908. return 0;
  909. drop_unlock:
  910. spin_unlock_irqrestore(&idev->lock, flags);
  911. drop:
  912. IRDA_WARNING("%s: dropping packet - %s\n", __FUNCTION__, msg);
  913. dev_kfree_skb_any(skb);
  914. idev->stats.tx_errors++;
  915. idev->stats.tx_dropped++;
  916. /* Don't even think about returning NET_XMIT_DROP (=1) here!
  917. * In fact any retval!=0 causes the packet scheduler to requeue the
  918. * packet for later retry of transmission - which isn't exactly
  919. * what we want after we've just called dev_kfree_skb_any ;-)
  920. */
  921. return 0;
  922. }
  923. static void vlsi_tx_interrupt(struct net_device *ndev)
  924. {
  925. vlsi_irda_dev_t *idev = ndev->priv;
  926. struct vlsi_ring *r = idev->tx_ring;
  927. struct ring_descr *rd;
  928. unsigned iobase;
  929. int ret;
  930. u16 config;
  931. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  932. if (rd_is_active(rd))
  933. break;
  934. ret = vlsi_process_tx(r, rd);
  935. if (ret < 0) {
  936. ret = -ret;
  937. idev->stats.tx_errors++;
  938. if (ret & VLSI_TX_DROP)
  939. idev->stats.tx_dropped++;
  940. if (ret & VLSI_TX_FIFO)
  941. idev->stats.tx_fifo_errors++;
  942. }
  943. else if (ret > 0){
  944. idev->stats.tx_packets++;
  945. idev->stats.tx_bytes += ret;
  946. }
  947. }
  948. iobase = ndev->base_addr;
  949. if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
  950. vlsi_set_baud(idev, iobase);
  951. config = inw(iobase+VLSI_PIO_IRCFG);
  952. if (rd == NULL) /* tx ring empty: re-enable rx */
  953. outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
  954. else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
  955. int fifocnt;
  956. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  957. if (fifocnt != 0) {
  958. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
  959. __FUNCTION__, fifocnt);
  960. }
  961. outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
  962. }
  963. outw(0, iobase+VLSI_PIO_PROMPT);
  964. if (netif_queue_stopped(ndev) && !idev->new_baud) {
  965. netif_wake_queue(ndev);
  966. IRDA_DEBUG(3, "%s: queue awoken\n", __FUNCTION__);
  967. }
  968. }
  969. /* caller must have stopped the controller from busmastering */
  970. static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
  971. {
  972. struct vlsi_ring *r = idev->tx_ring;
  973. struct ring_descr *rd;
  974. int ret;
  975. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  976. ret = 0;
  977. if (rd_is_active(rd)) {
  978. rd_set_status(rd, 0);
  979. rd_set_count(rd, 0);
  980. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  981. if (rd->skb) {
  982. dev_kfree_skb_any(rd->skb);
  983. rd->skb = NULL;
  984. }
  985. IRDA_DEBUG(0, "%s - dropping tx packet\n", __FUNCTION__);
  986. ret = -VLSI_TX_DROP;
  987. }
  988. else
  989. ret = vlsi_process_tx(r, rd);
  990. if (ret < 0) {
  991. ret = -ret;
  992. idev->stats.tx_errors++;
  993. if (ret & VLSI_TX_DROP)
  994. idev->stats.tx_dropped++;
  995. if (ret & VLSI_TX_FIFO)
  996. idev->stats.tx_fifo_errors++;
  997. }
  998. else if (ret > 0){
  999. idev->stats.tx_packets++;
  1000. idev->stats.tx_bytes += ret;
  1001. }
  1002. }
  1003. }
  1004. /********************************************************/
  1005. static int vlsi_start_clock(struct pci_dev *pdev)
  1006. {
  1007. u8 clkctl, lock;
  1008. int i, count;
  1009. if (clksrc < 2) { /* auto or PLL: try PLL */
  1010. clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
  1011. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1012. /* procedure to detect PLL lock synchronisation:
  1013. * after 0.5 msec initial delay we expect to find 3 PLL lock
  1014. * indications within 10 msec for successful PLL detection.
  1015. */
  1016. udelay(500);
  1017. count = 0;
  1018. for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
  1019. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
  1020. if (lock&CLKCTL_LOCK) {
  1021. if (++count >= 3)
  1022. break;
  1023. }
  1024. udelay(50);
  1025. }
  1026. if (count < 3) {
  1027. if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
  1028. IRDA_ERROR("%s: no PLL or failed to lock!\n",
  1029. __FUNCTION__);
  1030. clkctl = CLKCTL_CLKSTP;
  1031. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1032. return -1;
  1033. }
  1034. else /* was: clksrc=0(auto) */
  1035. clksrc = 3; /* fallback to 40MHz XCLK (OB800) */
  1036. IRDA_DEBUG(0, "%s: PLL not locked, fallback to clksrc=%d\n",
  1037. __FUNCTION__, clksrc);
  1038. }
  1039. else
  1040. clksrc = 1; /* got successful PLL lock */
  1041. }
  1042. if (clksrc != 1) {
  1043. /* we get here if either no PLL detected in auto-mode or
  1044. an external clock source was explicitly specified */
  1045. clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
  1046. if (clksrc == 3)
  1047. clkctl |= CLKCTL_XCKSEL;
  1048. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1049. /* no way to test for working XCLK */
  1050. }
  1051. else
  1052. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
  1053. /* ok, now going to connect the chip with the clock source */
  1054. clkctl &= ~CLKCTL_CLKSTP;
  1055. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1056. return 0;
  1057. }
  1058. static void vlsi_stop_clock(struct pci_dev *pdev)
  1059. {
  1060. u8 clkctl;
  1061. /* disconnect chip from clock source */
  1062. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
  1063. clkctl |= CLKCTL_CLKSTP;
  1064. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1065. /* disable all clock sources */
  1066. clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
  1067. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1068. }
  1069. /********************************************************/
  1070. /* writing all-zero to the VLSI PCI IO register area seems to prevent
  1071. * some occasional situations where the hardware fails (symptoms are
  1072. * what appears as stalled tx/rx state machines, i.e. everything ok for
  1073. * receive or transmit but hw makes no progress or is unable to access
  1074. * the bus memory locations).
  1075. * Best place to call this is immediately after/before the internal clock
  1076. * gets started/stopped.
  1077. */
  1078. static inline void vlsi_clear_regs(unsigned iobase)
  1079. {
  1080. unsigned i;
  1081. const unsigned chip_io_extent = 32;
  1082. for (i = 0; i < chip_io_extent; i += sizeof(u16))
  1083. outw(0, iobase + i);
  1084. }
  1085. static int vlsi_init_chip(struct pci_dev *pdev)
  1086. {
  1087. struct net_device *ndev = pci_get_drvdata(pdev);
  1088. vlsi_irda_dev_t *idev = ndev->priv;
  1089. unsigned iobase;
  1090. u16 ptr;
  1091. /* start the clock and clean the registers */
  1092. if (vlsi_start_clock(pdev)) {
  1093. IRDA_ERROR("%s: no valid clock source\n", __FUNCTION__);
  1094. return -1;
  1095. }
  1096. iobase = ndev->base_addr;
  1097. vlsi_clear_regs(iobase);
  1098. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
  1099. outw(0, iobase+VLSI_PIO_IRENABLE); /* disable IrPHY-interface */
  1100. /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
  1101. outw(0, iobase+VLSI_PIO_IRCFG);
  1102. wmb();
  1103. outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT); /* max possible value=0x0fff */
  1104. outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
  1105. outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
  1106. iobase+VLSI_PIO_RINGSIZE);
  1107. ptr = inw(iobase+VLSI_PIO_RINGPTR);
  1108. atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
  1109. atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
  1110. atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
  1111. atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
  1112. vlsi_set_baud(idev, iobase); /* idev->new_baud used as provided by caller */
  1113. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* just in case - w/c pending IRQ's */
  1114. wmb();
  1115. /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
  1116. * basically every received pulse fires an ACTIVITY-INT
  1117. * leading to >>1000 INT's per second instead of few 10
  1118. */
  1119. outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
  1120. return 0;
  1121. }
  1122. static int vlsi_start_hw(vlsi_irda_dev_t *idev)
  1123. {
  1124. struct pci_dev *pdev = idev->pdev;
  1125. struct net_device *ndev = pci_get_drvdata(pdev);
  1126. unsigned iobase = ndev->base_addr;
  1127. u8 byte;
  1128. /* we don't use the legacy UART, disable its address decoding */
  1129. pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
  1130. byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
  1131. pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
  1132. /* enable PCI busmaster access to our 16MB page */
  1133. pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
  1134. pci_set_master(pdev);
  1135. if (vlsi_init_chip(pdev) < 0) {
  1136. pci_disable_device(pdev);
  1137. return -1;
  1138. }
  1139. vlsi_fill_rx(idev->rx_ring);
  1140. do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
  1141. outw(0, iobase+VLSI_PIO_PROMPT); /* kick hw state machine */
  1142. return 0;
  1143. }
  1144. static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
  1145. {
  1146. struct pci_dev *pdev = idev->pdev;
  1147. struct net_device *ndev = pci_get_drvdata(pdev);
  1148. unsigned iobase = ndev->base_addr;
  1149. unsigned long flags;
  1150. spin_lock_irqsave(&idev->lock,flags);
  1151. outw(0, iobase+VLSI_PIO_IRENABLE);
  1152. outw(0, iobase+VLSI_PIO_IRCFG); /* disable everything */
  1153. /* disable and w/c irqs */
  1154. outb(0, iobase+VLSI_PIO_IRINTR);
  1155. wmb();
  1156. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
  1157. spin_unlock_irqrestore(&idev->lock,flags);
  1158. vlsi_unarm_tx(idev);
  1159. vlsi_unarm_rx(idev);
  1160. vlsi_clear_regs(iobase);
  1161. vlsi_stop_clock(pdev);
  1162. pci_disable_device(pdev);
  1163. return 0;
  1164. }
  1165. /**************************************************************/
  1166. static struct net_device_stats * vlsi_get_stats(struct net_device *ndev)
  1167. {
  1168. vlsi_irda_dev_t *idev = ndev->priv;
  1169. return &idev->stats;
  1170. }
  1171. static void vlsi_tx_timeout(struct net_device *ndev)
  1172. {
  1173. vlsi_irda_dev_t *idev = ndev->priv;
  1174. vlsi_reg_debug(ndev->base_addr, __FUNCTION__);
  1175. vlsi_ring_debug(idev->tx_ring);
  1176. if (netif_running(ndev))
  1177. netif_stop_queue(ndev);
  1178. vlsi_stop_hw(idev);
  1179. /* now simply restart the whole thing */
  1180. if (!idev->new_baud)
  1181. idev->new_baud = idev->baud; /* keep current baudrate */
  1182. if (vlsi_start_hw(idev))
  1183. IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
  1184. __FUNCTION__, PCIDEV_NAME(idev->pdev), ndev->name);
  1185. else
  1186. netif_start_queue(ndev);
  1187. }
  1188. static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1189. {
  1190. vlsi_irda_dev_t *idev = ndev->priv;
  1191. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1192. unsigned long flags;
  1193. u16 fifocnt;
  1194. int ret = 0;
  1195. switch (cmd) {
  1196. case SIOCSBANDWIDTH:
  1197. if (!capable(CAP_NET_ADMIN)) {
  1198. ret = -EPERM;
  1199. break;
  1200. }
  1201. spin_lock_irqsave(&idev->lock, flags);
  1202. idev->new_baud = irq->ifr_baudrate;
  1203. /* when called from userland there might be a minor race window here
  1204. * if the stack tries to change speed concurrently - which would be
  1205. * pretty strange anyway with the userland having full control...
  1206. */
  1207. vlsi_set_baud(idev, ndev->base_addr);
  1208. spin_unlock_irqrestore(&idev->lock, flags);
  1209. break;
  1210. case SIOCSMEDIABUSY:
  1211. if (!capable(CAP_NET_ADMIN)) {
  1212. ret = -EPERM;
  1213. break;
  1214. }
  1215. irda_device_set_media_busy(ndev, TRUE);
  1216. break;
  1217. case SIOCGRECEIVING:
  1218. /* the best we can do: check whether there are any bytes in rx fifo.
  1219. * The trustable window (in case some data arrives just afterwards)
  1220. * may be as short as 1usec or so at 4Mbps.
  1221. */
  1222. fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  1223. irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
  1224. break;
  1225. default:
  1226. IRDA_WARNING("%s: notsupp - cmd=%04x\n",
  1227. __FUNCTION__, cmd);
  1228. ret = -EOPNOTSUPP;
  1229. }
  1230. return ret;
  1231. }
  1232. /********************************************************/
  1233. static irqreturn_t vlsi_interrupt(int irq, void *dev_instance,
  1234. struct pt_regs *regs)
  1235. {
  1236. struct net_device *ndev = dev_instance;
  1237. vlsi_irda_dev_t *idev = ndev->priv;
  1238. unsigned iobase;
  1239. u8 irintr;
  1240. int boguscount = 5;
  1241. unsigned long flags;
  1242. int handled = 0;
  1243. iobase = ndev->base_addr;
  1244. spin_lock_irqsave(&idev->lock,flags);
  1245. do {
  1246. irintr = inb(iobase+VLSI_PIO_IRINTR);
  1247. mb();
  1248. outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
  1249. if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
  1250. break;
  1251. handled = 1;
  1252. if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
  1253. break; /* nothing todo if only activity */
  1254. if (irintr&IRINTR_RPKTINT)
  1255. vlsi_rx_interrupt(ndev);
  1256. if (irintr&IRINTR_TPKTINT)
  1257. vlsi_tx_interrupt(ndev);
  1258. } while (--boguscount > 0);
  1259. spin_unlock_irqrestore(&idev->lock,flags);
  1260. if (boguscount <= 0)
  1261. IRDA_MESSAGE("%s: too much work in interrupt!\n",
  1262. __FUNCTION__);
  1263. return IRQ_RETVAL(handled);
  1264. }
  1265. /********************************************************/
  1266. static int vlsi_open(struct net_device *ndev)
  1267. {
  1268. vlsi_irda_dev_t *idev = ndev->priv;
  1269. int err = -EAGAIN;
  1270. char hwname[32];
  1271. if (pci_request_regions(idev->pdev, drivername)) {
  1272. IRDA_WARNING("%s: io resource busy\n", __FUNCTION__);
  1273. goto errout;
  1274. }
  1275. ndev->base_addr = pci_resource_start(idev->pdev,0);
  1276. ndev->irq = idev->pdev->irq;
  1277. /* under some rare occasions the chip apparently comes up with
  1278. * IRQ's pending. We better w/c pending IRQ and disable them all
  1279. */
  1280. outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
  1281. if (request_irq(ndev->irq, vlsi_interrupt, SA_SHIRQ,
  1282. drivername, ndev)) {
  1283. IRDA_WARNING("%s: couldn't get IRQ: %d\n",
  1284. __FUNCTION__, ndev->irq);
  1285. goto errout_io;
  1286. }
  1287. if ((err = vlsi_create_hwif(idev)) != 0)
  1288. goto errout_irq;
  1289. sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
  1290. idev->irlap = irlap_open(ndev,&idev->qos,hwname);
  1291. if (!idev->irlap)
  1292. goto errout_free_ring;
  1293. do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
  1294. idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
  1295. if ((err = vlsi_start_hw(idev)) != 0)
  1296. goto errout_close_irlap;
  1297. netif_start_queue(ndev);
  1298. IRDA_MESSAGE("%s: device %s operational\n", __FUNCTION__, ndev->name);
  1299. return 0;
  1300. errout_close_irlap:
  1301. irlap_close(idev->irlap);
  1302. errout_free_ring:
  1303. vlsi_destroy_hwif(idev);
  1304. errout_irq:
  1305. free_irq(ndev->irq,ndev);
  1306. errout_io:
  1307. pci_release_regions(idev->pdev);
  1308. errout:
  1309. return err;
  1310. }
  1311. static int vlsi_close(struct net_device *ndev)
  1312. {
  1313. vlsi_irda_dev_t *idev = ndev->priv;
  1314. netif_stop_queue(ndev);
  1315. if (idev->irlap)
  1316. irlap_close(idev->irlap);
  1317. idev->irlap = NULL;
  1318. vlsi_stop_hw(idev);
  1319. vlsi_destroy_hwif(idev);
  1320. free_irq(ndev->irq,ndev);
  1321. pci_release_regions(idev->pdev);
  1322. IRDA_MESSAGE("%s: device %s stopped\n", __FUNCTION__, ndev->name);
  1323. return 0;
  1324. }
  1325. static int vlsi_irda_init(struct net_device *ndev)
  1326. {
  1327. vlsi_irda_dev_t *idev = ndev->priv;
  1328. struct pci_dev *pdev = idev->pdev;
  1329. SET_MODULE_OWNER(ndev);
  1330. ndev->irq = pdev->irq;
  1331. ndev->base_addr = pci_resource_start(pdev,0);
  1332. /* PCI busmastering
  1333. * see include file for details why we need these 2 masks, in this order!
  1334. */
  1335. if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW)
  1336. || pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
  1337. IRDA_ERROR("%s: aborting due to PCI BM-DMA address limitations\n", __FUNCTION__);
  1338. return -1;
  1339. }
  1340. irda_init_max_qos_capabilies(&idev->qos);
  1341. /* the VLSI82C147 does not support 576000! */
  1342. idev->qos.baud_rate.bits = IR_2400 | IR_9600
  1343. | IR_19200 | IR_38400 | IR_57600 | IR_115200
  1344. | IR_1152000 | (IR_4000000 << 8);
  1345. idev->qos.min_turn_time.bits = qos_mtt_bits;
  1346. irda_qos_bits_to_value(&idev->qos);
  1347. /* currently no public media definitions for IrDA */
  1348. ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
  1349. ndev->if_port = IF_PORT_UNKNOWN;
  1350. ndev->open = vlsi_open;
  1351. ndev->stop = vlsi_close;
  1352. ndev->get_stats = vlsi_get_stats;
  1353. ndev->hard_start_xmit = vlsi_hard_start_xmit;
  1354. ndev->do_ioctl = vlsi_ioctl;
  1355. ndev->tx_timeout = vlsi_tx_timeout;
  1356. ndev->watchdog_timeo = 500*HZ/1000; /* max. allowed turn time for IrLAP */
  1357. SET_NETDEV_DEV(ndev, &pdev->dev);
  1358. return 0;
  1359. }
  1360. /**************************************************************/
  1361. static int __devinit
  1362. vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1363. {
  1364. struct net_device *ndev;
  1365. vlsi_irda_dev_t *idev;
  1366. if (pci_enable_device(pdev))
  1367. goto out;
  1368. else
  1369. pdev->current_state = 0; /* hw must be running now */
  1370. IRDA_MESSAGE("%s: IrDA PCI controller %s detected\n",
  1371. drivername, PCIDEV_NAME(pdev));
  1372. if ( !pci_resource_start(pdev,0)
  1373. || !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
  1374. IRDA_ERROR("%s: bar 0 invalid", __FUNCTION__);
  1375. goto out_disable;
  1376. }
  1377. ndev = alloc_irdadev(sizeof(*idev));
  1378. if (ndev==NULL) {
  1379. IRDA_ERROR("%s: Unable to allocate device memory.\n",
  1380. __FUNCTION__);
  1381. goto out_disable;
  1382. }
  1383. idev = ndev->priv;
  1384. spin_lock_init(&idev->lock);
  1385. init_MUTEX(&idev->sem);
  1386. down(&idev->sem);
  1387. idev->pdev = pdev;
  1388. if (vlsi_irda_init(ndev) < 0)
  1389. goto out_freedev;
  1390. if (register_netdev(ndev) < 0) {
  1391. IRDA_ERROR("%s: register_netdev failed\n", __FUNCTION__);
  1392. goto out_freedev;
  1393. }
  1394. if (vlsi_proc_root != NULL) {
  1395. struct proc_dir_entry *ent;
  1396. ent = create_proc_entry(ndev->name, S_IFREG|S_IRUGO, vlsi_proc_root);
  1397. if (!ent) {
  1398. IRDA_WARNING("%s: failed to create proc entry\n",
  1399. __FUNCTION__);
  1400. } else {
  1401. ent->data = ndev;
  1402. ent->proc_fops = VLSI_PROC_FOPS;
  1403. ent->size = 0;
  1404. }
  1405. idev->proc_entry = ent;
  1406. }
  1407. IRDA_MESSAGE("%s: registered device %s\n", drivername, ndev->name);
  1408. pci_set_drvdata(pdev, ndev);
  1409. up(&idev->sem);
  1410. return 0;
  1411. out_freedev:
  1412. up(&idev->sem);
  1413. free_netdev(ndev);
  1414. out_disable:
  1415. pci_disable_device(pdev);
  1416. out:
  1417. pci_set_drvdata(pdev, NULL);
  1418. return -ENODEV;
  1419. }
  1420. static void __devexit vlsi_irda_remove(struct pci_dev *pdev)
  1421. {
  1422. struct net_device *ndev = pci_get_drvdata(pdev);
  1423. vlsi_irda_dev_t *idev;
  1424. if (!ndev) {
  1425. IRDA_ERROR("%s: lost netdevice?\n", drivername);
  1426. return;
  1427. }
  1428. unregister_netdev(ndev);
  1429. idev = ndev->priv;
  1430. down(&idev->sem);
  1431. if (idev->proc_entry) {
  1432. remove_proc_entry(ndev->name, vlsi_proc_root);
  1433. idev->proc_entry = NULL;
  1434. }
  1435. up(&idev->sem);
  1436. free_netdev(ndev);
  1437. pci_set_drvdata(pdev, NULL);
  1438. IRDA_MESSAGE("%s: %s removed\n", drivername, PCIDEV_NAME(pdev));
  1439. }
  1440. #ifdef CONFIG_PM
  1441. /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
  1442. * Some of the Linux PCI-PM code however depends on this, for example in
  1443. * pci_set_power_state(). So we have to take care to perform the required
  1444. * operations on our own (particularly reflecting the pdev->current_state)
  1445. * otherwise we might get cheated by pci-pm.
  1446. */
  1447. static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
  1448. {
  1449. struct net_device *ndev = pci_get_drvdata(pdev);
  1450. vlsi_irda_dev_t *idev;
  1451. if (!ndev) {
  1452. IRDA_ERROR("%s - %s: no netdevice \n",
  1453. __FUNCTION__, PCIDEV_NAME(pdev));
  1454. return 0;
  1455. }
  1456. idev = ndev->priv;
  1457. down(&idev->sem);
  1458. if (pdev->current_state != 0) { /* already suspended */
  1459. if (state.event > pdev->current_state) { /* simply go deeper */
  1460. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1461. pdev->current_state = state.event;
  1462. }
  1463. else
  1464. IRDA_ERROR("%s - %s: invalid suspend request %u -> %u\n", __FUNCTION__, PCIDEV_NAME(pdev), pdev->current_state, state.event);
  1465. up(&idev->sem);
  1466. return 0;
  1467. }
  1468. if (netif_running(ndev)) {
  1469. netif_device_detach(ndev);
  1470. vlsi_stop_hw(idev);
  1471. pci_save_state(pdev);
  1472. if (!idev->new_baud)
  1473. /* remember speed settings to restore on resume */
  1474. idev->new_baud = idev->baud;
  1475. }
  1476. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1477. pdev->current_state = state.event;
  1478. idev->resume_ok = 1;
  1479. up(&idev->sem);
  1480. return 0;
  1481. }
  1482. static int vlsi_irda_resume(struct pci_dev *pdev)
  1483. {
  1484. struct net_device *ndev = pci_get_drvdata(pdev);
  1485. vlsi_irda_dev_t *idev;
  1486. if (!ndev) {
  1487. IRDA_ERROR("%s - %s: no netdevice \n",
  1488. __FUNCTION__, PCIDEV_NAME(pdev));
  1489. return 0;
  1490. }
  1491. idev = ndev->priv;
  1492. down(&idev->sem);
  1493. if (pdev->current_state == 0) {
  1494. up(&idev->sem);
  1495. IRDA_WARNING("%s - %s: already resumed\n",
  1496. __FUNCTION__, PCIDEV_NAME(pdev));
  1497. return 0;
  1498. }
  1499. pci_set_power_state(pdev, PCI_D0);
  1500. pdev->current_state = PM_EVENT_ON;
  1501. if (!idev->resume_ok) {
  1502. /* should be obsolete now - but used to happen due to:
  1503. * - pci layer initially setting pdev->current_state = 4 (unknown)
  1504. * - pci layer did not walk the save_state-tree (might be APM problem)
  1505. * so we could not refuse to suspend from undefined state
  1506. * - vlsi_irda_suspend detected invalid state and refused to save
  1507. * configuration for resume - but was too late to stop suspending
  1508. * - vlsi_irda_resume got screwed when trying to resume from garbage
  1509. *
  1510. * now we explicitly set pdev->current_state = 0 after enabling the
  1511. * device and independently resume_ok should catch any garbage config.
  1512. */
  1513. IRDA_WARNING("%s - hm, nothing to resume?\n", __FUNCTION__);
  1514. up(&idev->sem);
  1515. return 0;
  1516. }
  1517. if (netif_running(ndev)) {
  1518. pci_restore_state(pdev);
  1519. vlsi_start_hw(idev);
  1520. netif_device_attach(ndev);
  1521. }
  1522. idev->resume_ok = 0;
  1523. up(&idev->sem);
  1524. return 0;
  1525. }
  1526. #endif /* CONFIG_PM */
  1527. /*********************************************************/
  1528. static struct pci_driver vlsi_irda_driver = {
  1529. .name = drivername,
  1530. .id_table = vlsi_irda_table,
  1531. .probe = vlsi_irda_probe,
  1532. .remove = __devexit_p(vlsi_irda_remove),
  1533. #ifdef CONFIG_PM
  1534. .suspend = vlsi_irda_suspend,
  1535. .resume = vlsi_irda_resume,
  1536. #endif
  1537. };
  1538. #define PROC_DIR ("driver/" DRIVER_NAME)
  1539. static int __init vlsi_mod_init(void)
  1540. {
  1541. int i, ret;
  1542. if (clksrc < 0 || clksrc > 3) {
  1543. IRDA_ERROR("%s: invalid clksrc=%d\n", drivername, clksrc);
  1544. return -1;
  1545. }
  1546. for (i = 0; i < 2; i++) {
  1547. switch(ringsize[i]) {
  1548. case 4:
  1549. case 8:
  1550. case 16:
  1551. case 32:
  1552. case 64:
  1553. break;
  1554. default:
  1555. IRDA_WARNING("%s: invalid %s ringsize %d, using default=8", drivername, (i)?"rx":"tx", ringsize[i]);
  1556. ringsize[i] = 8;
  1557. break;
  1558. }
  1559. }
  1560. sirpulse = !!sirpulse;
  1561. /* proc_mkdir returns NULL if !CONFIG_PROC_FS.
  1562. * Failure to create the procfs entry is handled like running
  1563. * without procfs - it's not required for the driver to work.
  1564. */
  1565. vlsi_proc_root = proc_mkdir(PROC_DIR, NULL);
  1566. if (vlsi_proc_root) {
  1567. /* protect registered procdir against module removal.
  1568. * Because we are in the module init path there's no race
  1569. * window after create_proc_entry (and no barrier needed).
  1570. */
  1571. vlsi_proc_root->owner = THIS_MODULE;
  1572. }
  1573. ret = pci_module_init(&vlsi_irda_driver);
  1574. if (ret && vlsi_proc_root)
  1575. remove_proc_entry(PROC_DIR, NULL);
  1576. return ret;
  1577. }
  1578. static void __exit vlsi_mod_exit(void)
  1579. {
  1580. pci_unregister_driver(&vlsi_irda_driver);
  1581. if (vlsi_proc_root)
  1582. remove_proc_entry(PROC_DIR, NULL);
  1583. }
  1584. module_init(vlsi_mod_init);
  1585. module_exit(vlsi_mod_exit);