bpp.c 31 KB

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