acsi_slm.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * acsi_slm.c -- Device driver for the Atari SLM laser printer
  3. *
  4. * Copyright 1995 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. */
  11. /*
  12. Notes:
  13. The major number for SLM printers is 28 (like ACSI), but as a character
  14. device, not block device. The minor number is the number of the printer (if
  15. you have more than one SLM; currently max. 2 (#define-constant) SLMs are
  16. supported). The device can be opened for reading and writing. If reading it,
  17. you get some status infos (MODE SENSE data). Writing mode is used for the data
  18. to be printed. Some ioctls allow to get the printer status and to tune printer
  19. modes and some internal variables.
  20. A special problem of the SLM driver is the timing and thus the buffering of
  21. the print data. The problem is that all the data for one page must be present
  22. in memory when printing starts, else --when swapping occurs-- the timing could
  23. not be guaranteed. There are several ways to assure this:
  24. 1) Reserve a buffer of 1196k (maximum page size) statically by
  25. atari_stram_alloc(). The data are collected there until they're complete,
  26. and then printing starts. Since the buffer is reserved, no further
  27. considerations about memory and swapping are needed. So this is the
  28. simplest method, but it needs a lot of memory for just the SLM.
  29. An striking advantage of this method is (supposed the SLM_CONT_CNT_REPROG
  30. method works, see there), that there are no timing problems with the DMA
  31. anymore.
  32. 2) The other method would be to reserve the buffer dynamically each time
  33. printing is required. I could think of looking at mem_map where the
  34. largest unallocted ST-RAM area is, taking the area, and then extending it
  35. by swapping out the neighbored pages, until the needed size is reached.
  36. This requires some mm hacking, but seems possible. The only obstacle could
  37. be pages that cannot be swapped out (reserved pages)...
  38. 3) Another possibility would be to leave the real data in user space and to
  39. work with two dribble buffers of about 32k in the driver: While the one
  40. buffer is DMAed to the SLM, the other can be filled with new data. But
  41. to keep the timing, that requires that the user data remain in memory and
  42. are not swapped out. Requires mm hacking, too, but maybe not so bad as
  43. method 2).
  44. */
  45. #include <linux/module.h>
  46. #include <linux/errno.h>
  47. #include <linux/sched.h>
  48. #include <linux/timer.h>
  49. #include <linux/fs.h>
  50. #include <linux/major.h>
  51. #include <linux/kernel.h>
  52. #include <linux/delay.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/time.h>
  55. #include <linux/mm.h>
  56. #include <linux/slab.h>
  57. #include <linux/smp_lock.h>
  58. #include <asm/pgtable.h>
  59. #include <asm/system.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/atarihw.h>
  62. #include <asm/atariints.h>
  63. #include <asm/atari_acsi.h>
  64. #include <asm/atari_stdma.h>
  65. #include <asm/atari_stram.h>
  66. #include <asm/atari_SLM.h>
  67. #undef DEBUG
  68. /* Define this if the page data are continuous in physical memory. That
  69. * requires less reprogramming of the ST-DMA */
  70. #define SLM_CONTINUOUS_DMA
  71. /* Use continuous reprogramming of the ST-DMA counter register. This is
  72. * --strictly speaking-- not allowed, Atari recommends not to look at the
  73. * counter register while a DMA is going on. But I don't know if that applies
  74. * only for reading the register, or also writing to it. Writing only works
  75. * fine for me... The advantage is that the timing becomes absolutely
  76. * uncritical: Just update each, say 200ms, the counter reg to its maximum,
  77. * and the DMA will work until the status byte interrupt occurs.
  78. */
  79. #define SLM_CONT_CNT_REPROG
  80. #define CMDSET_TARG_LUN(cmd,targ,lun) \
  81. do { \
  82. cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5; \
  83. cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5; \
  84. } while(0)
  85. #define START_TIMER(to) mod_timer(&slm_timer, jiffies + (to))
  86. #define STOP_TIMER() del_timer(&slm_timer)
  87. static char slmreqsense_cmd[6] = { 0x03, 0, 0, 0, 0, 0 };
  88. static char slmprint_cmd[6] = { 0x0a, 0, 0, 0, 0, 0 };
  89. static char slminquiry_cmd[6] = { 0x12, 0, 0, 0, 0, 0x80 };
  90. static char slmmsense_cmd[6] = { 0x1a, 0, 0, 0, 255, 0 };
  91. #if 0
  92. static char slmmselect_cmd[6] = { 0x15, 0, 0, 0, 0, 0 };
  93. #endif
  94. #define MAX_SLM 2
  95. static struct slm {
  96. unsigned target; /* target number */
  97. unsigned lun; /* LUN in target controller */
  98. atomic_t wr_ok; /* set to 0 if output part busy */
  99. atomic_t rd_ok; /* set to 0 if status part busy */
  100. } slm_info[MAX_SLM];
  101. int N_SLM_Printers = 0;
  102. /* printer buffer */
  103. static unsigned char *SLMBuffer; /* start of buffer */
  104. static unsigned char *BufferP; /* current position in buffer */
  105. static int BufferSize; /* length of buffer for page size */
  106. typedef enum { IDLE, FILLING, PRINTING } SLMSTATE;
  107. static SLMSTATE SLMState;
  108. static int SLMBufOwner; /* SLM# currently using the buffer */
  109. /* DMA variables */
  110. #ifndef SLM_CONT_CNT_REPROG
  111. static unsigned long SLMCurAddr; /* current base addr of DMA chunk */
  112. static unsigned long SLMEndAddr; /* expected end addr */
  113. static unsigned long SLMSliceSize; /* size of one DMA chunk */
  114. #endif
  115. static int SLMError;
  116. /* wait queues */
  117. static DECLARE_WAIT_QUEUE_HEAD(slm_wait); /* waiting for buffer */
  118. static DECLARE_WAIT_QUEUE_HEAD(print_wait); /* waiting for printing finished */
  119. /* status codes */
  120. #define SLMSTAT_OK 0x00
  121. #define SLMSTAT_ORNERY 0x02
  122. #define SLMSTAT_TONER 0x03
  123. #define SLMSTAT_WARMUP 0x04
  124. #define SLMSTAT_PAPER 0x05
  125. #define SLMSTAT_DRUM 0x06
  126. #define SLMSTAT_INJAM 0x07
  127. #define SLMSTAT_THRJAM 0x08
  128. #define SLMSTAT_OUTJAM 0x09
  129. #define SLMSTAT_COVER 0x0a
  130. #define SLMSTAT_FUSER 0x0b
  131. #define SLMSTAT_IMAGER 0x0c
  132. #define SLMSTAT_MOTOR 0x0d
  133. #define SLMSTAT_VIDEO 0x0e
  134. #define SLMSTAT_SYSTO 0x10
  135. #define SLMSTAT_OPCODE 0x12
  136. #define SLMSTAT_DEVNUM 0x15
  137. #define SLMSTAT_PARAM 0x1a
  138. #define SLMSTAT_ACSITO 0x1b /* driver defined */
  139. #define SLMSTAT_NOTALL 0x1c /* driver defined */
  140. static char *SLMErrors[] = {
  141. /* 0x00 */ "OK and ready",
  142. /* 0x01 */ NULL,
  143. /* 0x02 */ "ornery printer",
  144. /* 0x03 */ "toner empty",
  145. /* 0x04 */ "warming up",
  146. /* 0x05 */ "paper empty",
  147. /* 0x06 */ "drum empty",
  148. /* 0x07 */ "input jam",
  149. /* 0x08 */ "through jam",
  150. /* 0x09 */ "output jam",
  151. /* 0x0a */ "cover open",
  152. /* 0x0b */ "fuser malfunction",
  153. /* 0x0c */ "imager malfunction",
  154. /* 0x0d */ "motor malfunction",
  155. /* 0x0e */ "video malfunction",
  156. /* 0x0f */ NULL,
  157. /* 0x10 */ "printer system timeout",
  158. /* 0x11 */ NULL,
  159. /* 0x12 */ "invalid operation code",
  160. /* 0x13 */ NULL,
  161. /* 0x14 */ NULL,
  162. /* 0x15 */ "invalid device number",
  163. /* 0x16 */ NULL,
  164. /* 0x17 */ NULL,
  165. /* 0x18 */ NULL,
  166. /* 0x19 */ NULL,
  167. /* 0x1a */ "invalid parameter list",
  168. /* 0x1b */ "ACSI timeout",
  169. /* 0x1c */ "not all printed"
  170. };
  171. #define N_ERRORS (sizeof(SLMErrors)/sizeof(*SLMErrors))
  172. /* real (driver caused) error? */
  173. #define IS_REAL_ERROR(x) (x > 0x10)
  174. static struct {
  175. char *name;
  176. int w, h;
  177. } StdPageSize[] = {
  178. { "Letter", 2400, 3180 },
  179. { "Legal", 2400, 4080 },
  180. { "A4", 2336, 3386 },
  181. { "B5", 2016, 2914 }
  182. };
  183. #define N_STD_SIZES (sizeof(StdPageSize)/sizeof(*StdPageSize))
  184. #define SLM_BUFFER_SIZE (2336*3386/8) /* A4 for now */
  185. #define SLM_DMA_AMOUNT 255 /* #sectors to program the DMA for */
  186. #ifdef SLM_CONTINUOUS_DMA
  187. # define SLM_DMA_INT_OFFSET 0 /* DMA goes until seccnt 0, no offs */
  188. # define SLM_DMA_END_OFFSET 32 /* 32 Byte ST-DMA FIFO */
  189. # define SLM_SLICE_SIZE(w) (255*512)
  190. #else
  191. # define SLM_DMA_INT_OFFSET 32 /* 32 Byte ST-DMA FIFO */
  192. # define SLM_DMA_END_OFFSET 32 /* 32 Byte ST-DMA FIFO */
  193. # define SLM_SLICE_SIZE(w) ((254*512)/(w/8)*(w/8))
  194. #endif
  195. /* calculate the number of jiffies to wait for 'n' bytes */
  196. #ifdef SLM_CONT_CNT_REPROG
  197. #define DMA_TIME_FOR(n) 50
  198. #define DMA_STARTUP_TIME 0
  199. #else
  200. #define DMA_TIME_FOR(n) (n/1400-1)
  201. #define DMA_STARTUP_TIME 650
  202. #endif
  203. /***************************** Prototypes *****************************/
  204. static char *slm_errstr( int stat );
  205. static int slm_getstats( char *buffer, int device );
  206. static ssize_t slm_read( struct file* file, char *buf, size_t count, loff_t
  207. *ppos );
  208. static void start_print( int device );
  209. static irqreturn_t slm_interrupt(int irc, void *data);
  210. static void slm_test_ready( unsigned long dummy );
  211. static void set_dma_addr( unsigned long paddr );
  212. static unsigned long get_dma_addr( void );
  213. static ssize_t slm_write( struct file *file, const char *buf, size_t count,
  214. loff_t *ppos );
  215. static int slm_ioctl( struct inode *inode, struct file *file, unsigned int
  216. cmd, unsigned long arg );
  217. static int slm_open( struct inode *inode, struct file *file );
  218. static int slm_release( struct inode *inode, struct file *file );
  219. static int slm_req_sense( int device );
  220. static int slm_mode_sense( int device, char *buffer, int abs_flag );
  221. #if 0
  222. static int slm_mode_select( int device, char *buffer, int len, int
  223. default_flag );
  224. #endif
  225. static int slm_get_pagesize( int device, int *w, int *h );
  226. /************************* End of Prototypes **************************/
  227. static DEFINE_TIMER(slm_timer, slm_test_ready, 0, 0);
  228. static struct file_operations slm_fops = {
  229. .owner = THIS_MODULE,
  230. .read = slm_read,
  231. .write = slm_write,
  232. .ioctl = slm_ioctl,
  233. .open = slm_open,
  234. .release = slm_release,
  235. };
  236. /* ---------------------------------------------------------------------- */
  237. /* Status Functions */
  238. static char *slm_errstr( int stat )
  239. { char *p;
  240. static char str[22];
  241. stat &= 0x1f;
  242. if (stat >= 0 && stat < N_ERRORS && (p = SLMErrors[stat]))
  243. return( p );
  244. sprintf( str, "unknown status 0x%02x", stat );
  245. return( str );
  246. }
  247. static int slm_getstats( char *buffer, int device )
  248. { int len = 0, stat, i, w, h;
  249. unsigned char buf[256];
  250. stat = slm_mode_sense( device, buf, 0 );
  251. if (IS_REAL_ERROR(stat))
  252. return( -EIO );
  253. #define SHORTDATA(i) ((buf[i] << 8) | buf[i+1])
  254. #define BOOLDATA(i,mask) ((buf[i] & mask) ? "on" : "off")
  255. w = SHORTDATA( 3 );
  256. h = SHORTDATA( 1 );
  257. len += sprintf( buffer+len, "Status\t\t%s\n",
  258. slm_errstr( stat ) );
  259. len += sprintf( buffer+len, "Page Size\t%dx%d",
  260. w, h );
  261. for( i = 0; i < N_STD_SIZES; ++i ) {
  262. if (w == StdPageSize[i].w && h == StdPageSize[i].h)
  263. break;
  264. }
  265. if (i < N_STD_SIZES)
  266. len += sprintf( buffer+len, " (%s)", StdPageSize[i].name );
  267. buffer[len++] = '\n';
  268. len += sprintf( buffer+len, "Top/Left Margin\t%d/%d\n",
  269. SHORTDATA( 5 ), SHORTDATA( 7 ) );
  270. len += sprintf( buffer+len, "Manual Feed\t%s\n",
  271. BOOLDATA( 9, 0x01 ) );
  272. len += sprintf( buffer+len, "Input Select\t%d\n",
  273. (buf[9] >> 1) & 7 );
  274. len += sprintf( buffer+len, "Auto Select\t%s\n",
  275. BOOLDATA( 9, 0x10 ) );
  276. len += sprintf( buffer+len, "Prefeed Paper\t%s\n",
  277. BOOLDATA( 9, 0x20 ) );
  278. len += sprintf( buffer+len, "Thick Pixels\t%s\n",
  279. BOOLDATA( 9, 0x40 ) );
  280. len += sprintf( buffer+len, "H/V Resol.\t%d/%d dpi\n",
  281. SHORTDATA( 12 ), SHORTDATA( 10 ) );
  282. len += sprintf( buffer+len, "System Timeout\t%d\n",
  283. buf[14] );
  284. len += sprintf( buffer+len, "Scan Time\t%d\n",
  285. SHORTDATA( 15 ) );
  286. len += sprintf( buffer+len, "Page Count\t%d\n",
  287. SHORTDATA( 17 ) );
  288. len += sprintf( buffer+len, "In/Out Cap.\t%d/%d\n",
  289. SHORTDATA( 19 ), SHORTDATA( 21 ) );
  290. len += sprintf( buffer+len, "Stagger Output\t%s\n",
  291. BOOLDATA( 23, 0x01 ) );
  292. len += sprintf( buffer+len, "Output Select\t%d\n",
  293. (buf[23] >> 1) & 7 );
  294. len += sprintf( buffer+len, "Duplex Print\t%s\n",
  295. BOOLDATA( 23, 0x10 ) );
  296. len += sprintf( buffer+len, "Color Sep.\t%s\n",
  297. BOOLDATA( 23, 0x20 ) );
  298. return( len );
  299. }
  300. static ssize_t slm_read( struct file *file, char *buf, size_t count,
  301. loff_t *ppos )
  302. {
  303. struct inode *node = file->f_dentry->d_inode;
  304. unsigned long page;
  305. int length;
  306. int end;
  307. if (!(page = __get_free_page( GFP_KERNEL )))
  308. return( -ENOMEM );
  309. length = slm_getstats( (char *)page, iminor(node) );
  310. if (length < 0) {
  311. count = length;
  312. goto out;
  313. }
  314. if (file->f_pos >= length) {
  315. count = 0;
  316. goto out;
  317. }
  318. if (count + file->f_pos > length)
  319. count = length - file->f_pos;
  320. end = count + file->f_pos;
  321. if (copy_to_user(buf, (char *)page + file->f_pos, count)) {
  322. count = -EFAULT;
  323. goto out;
  324. }
  325. file->f_pos = end;
  326. out: free_page( page );
  327. return( count );
  328. }
  329. /* ---------------------------------------------------------------------- */
  330. /* Printing */
  331. static void start_print( int device )
  332. { struct slm *sip = &slm_info[device];
  333. unsigned char *cmd;
  334. unsigned long paddr;
  335. int i;
  336. stdma_lock( slm_interrupt, NULL );
  337. CMDSET_TARG_LUN( slmprint_cmd, sip->target, sip->lun );
  338. cmd = slmprint_cmd;
  339. paddr = virt_to_phys( SLMBuffer );
  340. dma_cache_maintenance( paddr, virt_to_phys(BufferP)-paddr, 1 );
  341. DISABLE_IRQ();
  342. /* Low on A1 */
  343. dma_wd.dma_mode_status = 0x88;
  344. MFPDELAY();
  345. /* send the command bytes except the last */
  346. for( i = 0; i < 5; ++i ) {
  347. DMA_LONG_WRITE( *cmd++, 0x8a );
  348. udelay(20);
  349. if (!acsi_wait_for_IRQ( HZ/2 )) {
  350. SLMError = 1;
  351. return; /* timeout */
  352. }
  353. }
  354. /* last command byte */
  355. DMA_LONG_WRITE( *cmd++, 0x82 );
  356. MFPDELAY();
  357. /* set DMA address */
  358. set_dma_addr( paddr );
  359. /* program DMA for write and select sector counter reg */
  360. dma_wd.dma_mode_status = 0x192;
  361. MFPDELAY();
  362. /* program for 255*512 bytes and start DMA */
  363. DMA_LONG_WRITE( SLM_DMA_AMOUNT, 0x112 );
  364. #ifndef SLM_CONT_CNT_REPROG
  365. SLMCurAddr = paddr;
  366. SLMEndAddr = paddr + SLMSliceSize + SLM_DMA_INT_OFFSET;
  367. #endif
  368. START_TIMER( DMA_STARTUP_TIME + DMA_TIME_FOR( SLMSliceSize ));
  369. #if !defined(SLM_CONT_CNT_REPROG) && defined(DEBUG)
  370. printk( "SLM: CurAddr=%#lx EndAddr=%#lx timer=%ld\n",
  371. SLMCurAddr, SLMEndAddr, DMA_TIME_FOR( SLMSliceSize ) );
  372. #endif
  373. ENABLE_IRQ();
  374. }
  375. /* Only called when an error happened or at the end of a page */
  376. static irqreturn_t slm_interrupt(int irc, void *data)
  377. { unsigned long addr;
  378. int stat;
  379. STOP_TIMER();
  380. addr = get_dma_addr();
  381. stat = acsi_getstatus();
  382. SLMError = (stat < 0) ? SLMSTAT_ACSITO :
  383. (addr < virt_to_phys(BufferP)) ? SLMSTAT_NOTALL :
  384. stat;
  385. dma_wd.dma_mode_status = 0x80;
  386. MFPDELAY();
  387. #ifdef DEBUG
  388. printk( "SLM: interrupt, addr=%#lx, error=%d\n", addr, SLMError );
  389. #endif
  390. wake_up( &print_wait );
  391. stdma_release();
  392. ENABLE_IRQ();
  393. return IRQ_HANDLED;
  394. }
  395. static void slm_test_ready( unsigned long dummy )
  396. {
  397. #ifdef SLM_CONT_CNT_REPROG
  398. /* program for 255*512 bytes again */
  399. dma_wd.fdc_acces_seccount = SLM_DMA_AMOUNT;
  400. START_TIMER( DMA_TIME_FOR(0) );
  401. #ifdef DEBUG
  402. printk( "SLM: reprogramming timer for %d jiffies, addr=%#lx\n",
  403. DMA_TIME_FOR(0), get_dma_addr() );
  404. #endif
  405. #else /* !SLM_CONT_CNT_REPROG */
  406. unsigned long flags, addr;
  407. int d, ti;
  408. #ifdef DEBUG
  409. struct timeval start_tm, end_tm;
  410. int did_wait = 0;
  411. #endif
  412. local_irq_save(flags);
  413. addr = get_dma_addr();
  414. if ((d = SLMEndAddr - addr) > 0) {
  415. local_irq_restore(flags);
  416. /* slice not yet finished, decide whether to start another timer or to
  417. * busy-wait */
  418. ti = DMA_TIME_FOR( d );
  419. if (ti > 0) {
  420. #ifdef DEBUG
  421. printk( "SLM: reprogramming timer for %d jiffies, rest %d bytes\n",
  422. ti, d );
  423. #endif
  424. START_TIMER( ti );
  425. return;
  426. }
  427. /* wait for desired end address to be reached */
  428. #ifdef DEBUG
  429. do_gettimeofday( &start_tm );
  430. did_wait = 1;
  431. #endif
  432. local_irq_disable();
  433. while( get_dma_addr() < SLMEndAddr )
  434. barrier();
  435. }
  436. /* slice finished, start next one */
  437. SLMCurAddr += SLMSliceSize;
  438. #ifdef SLM_CONTINUOUS_DMA
  439. /* program for 255*512 bytes again */
  440. dma_wd.fdc_acces_seccount = SLM_DMA_AMOUNT;
  441. #else
  442. /* set DMA address;
  443. * add 2 bytes for the ones in the SLM controller FIFO! */
  444. set_dma_addr( SLMCurAddr + 2 );
  445. /* toggle DMA to write and select sector counter reg */
  446. dma_wd.dma_mode_status = 0x92;
  447. MFPDELAY();
  448. dma_wd.dma_mode_status = 0x192;
  449. MFPDELAY();
  450. /* program for 255*512 bytes and start DMA */
  451. DMA_LONG_WRITE( SLM_DMA_AMOUNT, 0x112 );
  452. #endif
  453. local_irq_restore(flags);
  454. #ifdef DEBUG
  455. if (did_wait) {
  456. int ms;
  457. do_gettimeofday( &end_tm );
  458. ms = (end_tm.tv_sec*1000000+end_tm.tv_usec) -
  459. (start_tm.tv_sec*1000000+start_tm.tv_usec);
  460. printk( "SLM: did %ld.%ld ms busy waiting for %d bytes\n",
  461. ms/1000, ms%1000, d );
  462. }
  463. else
  464. printk( "SLM: didn't wait (!)\n" );
  465. #endif
  466. if ((unsigned char *)PTOV( SLMCurAddr + SLMSliceSize ) >= BufferP) {
  467. /* will be last slice, no timer necessary */
  468. #ifdef DEBUG
  469. printk( "SLM: CurAddr=%#lx EndAddr=%#lx last slice -> no timer\n",
  470. SLMCurAddr, SLMEndAddr );
  471. #endif
  472. }
  473. else {
  474. /* not last slice */
  475. SLMEndAddr = SLMCurAddr + SLMSliceSize + SLM_DMA_INT_OFFSET;
  476. START_TIMER( DMA_TIME_FOR( SLMSliceSize ));
  477. #ifdef DEBUG
  478. printk( "SLM: CurAddr=%#lx EndAddr=%#lx timer=%ld\n",
  479. SLMCurAddr, SLMEndAddr, DMA_TIME_FOR( SLMSliceSize ) );
  480. #endif
  481. }
  482. #endif /* SLM_CONT_CNT_REPROG */
  483. }
  484. static void set_dma_addr( unsigned long paddr )
  485. { unsigned long flags;
  486. local_irq_save(flags);
  487. dma_wd.dma_lo = (unsigned char)paddr;
  488. paddr >>= 8;
  489. MFPDELAY();
  490. dma_wd.dma_md = (unsigned char)paddr;
  491. paddr >>= 8;
  492. MFPDELAY();
  493. if (ATARIHW_PRESENT( EXTD_DMA ))
  494. st_dma_ext_dmahi = (unsigned short)paddr;
  495. else
  496. dma_wd.dma_hi = (unsigned char)paddr;
  497. MFPDELAY();
  498. local_irq_restore(flags);
  499. }
  500. static unsigned long get_dma_addr( void )
  501. { unsigned long addr;
  502. addr = dma_wd.dma_lo & 0xff;
  503. MFPDELAY();
  504. addr |= (dma_wd.dma_md & 0xff) << 8;
  505. MFPDELAY();
  506. addr |= (dma_wd.dma_hi & 0xff) << 16;
  507. MFPDELAY();
  508. return( addr );
  509. }
  510. static ssize_t slm_write( struct file *file, const char *buf, size_t count,
  511. loff_t *ppos )
  512. {
  513. struct inode *node = file->f_dentry->d_inode;
  514. int device = iminor(node);
  515. int n, filled, w, h;
  516. while( SLMState == PRINTING ||
  517. (SLMState == FILLING && SLMBufOwner != device) ) {
  518. interruptible_sleep_on( &slm_wait );
  519. if (signal_pending(current))
  520. return( -ERESTARTSYS );
  521. }
  522. if (SLMState == IDLE) {
  523. /* first data of page: get current page size */
  524. if (slm_get_pagesize( device, &w, &h ))
  525. return( -EIO );
  526. BufferSize = w*h/8;
  527. if (BufferSize > SLM_BUFFER_SIZE)
  528. return( -ENOMEM );
  529. SLMState = FILLING;
  530. SLMBufOwner = device;
  531. }
  532. n = count;
  533. filled = BufferP - SLMBuffer;
  534. if (filled + n > BufferSize)
  535. n = BufferSize - filled;
  536. if (copy_from_user(BufferP, buf, n))
  537. return -EFAULT;
  538. BufferP += n;
  539. filled += n;
  540. if (filled == BufferSize) {
  541. /* Check the paper size again! The user may have switched it in the
  542. * time between starting the data and finishing them. Would end up in
  543. * a trashy page... */
  544. if (slm_get_pagesize( device, &w, &h ))
  545. return( -EIO );
  546. if (BufferSize != w*h/8) {
  547. printk( KERN_NOTICE "slm%d: page size changed while printing\n",
  548. device );
  549. return( -EAGAIN );
  550. }
  551. SLMState = PRINTING;
  552. /* choose a slice size that is a multiple of the line size */
  553. #ifndef SLM_CONT_CNT_REPROG
  554. SLMSliceSize = SLM_SLICE_SIZE(w);
  555. #endif
  556. start_print( device );
  557. sleep_on( &print_wait );
  558. if (SLMError && IS_REAL_ERROR(SLMError)) {
  559. printk( KERN_ERR "slm%d: %s\n", device, slm_errstr(SLMError) );
  560. n = -EIO;
  561. }
  562. SLMState = IDLE;
  563. BufferP = SLMBuffer;
  564. wake_up_interruptible( &slm_wait );
  565. }
  566. return( n );
  567. }
  568. /* ---------------------------------------------------------------------- */
  569. /* ioctl Functions */
  570. static int slm_ioctl( struct inode *inode, struct file *file,
  571. unsigned int cmd, unsigned long arg )
  572. { int device = iminor(inode), err;
  573. /* I can think of setting:
  574. * - manual feed
  575. * - paper format
  576. * - copy count
  577. * - ...
  578. * but haven't implemented that yet :-)
  579. * BTW, has anybody better docs about the MODE SENSE/MODE SELECT data?
  580. */
  581. switch( cmd ) {
  582. case SLMIORESET: /* reset buffer, i.e. empty the buffer */
  583. if (!(file->f_mode & 2))
  584. return( -EINVAL );
  585. if (SLMState == PRINTING)
  586. return( -EBUSY );
  587. SLMState = IDLE;
  588. BufferP = SLMBuffer;
  589. wake_up_interruptible( &slm_wait );
  590. return( 0 );
  591. case SLMIOGSTAT: { /* get status */
  592. int stat;
  593. char *str;
  594. stat = slm_req_sense( device );
  595. if (arg) {
  596. str = slm_errstr( stat );
  597. if (put_user(stat,
  598. (long *)&((struct SLM_status *)arg)->stat))
  599. return -EFAULT;
  600. if (copy_to_user( ((struct SLM_status *)arg)->str, str,
  601. strlen(str) + 1))
  602. return -EFAULT;
  603. }
  604. return( stat );
  605. }
  606. case SLMIOGPSIZE: { /* get paper size */
  607. int w, h;
  608. if ((err = slm_get_pagesize( device, &w, &h ))) return( err );
  609. if (put_user(w, (long *)&((struct SLM_paper_size *)arg)->width))
  610. return -EFAULT;
  611. if (put_user(h, (long *)&((struct SLM_paper_size *)arg)->height))
  612. return -EFAULT;
  613. return( 0 );
  614. }
  615. case SLMIOGMFEED: /* get manual feed */
  616. return( -EINVAL );
  617. case SLMIOSPSIZE: /* set paper size */
  618. return( -EINVAL );
  619. case SLMIOSMFEED: /* set manual feed */
  620. return( -EINVAL );
  621. }
  622. return( -EINVAL );
  623. }
  624. /* ---------------------------------------------------------------------- */
  625. /* Opening and Closing */
  626. static int slm_open( struct inode *inode, struct file *file )
  627. { int device;
  628. struct slm *sip;
  629. device = iminor(inode);
  630. if (device >= N_SLM_Printers)
  631. return( -ENXIO );
  632. sip = &slm_info[device];
  633. if (file->f_mode & 2) {
  634. /* open for writing is exclusive */
  635. if ( !atomic_dec_and_test(&sip->wr_ok) ) {
  636. atomic_inc(&sip->wr_ok);
  637. return( -EBUSY );
  638. }
  639. }
  640. if (file->f_mode & 1) {
  641. /* open for reading is exclusive */
  642. if ( !atomic_dec_and_test(&sip->rd_ok) ) {
  643. atomic_inc(&sip->rd_ok);
  644. return( -EBUSY );
  645. }
  646. }
  647. return( 0 );
  648. }
  649. static int slm_release( struct inode *inode, struct file *file )
  650. { int device;
  651. struct slm *sip;
  652. device = iminor(inode);
  653. sip = &slm_info[device];
  654. if (file->f_mode & 2)
  655. atomic_inc( &sip->wr_ok );
  656. if (file->f_mode & 1)
  657. atomic_inc( &sip->rd_ok );
  658. return( 0 );
  659. }
  660. /* ---------------------------------------------------------------------- */
  661. /* ACSI Primitives for the SLM */
  662. static int slm_req_sense( int device )
  663. { int stat, rv;
  664. struct slm *sip = &slm_info[device];
  665. stdma_lock( NULL, NULL );
  666. CMDSET_TARG_LUN( slmreqsense_cmd, sip->target, sip->lun );
  667. if (!acsicmd_nodma( slmreqsense_cmd, 0 ) ||
  668. (stat = acsi_getstatus()) < 0)
  669. rv = SLMSTAT_ACSITO;
  670. else
  671. rv = stat & 0x1f;
  672. ENABLE_IRQ();
  673. stdma_release();
  674. return( rv );
  675. }
  676. static int slm_mode_sense( int device, char *buffer, int abs_flag )
  677. { unsigned char stat, len;
  678. int rv = 0;
  679. struct slm *sip = &slm_info[device];
  680. stdma_lock( NULL, NULL );
  681. CMDSET_TARG_LUN( slmmsense_cmd, sip->target, sip->lun );
  682. slmmsense_cmd[5] = abs_flag ? 0x80 : 0;
  683. if (!acsicmd_nodma( slmmsense_cmd, 0 )) {
  684. rv = SLMSTAT_ACSITO;
  685. goto the_end;
  686. }
  687. if (!acsi_extstatus( &stat, 1 )) {
  688. acsi_end_extstatus();
  689. rv = SLMSTAT_ACSITO;
  690. goto the_end;
  691. }
  692. if (!acsi_extstatus( &len, 1 )) {
  693. acsi_end_extstatus();
  694. rv = SLMSTAT_ACSITO;
  695. goto the_end;
  696. }
  697. buffer[0] = len;
  698. if (!acsi_extstatus( buffer+1, len )) {
  699. acsi_end_extstatus();
  700. rv = SLMSTAT_ACSITO;
  701. goto the_end;
  702. }
  703. acsi_end_extstatus();
  704. rv = stat & 0x1f;
  705. the_end:
  706. ENABLE_IRQ();
  707. stdma_release();
  708. return( rv );
  709. }
  710. #if 0
  711. /* currently unused */
  712. static int slm_mode_select( int device, char *buffer, int len,
  713. int default_flag )
  714. { int stat, rv;
  715. struct slm *sip = &slm_info[device];
  716. stdma_lock( NULL, NULL );
  717. CMDSET_TARG_LUN( slmmselect_cmd, sip->target, sip->lun );
  718. slmmselect_cmd[5] = default_flag ? 0x80 : 0;
  719. if (!acsicmd_nodma( slmmselect_cmd, 0 )) {
  720. rv = SLMSTAT_ACSITO;
  721. goto the_end;
  722. }
  723. if (!default_flag) {
  724. unsigned char c = len;
  725. if (!acsi_extcmd( &c, 1 )) {
  726. rv = SLMSTAT_ACSITO;
  727. goto the_end;
  728. }
  729. if (!acsi_extcmd( buffer, len )) {
  730. rv = SLMSTAT_ACSITO;
  731. goto the_end;
  732. }
  733. }
  734. stat = acsi_getstatus();
  735. rv = (stat < 0 ? SLMSTAT_ACSITO : stat);
  736. the_end:
  737. ENABLE_IRQ();
  738. stdma_release();
  739. return( rv );
  740. }
  741. #endif
  742. static int slm_get_pagesize( int device, int *w, int *h )
  743. { char buf[256];
  744. int stat;
  745. stat = slm_mode_sense( device, buf, 0 );
  746. ENABLE_IRQ();
  747. stdma_release();
  748. if (stat != SLMSTAT_OK)
  749. return( -EIO );
  750. *w = (buf[3] << 8) | buf[4];
  751. *h = (buf[1] << 8) | buf[2];
  752. return( 0 );
  753. }
  754. /* ---------------------------------------------------------------------- */
  755. /* Initialization */
  756. int attach_slm( int target, int lun )
  757. { static int did_register;
  758. int len;
  759. if (N_SLM_Printers >= MAX_SLM) {
  760. printk( KERN_WARNING "Too much SLMs\n" );
  761. return( 0 );
  762. }
  763. /* do an INQUIRY */
  764. udelay(100);
  765. CMDSET_TARG_LUN( slminquiry_cmd, target, lun );
  766. if (!acsicmd_nodma( slminquiry_cmd, 0 )) {
  767. inq_timeout:
  768. printk( KERN_ERR "SLM inquiry command timed out.\n" );
  769. inq_fail:
  770. acsi_end_extstatus();
  771. return( 0 );
  772. }
  773. /* read status and header of return data */
  774. if (!acsi_extstatus( SLMBuffer, 6 ))
  775. goto inq_timeout;
  776. if (SLMBuffer[1] != 2) { /* device type == printer? */
  777. printk( KERN_ERR "SLM inquiry returned device type != printer\n" );
  778. goto inq_fail;
  779. }
  780. len = SLMBuffer[5];
  781. /* read id string */
  782. if (!acsi_extstatus( SLMBuffer, len ))
  783. goto inq_timeout;
  784. acsi_end_extstatus();
  785. SLMBuffer[len] = 0;
  786. if (!did_register) {
  787. did_register = 1;
  788. }
  789. slm_info[N_SLM_Printers].target = target;
  790. slm_info[N_SLM_Printers].lun = lun;
  791. atomic_set(&slm_info[N_SLM_Printers].wr_ok, 1 );
  792. atomic_set(&slm_info[N_SLM_Printers].rd_ok, 1 );
  793. printk( KERN_INFO " Printer: %s\n", SLMBuffer );
  794. printk( KERN_INFO "Detected slm%d at id %d lun %d\n",
  795. N_SLM_Printers, target, lun );
  796. N_SLM_Printers++;
  797. return( 1 );
  798. }
  799. int slm_init( void )
  800. {
  801. int i;
  802. if (register_chrdev( ACSI_MAJOR, "slm", &slm_fops )) {
  803. printk( KERN_ERR "Unable to get major %d for ACSI SLM\n", ACSI_MAJOR );
  804. return -EBUSY;
  805. }
  806. if (!(SLMBuffer = atari_stram_alloc( SLM_BUFFER_SIZE, "SLM" ))) {
  807. printk( KERN_ERR "Unable to get SLM ST-Ram buffer.\n" );
  808. unregister_chrdev( ACSI_MAJOR, "slm" );
  809. return -ENOMEM;
  810. }
  811. BufferP = SLMBuffer;
  812. SLMState = IDLE;
  813. return 0;
  814. }
  815. #ifdef MODULE
  816. /* from acsi.c */
  817. void acsi_attach_SLMs( int (*attach_func)( int, int ) );
  818. int init_module(void)
  819. {
  820. int err;
  821. if ((err = slm_init()))
  822. return( err );
  823. /* This calls attach_slm() for every target/lun where acsi.c detected a
  824. * printer */
  825. acsi_attach_SLMs( attach_slm );
  826. return( 0 );
  827. }
  828. void cleanup_module(void)
  829. {
  830. if (unregister_chrdev( ACSI_MAJOR, "slm" ) != 0)
  831. printk( KERN_ERR "acsi_slm: cleanup_module failed\n");
  832. atari_stram_free( SLMBuffer );
  833. }
  834. #endif