bpp.c 30 KB

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