acsi_slm.c 26 KB

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