bpp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*
  2. * drivers/sbus/char/bpp.c
  3. *
  4. * Copyright (c) 1995 Picture Elements
  5. * Stephen Williams (steve@icarus.com)
  6. * Gus Baldauf (gbaldauf@ix.netcom.com)
  7. *
  8. * Linux/SPARC port by Peter Zaitcev.
  9. * Integration into SPARC tree by Tom Dyas.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/timer.h>
  19. #include <linux/ioport.h>
  20. #include <linux/major.h>
  21. #include <linux/devfs_fs_kernel.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/io.h>
  24. #if defined(__i386__)
  25. # include <asm/system.h>
  26. #endif
  27. #if defined(__sparc__)
  28. # include <linux/init.h>
  29. # include <linux/delay.h> /* udelay() */
  30. # include <asm/oplib.h> /* OpenProm Library */
  31. # include <asm/sbus.h>
  32. #endif
  33. #include <asm/bpp.h>
  34. #define BPP_PROBE_CODE 0x55
  35. #define BPP_DELAY 100
  36. static const unsigned BPP_MAJOR = LP_MAJOR;
  37. static const char* dev_name = "bpp";
  38. /* When switching from compatibility to a mode where I can read, try
  39. the following mode first. */
  40. /* const unsigned char DEFAULT_ECP = 0x10; */
  41. static const unsigned char DEFAULT_ECP = 0x30;
  42. static const unsigned char DEFAULT_NIBBLE = 0x00;
  43. /*
  44. * These are 1284 time constraints, in units of jiffies.
  45. */
  46. static const unsigned long TIME_PSetup = 1;
  47. static const unsigned long TIME_PResponse = 6;
  48. static const unsigned long TIME_IDLE_LIMIT = 2000;
  49. /*
  50. * One instance per supported subdevice...
  51. */
  52. # define BPP_NO 3
  53. enum IEEE_Mode { COMPATIBILITY, NIBBLE, ECP, ECP_RLE, EPP };
  54. struct inst {
  55. unsigned present : 1; /* True if the hardware exists */
  56. unsigned enhanced : 1; /* True if the hardware in "enhanced" */
  57. unsigned opened : 1; /* True if the device is opened already */
  58. unsigned run_flag : 1; /* True if waiting for a repeate byte */
  59. unsigned char direction; /* 0 --> out, 0x20 --> IN */
  60. unsigned char pp_state; /* State of host controlled pins. */
  61. enum IEEE_Mode mode;
  62. unsigned char run_length;
  63. unsigned char repeat_byte;
  64. };
  65. static struct inst instances[BPP_NO];
  66. #if defined(__i386__)
  67. static const unsigned short base_addrs[BPP_NO] = { 0x278, 0x378, 0x3bc };
  68. /*
  69. * These are for data access.
  70. * Control lines accesses are hidden in set_bits() and get_bits().
  71. * The exception is the probe procedure, which is system-dependent.
  72. */
  73. #define bpp_outb_p(data, base) outb_p((data), (base))
  74. #define bpp_inb(base) inb(base)
  75. #define bpp_inb_p(base) inb_p(base)
  76. /*
  77. * This method takes the pin values mask and sets the hardware pins to
  78. * the requested value: 1 == high voltage, 0 == low voltage. This
  79. * burries the annoying PC bit inversion and preserves the direction
  80. * flag.
  81. */
  82. static void set_pins(unsigned short pins, unsigned minor)
  83. {
  84. unsigned char bits = instances[minor].direction; /* == 0x20 */
  85. if (! (pins & BPP_PP_nStrobe)) bits |= 1;
  86. if (! (pins & BPP_PP_nAutoFd)) bits |= 2;
  87. if ( pins & BPP_PP_nInit) bits |= 4;
  88. if (! (pins & BPP_PP_nSelectIn)) bits |= 8;
  89. instances[minor].pp_state = bits;
  90. outb_p(bits, base_addrs[minor]+2);
  91. }
  92. static unsigned short get_pins(unsigned minor)
  93. {
  94. unsigned short bits = 0;
  95. unsigned value = instances[minor].pp_state;
  96. if (! (value & 0x01)) bits |= BPP_PP_nStrobe;
  97. if (! (value & 0x02)) bits |= BPP_PP_nAutoFd;
  98. if (value & 0x04) bits |= BPP_PP_nInit;
  99. if (! (value & 0x08)) bits |= BPP_PP_nSelectIn;
  100. value = inb_p(base_addrs[minor]+1);
  101. if (value & 0x08) bits |= BPP_GP_nFault;
  102. if (value & 0x10) bits |= BPP_GP_Select;
  103. if (value & 0x20) bits |= BPP_GP_PError;
  104. if (value & 0x40) bits |= BPP_GP_nAck;
  105. if (! (value & 0x80)) bits |= BPP_GP_Busy;
  106. return bits;
  107. }
  108. #endif /* __i386__ */
  109. #if defined(__sparc__)
  110. /*
  111. * Register block
  112. */
  113. /* DMA registers */
  114. #define BPP_CSR 0x00
  115. #define BPP_ADDR 0x04
  116. #define BPP_BCNT 0x08
  117. #define BPP_TST_CSR 0x0C
  118. /* Parallel Port registers */
  119. #define BPP_HCR 0x10
  120. #define BPP_OCR 0x12
  121. #define BPP_DR 0x14
  122. #define BPP_TCR 0x15
  123. #define BPP_OR 0x16
  124. #define BPP_IR 0x17
  125. #define BPP_ICR 0x18
  126. #define BPP_SIZE 0x1A
  127. /* BPP_CSR. Bits of type RW1 are cleared with writting '1'. */
  128. #define P_DEV_ID_MASK 0xf0000000 /* R */
  129. #define P_DEV_ID_ZEBRA 0x40000000
  130. #define P_DEV_ID_L64854 0xa0000000 /* == NCR 89C100+89C105. Pity. */
  131. #define P_NA_LOADED 0x08000000 /* R NA wirtten but was not used */
  132. #define P_A_LOADED 0x04000000 /* R */
  133. #define P_DMA_ON 0x02000000 /* R DMA is not disabled */
  134. #define P_EN_NEXT 0x01000000 /* RW */
  135. #define P_TCI_DIS 0x00800000 /* RW TCI forbidden from interrupts */
  136. #define P_DIAG 0x00100000 /* RW Disables draining and resetting
  137. of P-FIFO on loading of P_ADDR*/
  138. #define P_BURST_SIZE 0x000c0000 /* RW SBus burst size */
  139. #define P_BURST_8 0x00000000
  140. #define P_BURST_4 0x00040000
  141. #define P_BURST_1 0x00080000 /* "No burst" write */
  142. #define P_TC 0x00004000 /* RW1 Term Count, can be cleared when
  143. P_EN_NEXT=1 */
  144. #define P_EN_CNT 0x00002000 /* RW */
  145. #define P_EN_DMA 0x00000200 /* RW */
  146. #define P_WRITE 0x00000100 /* R DMA dir, 1=to ram, 0=to port */
  147. #define P_RESET 0x00000080 /* RW */
  148. #define P_SLAVE_ERR 0x00000040 /* RW1 Access size error */
  149. #define P_INVALIDATE 0x00000020 /* W Drop P-FIFO */
  150. #define P_INT_EN 0x00000010 /* RW OK to P_INT_PEND||P_ERR_PEND */
  151. #define P_DRAINING 0x0000000c /* R P-FIFO is draining to memory */
  152. #define P_ERR_PEND 0x00000002 /* R */
  153. #define P_INT_PEND 0x00000001 /* R */
  154. /* BPP_HCR. Time is in increments of SBus clock. */
  155. #define P_HCR_TEST 0x8000 /* Allows buried counters to be read */
  156. #define P_HCR_DSW 0x7f00 /* Data strobe width (in ticks) */
  157. #define P_HCR_DDS 0x007f /* Data setup before strobe (in ticks) */
  158. /* BPP_OCR. */
  159. #define P_OCR_MEM_CLR 0x8000
  160. #define P_OCR_DATA_SRC 0x4000 /* ) */
  161. #define P_OCR_DS_DSEL 0x2000 /* ) Bidirectional */
  162. #define P_OCR_BUSY_DSEL 0x1000 /* ) selects */
  163. #define P_OCR_ACK_DSEL 0x0800 /* ) */
  164. #define P_OCR_EN_DIAG 0x0400
  165. #define P_OCR_BUSY_OP 0x0200 /* Busy operation */
  166. #define P_OCR_ACK_OP 0x0100 /* Ack operation */
  167. #define P_OCR_SRST 0x0080 /* Reset state machines. Not selfcleaning. */
  168. #define P_OCR_IDLE 0x0008 /* PP data transfer state machine is idle */
  169. #define P_OCR_V_ILCK 0x0002 /* Versatec faded. Zebra only. */
  170. #define P_OCR_EN_VER 0x0001 /* Enable Versatec (0 - enable). Zebra only. */
  171. /* BPP_TCR */
  172. #define P_TCR_DIR 0x08
  173. #define P_TCR_BUSY 0x04
  174. #define P_TCR_ACK 0x02
  175. #define P_TCR_DS 0x01 /* Strobe */
  176. /* BPP_OR */
  177. #define P_OR_V3 0x20 /* ) */
  178. #define P_OR_V2 0x10 /* ) on Zebra only */
  179. #define P_OR_V1 0x08 /* ) */
  180. #define P_OR_INIT 0x04
  181. #define P_OR_AFXN 0x02 /* Auto Feed */
  182. #define P_OR_SLCT_IN 0x01
  183. /* BPP_IR */
  184. #define P_IR_PE 0x04
  185. #define P_IR_SLCT 0x02
  186. #define P_IR_ERR 0x01
  187. /* BPP_ICR */
  188. #define P_DS_IRQ 0x8000 /* RW1 */
  189. #define P_ACK_IRQ 0x4000 /* RW1 */
  190. #define P_BUSY_IRQ 0x2000 /* RW1 */
  191. #define P_PE_IRQ 0x1000 /* RW1 */
  192. #define P_SLCT_IRQ 0x0800 /* RW1 */
  193. #define P_ERR_IRQ 0x0400 /* RW1 */
  194. #define P_DS_IRQ_EN 0x0200 /* RW Always on rising edge */
  195. #define P_ACK_IRQ_EN 0x0100 /* RW Always on rising edge */
  196. #define P_BUSY_IRP 0x0080 /* RW 1= rising edge */
  197. #define P_BUSY_IRQ_EN 0x0040 /* RW */
  198. #define P_PE_IRP 0x0020 /* RW 1= rising edge */
  199. #define P_PE_IRQ_EN 0x0010 /* RW */
  200. #define P_SLCT_IRP 0x0008 /* RW 1= rising edge */
  201. #define P_SLCT_IRQ_EN 0x0004 /* RW */
  202. #define P_ERR_IRP 0x0002 /* RW1 1= rising edge */
  203. #define P_ERR_IRQ_EN 0x0001 /* RW */
  204. static void __iomem *base_addrs[BPP_NO];
  205. #define bpp_outb_p(data, base) sbus_writeb(data, (base) + BPP_DR)
  206. #define bpp_inb_p(base) sbus_readb((base) + BPP_DR)
  207. #define bpp_inb(base) sbus_readb((base) + BPP_DR)
  208. static void set_pins(unsigned short pins, unsigned minor)
  209. {
  210. void __iomem *base = base_addrs[minor];
  211. unsigned char bits_tcr = 0, bits_or = 0;
  212. if (instances[minor].direction & 0x20) bits_tcr |= P_TCR_DIR;
  213. if ( pins & BPP_PP_nStrobe) bits_tcr |= P_TCR_DS;
  214. if ( pins & BPP_PP_nAutoFd) bits_or |= P_OR_AFXN;
  215. if (! (pins & BPP_PP_nInit)) bits_or |= P_OR_INIT;
  216. if (! (pins & BPP_PP_nSelectIn)) bits_or |= P_OR_SLCT_IN;
  217. sbus_writeb(bits_or, base + BPP_OR);
  218. sbus_writeb(bits_tcr, base + BPP_TCR);
  219. }
  220. /*
  221. * i386 people read output pins from a software image.
  222. * We may get them back from hardware.
  223. * Again, inversion of pins must he buried here.
  224. */
  225. static unsigned short get_pins(unsigned minor)
  226. {
  227. void __iomem *base = base_addrs[minor];
  228. unsigned short bits = 0;
  229. unsigned value_tcr = sbus_readb(base + BPP_TCR);
  230. unsigned value_ir = sbus_readb(base + BPP_IR);
  231. unsigned value_or = sbus_readb(base + BPP_OR);
  232. if (value_tcr & P_TCR_DS) bits |= BPP_PP_nStrobe;
  233. if (value_or & P_OR_AFXN) bits |= BPP_PP_nAutoFd;
  234. if (! (value_or & P_OR_INIT)) bits |= BPP_PP_nInit;
  235. if (! (value_or & P_OR_SLCT_IN)) bits |= BPP_PP_nSelectIn;
  236. if (value_ir & P_IR_ERR) bits |= BPP_GP_nFault;
  237. if (! (value_ir & P_IR_SLCT)) bits |= BPP_GP_Select;
  238. if (! (value_ir & P_IR_PE)) bits |= BPP_GP_PError;
  239. if (! (value_tcr & P_TCR_ACK)) bits |= BPP_GP_nAck;
  240. if (value_tcr & P_TCR_BUSY) bits |= BPP_GP_Busy;
  241. return bits;
  242. }
  243. #endif /* __sparc__ */
  244. static void snooze(unsigned long snooze_time, unsigned minor)
  245. {
  246. schedule_timeout_uninterruptible(snooze_time + 1);
  247. }
  248. static int wait_for(unsigned short set, unsigned short clr,
  249. unsigned long delay, unsigned minor)
  250. {
  251. unsigned short pins = get_pins(minor);
  252. unsigned long extime = 0;
  253. /*
  254. * Try a real fast scan for the first jiffy, in case the device
  255. * responds real good. The first while loop guesses an expire
  256. * time accounting for possible wraparound of jiffies.
  257. */
  258. while (time_after_eq(jiffies, extime)) extime = jiffies + 1;
  259. while ( (time_before(jiffies, extime))
  260. && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  261. pins = get_pins(minor);
  262. }
  263. delay -= 1;
  264. /*
  265. * If my delay expired or the pins are still not where I want
  266. * them, then resort to using the timer and greatly reduce my
  267. * sample rate. If the peripheral is going to be slow, this will
  268. * give the CPU up to some more worthy process.
  269. */
  270. while ( delay && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  271. snooze(1, minor);
  272. pins = get_pins(minor);
  273. delay -= 1;
  274. }
  275. if (delay == 0) return -1;
  276. else return pins;
  277. }
  278. /*
  279. * Return ZERO(0) If the negotiation succeeds, an errno otherwise. An
  280. * errno means something broke, and I do not yet know how to fix it.
  281. */
  282. static int negotiate(unsigned char mode, unsigned minor)
  283. {
  284. int rc;
  285. unsigned short pins = get_pins(minor);
  286. if (pins & BPP_PP_nSelectIn) return -EIO;
  287. /* Event 0: Write the mode to the data lines */
  288. bpp_outb_p(mode, base_addrs[minor]);
  289. snooze(TIME_PSetup, minor);
  290. /* Event 1: Strobe the mode code into the peripheral */
  291. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  292. /* Wait for Event 2: Peripheral responds as a 1284 device. */
  293. rc = wait_for(BPP_GP_PError|BPP_GP_Select|BPP_GP_nFault,
  294. BPP_GP_nAck,
  295. TIME_PResponse,
  296. minor);
  297. if (rc == -1) return -ETIMEDOUT;
  298. /* Event 3: latch extensibility request */
  299. set_pins(BPP_PP_nSelectIn|BPP_PP_nInit, minor);
  300. /* ... quick nap while peripheral ponders the byte i'm sending...*/
  301. snooze(1, minor);
  302. /* Event 4: restore strobe, to ACK peripheral's response. */
  303. set_pins(BPP_PP_nSelectIn|BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  304. /* Wait for Event 6: Peripheral latches response bits */
  305. rc = wait_for(BPP_GP_nAck, 0, TIME_PSetup+TIME_PResponse, minor);
  306. if (rc == -1) return -EIO;
  307. /* A 1284 device cannot refuse nibble mode */
  308. if (mode == DEFAULT_NIBBLE) return 0;
  309. if (pins & BPP_GP_Select) return 0;
  310. return -EPROTONOSUPPORT;
  311. }
  312. static int terminate(unsigned minor)
  313. {
  314. int rc;
  315. /* Event 22: Request termination of 1284 mode */
  316. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  317. /* Wait for Events 23 and 24: ACK termination request. */
  318. rc = wait_for(BPP_GP_Busy|BPP_GP_nFault,
  319. BPP_GP_nAck,
  320. TIME_PSetup+TIME_PResponse,
  321. minor);
  322. instances[minor].direction = 0;
  323. instances[minor].mode = COMPATIBILITY;
  324. if (rc == -1) {
  325. return -EIO;
  326. }
  327. /* Event 25: Handshake by lowering nAutoFd */
  328. set_pins(BPP_PP_nStrobe|BPP_PP_nInit, minor);
  329. /* Event 26: Peripheral wiggles lines... */
  330. /* Event 27: Peripheral sets nAck HIGH to ack handshake */
  331. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  332. if (rc == -1) {
  333. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  334. return -EIO;
  335. }
  336. /* Event 28: Finish phase by raising nAutoFd */
  337. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  338. return 0;
  339. }
  340. static DEFINE_SPINLOCK(bpp_open_lock);
  341. /*
  342. * Allow only one process to open the device at a time.
  343. */
  344. static int bpp_open(struct inode *inode, struct file *f)
  345. {
  346. unsigned minor = iminor(inode);
  347. int ret;
  348. spin_lock(&bpp_open_lock);
  349. ret = 0;
  350. if (minor >= BPP_NO) {
  351. ret = -ENODEV;
  352. } else {
  353. if (! instances[minor].present) {
  354. ret = -ENODEV;
  355. } else {
  356. if (instances[minor].opened)
  357. ret = -EBUSY;
  358. else
  359. instances[minor].opened = 1;
  360. }
  361. }
  362. spin_unlock(&bpp_open_lock);
  363. return ret;
  364. }
  365. /*
  366. * When the process closes the device, this method is called to clean
  367. * up and reset the hardware. Always leave the device in compatibility
  368. * mode as this is a reasonable place to clean up from messes made by
  369. * ioctls, or other mayhem.
  370. */
  371. static int bpp_release(struct inode *inode, struct file *f)
  372. {
  373. unsigned minor = iminor(inode);
  374. spin_lock(&bpp_open_lock);
  375. instances[minor].opened = 0;
  376. if (instances[minor].mode != COMPATIBILITY)
  377. terminate(minor);
  378. spin_unlock(&bpp_open_lock);
  379. return 0;
  380. }
  381. static long read_nibble(unsigned minor, char __user *c, unsigned long cnt)
  382. {
  383. unsigned long remaining = cnt;
  384. long rc;
  385. while (remaining > 0) {
  386. unsigned char byte = 0;
  387. int pins;
  388. /* Event 7: request nibble */
  389. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  390. /* Wait for event 9: Peripher strobes first nibble */
  391. pins = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  392. if (pins == -1) return -ETIMEDOUT;
  393. /* Event 10: I handshake nibble */
  394. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  395. if (pins & BPP_GP_nFault) byte |= 0x01;
  396. if (pins & BPP_GP_Select) byte |= 0x02;
  397. if (pins & BPP_GP_PError) byte |= 0x04;
  398. if (pins & BPP_GP_Busy) byte |= 0x08;
  399. /* Wait for event 11: Peripheral handshakes nibble */
  400. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  401. /* Event 7: request nibble */
  402. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  403. /* Wait for event 9: Peripher strobes first nibble */
  404. pins = wait_for(0, BPP_GP_nAck, TIME_PResponse, minor);
  405. if (rc == -1) return -ETIMEDOUT;
  406. /* Event 10: I handshake nibble */
  407. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  408. if (pins & BPP_GP_nFault) byte |= 0x10;
  409. if (pins & BPP_GP_Select) byte |= 0x20;
  410. if (pins & BPP_GP_PError) byte |= 0x40;
  411. if (pins & BPP_GP_Busy) byte |= 0x80;
  412. if (put_user(byte, c))
  413. return -EFAULT;
  414. c += 1;
  415. remaining -= 1;
  416. /* Wait for event 11: Peripheral handshakes nibble */
  417. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  418. if (rc == -1) return -EIO;
  419. }
  420. return cnt - remaining;
  421. }
  422. static long read_ecp(unsigned minor, char __user *c, unsigned long cnt)
  423. {
  424. unsigned long remaining;
  425. long rc;
  426. /* Turn ECP mode from forward to reverse if needed. */
  427. if (! instances[minor].direction) {
  428. unsigned short pins = get_pins(minor);
  429. /* Event 38: Turn the bus around */
  430. instances[minor].direction = 0x20;
  431. pins &= ~BPP_PP_nAutoFd;
  432. set_pins(pins, minor);
  433. /* Event 39: Set pins for reverse mode. */
  434. snooze(TIME_PSetup, minor);
  435. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  436. /* Wait for event 40: Peripheral ready to be strobed */
  437. rc = wait_for(0, BPP_GP_PError, TIME_PResponse, minor);
  438. if (rc == -1) return -ETIMEDOUT;
  439. }
  440. remaining = cnt;
  441. while (remaining > 0) {
  442. /* If there is a run length for a repeated byte, repeat */
  443. /* that byte a few times. */
  444. if (instances[minor].run_length && !instances[minor].run_flag) {
  445. char buffer[128];
  446. unsigned idx;
  447. unsigned repeat = remaining < instances[minor].run_length
  448. ? remaining
  449. : instances[minor].run_length;
  450. for (idx = 0 ; idx < repeat ; idx += 1)
  451. buffer[idx] = instances[minor].repeat_byte;
  452. if (copy_to_user(c, buffer, repeat))
  453. return -EFAULT;
  454. remaining -= repeat;
  455. c += repeat;
  456. instances[minor].run_length -= repeat;
  457. }
  458. if (remaining == 0) break;
  459. /* Wait for Event 43: Data active on the bus. */
  460. rc = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  461. if (rc == -1) break;
  462. if (rc & BPP_GP_Busy) {
  463. /* OK, this is data. read it in. */
  464. unsigned char byte = bpp_inb(base_addrs[minor]);
  465. if (put_user(byte, c))
  466. return -EFAULT;
  467. c += 1;
  468. remaining -= 1;
  469. if (instances[minor].run_flag) {
  470. instances[minor].repeat_byte = byte;
  471. instances[minor].run_flag = 0;
  472. }
  473. } else {
  474. unsigned char byte = bpp_inb(base_addrs[minor]);
  475. if (byte & 0x80) {
  476. printk("bpp%d: "
  477. "Ignoring ECP channel %u from device.\n",
  478. minor, byte & 0x7f);
  479. } else {
  480. instances[minor].run_length = byte;
  481. instances[minor].run_flag = 1;
  482. }
  483. }
  484. /* Event 44: I got it. */
  485. set_pins(BPP_PP_nStrobe|BPP_PP_nAutoFd|BPP_PP_nSelectIn, minor);
  486. /* Wait for event 45: peripheral handshake */
  487. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  488. if (rc == -1) return -ETIMEDOUT;
  489. /* Event 46: Finish handshake */
  490. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  491. }
  492. return cnt - remaining;
  493. }
  494. static ssize_t bpp_read(struct file *f, char __user *c, size_t cnt, loff_t * ppos)
  495. {
  496. long rc;
  497. unsigned minor = iminor(f->f_dentry->d_inode);
  498. if (minor >= BPP_NO) return -ENODEV;
  499. if (!instances[minor].present) return -ENODEV;
  500. switch (instances[minor].mode) {
  501. default:
  502. if (instances[minor].mode != COMPATIBILITY)
  503. terminate(minor);
  504. if (instances[minor].enhanced) {
  505. /* For now, do all reads with ECP-RLE mode */
  506. unsigned short pins;
  507. rc = negotiate(DEFAULT_ECP, minor);
  508. if (rc < 0) break;
  509. instances[minor].mode = ECP_RLE;
  510. /* Event 30: set nAutoFd low to setup for ECP mode */
  511. pins = get_pins(minor);
  512. pins &= ~BPP_PP_nAutoFd;
  513. set_pins(pins, minor);
  514. /* Wait for Event 31: peripheral ready */
  515. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  516. if (rc == -1) return -ETIMEDOUT;
  517. rc = read_ecp(minor, c, cnt);
  518. } else {
  519. rc = negotiate(DEFAULT_NIBBLE, minor);
  520. if (rc < 0) break;
  521. instances[minor].mode = NIBBLE;
  522. rc = read_nibble(minor, c, cnt);
  523. }
  524. break;
  525. case NIBBLE:
  526. rc = read_nibble(minor, c, cnt);
  527. break;
  528. case ECP:
  529. case ECP_RLE:
  530. rc = read_ecp(minor, c, cnt);
  531. break;
  532. }
  533. return rc;
  534. }
  535. /*
  536. * Compatibility mode handshaking is a matter of writing data,
  537. * strobing it, and waiting for the printer to stop being busy.
  538. */
  539. static long write_compat(unsigned minor, const char __user *c, unsigned long cnt)
  540. {
  541. long rc;
  542. unsigned short pins = get_pins(minor);
  543. unsigned long remaining = cnt;
  544. while (remaining > 0) {
  545. unsigned char byte;
  546. if (get_user(byte, c))
  547. return -EFAULT;
  548. c += 1;
  549. rc = wait_for(BPP_GP_nAck, BPP_GP_Busy, TIME_IDLE_LIMIT, minor);
  550. if (rc == -1) return -ETIMEDOUT;
  551. bpp_outb_p(byte, base_addrs[minor]);
  552. remaining -= 1;
  553. /* snooze(1, minor); */
  554. pins &= ~BPP_PP_nStrobe;
  555. set_pins(pins, minor);
  556. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  557. pins |= BPP_PP_nStrobe;
  558. set_pins(pins, minor);
  559. }
  560. return cnt - remaining;
  561. }
  562. /*
  563. * Write data using ECP mode. Watch out that the port may be set up
  564. * for reading. If so, turn the port around.
  565. */
  566. static long write_ecp(unsigned minor, const char __user *c, unsigned long cnt)
  567. {
  568. unsigned short pins = get_pins(minor);
  569. unsigned long remaining = cnt;
  570. if (instances[minor].direction) {
  571. int rc;
  572. /* Event 47 Request bus be turned around */
  573. pins |= BPP_PP_nInit;
  574. set_pins(pins, minor);
  575. /* Wait for Event 49: Peripheral relinquished bus */
  576. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  577. pins |= BPP_PP_nAutoFd;
  578. instances[minor].direction = 0;
  579. set_pins(pins, minor);
  580. }
  581. while (remaining > 0) {
  582. unsigned char byte;
  583. int rc;
  584. if (get_user(byte, c))
  585. return -EFAULT;
  586. rc = wait_for(0, BPP_GP_Busy, TIME_PResponse, minor);
  587. if (rc == -1) return -ETIMEDOUT;
  588. c += 1;
  589. bpp_outb_p(byte, base_addrs[minor]);
  590. pins &= ~BPP_PP_nStrobe;
  591. set_pins(pins, minor);
  592. pins |= BPP_PP_nStrobe;
  593. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  594. if (rc == -1) return -EIO;
  595. set_pins(pins, minor);
  596. }
  597. return cnt - remaining;
  598. }
  599. /*
  600. * Write to the peripheral. Be sensitive of the current mode. If I'm
  601. * in a mode that can be turned around (ECP) then just do
  602. * that. Otherwise, terminate and do my writing in compat mode. This
  603. * is the safest course as any device can handle it.
  604. */
  605. static ssize_t bpp_write(struct file *f, const char __user *c, size_t cnt, loff_t * ppos)
  606. {
  607. long errno = 0;
  608. unsigned minor = iminor(f->f_dentry->d_inode);
  609. if (minor >= BPP_NO) return -ENODEV;
  610. if (!instances[minor].present) return -ENODEV;
  611. switch (instances[minor].mode) {
  612. case ECP:
  613. case ECP_RLE:
  614. errno = write_ecp(minor, c, cnt);
  615. break;
  616. case COMPATIBILITY:
  617. errno = write_compat(minor, c, cnt);
  618. break;
  619. default:
  620. terminate(minor);
  621. errno = write_compat(minor, c, cnt);
  622. }
  623. return errno;
  624. }
  625. static int bpp_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
  626. unsigned long arg)
  627. {
  628. int errno = 0;
  629. unsigned minor = iminor(inode);
  630. if (minor >= BPP_NO) return -ENODEV;
  631. if (!instances[minor].present) return -ENODEV;
  632. switch (cmd) {
  633. case BPP_PUT_PINS:
  634. set_pins(arg, minor);
  635. break;
  636. case BPP_GET_PINS:
  637. errno = get_pins(minor);
  638. break;
  639. case BPP_PUT_DATA:
  640. bpp_outb_p(arg, base_addrs[minor]);
  641. break;
  642. case BPP_GET_DATA:
  643. errno = bpp_inb_p(base_addrs[minor]);
  644. break;
  645. case BPP_SET_INPUT:
  646. if (arg)
  647. if (instances[minor].enhanced) {
  648. unsigned short bits = get_pins(minor);
  649. instances[minor].direction = 0x20;
  650. set_pins(bits, minor);
  651. } else {
  652. errno = -ENOTTY;
  653. }
  654. else {
  655. unsigned short bits = get_pins(minor);
  656. instances[minor].direction = 0x00;
  657. set_pins(bits, minor);
  658. }
  659. break;
  660. default:
  661. errno = -EINVAL;
  662. }
  663. return errno;
  664. }
  665. static struct file_operations bpp_fops = {
  666. .owner = THIS_MODULE,
  667. .read = bpp_read,
  668. .write = bpp_write,
  669. .ioctl = bpp_ioctl,
  670. .open = bpp_open,
  671. .release = bpp_release,
  672. };
  673. #if defined(__i386__)
  674. #define collectLptPorts() {}
  675. static void probeLptPort(unsigned idx)
  676. {
  677. unsigned int testvalue;
  678. const unsigned short lpAddr = base_addrs[idx];
  679. instances[idx].present = 0;
  680. instances[idx].enhanced = 0;
  681. instances[idx].direction = 0;
  682. instances[idx].mode = COMPATIBILITY;
  683. instances[idx].run_length = 0;
  684. instances[idx].run_flag = 0;
  685. if (!request_region(lpAddr,3, dev_name)) return;
  686. /*
  687. * First, make sure the instance exists. Do this by writing to
  688. * the data latch and reading the value back. If the port *is*
  689. * present, test to see if it supports extended-mode
  690. * operation. This will be required for IEEE1284 reverse
  691. * transfers.
  692. */
  693. outb_p(BPP_PROBE_CODE, lpAddr);
  694. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  695. ;
  696. testvalue = inb_p(lpAddr);
  697. if (testvalue == BPP_PROBE_CODE) {
  698. unsigned save;
  699. instances[idx].present = 1;
  700. save = inb_p(lpAddr+2);
  701. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  702. ;
  703. outb_p(save|0x20, lpAddr+2);
  704. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  705. ;
  706. outb_p(~BPP_PROBE_CODE, lpAddr);
  707. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  708. ;
  709. testvalue = inb_p(lpAddr);
  710. if ((testvalue&0xff) == (0xff&~BPP_PROBE_CODE))
  711. instances[idx].enhanced = 0;
  712. else
  713. instances[idx].enhanced = 1;
  714. outb_p(save, lpAddr+2);
  715. }
  716. else {
  717. release_region(lpAddr,3);
  718. }
  719. /*
  720. * Leave the port in compat idle mode.
  721. */
  722. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  723. printk("bpp%d: Port at 0x%03x: Enhanced mode %s\n", idx, base_addrs[idx],
  724. instances[idx].enhanced? "SUPPORTED" : "UNAVAILABLE");
  725. }
  726. static inline void freeLptPort(int idx)
  727. {
  728. release_region(base_addrs[idx], 3);
  729. }
  730. #endif
  731. #if defined(__sparc__)
  732. static void __iomem *map_bpp(struct sbus_dev *dev, int idx)
  733. {
  734. return sbus_ioremap(&dev->resource[0], 0, BPP_SIZE, "bpp");
  735. }
  736. static int collectLptPorts(void)
  737. {
  738. struct sbus_bus *bus;
  739. struct sbus_dev *dev;
  740. int count;
  741. count = 0;
  742. for_all_sbusdev(dev, bus) {
  743. if (strcmp(dev->prom_name, "SUNW,bpp") == 0) {
  744. if (count >= BPP_NO) {
  745. printk(KERN_NOTICE
  746. "bpp: More than %d bpp ports,"
  747. " rest is ignored\n", BPP_NO);
  748. return count;
  749. }
  750. base_addrs[count] = map_bpp(dev, count);
  751. count++;
  752. }
  753. }
  754. return count;
  755. }
  756. static void probeLptPort(unsigned idx)
  757. {
  758. void __iomem *rp = base_addrs[idx];
  759. __u32 csr;
  760. char *brand;
  761. instances[idx].present = 0;
  762. instances[idx].enhanced = 0;
  763. instances[idx].direction = 0;
  764. instances[idx].mode = COMPATIBILITY;
  765. instances[idx].run_length = 0;
  766. instances[idx].run_flag = 0;
  767. if (!rp) return;
  768. instances[idx].present = 1;
  769. instances[idx].enhanced = 1; /* Sure */
  770. csr = sbus_readl(rp + BPP_CSR);
  771. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  772. udelay(20);
  773. csr = sbus_readl(rp + BPP_CSR);
  774. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  775. printk("bpp%d: DRAINING still active (0x%08x)\n", idx, csr);
  776. }
  777. }
  778. printk("bpp%d: reset with 0x%08x ..", idx, csr);
  779. sbus_writel((csr | P_RESET) & ~P_INT_EN, rp + BPP_CSR);
  780. udelay(500);
  781. sbus_writel(sbus_readl(rp + BPP_CSR) & ~P_RESET, rp + BPP_CSR);
  782. csr = sbus_readl(rp + BPP_CSR);
  783. printk(" done with csr=0x%08x ocr=0x%04x\n",
  784. csr, sbus_readw(rp + BPP_OCR));
  785. switch (csr & P_DEV_ID_MASK) {
  786. case P_DEV_ID_ZEBRA:
  787. brand = "Zebra";
  788. break;
  789. case P_DEV_ID_L64854:
  790. brand = "DMA2";
  791. break;
  792. default:
  793. brand = "Unknown";
  794. }
  795. printk("bpp%d: %s at %p\n", idx, brand, rp);
  796. /*
  797. * Leave the port in compat idle mode.
  798. */
  799. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  800. return;
  801. }
  802. static inline void freeLptPort(int idx)
  803. {
  804. sbus_iounmap(base_addrs[idx], BPP_SIZE);
  805. }
  806. #endif
  807. static int __init bpp_init(void)
  808. {
  809. int rc;
  810. unsigned idx;
  811. rc = collectLptPorts();
  812. if (rc == 0)
  813. return -ENODEV;
  814. rc = register_chrdev(BPP_MAJOR, dev_name, &bpp_fops);
  815. if (rc < 0)
  816. return rc;
  817. for (idx = 0; idx < BPP_NO; idx++) {
  818. instances[idx].opened = 0;
  819. probeLptPort(idx);
  820. }
  821. return 0;
  822. }
  823. static void __exit bpp_cleanup(void)
  824. {
  825. unsigned idx;
  826. for (idx = 0; idx < BPP_NO; idx++)
  827. devfs_remove("bpp/%d", idx);
  828. devfs_remove("bpp");
  829. unregister_chrdev(BPP_MAJOR, dev_name);
  830. for (idx = 0; idx < BPP_NO; idx++) {
  831. if (instances[idx].present)
  832. freeLptPort(idx);
  833. }
  834. }
  835. module_init(bpp_init);
  836. module_exit(bpp_cleanup);
  837. MODULE_LICENSE("GPL");