bpp.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. devfs_mk_dir("bpp");
  822. for (idx = 0; idx < BPP_NO; idx++) {
  823. devfs_mk_cdev(MKDEV(BPP_MAJOR, idx),
  824. S_IFCHR | S_IRUSR | S_IWUSR, "bpp/%d", idx);
  825. }
  826. return 0;
  827. }
  828. static void __exit bpp_cleanup(void)
  829. {
  830. unsigned idx;
  831. for (idx = 0; idx < BPP_NO; idx++)
  832. devfs_remove("bpp/%d", idx);
  833. devfs_remove("bpp");
  834. unregister_chrdev(BPP_MAJOR, dev_name);
  835. for (idx = 0; idx < BPP_NO; idx++) {
  836. if (instances[idx].present)
  837. freeLptPort(idx);
  838. }
  839. }
  840. module_init(bpp_init);
  841. module_exit(bpp_cleanup);
  842. MODULE_LICENSE("GPL");