vlsi_ir.c 51 KB

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