bpp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. set_current_state(TASK_UNINTERRUPTIBLE);
  247. schedule_timeout(snooze_time + 1);
  248. }
  249. static int wait_for(unsigned short set, unsigned short clr,
  250. unsigned long delay, unsigned minor)
  251. {
  252. unsigned short pins = get_pins(minor);
  253. unsigned long extime = 0;
  254. /*
  255. * Try a real fast scan for the first jiffy, in case the device
  256. * responds real good. The first while loop guesses an expire
  257. * time accounting for possible wraparound of jiffies.
  258. */
  259. while (time_after_eq(jiffies, extime)) extime = jiffies + 1;
  260. while ( (time_before(jiffies, extime))
  261. && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  262. pins = get_pins(minor);
  263. }
  264. delay -= 1;
  265. /*
  266. * If my delay expired or the pins are still not where I want
  267. * them, then resort to using the timer and greatly reduce my
  268. * sample rate. If the peripheral is going to be slow, this will
  269. * give the CPU up to some more worthy process.
  270. */
  271. while ( delay && (((pins & set) != set) || ((pins & clr) != 0)) ) {
  272. snooze(1, minor);
  273. pins = get_pins(minor);
  274. delay -= 1;
  275. }
  276. if (delay == 0) return -1;
  277. else return pins;
  278. }
  279. /*
  280. * Return ZERO(0) If the negotiation succeeds, an errno otherwise. An
  281. * errno means something broke, and I do not yet know how to fix it.
  282. */
  283. static int negotiate(unsigned char mode, unsigned minor)
  284. {
  285. int rc;
  286. unsigned short pins = get_pins(minor);
  287. if (pins & BPP_PP_nSelectIn) return -EIO;
  288. /* Event 0: Write the mode to the data lines */
  289. bpp_outb_p(mode, base_addrs[minor]);
  290. snooze(TIME_PSetup, minor);
  291. /* Event 1: Strobe the mode code into the peripheral */
  292. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  293. /* Wait for Event 2: Peripheral responds as a 1284 device. */
  294. rc = wait_for(BPP_GP_PError|BPP_GP_Select|BPP_GP_nFault,
  295. BPP_GP_nAck,
  296. TIME_PResponse,
  297. minor);
  298. if (rc == -1) return -ETIMEDOUT;
  299. /* Event 3: latch extensibility request */
  300. set_pins(BPP_PP_nSelectIn|BPP_PP_nInit, minor);
  301. /* ... quick nap while peripheral ponders the byte i'm sending...*/
  302. snooze(1, minor);
  303. /* Event 4: restore strobe, to ACK peripheral's response. */
  304. set_pins(BPP_PP_nSelectIn|BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  305. /* Wait for Event 6: Peripheral latches response bits */
  306. rc = wait_for(BPP_GP_nAck, 0, TIME_PSetup+TIME_PResponse, minor);
  307. if (rc == -1) return -EIO;
  308. /* A 1284 device cannot refuse nibble mode */
  309. if (mode == DEFAULT_NIBBLE) return 0;
  310. if (pins & BPP_GP_Select) return 0;
  311. return -EPROTONOSUPPORT;
  312. }
  313. static int terminate(unsigned minor)
  314. {
  315. int rc;
  316. /* Event 22: Request termination of 1284 mode */
  317. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  318. /* Wait for Events 23 and 24: ACK termination request. */
  319. rc = wait_for(BPP_GP_Busy|BPP_GP_nFault,
  320. BPP_GP_nAck,
  321. TIME_PSetup+TIME_PResponse,
  322. minor);
  323. instances[minor].direction = 0;
  324. instances[minor].mode = COMPATIBILITY;
  325. if (rc == -1) {
  326. return -EIO;
  327. }
  328. /* Event 25: Handshake by lowering nAutoFd */
  329. set_pins(BPP_PP_nStrobe|BPP_PP_nInit, minor);
  330. /* Event 26: Peripheral wiggles lines... */
  331. /* Event 27: Peripheral sets nAck HIGH to ack handshake */
  332. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  333. if (rc == -1) {
  334. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  335. return -EIO;
  336. }
  337. /* Event 28: Finish phase by raising nAutoFd */
  338. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, minor);
  339. return 0;
  340. }
  341. static DEFINE_SPINLOCK(bpp_open_lock);
  342. /*
  343. * Allow only one process to open the device at a time.
  344. */
  345. static int bpp_open(struct inode *inode, struct file *f)
  346. {
  347. unsigned minor = iminor(inode);
  348. int ret;
  349. spin_lock(&bpp_open_lock);
  350. ret = 0;
  351. if (minor >= BPP_NO) {
  352. ret = -ENODEV;
  353. } else {
  354. if (! instances[minor].present) {
  355. ret = -ENODEV;
  356. } else {
  357. if (instances[minor].opened)
  358. ret = -EBUSY;
  359. else
  360. instances[minor].opened = 1;
  361. }
  362. }
  363. spin_unlock(&bpp_open_lock);
  364. return ret;
  365. }
  366. /*
  367. * When the process closes the device, this method is called to clean
  368. * up and reset the hardware. Always leave the device in compatibility
  369. * mode as this is a reasonable place to clean up from messes made by
  370. * ioctls, or other mayhem.
  371. */
  372. static int bpp_release(struct inode *inode, struct file *f)
  373. {
  374. unsigned minor = iminor(inode);
  375. spin_lock(&bpp_open_lock);
  376. instances[minor].opened = 0;
  377. if (instances[minor].mode != COMPATIBILITY)
  378. terminate(minor);
  379. spin_unlock(&bpp_open_lock);
  380. return 0;
  381. }
  382. static long read_nibble(unsigned minor, char __user *c, unsigned long cnt)
  383. {
  384. unsigned long remaining = cnt;
  385. long rc;
  386. while (remaining > 0) {
  387. unsigned char byte = 0;
  388. int pins;
  389. /* Event 7: request nibble */
  390. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  391. /* Wait for event 9: Peripher strobes first nibble */
  392. pins = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  393. if (pins == -1) return -ETIMEDOUT;
  394. /* Event 10: I handshake nibble */
  395. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  396. if (pins & BPP_GP_nFault) byte |= 0x01;
  397. if (pins & BPP_GP_Select) byte |= 0x02;
  398. if (pins & BPP_GP_PError) byte |= 0x04;
  399. if (pins & BPP_GP_Busy) byte |= 0x08;
  400. /* Wait for event 11: Peripheral handshakes nibble */
  401. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  402. /* Event 7: request nibble */
  403. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe, minor);
  404. /* Wait for event 9: Peripher strobes first nibble */
  405. pins = wait_for(0, BPP_GP_nAck, TIME_PResponse, minor);
  406. if (rc == -1) return -ETIMEDOUT;
  407. /* Event 10: I handshake nibble */
  408. set_pins(BPP_PP_nSelectIn|BPP_PP_nStrobe|BPP_PP_nAutoFd, minor);
  409. if (pins & BPP_GP_nFault) byte |= 0x10;
  410. if (pins & BPP_GP_Select) byte |= 0x20;
  411. if (pins & BPP_GP_PError) byte |= 0x40;
  412. if (pins & BPP_GP_Busy) byte |= 0x80;
  413. if (put_user(byte, c))
  414. return -EFAULT;
  415. c += 1;
  416. remaining -= 1;
  417. /* Wait for event 11: Peripheral handshakes nibble */
  418. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  419. if (rc == -1) return -EIO;
  420. }
  421. return cnt - remaining;
  422. }
  423. static long read_ecp(unsigned minor, char __user *c, unsigned long cnt)
  424. {
  425. unsigned long remaining;
  426. long rc;
  427. /* Turn ECP mode from forward to reverse if needed. */
  428. if (! instances[minor].direction) {
  429. unsigned short pins = get_pins(minor);
  430. /* Event 38: Turn the bus around */
  431. instances[minor].direction = 0x20;
  432. pins &= ~BPP_PP_nAutoFd;
  433. set_pins(pins, minor);
  434. /* Event 39: Set pins for reverse mode. */
  435. snooze(TIME_PSetup, minor);
  436. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  437. /* Wait for event 40: Peripheral ready to be strobed */
  438. rc = wait_for(0, BPP_GP_PError, TIME_PResponse, minor);
  439. if (rc == -1) return -ETIMEDOUT;
  440. }
  441. remaining = cnt;
  442. while (remaining > 0) {
  443. /* If there is a run length for a repeated byte, repeat */
  444. /* that byte a few times. */
  445. if (instances[minor].run_length && !instances[minor].run_flag) {
  446. char buffer[128];
  447. unsigned idx;
  448. unsigned repeat = remaining < instances[minor].run_length
  449. ? remaining
  450. : instances[minor].run_length;
  451. for (idx = 0 ; idx < repeat ; idx += 1)
  452. buffer[idx] = instances[minor].repeat_byte;
  453. if (copy_to_user(c, buffer, repeat))
  454. return -EFAULT;
  455. remaining -= repeat;
  456. c += repeat;
  457. instances[minor].run_length -= repeat;
  458. }
  459. if (remaining == 0) break;
  460. /* Wait for Event 43: Data active on the bus. */
  461. rc = wait_for(0, BPP_GP_nAck, TIME_IDLE_LIMIT, minor);
  462. if (rc == -1) break;
  463. if (rc & BPP_GP_Busy) {
  464. /* OK, this is data. read it in. */
  465. unsigned char byte = bpp_inb(base_addrs[minor]);
  466. if (put_user(byte, c))
  467. return -EFAULT;
  468. c += 1;
  469. remaining -= 1;
  470. if (instances[minor].run_flag) {
  471. instances[minor].repeat_byte = byte;
  472. instances[minor].run_flag = 0;
  473. }
  474. } else {
  475. unsigned char byte = bpp_inb(base_addrs[minor]);
  476. if (byte & 0x80) {
  477. printk("bpp%d: "
  478. "Ignoring ECP channel %u from device.\n",
  479. minor, byte & 0x7f);
  480. } else {
  481. instances[minor].run_length = byte;
  482. instances[minor].run_flag = 1;
  483. }
  484. }
  485. /* Event 44: I got it. */
  486. set_pins(BPP_PP_nStrobe|BPP_PP_nAutoFd|BPP_PP_nSelectIn, minor);
  487. /* Wait for event 45: peripheral handshake */
  488. rc = wait_for(BPP_GP_nAck, 0, TIME_PResponse, minor);
  489. if (rc == -1) return -ETIMEDOUT;
  490. /* Event 46: Finish handshake */
  491. set_pins(BPP_PP_nStrobe|BPP_PP_nSelectIn, minor);
  492. }
  493. return cnt - remaining;
  494. }
  495. static ssize_t bpp_read(struct file *f, char __user *c, size_t cnt, loff_t * ppos)
  496. {
  497. long rc;
  498. unsigned minor = iminor(f->f_dentry->d_inode);
  499. if (minor >= BPP_NO) return -ENODEV;
  500. if (!instances[minor].present) return -ENODEV;
  501. switch (instances[minor].mode) {
  502. default:
  503. if (instances[minor].mode != COMPATIBILITY)
  504. terminate(minor);
  505. if (instances[minor].enhanced) {
  506. /* For now, do all reads with ECP-RLE mode */
  507. unsigned short pins;
  508. rc = negotiate(DEFAULT_ECP, minor);
  509. if (rc < 0) break;
  510. instances[minor].mode = ECP_RLE;
  511. /* Event 30: set nAutoFd low to setup for ECP mode */
  512. pins = get_pins(minor);
  513. pins &= ~BPP_PP_nAutoFd;
  514. set_pins(pins, minor);
  515. /* Wait for Event 31: peripheral ready */
  516. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  517. if (rc == -1) return -ETIMEDOUT;
  518. rc = read_ecp(minor, c, cnt);
  519. } else {
  520. rc = negotiate(DEFAULT_NIBBLE, minor);
  521. if (rc < 0) break;
  522. instances[minor].mode = NIBBLE;
  523. rc = read_nibble(minor, c, cnt);
  524. }
  525. break;
  526. case NIBBLE:
  527. rc = read_nibble(minor, c, cnt);
  528. break;
  529. case ECP:
  530. case ECP_RLE:
  531. rc = read_ecp(minor, c, cnt);
  532. break;
  533. }
  534. return rc;
  535. }
  536. /*
  537. * Compatibility mode handshaking is a matter of writing data,
  538. * strobing it, and waiting for the printer to stop being busy.
  539. */
  540. static long write_compat(unsigned minor, const char __user *c, unsigned long cnt)
  541. {
  542. long rc;
  543. unsigned short pins = get_pins(minor);
  544. unsigned long remaining = cnt;
  545. while (remaining > 0) {
  546. unsigned char byte;
  547. if (get_user(byte, c))
  548. return -EFAULT;
  549. c += 1;
  550. rc = wait_for(BPP_GP_nAck, BPP_GP_Busy, TIME_IDLE_LIMIT, minor);
  551. if (rc == -1) return -ETIMEDOUT;
  552. bpp_outb_p(byte, base_addrs[minor]);
  553. remaining -= 1;
  554. /* snooze(1, minor); */
  555. pins &= ~BPP_PP_nStrobe;
  556. set_pins(pins, minor);
  557. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  558. pins |= BPP_PP_nStrobe;
  559. set_pins(pins, minor);
  560. }
  561. return cnt - remaining;
  562. }
  563. /*
  564. * Write data using ECP mode. Watch out that the port may be set up
  565. * for reading. If so, turn the port around.
  566. */
  567. static long write_ecp(unsigned minor, const char __user *c, unsigned long cnt)
  568. {
  569. unsigned short pins = get_pins(minor);
  570. unsigned long remaining = cnt;
  571. if (instances[minor].direction) {
  572. int rc;
  573. /* Event 47 Request bus be turned around */
  574. pins |= BPP_PP_nInit;
  575. set_pins(pins, minor);
  576. /* Wait for Event 49: Peripheral relinquished bus */
  577. rc = wait_for(BPP_GP_PError, 0, TIME_PResponse, minor);
  578. pins |= BPP_PP_nAutoFd;
  579. instances[minor].direction = 0;
  580. set_pins(pins, minor);
  581. }
  582. while (remaining > 0) {
  583. unsigned char byte;
  584. int rc;
  585. if (get_user(byte, c))
  586. return -EFAULT;
  587. rc = wait_for(0, BPP_GP_Busy, TIME_PResponse, minor);
  588. if (rc == -1) return -ETIMEDOUT;
  589. c += 1;
  590. bpp_outb_p(byte, base_addrs[minor]);
  591. pins &= ~BPP_PP_nStrobe;
  592. set_pins(pins, minor);
  593. pins |= BPP_PP_nStrobe;
  594. rc = wait_for(BPP_GP_Busy, 0, TIME_PResponse, minor);
  595. if (rc == -1) return -EIO;
  596. set_pins(pins, minor);
  597. }
  598. return cnt - remaining;
  599. }
  600. /*
  601. * Write to the peripheral. Be sensitive of the current mode. If I'm
  602. * in a mode that can be turned around (ECP) then just do
  603. * that. Otherwise, terminate and do my writing in compat mode. This
  604. * is the safest course as any device can handle it.
  605. */
  606. static ssize_t bpp_write(struct file *f, const char __user *c, size_t cnt, loff_t * ppos)
  607. {
  608. long errno = 0;
  609. unsigned minor = iminor(f->f_dentry->d_inode);
  610. if (minor >= BPP_NO) return -ENODEV;
  611. if (!instances[minor].present) return -ENODEV;
  612. switch (instances[minor].mode) {
  613. case ECP:
  614. case ECP_RLE:
  615. errno = write_ecp(minor, c, cnt);
  616. break;
  617. case COMPATIBILITY:
  618. errno = write_compat(minor, c, cnt);
  619. break;
  620. default:
  621. terminate(minor);
  622. errno = write_compat(minor, c, cnt);
  623. }
  624. return errno;
  625. }
  626. static int bpp_ioctl(struct inode *inode, struct file *f, unsigned int cmd,
  627. unsigned long arg)
  628. {
  629. int errno = 0;
  630. unsigned minor = iminor(inode);
  631. if (minor >= BPP_NO) return -ENODEV;
  632. if (!instances[minor].present) return -ENODEV;
  633. switch (cmd) {
  634. case BPP_PUT_PINS:
  635. set_pins(arg, minor);
  636. break;
  637. case BPP_GET_PINS:
  638. errno = get_pins(minor);
  639. break;
  640. case BPP_PUT_DATA:
  641. bpp_outb_p(arg, base_addrs[minor]);
  642. break;
  643. case BPP_GET_DATA:
  644. errno = bpp_inb_p(base_addrs[minor]);
  645. break;
  646. case BPP_SET_INPUT:
  647. if (arg)
  648. if (instances[minor].enhanced) {
  649. unsigned short bits = get_pins(minor);
  650. instances[minor].direction = 0x20;
  651. set_pins(bits, minor);
  652. } else {
  653. errno = -ENOTTY;
  654. }
  655. else {
  656. unsigned short bits = get_pins(minor);
  657. instances[minor].direction = 0x00;
  658. set_pins(bits, minor);
  659. }
  660. break;
  661. default:
  662. errno = -EINVAL;
  663. }
  664. return errno;
  665. }
  666. static struct file_operations bpp_fops = {
  667. .owner = THIS_MODULE,
  668. .read = bpp_read,
  669. .write = bpp_write,
  670. .ioctl = bpp_ioctl,
  671. .open = bpp_open,
  672. .release = bpp_release,
  673. };
  674. #if defined(__i386__)
  675. #define collectLptPorts() {}
  676. static void probeLptPort(unsigned idx)
  677. {
  678. unsigned int testvalue;
  679. const unsigned short lpAddr = base_addrs[idx];
  680. instances[idx].present = 0;
  681. instances[idx].enhanced = 0;
  682. instances[idx].direction = 0;
  683. instances[idx].mode = COMPATIBILITY;
  684. instances[idx].run_length = 0;
  685. instances[idx].run_flag = 0;
  686. if (!request_region(lpAddr,3, dev_name)) return;
  687. /*
  688. * First, make sure the instance exists. Do this by writing to
  689. * the data latch and reading the value back. If the port *is*
  690. * present, test to see if it supports extended-mode
  691. * operation. This will be required for IEEE1284 reverse
  692. * transfers.
  693. */
  694. outb_p(BPP_PROBE_CODE, lpAddr);
  695. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  696. ;
  697. testvalue = inb_p(lpAddr);
  698. if (testvalue == BPP_PROBE_CODE) {
  699. unsigned save;
  700. instances[idx].present = 1;
  701. save = inb_p(lpAddr+2);
  702. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  703. ;
  704. outb_p(save|0x20, lpAddr+2);
  705. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  706. ;
  707. outb_p(~BPP_PROBE_CODE, lpAddr);
  708. for (testvalue=0; testvalue<BPP_DELAY; testvalue++)
  709. ;
  710. testvalue = inb_p(lpAddr);
  711. if ((testvalue&0xff) == (0xff&~BPP_PROBE_CODE))
  712. instances[idx].enhanced = 0;
  713. else
  714. instances[idx].enhanced = 1;
  715. outb_p(save, lpAddr+2);
  716. }
  717. else {
  718. release_region(lpAddr,3);
  719. }
  720. /*
  721. * Leave the port in compat idle mode.
  722. */
  723. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  724. printk("bpp%d: Port at 0x%03x: Enhanced mode %s\n", idx, base_addrs[idx],
  725. instances[idx].enhanced? "SUPPORTED" : "UNAVAILABLE");
  726. }
  727. static inline void freeLptPort(int idx)
  728. {
  729. release_region(base_addrs[idx], 3);
  730. }
  731. #endif
  732. #if defined(__sparc__)
  733. static void __iomem *map_bpp(struct sbus_dev *dev, int idx)
  734. {
  735. return sbus_ioremap(&dev->resource[0], 0, BPP_SIZE, "bpp");
  736. }
  737. static int collectLptPorts(void)
  738. {
  739. struct sbus_bus *bus;
  740. struct sbus_dev *dev;
  741. int count;
  742. count = 0;
  743. for_all_sbusdev(dev, bus) {
  744. if (strcmp(dev->prom_name, "SUNW,bpp") == 0) {
  745. if (count >= BPP_NO) {
  746. printk(KERN_NOTICE
  747. "bpp: More than %d bpp ports,"
  748. " rest is ignored\n", BPP_NO);
  749. return count;
  750. }
  751. base_addrs[count] = map_bpp(dev, count);
  752. count++;
  753. }
  754. }
  755. return count;
  756. }
  757. static void probeLptPort(unsigned idx)
  758. {
  759. void __iomem *rp = base_addrs[idx];
  760. __u32 csr;
  761. char *brand;
  762. instances[idx].present = 0;
  763. instances[idx].enhanced = 0;
  764. instances[idx].direction = 0;
  765. instances[idx].mode = COMPATIBILITY;
  766. instances[idx].run_length = 0;
  767. instances[idx].run_flag = 0;
  768. if (!rp) return;
  769. instances[idx].present = 1;
  770. instances[idx].enhanced = 1; /* Sure */
  771. csr = sbus_readl(rp + BPP_CSR);
  772. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  773. udelay(20);
  774. csr = sbus_readl(rp + BPP_CSR);
  775. if ((csr & P_DRAINING) != 0 && (csr & P_ERR_PEND) == 0) {
  776. printk("bpp%d: DRAINING still active (0x%08x)\n", idx, csr);
  777. }
  778. }
  779. printk("bpp%d: reset with 0x%08x ..", idx, csr);
  780. sbus_writel((csr | P_RESET) & ~P_INT_EN, rp + BPP_CSR);
  781. udelay(500);
  782. sbus_writel(sbus_readl(rp + BPP_CSR) & ~P_RESET, rp + BPP_CSR);
  783. csr = sbus_readl(rp + BPP_CSR);
  784. printk(" done with csr=0x%08x ocr=0x%04x\n",
  785. csr, sbus_readw(rp + BPP_OCR));
  786. switch (csr & P_DEV_ID_MASK) {
  787. case P_DEV_ID_ZEBRA:
  788. brand = "Zebra";
  789. break;
  790. case P_DEV_ID_L64854:
  791. brand = "DMA2";
  792. break;
  793. default:
  794. brand = "Unknown";
  795. }
  796. printk("bpp%d: %s at %p\n", idx, brand, rp);
  797. /*
  798. * Leave the port in compat idle mode.
  799. */
  800. set_pins(BPP_PP_nAutoFd|BPP_PP_nStrobe|BPP_PP_nInit, idx);
  801. return;
  802. }
  803. static inline void freeLptPort(int idx)
  804. {
  805. sbus_iounmap(base_addrs[idx], BPP_SIZE);
  806. }
  807. #endif
  808. static int __init bpp_init(void)
  809. {
  810. int rc;
  811. unsigned idx;
  812. rc = collectLptPorts();
  813. if (rc == 0)
  814. return -ENODEV;
  815. rc = register_chrdev(BPP_MAJOR, dev_name, &bpp_fops);
  816. if (rc < 0)
  817. return rc;
  818. for (idx = 0; idx < BPP_NO; idx++) {
  819. instances[idx].opened = 0;
  820. probeLptPort(idx);
  821. }
  822. devfs_mk_dir("bpp");
  823. for (idx = 0; idx < BPP_NO; idx++) {
  824. devfs_mk_cdev(MKDEV(BPP_MAJOR, idx),
  825. S_IFCHR | S_IRUSR | S_IWUSR, "bpp/%d", idx);
  826. }
  827. return 0;
  828. }
  829. static void __exit bpp_cleanup(void)
  830. {
  831. unsigned idx;
  832. for (idx = 0; idx < BPP_NO; idx++)
  833. devfs_remove("bpp/%d", idx);
  834. devfs_remove("bpp");
  835. unregister_chrdev(BPP_MAJOR, dev_name);
  836. for (idx = 0; idx < BPP_NO; idx++) {
  837. if (instances[idx].present)
  838. freeLptPort(idx);
  839. }
  840. }
  841. module_init(bpp_init);
  842. module_exit(bpp_cleanup);
  843. MODULE_LICENSE("GPL");