vlsi_ir.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  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. if (rd->buf)
  419. kfree(rd->buf);
  420. }
  421. kfree(r);
  422. return 0;
  423. }
  424. static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
  425. {
  426. char *ringarea;
  427. struct ring_descr_hw *hwmap;
  428. idev->virtaddr = NULL;
  429. idev->busaddr = 0;
  430. ringarea = pci_alloc_consistent(idev->pdev, HW_RING_AREA_SIZE, &idev->busaddr);
  431. if (!ringarea) {
  432. IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
  433. __FUNCTION__);
  434. goto out;
  435. }
  436. memset(ringarea, 0, HW_RING_AREA_SIZE);
  437. hwmap = (struct ring_descr_hw *)ringarea;
  438. idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
  439. XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
  440. if (idev->rx_ring == NULL)
  441. goto out_unmap;
  442. hwmap += MAX_RING_DESCR;
  443. idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
  444. XFER_BUF_SIZE, PCI_DMA_TODEVICE);
  445. if (idev->tx_ring == NULL)
  446. goto out_free_rx;
  447. idev->virtaddr = ringarea;
  448. return 0;
  449. out_free_rx:
  450. vlsi_free_ring(idev->rx_ring);
  451. out_unmap:
  452. idev->rx_ring = idev->tx_ring = NULL;
  453. pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
  454. idev->busaddr = 0;
  455. out:
  456. return -ENOMEM;
  457. }
  458. static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
  459. {
  460. vlsi_free_ring(idev->rx_ring);
  461. vlsi_free_ring(idev->tx_ring);
  462. idev->rx_ring = idev->tx_ring = NULL;
  463. if (idev->busaddr)
  464. pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
  465. idev->virtaddr = NULL;
  466. idev->busaddr = 0;
  467. return 0;
  468. }
  469. /********************************************************/
  470. static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
  471. {
  472. u16 status;
  473. int crclen, len = 0;
  474. struct sk_buff *skb;
  475. int ret = 0;
  476. struct net_device *ndev = (struct net_device *)pci_get_drvdata(r->pdev);
  477. vlsi_irda_dev_t *idev = ndev->priv;
  478. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  479. /* dma buffer now owned by the CPU */
  480. status = rd_get_status(rd);
  481. if (status & RD_RX_ERROR) {
  482. if (status & RD_RX_OVER)
  483. ret |= VLSI_RX_OVER;
  484. if (status & RD_RX_LENGTH)
  485. ret |= VLSI_RX_LENGTH;
  486. if (status & RD_RX_PHYERR)
  487. ret |= VLSI_RX_FRAME;
  488. if (status & RD_RX_CRCERR)
  489. ret |= VLSI_RX_CRC;
  490. goto done;
  491. }
  492. len = rd_get_count(rd);
  493. crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
  494. len -= crclen; /* remove trailing CRC */
  495. if (len <= 0) {
  496. IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __FUNCTION__, len);
  497. ret |= VLSI_RX_DROP;
  498. goto done;
  499. }
  500. if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
  501. /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
  502. * endian-adjustment there just in place will dirty a cache line
  503. * which belongs to the map and thus we must be sure it will
  504. * get flushed before giving the buffer back to hardware.
  505. * vlsi_fill_rx() will do this anyway - but here we rely on.
  506. */
  507. le16_to_cpus(rd->buf+len);
  508. if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
  509. IRDA_DEBUG(0, "%s: crc error\n", __FUNCTION__);
  510. ret |= VLSI_RX_CRC;
  511. goto done;
  512. }
  513. }
  514. if (!rd->skb) {
  515. IRDA_WARNING("%s: rx packet lost\n", __FUNCTION__);
  516. ret |= VLSI_RX_DROP;
  517. goto done;
  518. }
  519. skb = rd->skb;
  520. rd->skb = NULL;
  521. skb->dev = ndev;
  522. memcpy(skb_put(skb,len), rd->buf, len);
  523. skb->mac.raw = skb->data;
  524. if (in_interrupt())
  525. netif_rx(skb);
  526. else
  527. netif_rx_ni(skb);
  528. ndev->last_rx = jiffies;
  529. done:
  530. rd_set_status(rd, 0);
  531. rd_set_count(rd, 0);
  532. /* buffer still owned by CPU */
  533. return (ret) ? -ret : len;
  534. }
  535. static void vlsi_fill_rx(struct vlsi_ring *r)
  536. {
  537. struct ring_descr *rd;
  538. for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
  539. if (rd_is_active(rd)) {
  540. IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
  541. __FUNCTION__);
  542. vlsi_ring_debug(r);
  543. break;
  544. }
  545. if (!rd->skb) {
  546. rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
  547. if (rd->skb) {
  548. skb_reserve(rd->skb,1);
  549. rd->skb->protocol = htons(ETH_P_IRDA);
  550. }
  551. else
  552. break; /* probably not worth logging? */
  553. }
  554. /* give dma buffer back to busmaster */
  555. pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
  556. rd_activate(rd);
  557. }
  558. }
  559. static void vlsi_rx_interrupt(struct net_device *ndev)
  560. {
  561. vlsi_irda_dev_t *idev = ndev->priv;
  562. struct vlsi_ring *r = idev->rx_ring;
  563. struct ring_descr *rd;
  564. int ret;
  565. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  566. if (rd_is_active(rd))
  567. break;
  568. ret = vlsi_process_rx(r, rd);
  569. if (ret < 0) {
  570. ret = -ret;
  571. idev->stats.rx_errors++;
  572. if (ret & VLSI_RX_DROP)
  573. idev->stats.rx_dropped++;
  574. if (ret & VLSI_RX_OVER)
  575. idev->stats.rx_over_errors++;
  576. if (ret & VLSI_RX_LENGTH)
  577. idev->stats.rx_length_errors++;
  578. if (ret & VLSI_RX_FRAME)
  579. idev->stats.rx_frame_errors++;
  580. if (ret & VLSI_RX_CRC)
  581. idev->stats.rx_crc_errors++;
  582. }
  583. else if (ret > 0) {
  584. idev->stats.rx_packets++;
  585. idev->stats.rx_bytes += ret;
  586. }
  587. }
  588. do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
  589. vlsi_fill_rx(r);
  590. if (ring_first(r) == NULL) {
  591. /* we are in big trouble, if this should ever happen */
  592. IRDA_ERROR("%s: rx ring exhausted!\n", __FUNCTION__);
  593. vlsi_ring_debug(r);
  594. }
  595. else
  596. outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
  597. }
  598. /* caller must have stopped the controller from busmastering */
  599. static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
  600. {
  601. struct vlsi_ring *r = idev->rx_ring;
  602. struct ring_descr *rd;
  603. int ret;
  604. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  605. ret = 0;
  606. if (rd_is_active(rd)) {
  607. rd_set_status(rd, 0);
  608. if (rd_get_count(rd)) {
  609. IRDA_DEBUG(0, "%s - dropping rx packet\n", __FUNCTION__);
  610. ret = -VLSI_RX_DROP;
  611. }
  612. rd_set_count(rd, 0);
  613. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  614. if (rd->skb) {
  615. dev_kfree_skb_any(rd->skb);
  616. rd->skb = NULL;
  617. }
  618. }
  619. else
  620. ret = vlsi_process_rx(r, rd);
  621. if (ret < 0) {
  622. ret = -ret;
  623. idev->stats.rx_errors++;
  624. if (ret & VLSI_RX_DROP)
  625. idev->stats.rx_dropped++;
  626. if (ret & VLSI_RX_OVER)
  627. idev->stats.rx_over_errors++;
  628. if (ret & VLSI_RX_LENGTH)
  629. idev->stats.rx_length_errors++;
  630. if (ret & VLSI_RX_FRAME)
  631. idev->stats.rx_frame_errors++;
  632. if (ret & VLSI_RX_CRC)
  633. idev->stats.rx_crc_errors++;
  634. }
  635. else if (ret > 0) {
  636. idev->stats.rx_packets++;
  637. idev->stats.rx_bytes += ret;
  638. }
  639. }
  640. }
  641. /********************************************************/
  642. static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
  643. {
  644. u16 status;
  645. int len;
  646. int ret;
  647. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  648. /* dma buffer now owned by the CPU */
  649. status = rd_get_status(rd);
  650. if (status & RD_TX_UNDRN)
  651. ret = VLSI_TX_FIFO;
  652. else
  653. ret = 0;
  654. rd_set_status(rd, 0);
  655. if (rd->skb) {
  656. len = rd->skb->len;
  657. dev_kfree_skb_any(rd->skb);
  658. rd->skb = NULL;
  659. }
  660. else /* tx-skb already freed? - should never happen */
  661. len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
  662. rd_set_count(rd, 0);
  663. /* dma buffer still owned by the CPU */
  664. return (ret) ? -ret : len;
  665. }
  666. static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
  667. {
  668. u16 nphyctl;
  669. u16 config;
  670. unsigned mode;
  671. int ret;
  672. int baudrate;
  673. int fifocnt;
  674. baudrate = idev->new_baud;
  675. IRDA_DEBUG(2, "%s: %d -> %d\n", __FUNCTION__, idev->baud, idev->new_baud);
  676. if (baudrate == 4000000) {
  677. mode = IFF_FIR;
  678. config = IRCFG_FIR;
  679. nphyctl = PHYCTL_FIR;
  680. }
  681. else if (baudrate == 1152000) {
  682. mode = IFF_MIR;
  683. config = IRCFG_MIR | IRCFG_CRC16;
  684. nphyctl = PHYCTL_MIR(clksrc==3);
  685. }
  686. else {
  687. mode = IFF_SIR;
  688. config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
  689. switch(baudrate) {
  690. default:
  691. IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
  692. __FUNCTION__, baudrate);
  693. baudrate = 9600;
  694. /* fallthru */
  695. case 2400:
  696. case 9600:
  697. case 19200:
  698. case 38400:
  699. case 57600:
  700. case 115200:
  701. nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
  702. break;
  703. }
  704. }
  705. config |= IRCFG_MSTR | IRCFG_ENRX;
  706. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  707. if (fifocnt != 0) {
  708. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
  709. }
  710. outw(0, iobase+VLSI_PIO_IRENABLE);
  711. outw(config, iobase+VLSI_PIO_IRCFG);
  712. outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
  713. wmb();
  714. outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
  715. mb();
  716. udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
  717. /* read back settings for validation */
  718. config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
  719. if (mode == IFF_FIR)
  720. config ^= IRENABLE_FIR_ON;
  721. else if (mode == IFF_MIR)
  722. config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
  723. else
  724. config ^= IRENABLE_SIR_ON;
  725. if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
  726. IRDA_WARNING("%s: failed to set %s mode!\n", __FUNCTION__,
  727. (mode==IFF_SIR)?"SIR":((mode==IFF_MIR)?"MIR":"FIR"));
  728. ret = -1;
  729. }
  730. else {
  731. if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
  732. IRDA_WARNING("%s: failed to apply baudrate %d\n",
  733. __FUNCTION__, baudrate);
  734. ret = -1;
  735. }
  736. else {
  737. idev->mode = mode;
  738. idev->baud = baudrate;
  739. idev->new_baud = 0;
  740. ret = 0;
  741. }
  742. }
  743. if (ret)
  744. vlsi_reg_debug(iobase,__FUNCTION__);
  745. return ret;
  746. }
  747. static int vlsi_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  748. {
  749. vlsi_irda_dev_t *idev = ndev->priv;
  750. struct vlsi_ring *r = idev->tx_ring;
  751. struct ring_descr *rd;
  752. unsigned long flags;
  753. unsigned iobase = ndev->base_addr;
  754. u8 status;
  755. u16 config;
  756. int mtt;
  757. int len, speed;
  758. struct timeval now, ready;
  759. char *msg = NULL;
  760. speed = irda_get_next_speed(skb);
  761. spin_lock_irqsave(&idev->lock, flags);
  762. if (speed != -1 && speed != idev->baud) {
  763. netif_stop_queue(ndev);
  764. idev->new_baud = speed;
  765. status = RD_TX_CLRENTX; /* stop tx-ring after this frame */
  766. }
  767. else
  768. status = 0;
  769. if (skb->len == 0) {
  770. /* handle zero packets - should be speed change */
  771. if (status == 0) {
  772. msg = "bogus zero-length packet";
  773. goto drop_unlock;
  774. }
  775. /* due to the completely asynch tx operation we might have
  776. * IrLAP racing with the hardware here, f.e. if the controller
  777. * is just sending the last packet with current speed while
  778. * the LAP is already switching the speed using synchronous
  779. * len=0 packet. Immediate execution would lead to hw lockup
  780. * requiring a powercycle to reset. Good candidate to trigger
  781. * this is the final UA:RSP packet after receiving a DISC:CMD
  782. * when getting the LAP down.
  783. * Note that we are not protected by the queue_stop approach
  784. * because the final UA:RSP arrives _without_ request to apply
  785. * new-speed-after-this-packet - hence the driver doesn't know
  786. * this was the last packet and doesn't stop the queue. So the
  787. * forced switch to default speed from LAP gets through as fast
  788. * as only some 10 usec later while the UA:RSP is still processed
  789. * by the hardware and we would get screwed.
  790. */
  791. if (ring_first(idev->tx_ring) == NULL) {
  792. /* no race - tx-ring already empty */
  793. vlsi_set_baud(idev, iobase);
  794. netif_wake_queue(ndev);
  795. }
  796. else
  797. ;
  798. /* keep the speed change pending like it would
  799. * for any len>0 packet. tx completion interrupt
  800. * will apply it when the tx ring becomes empty.
  801. */
  802. spin_unlock_irqrestore(&idev->lock, flags);
  803. dev_kfree_skb_any(skb);
  804. return 0;
  805. }
  806. /* sanity checks - simply drop the packet */
  807. rd = ring_last(r);
  808. if (!rd) {
  809. msg = "ring full, but queue wasn't stopped";
  810. goto drop_unlock;
  811. }
  812. if (rd_is_active(rd)) {
  813. msg = "entry still owned by hw";
  814. goto drop_unlock;
  815. }
  816. if (!rd->buf) {
  817. msg = "tx ring entry without pci buffer";
  818. goto drop_unlock;
  819. }
  820. if (rd->skb) {
  821. msg = "ring entry with old skb still attached";
  822. goto drop_unlock;
  823. }
  824. /* no need for serialization or interrupt disable during mtt */
  825. spin_unlock_irqrestore(&idev->lock, flags);
  826. if ((mtt = irda_get_mtt(skb)) > 0) {
  827. ready.tv_usec = idev->last_rx.tv_usec + mtt;
  828. ready.tv_sec = idev->last_rx.tv_sec;
  829. if (ready.tv_usec >= 1000000) {
  830. ready.tv_usec -= 1000000;
  831. ready.tv_sec++; /* IrLAP 1.1: mtt always < 1 sec */
  832. }
  833. for(;;) {
  834. do_gettimeofday(&now);
  835. if (now.tv_sec > ready.tv_sec
  836. || (now.tv_sec==ready.tv_sec && now.tv_usec>=ready.tv_usec))
  837. break;
  838. udelay(100);
  839. /* must not sleep here - we are called under xmit_lock! */
  840. }
  841. }
  842. /* tx buffer already owned by CPU due to pci_dma_sync_single_for_cpu()
  843. * after subsequent tx-completion
  844. */
  845. if (idev->mode == IFF_SIR) {
  846. status |= RD_TX_DISCRC; /* no hw-crc creation */
  847. len = async_wrap_skb(skb, rd->buf, r->len);
  848. /* Some rare worst case situation in SIR mode might lead to
  849. * potential buffer overflow. The wrapper detects this, returns
  850. * with a shortened frame (without FCS/EOF) but doesn't provide
  851. * any error indication about the invalid packet which we are
  852. * going to transmit.
  853. * Therefore we log if the buffer got filled to the point, where the
  854. * wrapper would abort, i.e. when there are less than 5 bytes left to
  855. * allow appending the FCS/EOF.
  856. */
  857. if (len >= r->len-5)
  858. IRDA_WARNING("%s: possible buffer overflow with SIR wrapping!\n",
  859. __FUNCTION__);
  860. }
  861. else {
  862. /* hw deals with MIR/FIR mode wrapping */
  863. status |= RD_TX_PULSE; /* send 2 us highspeed indication pulse */
  864. len = skb->len;
  865. if (len > r->len) {
  866. msg = "frame exceeds tx buffer length";
  867. goto drop;
  868. }
  869. else
  870. memcpy(rd->buf, skb->data, len);
  871. }
  872. rd->skb = skb; /* remember skb for tx-complete stats */
  873. rd_set_count(rd, len);
  874. rd_set_status(rd, status); /* not yet active! */
  875. /* give dma buffer back to busmaster-hw (flush caches to make
  876. * CPU-driven changes visible from the pci bus).
  877. */
  878. pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
  879. /* Switching to TX mode here races with the controller
  880. * which may stop TX at any time when fetching an inactive descriptor
  881. * or one with CLR_ENTX set. So we switch on TX only, if TX was not running
  882. * _after_ the new descriptor was activated on the ring. This ensures
  883. * we will either find TX already stopped or we can be sure, there
  884. * will be a TX-complete interrupt even if the chip stopped doing
  885. * TX just after we found it still running. The ISR will then find
  886. * the non-empty ring and restart TX processing. The enclosing
  887. * spinlock provides the correct serialization to prevent race with isr.
  888. */
  889. spin_lock_irqsave(&idev->lock,flags);
  890. rd_activate(rd);
  891. if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
  892. int fifocnt;
  893. fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  894. if (fifocnt != 0) {
  895. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __FUNCTION__, fifocnt);
  896. }
  897. config = inw(iobase+VLSI_PIO_IRCFG);
  898. mb();
  899. outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
  900. wmb();
  901. outw(0, iobase+VLSI_PIO_PROMPT);
  902. }
  903. ndev->trans_start = jiffies;
  904. if (ring_put(r) == NULL) {
  905. netif_stop_queue(ndev);
  906. IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __FUNCTION__);
  907. }
  908. spin_unlock_irqrestore(&idev->lock, flags);
  909. return 0;
  910. drop_unlock:
  911. spin_unlock_irqrestore(&idev->lock, flags);
  912. drop:
  913. IRDA_WARNING("%s: dropping packet - %s\n", __FUNCTION__, msg);
  914. dev_kfree_skb_any(skb);
  915. idev->stats.tx_errors++;
  916. idev->stats.tx_dropped++;
  917. /* Don't even think about returning NET_XMIT_DROP (=1) here!
  918. * In fact any retval!=0 causes the packet scheduler to requeue the
  919. * packet for later retry of transmission - which isn't exactly
  920. * what we want after we've just called dev_kfree_skb_any ;-)
  921. */
  922. return 0;
  923. }
  924. static void vlsi_tx_interrupt(struct net_device *ndev)
  925. {
  926. vlsi_irda_dev_t *idev = ndev->priv;
  927. struct vlsi_ring *r = idev->tx_ring;
  928. struct ring_descr *rd;
  929. unsigned iobase;
  930. int ret;
  931. u16 config;
  932. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  933. if (rd_is_active(rd))
  934. break;
  935. ret = vlsi_process_tx(r, rd);
  936. if (ret < 0) {
  937. ret = -ret;
  938. idev->stats.tx_errors++;
  939. if (ret & VLSI_TX_DROP)
  940. idev->stats.tx_dropped++;
  941. if (ret & VLSI_TX_FIFO)
  942. idev->stats.tx_fifo_errors++;
  943. }
  944. else if (ret > 0){
  945. idev->stats.tx_packets++;
  946. idev->stats.tx_bytes += ret;
  947. }
  948. }
  949. iobase = ndev->base_addr;
  950. if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
  951. vlsi_set_baud(idev, iobase);
  952. config = inw(iobase+VLSI_PIO_IRCFG);
  953. if (rd == NULL) /* tx ring empty: re-enable rx */
  954. outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
  955. else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
  956. int fifocnt;
  957. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  958. if (fifocnt != 0) {
  959. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
  960. __FUNCTION__, fifocnt);
  961. }
  962. outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
  963. }
  964. outw(0, iobase+VLSI_PIO_PROMPT);
  965. if (netif_queue_stopped(ndev) && !idev->new_baud) {
  966. netif_wake_queue(ndev);
  967. IRDA_DEBUG(3, "%s: queue awoken\n", __FUNCTION__);
  968. }
  969. }
  970. /* caller must have stopped the controller from busmastering */
  971. static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
  972. {
  973. struct vlsi_ring *r = idev->tx_ring;
  974. struct ring_descr *rd;
  975. int ret;
  976. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  977. ret = 0;
  978. if (rd_is_active(rd)) {
  979. rd_set_status(rd, 0);
  980. rd_set_count(rd, 0);
  981. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  982. if (rd->skb) {
  983. dev_kfree_skb_any(rd->skb);
  984. rd->skb = NULL;
  985. }
  986. IRDA_DEBUG(0, "%s - dropping tx packet\n", __FUNCTION__);
  987. ret = -VLSI_TX_DROP;
  988. }
  989. else
  990. ret = vlsi_process_tx(r, rd);
  991. if (ret < 0) {
  992. ret = -ret;
  993. idev->stats.tx_errors++;
  994. if (ret & VLSI_TX_DROP)
  995. idev->stats.tx_dropped++;
  996. if (ret & VLSI_TX_FIFO)
  997. idev->stats.tx_fifo_errors++;
  998. }
  999. else if (ret > 0){
  1000. idev->stats.tx_packets++;
  1001. idev->stats.tx_bytes += ret;
  1002. }
  1003. }
  1004. }
  1005. /********************************************************/
  1006. static int vlsi_start_clock(struct pci_dev *pdev)
  1007. {
  1008. u8 clkctl, lock;
  1009. int i, count;
  1010. if (clksrc < 2) { /* auto or PLL: try PLL */
  1011. clkctl = CLKCTL_PD_INV | CLKCTL_CLKSTP;
  1012. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1013. /* procedure to detect PLL lock synchronisation:
  1014. * after 0.5 msec initial delay we expect to find 3 PLL lock
  1015. * indications within 10 msec for successful PLL detection.
  1016. */
  1017. udelay(500);
  1018. count = 0;
  1019. for (i = 500; i <= 10000; i += 50) { /* max 10 msec */
  1020. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &lock);
  1021. if (lock&CLKCTL_LOCK) {
  1022. if (++count >= 3)
  1023. break;
  1024. }
  1025. udelay(50);
  1026. }
  1027. if (count < 3) {
  1028. if (clksrc == 1) { /* explicitly asked for PLL hence bail out */
  1029. IRDA_ERROR("%s: no PLL or failed to lock!\n",
  1030. __FUNCTION__);
  1031. clkctl = CLKCTL_CLKSTP;
  1032. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1033. return -1;
  1034. }
  1035. else /* was: clksrc=0(auto) */
  1036. clksrc = 3; /* fallback to 40MHz XCLK (OB800) */
  1037. IRDA_DEBUG(0, "%s: PLL not locked, fallback to clksrc=%d\n",
  1038. __FUNCTION__, clksrc);
  1039. }
  1040. else
  1041. clksrc = 1; /* got successful PLL lock */
  1042. }
  1043. if (clksrc != 1) {
  1044. /* we get here if either no PLL detected in auto-mode or
  1045. an external clock source was explicitly specified */
  1046. clkctl = CLKCTL_EXTCLK | CLKCTL_CLKSTP;
  1047. if (clksrc == 3)
  1048. clkctl |= CLKCTL_XCKSEL;
  1049. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1050. /* no way to test for working XCLK */
  1051. }
  1052. else
  1053. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
  1054. /* ok, now going to connect the chip with the clock source */
  1055. clkctl &= ~CLKCTL_CLKSTP;
  1056. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1057. return 0;
  1058. }
  1059. static void vlsi_stop_clock(struct pci_dev *pdev)
  1060. {
  1061. u8 clkctl;
  1062. /* disconnect chip from clock source */
  1063. pci_read_config_byte(pdev, VLSI_PCI_CLKCTL, &clkctl);
  1064. clkctl |= CLKCTL_CLKSTP;
  1065. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1066. /* disable all clock sources */
  1067. clkctl &= ~(CLKCTL_EXTCLK | CLKCTL_PD_INV);
  1068. pci_write_config_byte(pdev, VLSI_PCI_CLKCTL, clkctl);
  1069. }
  1070. /********************************************************/
  1071. /* writing all-zero to the VLSI PCI IO register area seems to prevent
  1072. * some occasional situations where the hardware fails (symptoms are
  1073. * what appears as stalled tx/rx state machines, i.e. everything ok for
  1074. * receive or transmit but hw makes no progress or is unable to access
  1075. * the bus memory locations).
  1076. * Best place to call this is immediately after/before the internal clock
  1077. * gets started/stopped.
  1078. */
  1079. static inline void vlsi_clear_regs(unsigned iobase)
  1080. {
  1081. unsigned i;
  1082. const unsigned chip_io_extent = 32;
  1083. for (i = 0; i < chip_io_extent; i += sizeof(u16))
  1084. outw(0, iobase + i);
  1085. }
  1086. static int vlsi_init_chip(struct pci_dev *pdev)
  1087. {
  1088. struct net_device *ndev = pci_get_drvdata(pdev);
  1089. vlsi_irda_dev_t *idev = ndev->priv;
  1090. unsigned iobase;
  1091. u16 ptr;
  1092. /* start the clock and clean the registers */
  1093. if (vlsi_start_clock(pdev)) {
  1094. IRDA_ERROR("%s: no valid clock source\n", __FUNCTION__);
  1095. return -1;
  1096. }
  1097. iobase = ndev->base_addr;
  1098. vlsi_clear_regs(iobase);
  1099. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* w/c pending IRQ, disable all INT */
  1100. outw(0, iobase+VLSI_PIO_IRENABLE); /* disable IrPHY-interface */
  1101. /* disable everything, particularly IRCFG_MSTR - (also resetting the RING_PTR) */
  1102. outw(0, iobase+VLSI_PIO_IRCFG);
  1103. wmb();
  1104. outw(MAX_PACKET_LENGTH, iobase+VLSI_PIO_MAXPKT); /* max possible value=0x0fff */
  1105. outw(BUS_TO_RINGBASE(idev->busaddr), iobase+VLSI_PIO_RINGBASE);
  1106. outw(TX_RX_TO_RINGSIZE(idev->tx_ring->size, idev->rx_ring->size),
  1107. iobase+VLSI_PIO_RINGSIZE);
  1108. ptr = inw(iobase+VLSI_PIO_RINGPTR);
  1109. atomic_set(&idev->rx_ring->head, RINGPTR_GET_RX(ptr));
  1110. atomic_set(&idev->rx_ring->tail, RINGPTR_GET_RX(ptr));
  1111. atomic_set(&idev->tx_ring->head, RINGPTR_GET_TX(ptr));
  1112. atomic_set(&idev->tx_ring->tail, RINGPTR_GET_TX(ptr));
  1113. vlsi_set_baud(idev, iobase); /* idev->new_baud used as provided by caller */
  1114. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR); /* just in case - w/c pending IRQ's */
  1115. wmb();
  1116. /* DO NOT BLINDLY ENABLE IRINTR_ACTEN!
  1117. * basically every received pulse fires an ACTIVITY-INT
  1118. * leading to >>1000 INT's per second instead of few 10
  1119. */
  1120. outb(IRINTR_RPKTEN|IRINTR_TPKTEN, iobase+VLSI_PIO_IRINTR);
  1121. return 0;
  1122. }
  1123. static int vlsi_start_hw(vlsi_irda_dev_t *idev)
  1124. {
  1125. struct pci_dev *pdev = idev->pdev;
  1126. struct net_device *ndev = pci_get_drvdata(pdev);
  1127. unsigned iobase = ndev->base_addr;
  1128. u8 byte;
  1129. /* we don't use the legacy UART, disable its address decoding */
  1130. pci_read_config_byte(pdev, VLSI_PCI_IRMISC, &byte);
  1131. byte &= ~(IRMISC_UARTEN | IRMISC_UARTTST);
  1132. pci_write_config_byte(pdev, VLSI_PCI_IRMISC, byte);
  1133. /* enable PCI busmaster access to our 16MB page */
  1134. pci_write_config_byte(pdev, VLSI_PCI_MSTRPAGE, MSTRPAGE_VALUE);
  1135. pci_set_master(pdev);
  1136. if (vlsi_init_chip(pdev) < 0) {
  1137. pci_disable_device(pdev);
  1138. return -1;
  1139. }
  1140. vlsi_fill_rx(idev->rx_ring);
  1141. do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
  1142. outw(0, iobase+VLSI_PIO_PROMPT); /* kick hw state machine */
  1143. return 0;
  1144. }
  1145. static int vlsi_stop_hw(vlsi_irda_dev_t *idev)
  1146. {
  1147. struct pci_dev *pdev = idev->pdev;
  1148. struct net_device *ndev = pci_get_drvdata(pdev);
  1149. unsigned iobase = ndev->base_addr;
  1150. unsigned long flags;
  1151. spin_lock_irqsave(&idev->lock,flags);
  1152. outw(0, iobase+VLSI_PIO_IRENABLE);
  1153. outw(0, iobase+VLSI_PIO_IRCFG); /* disable everything */
  1154. /* disable and w/c irqs */
  1155. outb(0, iobase+VLSI_PIO_IRINTR);
  1156. wmb();
  1157. outb(IRINTR_INT_MASK, iobase+VLSI_PIO_IRINTR);
  1158. spin_unlock_irqrestore(&idev->lock,flags);
  1159. vlsi_unarm_tx(idev);
  1160. vlsi_unarm_rx(idev);
  1161. vlsi_clear_regs(iobase);
  1162. vlsi_stop_clock(pdev);
  1163. pci_disable_device(pdev);
  1164. return 0;
  1165. }
  1166. /**************************************************************/
  1167. static struct net_device_stats * vlsi_get_stats(struct net_device *ndev)
  1168. {
  1169. vlsi_irda_dev_t *idev = ndev->priv;
  1170. return &idev->stats;
  1171. }
  1172. static void vlsi_tx_timeout(struct net_device *ndev)
  1173. {
  1174. vlsi_irda_dev_t *idev = ndev->priv;
  1175. vlsi_reg_debug(ndev->base_addr, __FUNCTION__);
  1176. vlsi_ring_debug(idev->tx_ring);
  1177. if (netif_running(ndev))
  1178. netif_stop_queue(ndev);
  1179. vlsi_stop_hw(idev);
  1180. /* now simply restart the whole thing */
  1181. if (!idev->new_baud)
  1182. idev->new_baud = idev->baud; /* keep current baudrate */
  1183. if (vlsi_start_hw(idev))
  1184. IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
  1185. __FUNCTION__, PCIDEV_NAME(idev->pdev), ndev->name);
  1186. else
  1187. netif_start_queue(ndev);
  1188. }
  1189. static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1190. {
  1191. vlsi_irda_dev_t *idev = ndev->priv;
  1192. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1193. unsigned long flags;
  1194. u16 fifocnt;
  1195. int ret = 0;
  1196. switch (cmd) {
  1197. case SIOCSBANDWIDTH:
  1198. if (!capable(CAP_NET_ADMIN)) {
  1199. ret = -EPERM;
  1200. break;
  1201. }
  1202. spin_lock_irqsave(&idev->lock, flags);
  1203. idev->new_baud = irq->ifr_baudrate;
  1204. /* when called from userland there might be a minor race window here
  1205. * if the stack tries to change speed concurrently - which would be
  1206. * pretty strange anyway with the userland having full control...
  1207. */
  1208. vlsi_set_baud(idev, ndev->base_addr);
  1209. spin_unlock_irqrestore(&idev->lock, flags);
  1210. break;
  1211. case SIOCSMEDIABUSY:
  1212. if (!capable(CAP_NET_ADMIN)) {
  1213. ret = -EPERM;
  1214. break;
  1215. }
  1216. irda_device_set_media_busy(ndev, TRUE);
  1217. break;
  1218. case SIOCGRECEIVING:
  1219. /* the best we can do: check whether there are any bytes in rx fifo.
  1220. * The trustable window (in case some data arrives just afterwards)
  1221. * may be as short as 1usec or so at 4Mbps.
  1222. */
  1223. fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  1224. irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
  1225. break;
  1226. default:
  1227. IRDA_WARNING("%s: notsupp - cmd=%04x\n",
  1228. __FUNCTION__, cmd);
  1229. ret = -EOPNOTSUPP;
  1230. }
  1231. return ret;
  1232. }
  1233. /********************************************************/
  1234. static irqreturn_t vlsi_interrupt(int irq, void *dev_instance,
  1235. struct pt_regs *regs)
  1236. {
  1237. struct net_device *ndev = dev_instance;
  1238. vlsi_irda_dev_t *idev = ndev->priv;
  1239. unsigned iobase;
  1240. u8 irintr;
  1241. int boguscount = 5;
  1242. unsigned long flags;
  1243. int handled = 0;
  1244. iobase = ndev->base_addr;
  1245. spin_lock_irqsave(&idev->lock,flags);
  1246. do {
  1247. irintr = inb(iobase+VLSI_PIO_IRINTR);
  1248. mb();
  1249. outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
  1250. if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
  1251. break;
  1252. handled = 1;
  1253. if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
  1254. break; /* nothing todo if only activity */
  1255. if (irintr&IRINTR_RPKTINT)
  1256. vlsi_rx_interrupt(ndev);
  1257. if (irintr&IRINTR_TPKTINT)
  1258. vlsi_tx_interrupt(ndev);
  1259. } while (--boguscount > 0);
  1260. spin_unlock_irqrestore(&idev->lock,flags);
  1261. if (boguscount <= 0)
  1262. IRDA_MESSAGE("%s: too much work in interrupt!\n",
  1263. __FUNCTION__);
  1264. return IRQ_RETVAL(handled);
  1265. }
  1266. /********************************************************/
  1267. static int vlsi_open(struct net_device *ndev)
  1268. {
  1269. vlsi_irda_dev_t *idev = ndev->priv;
  1270. int err = -EAGAIN;
  1271. char hwname[32];
  1272. if (pci_request_regions(idev->pdev, drivername)) {
  1273. IRDA_WARNING("%s: io resource busy\n", __FUNCTION__);
  1274. goto errout;
  1275. }
  1276. ndev->base_addr = pci_resource_start(idev->pdev,0);
  1277. ndev->irq = idev->pdev->irq;
  1278. /* under some rare occasions the chip apparently comes up with
  1279. * IRQ's pending. We better w/c pending IRQ and disable them all
  1280. */
  1281. outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
  1282. if (request_irq(ndev->irq, vlsi_interrupt, SA_SHIRQ,
  1283. drivername, ndev)) {
  1284. IRDA_WARNING("%s: couldn't get IRQ: %d\n",
  1285. __FUNCTION__, ndev->irq);
  1286. goto errout_io;
  1287. }
  1288. if ((err = vlsi_create_hwif(idev)) != 0)
  1289. goto errout_irq;
  1290. sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
  1291. idev->irlap = irlap_open(ndev,&idev->qos,hwname);
  1292. if (!idev->irlap)
  1293. goto errout_free_ring;
  1294. do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
  1295. idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
  1296. if ((err = vlsi_start_hw(idev)) != 0)
  1297. goto errout_close_irlap;
  1298. netif_start_queue(ndev);
  1299. IRDA_MESSAGE("%s: device %s operational\n", __FUNCTION__, ndev->name);
  1300. return 0;
  1301. errout_close_irlap:
  1302. irlap_close(idev->irlap);
  1303. errout_free_ring:
  1304. vlsi_destroy_hwif(idev);
  1305. errout_irq:
  1306. free_irq(ndev->irq,ndev);
  1307. errout_io:
  1308. pci_release_regions(idev->pdev);
  1309. errout:
  1310. return err;
  1311. }
  1312. static int vlsi_close(struct net_device *ndev)
  1313. {
  1314. vlsi_irda_dev_t *idev = ndev->priv;
  1315. netif_stop_queue(ndev);
  1316. if (idev->irlap)
  1317. irlap_close(idev->irlap);
  1318. idev->irlap = NULL;
  1319. vlsi_stop_hw(idev);
  1320. vlsi_destroy_hwif(idev);
  1321. free_irq(ndev->irq,ndev);
  1322. pci_release_regions(idev->pdev);
  1323. IRDA_MESSAGE("%s: device %s stopped\n", __FUNCTION__, ndev->name);
  1324. return 0;
  1325. }
  1326. static int vlsi_irda_init(struct net_device *ndev)
  1327. {
  1328. vlsi_irda_dev_t *idev = ndev->priv;
  1329. struct pci_dev *pdev = idev->pdev;
  1330. SET_MODULE_OWNER(ndev);
  1331. ndev->irq = pdev->irq;
  1332. ndev->base_addr = pci_resource_start(pdev,0);
  1333. /* PCI busmastering
  1334. * see include file for details why we need these 2 masks, in this order!
  1335. */
  1336. if (pci_set_dma_mask(pdev,DMA_MASK_USED_BY_HW)
  1337. || pci_set_dma_mask(pdev,DMA_MASK_MSTRPAGE)) {
  1338. IRDA_ERROR("%s: aborting due to PCI BM-DMA address limitations\n", __FUNCTION__);
  1339. return -1;
  1340. }
  1341. irda_init_max_qos_capabilies(&idev->qos);
  1342. /* the VLSI82C147 does not support 576000! */
  1343. idev->qos.baud_rate.bits = IR_2400 | IR_9600
  1344. | IR_19200 | IR_38400 | IR_57600 | IR_115200
  1345. | IR_1152000 | (IR_4000000 << 8);
  1346. idev->qos.min_turn_time.bits = qos_mtt_bits;
  1347. irda_qos_bits_to_value(&idev->qos);
  1348. /* currently no public media definitions for IrDA */
  1349. ndev->flags |= IFF_PORTSEL | IFF_AUTOMEDIA;
  1350. ndev->if_port = IF_PORT_UNKNOWN;
  1351. ndev->open = vlsi_open;
  1352. ndev->stop = vlsi_close;
  1353. ndev->get_stats = vlsi_get_stats;
  1354. ndev->hard_start_xmit = vlsi_hard_start_xmit;
  1355. ndev->do_ioctl = vlsi_ioctl;
  1356. ndev->tx_timeout = vlsi_tx_timeout;
  1357. ndev->watchdog_timeo = 500*HZ/1000; /* max. allowed turn time for IrLAP */
  1358. SET_NETDEV_DEV(ndev, &pdev->dev);
  1359. return 0;
  1360. }
  1361. /**************************************************************/
  1362. static int __devinit
  1363. vlsi_irda_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1364. {
  1365. struct net_device *ndev;
  1366. vlsi_irda_dev_t *idev;
  1367. if (pci_enable_device(pdev))
  1368. goto out;
  1369. else
  1370. pdev->current_state = 0; /* hw must be running now */
  1371. IRDA_MESSAGE("%s: IrDA PCI controller %s detected\n",
  1372. drivername, PCIDEV_NAME(pdev));
  1373. if ( !pci_resource_start(pdev,0)
  1374. || !(pci_resource_flags(pdev,0) & IORESOURCE_IO) ) {
  1375. IRDA_ERROR("%s: bar 0 invalid", __FUNCTION__);
  1376. goto out_disable;
  1377. }
  1378. ndev = alloc_irdadev(sizeof(*idev));
  1379. if (ndev==NULL) {
  1380. IRDA_ERROR("%s: Unable to allocate device memory.\n",
  1381. __FUNCTION__);
  1382. goto out_disable;
  1383. }
  1384. idev = ndev->priv;
  1385. spin_lock_init(&idev->lock);
  1386. init_MUTEX(&idev->sem);
  1387. down(&idev->sem);
  1388. idev->pdev = pdev;
  1389. if (vlsi_irda_init(ndev) < 0)
  1390. goto out_freedev;
  1391. if (register_netdev(ndev) < 0) {
  1392. IRDA_ERROR("%s: register_netdev failed\n", __FUNCTION__);
  1393. goto out_freedev;
  1394. }
  1395. if (vlsi_proc_root != NULL) {
  1396. struct proc_dir_entry *ent;
  1397. ent = create_proc_entry(ndev->name, S_IFREG|S_IRUGO, vlsi_proc_root);
  1398. if (!ent) {
  1399. IRDA_WARNING("%s: failed to create proc entry\n",
  1400. __FUNCTION__);
  1401. } else {
  1402. ent->data = ndev;
  1403. ent->proc_fops = VLSI_PROC_FOPS;
  1404. ent->size = 0;
  1405. }
  1406. idev->proc_entry = ent;
  1407. }
  1408. IRDA_MESSAGE("%s: registered device %s\n", drivername, ndev->name);
  1409. pci_set_drvdata(pdev, ndev);
  1410. up(&idev->sem);
  1411. return 0;
  1412. out_freedev:
  1413. up(&idev->sem);
  1414. free_netdev(ndev);
  1415. out_disable:
  1416. pci_disable_device(pdev);
  1417. out:
  1418. pci_set_drvdata(pdev, NULL);
  1419. return -ENODEV;
  1420. }
  1421. static void __devexit vlsi_irda_remove(struct pci_dev *pdev)
  1422. {
  1423. struct net_device *ndev = pci_get_drvdata(pdev);
  1424. vlsi_irda_dev_t *idev;
  1425. if (!ndev) {
  1426. IRDA_ERROR("%s: lost netdevice?\n", drivername);
  1427. return;
  1428. }
  1429. unregister_netdev(ndev);
  1430. idev = ndev->priv;
  1431. down(&idev->sem);
  1432. if (idev->proc_entry) {
  1433. remove_proc_entry(ndev->name, vlsi_proc_root);
  1434. idev->proc_entry = NULL;
  1435. }
  1436. up(&idev->sem);
  1437. free_netdev(ndev);
  1438. pci_set_drvdata(pdev, NULL);
  1439. IRDA_MESSAGE("%s: %s removed\n", drivername, PCIDEV_NAME(pdev));
  1440. }
  1441. #ifdef CONFIG_PM
  1442. /* The Controller doesn't provide PCI PM capabilities as defined by PCI specs.
  1443. * Some of the Linux PCI-PM code however depends on this, for example in
  1444. * pci_set_power_state(). So we have to take care to perform the required
  1445. * operations on our own (particularly reflecting the pdev->current_state)
  1446. * otherwise we might get cheated by pci-pm.
  1447. */
  1448. static int vlsi_irda_suspend(struct pci_dev *pdev, pm_message_t state)
  1449. {
  1450. struct net_device *ndev = pci_get_drvdata(pdev);
  1451. vlsi_irda_dev_t *idev;
  1452. if (!ndev) {
  1453. IRDA_ERROR("%s - %s: no netdevice \n",
  1454. __FUNCTION__, PCIDEV_NAME(pdev));
  1455. return 0;
  1456. }
  1457. idev = ndev->priv;
  1458. down(&idev->sem);
  1459. if (pdev->current_state != 0) { /* already suspended */
  1460. if (state.event > pdev->current_state) { /* simply go deeper */
  1461. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1462. pdev->current_state = state.event;
  1463. }
  1464. else
  1465. IRDA_ERROR("%s - %s: invalid suspend request %u -> %u\n", __FUNCTION__, PCIDEV_NAME(pdev), pdev->current_state, state.event);
  1466. up(&idev->sem);
  1467. return 0;
  1468. }
  1469. if (netif_running(ndev)) {
  1470. netif_device_detach(ndev);
  1471. vlsi_stop_hw(idev);
  1472. pci_save_state(pdev);
  1473. if (!idev->new_baud)
  1474. /* remember speed settings to restore on resume */
  1475. idev->new_baud = idev->baud;
  1476. }
  1477. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1478. pdev->current_state = state.event;
  1479. idev->resume_ok = 1;
  1480. up(&idev->sem);
  1481. return 0;
  1482. }
  1483. static int vlsi_irda_resume(struct pci_dev *pdev)
  1484. {
  1485. struct net_device *ndev = pci_get_drvdata(pdev);
  1486. vlsi_irda_dev_t *idev;
  1487. if (!ndev) {
  1488. IRDA_ERROR("%s - %s: no netdevice \n",
  1489. __FUNCTION__, PCIDEV_NAME(pdev));
  1490. return 0;
  1491. }
  1492. idev = ndev->priv;
  1493. down(&idev->sem);
  1494. if (pdev->current_state == 0) {
  1495. up(&idev->sem);
  1496. IRDA_WARNING("%s - %s: already resumed\n",
  1497. __FUNCTION__, PCIDEV_NAME(pdev));
  1498. return 0;
  1499. }
  1500. pci_set_power_state(pdev, PCI_D0);
  1501. pdev->current_state = PM_EVENT_ON;
  1502. if (!idev->resume_ok) {
  1503. /* should be obsolete now - but used to happen due to:
  1504. * - pci layer initially setting pdev->current_state = 4 (unknown)
  1505. * - pci layer did not walk the save_state-tree (might be APM problem)
  1506. * so we could not refuse to suspend from undefined state
  1507. * - vlsi_irda_suspend detected invalid state and refused to save
  1508. * configuration for resume - but was too late to stop suspending
  1509. * - vlsi_irda_resume got screwed when trying to resume from garbage
  1510. *
  1511. * now we explicitly set pdev->current_state = 0 after enabling the
  1512. * device and independently resume_ok should catch any garbage config.
  1513. */
  1514. IRDA_WARNING("%s - hm, nothing to resume?\n", __FUNCTION__);
  1515. up(&idev->sem);
  1516. return 0;
  1517. }
  1518. if (netif_running(ndev)) {
  1519. pci_restore_state(pdev);
  1520. vlsi_start_hw(idev);
  1521. netif_device_attach(ndev);
  1522. }
  1523. idev->resume_ok = 0;
  1524. up(&idev->sem);
  1525. return 0;
  1526. }
  1527. #endif /* CONFIG_PM */
  1528. /*********************************************************/
  1529. static struct pci_driver vlsi_irda_driver = {
  1530. .name = drivername,
  1531. .id_table = vlsi_irda_table,
  1532. .probe = vlsi_irda_probe,
  1533. .remove = __devexit_p(vlsi_irda_remove),
  1534. #ifdef CONFIG_PM
  1535. .suspend = vlsi_irda_suspend,
  1536. .resume = vlsi_irda_resume,
  1537. #endif
  1538. };
  1539. #define PROC_DIR ("driver/" DRIVER_NAME)
  1540. static int __init vlsi_mod_init(void)
  1541. {
  1542. int i, ret;
  1543. if (clksrc < 0 || clksrc > 3) {
  1544. IRDA_ERROR("%s: invalid clksrc=%d\n", drivername, clksrc);
  1545. return -1;
  1546. }
  1547. for (i = 0; i < 2; i++) {
  1548. switch(ringsize[i]) {
  1549. case 4:
  1550. case 8:
  1551. case 16:
  1552. case 32:
  1553. case 64:
  1554. break;
  1555. default:
  1556. IRDA_WARNING("%s: invalid %s ringsize %d, using default=8", drivername, (i)?"rx":"tx", ringsize[i]);
  1557. ringsize[i] = 8;
  1558. break;
  1559. }
  1560. }
  1561. sirpulse = !!sirpulse;
  1562. /* create_proc_entry returns NULL if !CONFIG_PROC_FS.
  1563. * Failure to create the procfs entry is handled like running
  1564. * without procfs - it's not required for the driver to work.
  1565. */
  1566. vlsi_proc_root = create_proc_entry(PROC_DIR, S_IFDIR, NULL);
  1567. if (vlsi_proc_root) {
  1568. /* protect registered procdir against module removal.
  1569. * Because we are in the module init path there's no race
  1570. * window after create_proc_entry (and no barrier needed).
  1571. */
  1572. vlsi_proc_root->owner = THIS_MODULE;
  1573. }
  1574. ret = pci_module_init(&vlsi_irda_driver);
  1575. if (ret && vlsi_proc_root)
  1576. remove_proc_entry(PROC_DIR, NULL);
  1577. return ret;
  1578. }
  1579. static void __exit vlsi_mod_exit(void)
  1580. {
  1581. pci_unregister_driver(&vlsi_irda_driver);
  1582. if (vlsi_proc_root)
  1583. remove_proc_entry(PROC_DIR, NULL);
  1584. }
  1585. module_init(vlsi_mod_init);
  1586. module_exit(vlsi_mod_exit);