vlsi_ir.c 51 KB

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