vlsi_ir.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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/interrupt.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/mutex.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 DEFINE_PCI_DEVICE_TABLE(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. __func__, r, r->size, r->mask, r->len, r->dir, r->rd[0].hw);
  120. printk(KERN_DEBUG "%s - head = %d / tail = %d\n", __func__,
  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: ", __func__, 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. __func__, (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. pci_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 = netdev_priv(ndev);
  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. ndev->stats.rx_packets, ndev->stats.rx_bytes, ndev->stats.rx_errors,
  258. ndev->stats.rx_dropped);
  259. seq_printf(seq, " / overrun=%lu / length=%lu / frame=%lu / crc=%lu\n",
  260. ndev->stats.rx_over_errors, ndev->stats.rx_length_errors,
  261. ndev->stats.rx_frame_errors, ndev->stats.rx_crc_errors);
  262. seq_printf(seq, "TX: packets=%lu / bytes=%lu / errors=%lu / dropped=%lu / fifo=%lu\n",
  263. ndev->stats.tx_packets, ndev->stats.tx_bytes, ndev->stats.tx_errors,
  264. ndev->stats.tx_dropped, ndev->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 = netdev_priv(ndev);
  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 const 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. __func__, rd->buf);
  384. kfree(rd->buf);
  385. rd->buf = NULL;
  386. }
  387. for (j = 0; j < i; j++) {
  388. rd = r->rd + j;
  389. busaddr = rd_get_addr(rd);
  390. rd_set_addr_status(rd, 0, 0);
  391. if (busaddr)
  392. pci_unmap_single(pdev, busaddr, len, dir);
  393. kfree(rd->buf);
  394. rd->buf = NULL;
  395. }
  396. kfree(r);
  397. return NULL;
  398. }
  399. rd_set_addr_status(rd, busaddr, 0);
  400. /* initially, the dma buffer is owned by the CPU */
  401. rd->skb = NULL;
  402. }
  403. return r;
  404. }
  405. static int vlsi_free_ring(struct vlsi_ring *r)
  406. {
  407. struct ring_descr *rd;
  408. unsigned i;
  409. dma_addr_t busaddr;
  410. for (i = 0; i < r->size; i++) {
  411. rd = r->rd + i;
  412. if (rd->skb)
  413. dev_kfree_skb_any(rd->skb);
  414. busaddr = rd_get_addr(rd);
  415. rd_set_addr_status(rd, 0, 0);
  416. if (busaddr)
  417. pci_unmap_single(r->pdev, busaddr, r->len, r->dir);
  418. kfree(rd->buf);
  419. }
  420. kfree(r);
  421. return 0;
  422. }
  423. static int vlsi_create_hwif(vlsi_irda_dev_t *idev)
  424. {
  425. char *ringarea;
  426. struct ring_descr_hw *hwmap;
  427. idev->virtaddr = NULL;
  428. idev->busaddr = 0;
  429. ringarea = pci_alloc_consistent(idev->pdev, HW_RING_AREA_SIZE, &idev->busaddr);
  430. if (!ringarea) {
  431. IRDA_ERROR("%s: insufficient memory for descriptor rings\n",
  432. __func__);
  433. goto out;
  434. }
  435. memset(ringarea, 0, HW_RING_AREA_SIZE);
  436. hwmap = (struct ring_descr_hw *)ringarea;
  437. idev->rx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[1],
  438. XFER_BUF_SIZE, PCI_DMA_FROMDEVICE);
  439. if (idev->rx_ring == NULL)
  440. goto out_unmap;
  441. hwmap += MAX_RING_DESCR;
  442. idev->tx_ring = vlsi_alloc_ring(idev->pdev, hwmap, ringsize[0],
  443. XFER_BUF_SIZE, PCI_DMA_TODEVICE);
  444. if (idev->tx_ring == NULL)
  445. goto out_free_rx;
  446. idev->virtaddr = ringarea;
  447. return 0;
  448. out_free_rx:
  449. vlsi_free_ring(idev->rx_ring);
  450. out_unmap:
  451. idev->rx_ring = idev->tx_ring = NULL;
  452. pci_free_consistent(idev->pdev, HW_RING_AREA_SIZE, ringarea, idev->busaddr);
  453. idev->busaddr = 0;
  454. out:
  455. return -ENOMEM;
  456. }
  457. static int vlsi_destroy_hwif(vlsi_irda_dev_t *idev)
  458. {
  459. vlsi_free_ring(idev->rx_ring);
  460. vlsi_free_ring(idev->tx_ring);
  461. idev->rx_ring = idev->tx_ring = NULL;
  462. if (idev->busaddr)
  463. pci_free_consistent(idev->pdev,HW_RING_AREA_SIZE,idev->virtaddr,idev->busaddr);
  464. idev->virtaddr = NULL;
  465. idev->busaddr = 0;
  466. return 0;
  467. }
  468. /********************************************************/
  469. static int vlsi_process_rx(struct vlsi_ring *r, struct ring_descr *rd)
  470. {
  471. u16 status;
  472. int crclen, len = 0;
  473. struct sk_buff *skb;
  474. int ret = 0;
  475. struct net_device *ndev = (struct net_device *)pci_get_drvdata(r->pdev);
  476. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  477. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  478. /* dma buffer now owned by the CPU */
  479. status = rd_get_status(rd);
  480. if (status & RD_RX_ERROR) {
  481. if (status & RD_RX_OVER)
  482. ret |= VLSI_RX_OVER;
  483. if (status & RD_RX_LENGTH)
  484. ret |= VLSI_RX_LENGTH;
  485. if (status & RD_RX_PHYERR)
  486. ret |= VLSI_RX_FRAME;
  487. if (status & RD_RX_CRCERR)
  488. ret |= VLSI_RX_CRC;
  489. goto done;
  490. }
  491. len = rd_get_count(rd);
  492. crclen = (idev->mode==IFF_FIR) ? sizeof(u32) : sizeof(u16);
  493. len -= crclen; /* remove trailing CRC */
  494. if (len <= 0) {
  495. IRDA_DEBUG(0, "%s: strange frame (len=%d)\n", __func__, len);
  496. ret |= VLSI_RX_DROP;
  497. goto done;
  498. }
  499. if (idev->mode == IFF_SIR) { /* hw checks CRC in MIR, FIR mode */
  500. /* rd->buf is a streaming PCI_DMA_FROMDEVICE map. Doing the
  501. * endian-adjustment there just in place will dirty a cache line
  502. * which belongs to the map and thus we must be sure it will
  503. * get flushed before giving the buffer back to hardware.
  504. * vlsi_fill_rx() will do this anyway - but here we rely on.
  505. */
  506. le16_to_cpus(rd->buf+len);
  507. if (irda_calc_crc16(INIT_FCS,rd->buf,len+crclen) != GOOD_FCS) {
  508. IRDA_DEBUG(0, "%s: crc error\n", __func__);
  509. ret |= VLSI_RX_CRC;
  510. goto done;
  511. }
  512. }
  513. if (!rd->skb) {
  514. IRDA_WARNING("%s: rx packet lost\n", __func__);
  515. ret |= VLSI_RX_DROP;
  516. goto done;
  517. }
  518. skb = rd->skb;
  519. rd->skb = NULL;
  520. skb->dev = ndev;
  521. memcpy(skb_put(skb,len), rd->buf, len);
  522. skb_reset_mac_header(skb);
  523. if (in_interrupt())
  524. netif_rx(skb);
  525. else
  526. netif_rx_ni(skb);
  527. done:
  528. rd_set_status(rd, 0);
  529. rd_set_count(rd, 0);
  530. /* buffer still owned by CPU */
  531. return (ret) ? -ret : len;
  532. }
  533. static void vlsi_fill_rx(struct vlsi_ring *r)
  534. {
  535. struct ring_descr *rd;
  536. for (rd = ring_last(r); rd != NULL; rd = ring_put(r)) {
  537. if (rd_is_active(rd)) {
  538. IRDA_WARNING("%s: driver bug: rx descr race with hw\n",
  539. __func__);
  540. vlsi_ring_debug(r);
  541. break;
  542. }
  543. if (!rd->skb) {
  544. rd->skb = dev_alloc_skb(IRLAP_SKB_ALLOCSIZE);
  545. if (rd->skb) {
  546. skb_reserve(rd->skb,1);
  547. rd->skb->protocol = htons(ETH_P_IRDA);
  548. }
  549. else
  550. break; /* probably not worth logging? */
  551. }
  552. /* give dma buffer back to busmaster */
  553. pci_dma_sync_single_for_device(r->pdev, rd_get_addr(rd), r->len, r->dir);
  554. rd_activate(rd);
  555. }
  556. }
  557. static void vlsi_rx_interrupt(struct net_device *ndev)
  558. {
  559. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  560. struct vlsi_ring *r = idev->rx_ring;
  561. struct ring_descr *rd;
  562. int ret;
  563. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  564. if (rd_is_active(rd))
  565. break;
  566. ret = vlsi_process_rx(r, rd);
  567. if (ret < 0) {
  568. ret = -ret;
  569. ndev->stats.rx_errors++;
  570. if (ret & VLSI_RX_DROP)
  571. ndev->stats.rx_dropped++;
  572. if (ret & VLSI_RX_OVER)
  573. ndev->stats.rx_over_errors++;
  574. if (ret & VLSI_RX_LENGTH)
  575. ndev->stats.rx_length_errors++;
  576. if (ret & VLSI_RX_FRAME)
  577. ndev->stats.rx_frame_errors++;
  578. if (ret & VLSI_RX_CRC)
  579. ndev->stats.rx_crc_errors++;
  580. }
  581. else if (ret > 0) {
  582. ndev->stats.rx_packets++;
  583. ndev->stats.rx_bytes += ret;
  584. }
  585. }
  586. do_gettimeofday(&idev->last_rx); /* remember "now" for later mtt delay */
  587. vlsi_fill_rx(r);
  588. if (ring_first(r) == NULL) {
  589. /* we are in big trouble, if this should ever happen */
  590. IRDA_ERROR("%s: rx ring exhausted!\n", __func__);
  591. vlsi_ring_debug(r);
  592. }
  593. else
  594. outw(0, ndev->base_addr+VLSI_PIO_PROMPT);
  595. }
  596. /* caller must have stopped the controller from busmastering */
  597. static void vlsi_unarm_rx(vlsi_irda_dev_t *idev)
  598. {
  599. struct net_device *ndev = pci_get_drvdata(idev->pdev);
  600. struct vlsi_ring *r = idev->rx_ring;
  601. struct ring_descr *rd;
  602. int ret;
  603. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  604. ret = 0;
  605. if (rd_is_active(rd)) {
  606. rd_set_status(rd, 0);
  607. if (rd_get_count(rd)) {
  608. IRDA_DEBUG(0, "%s - dropping rx packet\n", __func__);
  609. ret = -VLSI_RX_DROP;
  610. }
  611. rd_set_count(rd, 0);
  612. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  613. if (rd->skb) {
  614. dev_kfree_skb_any(rd->skb);
  615. rd->skb = NULL;
  616. }
  617. }
  618. else
  619. ret = vlsi_process_rx(r, rd);
  620. if (ret < 0) {
  621. ret = -ret;
  622. ndev->stats.rx_errors++;
  623. if (ret & VLSI_RX_DROP)
  624. ndev->stats.rx_dropped++;
  625. if (ret & VLSI_RX_OVER)
  626. ndev->stats.rx_over_errors++;
  627. if (ret & VLSI_RX_LENGTH)
  628. ndev->stats.rx_length_errors++;
  629. if (ret & VLSI_RX_FRAME)
  630. ndev->stats.rx_frame_errors++;
  631. if (ret & VLSI_RX_CRC)
  632. ndev->stats.rx_crc_errors++;
  633. }
  634. else if (ret > 0) {
  635. ndev->stats.rx_packets++;
  636. ndev->stats.rx_bytes += ret;
  637. }
  638. }
  639. }
  640. /********************************************************/
  641. static int vlsi_process_tx(struct vlsi_ring *r, struct ring_descr *rd)
  642. {
  643. u16 status;
  644. int len;
  645. int ret;
  646. pci_dma_sync_single_for_cpu(r->pdev, rd_get_addr(rd), r->len, r->dir);
  647. /* dma buffer now owned by the CPU */
  648. status = rd_get_status(rd);
  649. if (status & RD_TX_UNDRN)
  650. ret = VLSI_TX_FIFO;
  651. else
  652. ret = 0;
  653. rd_set_status(rd, 0);
  654. if (rd->skb) {
  655. len = rd->skb->len;
  656. dev_kfree_skb_any(rd->skb);
  657. rd->skb = NULL;
  658. }
  659. else /* tx-skb already freed? - should never happen */
  660. len = rd_get_count(rd); /* incorrect for SIR! (due to wrapping) */
  661. rd_set_count(rd, 0);
  662. /* dma buffer still owned by the CPU */
  663. return (ret) ? -ret : len;
  664. }
  665. static int vlsi_set_baud(vlsi_irda_dev_t *idev, unsigned iobase)
  666. {
  667. u16 nphyctl;
  668. u16 config;
  669. unsigned mode;
  670. int ret;
  671. int baudrate;
  672. int fifocnt;
  673. baudrate = idev->new_baud;
  674. IRDA_DEBUG(2, "%s: %d -> %d\n", __func__, idev->baud, idev->new_baud);
  675. if (baudrate == 4000000) {
  676. mode = IFF_FIR;
  677. config = IRCFG_FIR;
  678. nphyctl = PHYCTL_FIR;
  679. }
  680. else if (baudrate == 1152000) {
  681. mode = IFF_MIR;
  682. config = IRCFG_MIR | IRCFG_CRC16;
  683. nphyctl = PHYCTL_MIR(clksrc==3);
  684. }
  685. else {
  686. mode = IFF_SIR;
  687. config = IRCFG_SIR | IRCFG_SIRFILT | IRCFG_RXANY;
  688. switch(baudrate) {
  689. default:
  690. IRDA_WARNING("%s: undefined baudrate %d - fallback to 9600!\n",
  691. __func__, baudrate);
  692. baudrate = 9600;
  693. /* fallthru */
  694. case 2400:
  695. case 9600:
  696. case 19200:
  697. case 38400:
  698. case 57600:
  699. case 115200:
  700. nphyctl = PHYCTL_SIR(baudrate,sirpulse,clksrc==3);
  701. break;
  702. }
  703. }
  704. config |= IRCFG_MSTR | IRCFG_ENRX;
  705. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  706. if (fifocnt != 0) {
  707. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n", __func__, fifocnt);
  708. }
  709. outw(0, iobase+VLSI_PIO_IRENABLE);
  710. outw(config, iobase+VLSI_PIO_IRCFG);
  711. outw(nphyctl, iobase+VLSI_PIO_NPHYCTL);
  712. wmb();
  713. outw(IRENABLE_PHYANDCLOCK, iobase+VLSI_PIO_IRENABLE);
  714. mb();
  715. udelay(1); /* chip applies IRCFG on next rising edge of its 8MHz clock */
  716. /* read back settings for validation */
  717. config = inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_MASK;
  718. if (mode == IFF_FIR)
  719. config ^= IRENABLE_FIR_ON;
  720. else if (mode == IFF_MIR)
  721. config ^= (IRENABLE_MIR_ON|IRENABLE_CRC16_ON);
  722. else
  723. config ^= IRENABLE_SIR_ON;
  724. if (config != (IRENABLE_PHYANDCLOCK|IRENABLE_ENRXST)) {
  725. IRDA_WARNING("%s: failed to set %s mode!\n", __func__,
  726. (mode==IFF_SIR)?"SIR":((mode==IFF_MIR)?"MIR":"FIR"));
  727. ret = -1;
  728. }
  729. else {
  730. if (inw(iobase+VLSI_PIO_PHYCTL) != nphyctl) {
  731. IRDA_WARNING("%s: failed to apply baudrate %d\n",
  732. __func__, baudrate);
  733. ret = -1;
  734. }
  735. else {
  736. idev->mode = mode;
  737. idev->baud = baudrate;
  738. idev->new_baud = 0;
  739. ret = 0;
  740. }
  741. }
  742. if (ret)
  743. vlsi_reg_debug(iobase,__func__);
  744. return ret;
  745. }
  746. static netdev_tx_t vlsi_hard_start_xmit(struct sk_buff *skb,
  747. struct net_device *ndev)
  748. {
  749. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  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 NETDEV_TX_OK;
  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 - called under netif_tx_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. __func__);
  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. skb_copy_from_linear_data(skb, rd->buf, 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", __func__, 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. if (ring_put(r) == NULL) {
  904. netif_stop_queue(ndev);
  905. IRDA_DEBUG(3, "%s: tx ring full - queue stopped\n", __func__);
  906. }
  907. spin_unlock_irqrestore(&idev->lock, flags);
  908. return NETDEV_TX_OK;
  909. drop_unlock:
  910. spin_unlock_irqrestore(&idev->lock, flags);
  911. drop:
  912. IRDA_WARNING("%s: dropping packet - %s\n", __func__, msg);
  913. dev_kfree_skb_any(skb);
  914. ndev->stats.tx_errors++;
  915. ndev->stats.tx_dropped++;
  916. /* Don't even think about returning NET_XMIT_DROP (=1) here!
  917. * In fact any retval!=0 causes the packet scheduler to requeue the
  918. * packet for later retry of transmission - which isn't exactly
  919. * what we want after we've just called dev_kfree_skb_any ;-)
  920. */
  921. return NETDEV_TX_OK;
  922. }
  923. static void vlsi_tx_interrupt(struct net_device *ndev)
  924. {
  925. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  926. struct vlsi_ring *r = idev->tx_ring;
  927. struct ring_descr *rd;
  928. unsigned iobase;
  929. int ret;
  930. u16 config;
  931. for (rd = ring_first(r); rd != NULL; rd = ring_get(r)) {
  932. if (rd_is_active(rd))
  933. break;
  934. ret = vlsi_process_tx(r, rd);
  935. if (ret < 0) {
  936. ret = -ret;
  937. ndev->stats.tx_errors++;
  938. if (ret & VLSI_TX_DROP)
  939. ndev->stats.tx_dropped++;
  940. if (ret & VLSI_TX_FIFO)
  941. ndev->stats.tx_fifo_errors++;
  942. }
  943. else if (ret > 0){
  944. ndev->stats.tx_packets++;
  945. ndev->stats.tx_bytes += ret;
  946. }
  947. }
  948. iobase = ndev->base_addr;
  949. if (idev->new_baud && rd == NULL) /* tx ring empty and speed change pending */
  950. vlsi_set_baud(idev, iobase);
  951. config = inw(iobase+VLSI_PIO_IRCFG);
  952. if (rd == NULL) /* tx ring empty: re-enable rx */
  953. outw((config & ~IRCFG_ENTX) | IRCFG_ENRX, iobase+VLSI_PIO_IRCFG);
  954. else if (!(inw(iobase+VLSI_PIO_IRENABLE) & IRENABLE_ENTXST)) {
  955. int fifocnt;
  956. fifocnt = inw(iobase+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  957. if (fifocnt != 0) {
  958. IRDA_DEBUG(0, "%s: rx fifo not empty(%d)\n",
  959. __func__, fifocnt);
  960. }
  961. outw(config | IRCFG_ENTX, iobase+VLSI_PIO_IRCFG);
  962. }
  963. outw(0, iobase+VLSI_PIO_PROMPT);
  964. if (netif_queue_stopped(ndev) && !idev->new_baud) {
  965. netif_wake_queue(ndev);
  966. IRDA_DEBUG(3, "%s: queue awoken\n", __func__);
  967. }
  968. }
  969. /* caller must have stopped the controller from busmastering */
  970. static void vlsi_unarm_tx(vlsi_irda_dev_t *idev)
  971. {
  972. struct net_device *ndev = pci_get_drvdata(idev->pdev);
  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", __func__);
  987. ret = -VLSI_TX_DROP;
  988. }
  989. else
  990. ret = vlsi_process_tx(r, rd);
  991. if (ret < 0) {
  992. ret = -ret;
  993. ndev->stats.tx_errors++;
  994. if (ret & VLSI_TX_DROP)
  995. ndev->stats.tx_dropped++;
  996. if (ret & VLSI_TX_FIFO)
  997. ndev->stats.tx_fifo_errors++;
  998. }
  999. else if (ret > 0){
  1000. ndev->stats.tx_packets++;
  1001. ndev->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. __func__);
  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. __func__, 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 = netdev_priv(ndev);
  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", __func__);
  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 void vlsi_tx_timeout(struct net_device *ndev)
  1168. {
  1169. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1170. vlsi_reg_debug(ndev->base_addr, __func__);
  1171. vlsi_ring_debug(idev->tx_ring);
  1172. if (netif_running(ndev))
  1173. netif_stop_queue(ndev);
  1174. vlsi_stop_hw(idev);
  1175. /* now simply restart the whole thing */
  1176. if (!idev->new_baud)
  1177. idev->new_baud = idev->baud; /* keep current baudrate */
  1178. if (vlsi_start_hw(idev))
  1179. IRDA_ERROR("%s: failed to restart hw - %s(%s) unusable!\n",
  1180. __func__, pci_name(idev->pdev), ndev->name);
  1181. else
  1182. netif_start_queue(ndev);
  1183. }
  1184. static int vlsi_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
  1185. {
  1186. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1187. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1188. unsigned long flags;
  1189. u16 fifocnt;
  1190. int ret = 0;
  1191. switch (cmd) {
  1192. case SIOCSBANDWIDTH:
  1193. if (!capable(CAP_NET_ADMIN)) {
  1194. ret = -EPERM;
  1195. break;
  1196. }
  1197. spin_lock_irqsave(&idev->lock, flags);
  1198. idev->new_baud = irq->ifr_baudrate;
  1199. /* when called from userland there might be a minor race window here
  1200. * if the stack tries to change speed concurrently - which would be
  1201. * pretty strange anyway with the userland having full control...
  1202. */
  1203. vlsi_set_baud(idev, ndev->base_addr);
  1204. spin_unlock_irqrestore(&idev->lock, flags);
  1205. break;
  1206. case SIOCSMEDIABUSY:
  1207. if (!capable(CAP_NET_ADMIN)) {
  1208. ret = -EPERM;
  1209. break;
  1210. }
  1211. irda_device_set_media_busy(ndev, TRUE);
  1212. break;
  1213. case SIOCGRECEIVING:
  1214. /* the best we can do: check whether there are any bytes in rx fifo.
  1215. * The trustable window (in case some data arrives just afterwards)
  1216. * may be as short as 1usec or so at 4Mbps.
  1217. */
  1218. fifocnt = inw(ndev->base_addr+VLSI_PIO_RCVBCNT) & RCVBCNT_MASK;
  1219. irq->ifr_receiving = (fifocnt!=0) ? 1 : 0;
  1220. break;
  1221. default:
  1222. IRDA_WARNING("%s: notsupp - cmd=%04x\n",
  1223. __func__, cmd);
  1224. ret = -EOPNOTSUPP;
  1225. }
  1226. return ret;
  1227. }
  1228. /********************************************************/
  1229. static irqreturn_t vlsi_interrupt(int irq, void *dev_instance)
  1230. {
  1231. struct net_device *ndev = dev_instance;
  1232. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1233. unsigned iobase;
  1234. u8 irintr;
  1235. int boguscount = 5;
  1236. unsigned long flags;
  1237. int handled = 0;
  1238. iobase = ndev->base_addr;
  1239. spin_lock_irqsave(&idev->lock,flags);
  1240. do {
  1241. irintr = inb(iobase+VLSI_PIO_IRINTR);
  1242. mb();
  1243. outb(irintr, iobase+VLSI_PIO_IRINTR); /* acknowledge asap */
  1244. if (!(irintr&=IRINTR_INT_MASK)) /* not our INT - probably shared */
  1245. break;
  1246. handled = 1;
  1247. if (unlikely(!(irintr & ~IRINTR_ACTIVITY)))
  1248. break; /* nothing todo if only activity */
  1249. if (irintr&IRINTR_RPKTINT)
  1250. vlsi_rx_interrupt(ndev);
  1251. if (irintr&IRINTR_TPKTINT)
  1252. vlsi_tx_interrupt(ndev);
  1253. } while (--boguscount > 0);
  1254. spin_unlock_irqrestore(&idev->lock,flags);
  1255. if (boguscount <= 0)
  1256. IRDA_MESSAGE("%s: too much work in interrupt!\n",
  1257. __func__);
  1258. return IRQ_RETVAL(handled);
  1259. }
  1260. /********************************************************/
  1261. static int vlsi_open(struct net_device *ndev)
  1262. {
  1263. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1264. int err = -EAGAIN;
  1265. char hwname[32];
  1266. if (pci_request_regions(idev->pdev, drivername)) {
  1267. IRDA_WARNING("%s: io resource busy\n", __func__);
  1268. goto errout;
  1269. }
  1270. ndev->base_addr = pci_resource_start(idev->pdev,0);
  1271. ndev->irq = idev->pdev->irq;
  1272. /* under some rare occasions the chip apparently comes up with
  1273. * IRQ's pending. We better w/c pending IRQ and disable them all
  1274. */
  1275. outb(IRINTR_INT_MASK, ndev->base_addr+VLSI_PIO_IRINTR);
  1276. if (request_irq(ndev->irq, vlsi_interrupt, IRQF_SHARED,
  1277. drivername, ndev)) {
  1278. IRDA_WARNING("%s: couldn't get IRQ: %d\n",
  1279. __func__, ndev->irq);
  1280. goto errout_io;
  1281. }
  1282. if ((err = vlsi_create_hwif(idev)) != 0)
  1283. goto errout_irq;
  1284. sprintf(hwname, "VLSI-FIR @ 0x%04x", (unsigned)ndev->base_addr);
  1285. idev->irlap = irlap_open(ndev,&idev->qos,hwname);
  1286. if (!idev->irlap)
  1287. goto errout_free_ring;
  1288. do_gettimeofday(&idev->last_rx); /* first mtt may start from now on */
  1289. idev->new_baud = 9600; /* start with IrPHY using 9600(SIR) mode */
  1290. if ((err = vlsi_start_hw(idev)) != 0)
  1291. goto errout_close_irlap;
  1292. netif_start_queue(ndev);
  1293. IRDA_MESSAGE("%s: device %s operational\n", __func__, ndev->name);
  1294. return 0;
  1295. errout_close_irlap:
  1296. irlap_close(idev->irlap);
  1297. errout_free_ring:
  1298. vlsi_destroy_hwif(idev);
  1299. errout_irq:
  1300. free_irq(ndev->irq,ndev);
  1301. errout_io:
  1302. pci_release_regions(idev->pdev);
  1303. errout:
  1304. return err;
  1305. }
  1306. static int vlsi_close(struct net_device *ndev)
  1307. {
  1308. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1309. netif_stop_queue(ndev);
  1310. if (idev->irlap)
  1311. irlap_close(idev->irlap);
  1312. idev->irlap = NULL;
  1313. vlsi_stop_hw(idev);
  1314. vlsi_destroy_hwif(idev);
  1315. free_irq(ndev->irq,ndev);
  1316. pci_release_regions(idev->pdev);
  1317. IRDA_MESSAGE("%s: device %s stopped\n", __func__, ndev->name);
  1318. return 0;
  1319. }
  1320. static const struct net_device_ops vlsi_netdev_ops = {
  1321. .ndo_open = vlsi_open,
  1322. .ndo_stop = vlsi_close,
  1323. .ndo_start_xmit = vlsi_hard_start_xmit,
  1324. .ndo_do_ioctl = vlsi_ioctl,
  1325. .ndo_tx_timeout = vlsi_tx_timeout,
  1326. };
  1327. static int vlsi_irda_init(struct net_device *ndev)
  1328. {
  1329. vlsi_irda_dev_t *idev = netdev_priv(ndev);
  1330. struct pci_dev *pdev = idev->pdev;
  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", __func__);
  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->netdev_ops = &vlsi_netdev_ops;
  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. ret = pci_register_driver(&vlsi_irda_driver);
  1562. if (ret && vlsi_proc_root)
  1563. remove_proc_entry(PROC_DIR, NULL);
  1564. return ret;
  1565. }
  1566. static void __exit vlsi_mod_exit(void)
  1567. {
  1568. pci_unregister_driver(&vlsi_irda_driver);
  1569. if (vlsi_proc_root)
  1570. remove_proc_entry(PROC_DIR, NULL);
  1571. }
  1572. module_init(vlsi_mod_init);
  1573. module_exit(vlsi_mod_exit);