con3215.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * drivers/s390/char/con3215.c
  3. * 3215 line mode terminal driver.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  8. *
  9. * Updated:
  10. * Aug-2000: Added tab support
  11. * Dan Morrison, IBM Corporation (dmorriso@cse.buffalo.edu)
  12. */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/kdev_t.h>
  17. #include <linux/tty.h>
  18. #include <linux/vt_kern.h>
  19. #include <linux/init.h>
  20. #include <linux/console.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/bootmem.h>
  24. #include <asm/ccwdev.h>
  25. #include <asm/cio.h>
  26. #include <asm/io.h>
  27. #include <asm/ebcdic.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/delay.h>
  30. #include <asm/cpcmd.h>
  31. #include <asm/setup.h>
  32. #include "ctrlchar.h"
  33. #define NR_3215 1
  34. #define NR_3215_REQ (4*NR_3215)
  35. #define RAW3215_BUFFER_SIZE 65536 /* output buffer size */
  36. #define RAW3215_INBUF_SIZE 256 /* input buffer size */
  37. #define RAW3215_MIN_SPACE 128 /* minimum free space for wakeup */
  38. #define RAW3215_MIN_WRITE 1024 /* min. length for immediate output */
  39. #define RAW3215_MAX_BYTES 3968 /* max. bytes to write with one ssch */
  40. #define RAW3215_MAX_NEWLINE 50 /* max. lines to write with one ssch */
  41. #define RAW3215_NR_CCWS 3
  42. #define RAW3215_TIMEOUT HZ/10 /* time for delayed output */
  43. #define RAW3215_FIXED 1 /* 3215 console device is not be freed */
  44. #define RAW3215_ACTIVE 2 /* set if the device is in use */
  45. #define RAW3215_WORKING 4 /* set if a request is being worked on */
  46. #define RAW3215_THROTTLED 8 /* set if reading is disabled */
  47. #define RAW3215_STOPPED 16 /* set if writing is disabled */
  48. #define RAW3215_CLOSING 32 /* set while in close process */
  49. #define RAW3215_TIMER_RUNS 64 /* set if the output delay timer is on */
  50. #define RAW3215_FLUSHING 128 /* set to flush buffer (no delay) */
  51. #define TAB_STOP_SIZE 8 /* tab stop size */
  52. /*
  53. * Request types for a 3215 device
  54. */
  55. enum raw3215_type {
  56. RAW3215_FREE, RAW3215_READ, RAW3215_WRITE
  57. };
  58. /*
  59. * Request structure for a 3215 device
  60. */
  61. struct raw3215_req {
  62. enum raw3215_type type; /* type of the request */
  63. int start, len; /* start index & len in output buffer */
  64. int delayable; /* indication to wait for more data */
  65. int residual; /* residual count for read request */
  66. struct ccw1 ccws[RAW3215_NR_CCWS]; /* space for the channel program */
  67. struct raw3215_info *info; /* pointer to main structure */
  68. struct raw3215_req *next; /* pointer to next request */
  69. } __attribute__ ((aligned(8)));
  70. struct raw3215_info {
  71. struct ccw_device *cdev; /* device for tty driver */
  72. spinlock_t *lock; /* pointer to irq lock */
  73. int flags; /* state flags */
  74. char *buffer; /* pointer to output buffer */
  75. char *inbuf; /* pointer to input buffer */
  76. int head; /* first free byte in output buffer */
  77. int count; /* number of bytes in output buffer */
  78. int written; /* number of bytes in write requests */
  79. struct tty_struct *tty; /* pointer to tty structure if present */
  80. struct tasklet_struct tasklet;
  81. struct raw3215_req *queued_read; /* pointer to queued read requests */
  82. struct raw3215_req *queued_write;/* pointer to queued write requests */
  83. wait_queue_head_t empty_wait; /* wait queue for flushing */
  84. struct timer_list timer; /* timer for delayed output */
  85. char *message; /* pending message from raw3215_irq */
  86. int msg_dstat; /* dstat for pending message */
  87. int msg_cstat; /* cstat for pending message */
  88. int line_pos; /* position on the line (for tabs) */
  89. char ubuffer[80]; /* copy_from_user buffer */
  90. };
  91. /* array of 3215 devices structures */
  92. static struct raw3215_info *raw3215[NR_3215];
  93. /* spinlock to protect the raw3215 array */
  94. static DEFINE_SPINLOCK(raw3215_device_lock);
  95. /* list of free request structures */
  96. static struct raw3215_req *raw3215_freelist;
  97. /* spinlock to protect free list */
  98. static spinlock_t raw3215_freelist_lock;
  99. static struct tty_driver *tty3215_driver;
  100. /*
  101. * Get a request structure from the free list
  102. */
  103. static inline struct raw3215_req *
  104. raw3215_alloc_req(void) {
  105. struct raw3215_req *req;
  106. unsigned long flags;
  107. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  108. req = raw3215_freelist;
  109. raw3215_freelist = req->next;
  110. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  111. return req;
  112. }
  113. /*
  114. * Put a request structure back to the free list
  115. */
  116. static inline void
  117. raw3215_free_req(struct raw3215_req *req) {
  118. unsigned long flags;
  119. if (req->type == RAW3215_FREE)
  120. return; /* don't free a free request */
  121. req->type = RAW3215_FREE;
  122. spin_lock_irqsave(&raw3215_freelist_lock, flags);
  123. req->next = raw3215_freelist;
  124. raw3215_freelist = req;
  125. spin_unlock_irqrestore(&raw3215_freelist_lock, flags);
  126. }
  127. /*
  128. * Set up a read request that reads up to 160 byte from the 3215 device.
  129. * If there is a queued read request it is used, but that shouldn't happen
  130. * because a 3215 terminal won't accept a new read before the old one is
  131. * completed.
  132. */
  133. static void
  134. raw3215_mk_read_req(struct raw3215_info *raw)
  135. {
  136. struct raw3215_req *req;
  137. struct ccw1 *ccw;
  138. /* there can only be ONE read request at a time */
  139. req = raw->queued_read;
  140. if (req == NULL) {
  141. /* no queued read request, use new req structure */
  142. req = raw3215_alloc_req();
  143. req->type = RAW3215_READ;
  144. req->info = raw;
  145. raw->queued_read = req;
  146. }
  147. ccw = req->ccws;
  148. ccw->cmd_code = 0x0A; /* read inquiry */
  149. ccw->flags = 0x20; /* ignore incorrect length */
  150. ccw->count = 160;
  151. ccw->cda = (__u32) __pa(raw->inbuf);
  152. }
  153. /*
  154. * Set up a write request with the information from the main structure.
  155. * A ccw chain is created that writes as much as possible from the output
  156. * buffer to the 3215 device. If a queued write exists it is replaced by
  157. * the new, probably lengthened request.
  158. */
  159. static void
  160. raw3215_mk_write_req(struct raw3215_info *raw)
  161. {
  162. struct raw3215_req *req;
  163. struct ccw1 *ccw;
  164. int len, count, ix, lines;
  165. if (raw->count <= raw->written)
  166. return;
  167. /* check if there is a queued write request */
  168. req = raw->queued_write;
  169. if (req == NULL) {
  170. /* no queued write request, use new req structure */
  171. req = raw3215_alloc_req();
  172. req->type = RAW3215_WRITE;
  173. req->info = raw;
  174. raw->queued_write = req;
  175. } else {
  176. raw->written -= req->len;
  177. }
  178. ccw = req->ccws;
  179. req->start = (raw->head - raw->count + raw->written) &
  180. (RAW3215_BUFFER_SIZE - 1);
  181. /*
  182. * now we have to count newlines. We can at max accept
  183. * RAW3215_MAX_NEWLINE newlines in a single ssch due to
  184. * a restriction in VM
  185. */
  186. lines = 0;
  187. ix = req->start;
  188. while (lines < RAW3215_MAX_NEWLINE && ix != raw->head) {
  189. if (raw->buffer[ix] == 0x15)
  190. lines++;
  191. ix = (ix + 1) & (RAW3215_BUFFER_SIZE - 1);
  192. }
  193. len = ((ix - 1 - req->start) & (RAW3215_BUFFER_SIZE - 1)) + 1;
  194. if (len > RAW3215_MAX_BYTES)
  195. len = RAW3215_MAX_BYTES;
  196. req->len = len;
  197. raw->written += len;
  198. /* set the indication if we should try to enlarge this request */
  199. req->delayable = (ix == raw->head) && (len < RAW3215_MIN_WRITE);
  200. ix = req->start;
  201. while (len > 0) {
  202. if (ccw > req->ccws)
  203. ccw[-1].flags |= 0x40; /* use command chaining */
  204. ccw->cmd_code = 0x01; /* write, auto carrier return */
  205. ccw->flags = 0x20; /* ignore incorrect length ind. */
  206. ccw->cda =
  207. (__u32) __pa(raw->buffer + ix);
  208. count = len;
  209. if (ix + count > RAW3215_BUFFER_SIZE)
  210. count = RAW3215_BUFFER_SIZE - ix;
  211. ccw->count = count;
  212. len -= count;
  213. ix = (ix + count) & (RAW3215_BUFFER_SIZE - 1);
  214. ccw++;
  215. }
  216. /*
  217. * Add a NOP to the channel program. 3215 devices are purely
  218. * emulated and its much better to avoid the channel end
  219. * interrupt in this case.
  220. */
  221. if (ccw > req->ccws)
  222. ccw[-1].flags |= 0x40; /* use command chaining */
  223. ccw->cmd_code = 0x03; /* NOP */
  224. ccw->flags = 0;
  225. ccw->cda = 0;
  226. ccw->count = 1;
  227. }
  228. /*
  229. * Start a read or a write request
  230. */
  231. static void
  232. raw3215_start_io(struct raw3215_info *raw)
  233. {
  234. struct raw3215_req *req;
  235. int res;
  236. req = raw->queued_read;
  237. if (req != NULL &&
  238. !(raw->flags & (RAW3215_WORKING | RAW3215_THROTTLED))) {
  239. /* dequeue request */
  240. raw->queued_read = NULL;
  241. res = ccw_device_start(raw->cdev, req->ccws,
  242. (unsigned long) req, 0, 0);
  243. if (res != 0) {
  244. /* do_IO failed, put request back to queue */
  245. raw->queued_read = req;
  246. } else {
  247. raw->flags |= RAW3215_WORKING;
  248. }
  249. }
  250. req = raw->queued_write;
  251. if (req != NULL &&
  252. !(raw->flags & (RAW3215_WORKING | RAW3215_STOPPED))) {
  253. /* dequeue request */
  254. raw->queued_write = NULL;
  255. res = ccw_device_start(raw->cdev, req->ccws,
  256. (unsigned long) req, 0, 0);
  257. if (res != 0) {
  258. /* do_IO failed, put request back to queue */
  259. raw->queued_write = req;
  260. } else {
  261. raw->flags |= RAW3215_WORKING;
  262. }
  263. }
  264. }
  265. /*
  266. * Function to start a delayed output after RAW3215_TIMEOUT seconds
  267. */
  268. static void
  269. raw3215_timeout(unsigned long __data)
  270. {
  271. struct raw3215_info *raw = (struct raw3215_info *) __data;
  272. unsigned long flags;
  273. spin_lock_irqsave(raw->lock, flags);
  274. if (raw->flags & RAW3215_TIMER_RUNS) {
  275. del_timer(&raw->timer);
  276. raw->flags &= ~RAW3215_TIMER_RUNS;
  277. raw3215_mk_write_req(raw);
  278. raw3215_start_io(raw);
  279. }
  280. spin_unlock_irqrestore(raw->lock, flags);
  281. }
  282. /*
  283. * Function to conditionally start an IO. A read is started immediately,
  284. * a write is only started immediately if the flush flag is on or the
  285. * amount of data is bigger than RAW3215_MIN_WRITE. If a write is not
  286. * done immediately a timer is started with a delay of RAW3215_TIMEOUT.
  287. */
  288. static inline void
  289. raw3215_try_io(struct raw3215_info *raw)
  290. {
  291. if (!(raw->flags & RAW3215_ACTIVE))
  292. return;
  293. if (raw->queued_read != NULL)
  294. raw3215_start_io(raw);
  295. else if (raw->queued_write != NULL) {
  296. if ((raw->queued_write->delayable == 0) ||
  297. (raw->flags & RAW3215_FLUSHING)) {
  298. /* execute write requests bigger than minimum size */
  299. raw3215_start_io(raw);
  300. if (raw->flags & RAW3215_TIMER_RUNS) {
  301. del_timer(&raw->timer);
  302. raw->flags &= ~RAW3215_TIMER_RUNS;
  303. }
  304. } else if (!(raw->flags & RAW3215_TIMER_RUNS)) {
  305. /* delay small writes */
  306. init_timer(&raw->timer);
  307. raw->timer.expires = RAW3215_TIMEOUT + jiffies;
  308. raw->timer.data = (unsigned long) raw;
  309. raw->timer.function = raw3215_timeout;
  310. add_timer(&raw->timer);
  311. raw->flags |= RAW3215_TIMER_RUNS;
  312. }
  313. }
  314. }
  315. /*
  316. * The bottom half handler routine for 3215 devices. It tries to start
  317. * the next IO and wakes up processes waiting on the tty.
  318. */
  319. static void
  320. raw3215_tasklet(void *data)
  321. {
  322. struct raw3215_info *raw;
  323. struct tty_struct *tty;
  324. unsigned long flags;
  325. raw = (struct raw3215_info *) data;
  326. spin_lock_irqsave(raw->lock, flags);
  327. raw3215_mk_write_req(raw);
  328. raw3215_try_io(raw);
  329. spin_unlock_irqrestore(raw->lock, flags);
  330. /* Check for pending message from raw3215_irq */
  331. if (raw->message != NULL) {
  332. printk(raw->message, raw->msg_dstat, raw->msg_cstat);
  333. raw->message = NULL;
  334. }
  335. tty = raw->tty;
  336. if (tty != NULL &&
  337. RAW3215_BUFFER_SIZE - raw->count >= RAW3215_MIN_SPACE) {
  338. tty_wakeup(tty);
  339. }
  340. }
  341. /*
  342. * Interrupt routine, called from common io layer
  343. */
  344. static void
  345. raw3215_irq(struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  346. {
  347. struct raw3215_info *raw;
  348. struct raw3215_req *req;
  349. struct tty_struct *tty;
  350. int cstat, dstat;
  351. int count, slen;
  352. raw = cdev->dev.driver_data;
  353. req = (struct raw3215_req *) intparm;
  354. cstat = irb->scsw.cstat;
  355. dstat = irb->scsw.dstat;
  356. if (cstat != 0) {
  357. raw->message = KERN_WARNING
  358. "Got nonzero channel status in raw3215_irq "
  359. "(dev sts 0x%2x, sch sts 0x%2x)";
  360. raw->msg_dstat = dstat;
  361. raw->msg_cstat = cstat;
  362. tasklet_schedule(&raw->tasklet);
  363. }
  364. if (dstat & 0x01) { /* we got a unit exception */
  365. dstat &= ~0x01; /* we can ignore it */
  366. }
  367. switch (dstat) {
  368. case 0x80:
  369. if (cstat != 0)
  370. break;
  371. /* Attention interrupt, someone hit the enter key */
  372. raw3215_mk_read_req(raw);
  373. if (MACHINE_IS_P390)
  374. memset(raw->inbuf, 0, RAW3215_INBUF_SIZE);
  375. tasklet_schedule(&raw->tasklet);
  376. break;
  377. case 0x08:
  378. case 0x0C:
  379. /* Channel end interrupt. */
  380. if ((raw = req->info) == NULL)
  381. return; /* That shouldn't happen ... */
  382. if (req->type == RAW3215_READ) {
  383. /* store residual count, then wait for device end */
  384. req->residual = irb->scsw.count;
  385. }
  386. if (dstat == 0x08)
  387. break;
  388. case 0x04:
  389. /* Device end interrupt. */
  390. if ((raw = req->info) == NULL)
  391. return; /* That shouldn't happen ... */
  392. if (req->type == RAW3215_READ && raw->tty != NULL) {
  393. unsigned int cchar;
  394. tty = raw->tty;
  395. count = 160 - req->residual;
  396. if (MACHINE_IS_P390) {
  397. slen = strnlen(raw->inbuf, RAW3215_INBUF_SIZE);
  398. if (count > slen)
  399. count = slen;
  400. } else
  401. if (count >= TTY_FLIPBUF_SIZE - tty->flip.count)
  402. count = TTY_FLIPBUF_SIZE - tty->flip.count - 1;
  403. EBCASC(raw->inbuf, count);
  404. cchar = ctrlchar_handle(raw->inbuf, count, tty);
  405. switch (cchar & CTRLCHAR_MASK) {
  406. case CTRLCHAR_SYSRQ:
  407. break;
  408. case CTRLCHAR_CTRL:
  409. tty->flip.count++;
  410. *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
  411. *tty->flip.char_buf_ptr++ = cchar;
  412. tty_flip_buffer_push(raw->tty);
  413. break;
  414. case CTRLCHAR_NONE:
  415. memcpy(tty->flip.char_buf_ptr,
  416. raw->inbuf, count);
  417. if (count < 2 ||
  418. (strncmp(raw->inbuf+count-2, "^n", 2) &&
  419. strncmp(raw->inbuf+count-2, "\252n", 2)) ) {
  420. /* don't add the auto \n */
  421. tty->flip.char_buf_ptr[count] = '\n';
  422. memset(tty->flip.flag_buf_ptr,
  423. TTY_NORMAL, count + 1);
  424. count++;
  425. } else
  426. count-=2;
  427. tty->flip.char_buf_ptr += count;
  428. tty->flip.flag_buf_ptr += count;
  429. tty->flip.count += count;
  430. tty_flip_buffer_push(raw->tty);
  431. break;
  432. }
  433. } else if (req->type == RAW3215_WRITE) {
  434. raw->count -= req->len;
  435. raw->written -= req->len;
  436. }
  437. raw->flags &= ~RAW3215_WORKING;
  438. raw3215_free_req(req);
  439. /* check for empty wait */
  440. if (waitqueue_active(&raw->empty_wait) &&
  441. raw->queued_write == NULL &&
  442. raw->queued_read == NULL) {
  443. wake_up_interruptible(&raw->empty_wait);
  444. }
  445. tasklet_schedule(&raw->tasklet);
  446. break;
  447. default:
  448. /* Strange interrupt, I'll do my best to clean up */
  449. if (req != NULL && req->type != RAW3215_FREE) {
  450. if (req->type == RAW3215_WRITE) {
  451. raw->count -= req->len;
  452. raw->written -= req->len;
  453. }
  454. raw->flags &= ~RAW3215_WORKING;
  455. raw3215_free_req(req);
  456. }
  457. raw->message = KERN_WARNING
  458. "Spurious interrupt in in raw3215_irq "
  459. "(dev sts 0x%2x, sch sts 0x%2x)";
  460. raw->msg_dstat = dstat;
  461. raw->msg_cstat = cstat;
  462. tasklet_schedule(&raw->tasklet);
  463. }
  464. return;
  465. }
  466. /*
  467. * Wait until length bytes are available int the output buffer.
  468. * Has to be called with the s390irq lock held. Can be called
  469. * disabled.
  470. */
  471. static void
  472. raw3215_make_room(struct raw3215_info *raw, unsigned int length)
  473. {
  474. while (RAW3215_BUFFER_SIZE - raw->count < length) {
  475. /* there might be a request pending */
  476. raw->flags |= RAW3215_FLUSHING;
  477. raw3215_mk_write_req(raw);
  478. raw3215_try_io(raw);
  479. raw->flags &= ~RAW3215_FLUSHING;
  480. #ifdef CONFIG_TN3215_CONSOLE
  481. wait_cons_dev();
  482. #endif
  483. /* Enough room freed up ? */
  484. if (RAW3215_BUFFER_SIZE - raw->count >= length)
  485. break;
  486. /* there might be another cpu waiting for the lock */
  487. spin_unlock(raw->lock);
  488. udelay(100);
  489. spin_lock(raw->lock);
  490. }
  491. }
  492. /*
  493. * String write routine for 3215 devices
  494. */
  495. static void
  496. raw3215_write(struct raw3215_info *raw, const char *str, unsigned int length)
  497. {
  498. unsigned long flags;
  499. int c, count;
  500. while (length > 0) {
  501. spin_lock_irqsave(raw->lock, flags);
  502. count = (length > RAW3215_BUFFER_SIZE) ?
  503. RAW3215_BUFFER_SIZE : length;
  504. length -= count;
  505. raw3215_make_room(raw, count);
  506. /* copy string to output buffer and convert it to EBCDIC */
  507. while (1) {
  508. c = min_t(int, count,
  509. min(RAW3215_BUFFER_SIZE - raw->count,
  510. RAW3215_BUFFER_SIZE - raw->head));
  511. if (c <= 0)
  512. break;
  513. memcpy(raw->buffer + raw->head, str, c);
  514. ASCEBC(raw->buffer + raw->head, c);
  515. raw->head = (raw->head + c) & (RAW3215_BUFFER_SIZE - 1);
  516. raw->count += c;
  517. raw->line_pos += c;
  518. str += c;
  519. count -= c;
  520. }
  521. if (!(raw->flags & RAW3215_WORKING)) {
  522. raw3215_mk_write_req(raw);
  523. /* start or queue request */
  524. raw3215_try_io(raw);
  525. }
  526. spin_unlock_irqrestore(raw->lock, flags);
  527. }
  528. }
  529. /*
  530. * Put character routine for 3215 devices
  531. */
  532. static void
  533. raw3215_putchar(struct raw3215_info *raw, unsigned char ch)
  534. {
  535. unsigned long flags;
  536. unsigned int length, i;
  537. spin_lock_irqsave(raw->lock, flags);
  538. if (ch == '\t') {
  539. length = TAB_STOP_SIZE - (raw->line_pos%TAB_STOP_SIZE);
  540. raw->line_pos += length;
  541. ch = ' ';
  542. } else if (ch == '\n') {
  543. length = 1;
  544. raw->line_pos = 0;
  545. } else {
  546. length = 1;
  547. raw->line_pos++;
  548. }
  549. raw3215_make_room(raw, length);
  550. for (i = 0; i < length; i++) {
  551. raw->buffer[raw->head] = (char) _ascebc[(int) ch];
  552. raw->head = (raw->head + 1) & (RAW3215_BUFFER_SIZE - 1);
  553. raw->count++;
  554. }
  555. if (!(raw->flags & RAW3215_WORKING)) {
  556. raw3215_mk_write_req(raw);
  557. /* start or queue request */
  558. raw3215_try_io(raw);
  559. }
  560. spin_unlock_irqrestore(raw->lock, flags);
  561. }
  562. /*
  563. * Flush routine, it simply sets the flush flag and tries to start
  564. * pending IO.
  565. */
  566. static void
  567. raw3215_flush_buffer(struct raw3215_info *raw)
  568. {
  569. unsigned long flags;
  570. spin_lock_irqsave(raw->lock, flags);
  571. if (raw->count > 0) {
  572. raw->flags |= RAW3215_FLUSHING;
  573. raw3215_try_io(raw);
  574. raw->flags &= ~RAW3215_FLUSHING;
  575. }
  576. spin_unlock_irqrestore(raw->lock, flags);
  577. }
  578. /*
  579. * Fire up a 3215 device.
  580. */
  581. static int
  582. raw3215_startup(struct raw3215_info *raw)
  583. {
  584. unsigned long flags;
  585. if (raw->flags & RAW3215_ACTIVE)
  586. return 0;
  587. raw->line_pos = 0;
  588. raw->flags |= RAW3215_ACTIVE;
  589. spin_lock_irqsave(raw->lock, flags);
  590. raw3215_try_io(raw);
  591. spin_unlock_irqrestore(raw->lock, flags);
  592. return 0;
  593. }
  594. /*
  595. * Shutdown a 3215 device.
  596. */
  597. static void
  598. raw3215_shutdown(struct raw3215_info *raw)
  599. {
  600. DECLARE_WAITQUEUE(wait, current);
  601. unsigned long flags;
  602. if (!(raw->flags & RAW3215_ACTIVE) || (raw->flags & RAW3215_FIXED))
  603. return;
  604. /* Wait for outstanding requests, then free irq */
  605. spin_lock_irqsave(raw->lock, flags);
  606. if ((raw->flags & RAW3215_WORKING) ||
  607. raw->queued_write != NULL ||
  608. raw->queued_read != NULL) {
  609. raw->flags |= RAW3215_CLOSING;
  610. add_wait_queue(&raw->empty_wait, &wait);
  611. set_current_state(TASK_INTERRUPTIBLE);
  612. spin_unlock_irqrestore(raw->lock, flags);
  613. schedule();
  614. spin_lock_irqsave(raw->lock, flags);
  615. remove_wait_queue(&raw->empty_wait, &wait);
  616. set_current_state(TASK_RUNNING);
  617. raw->flags &= ~(RAW3215_ACTIVE | RAW3215_CLOSING);
  618. }
  619. spin_unlock_irqrestore(raw->lock, flags);
  620. }
  621. static int
  622. raw3215_probe (struct ccw_device *cdev)
  623. {
  624. struct raw3215_info *raw;
  625. int line;
  626. raw = kmalloc(sizeof(struct raw3215_info) +
  627. RAW3215_INBUF_SIZE, GFP_KERNEL|GFP_DMA);
  628. if (raw == NULL)
  629. return -ENOMEM;
  630. spin_lock(&raw3215_device_lock);
  631. for (line = 0; line < NR_3215; line++) {
  632. if (!raw3215[line]) {
  633. raw3215[line] = raw;
  634. break;
  635. }
  636. }
  637. spin_unlock(&raw3215_device_lock);
  638. if (line == NR_3215) {
  639. kfree(raw);
  640. return -ENODEV;
  641. }
  642. raw->cdev = cdev;
  643. raw->lock = get_ccwdev_lock(cdev);
  644. raw->inbuf = (char *) raw + sizeof(struct raw3215_info);
  645. memset(raw, 0, sizeof(struct raw3215_info));
  646. raw->buffer = (char *) kmalloc(RAW3215_BUFFER_SIZE,
  647. GFP_KERNEL|GFP_DMA);
  648. if (raw->buffer == NULL) {
  649. spin_lock(&raw3215_device_lock);
  650. raw3215[line] = 0;
  651. spin_unlock(&raw3215_device_lock);
  652. kfree(raw);
  653. return -ENOMEM;
  654. }
  655. tasklet_init(&raw->tasklet,
  656. (void (*)(unsigned long)) raw3215_tasklet,
  657. (unsigned long) raw);
  658. init_waitqueue_head(&raw->empty_wait);
  659. cdev->dev.driver_data = raw;
  660. cdev->handler = raw3215_irq;
  661. return 0;
  662. }
  663. static void
  664. raw3215_remove (struct ccw_device *cdev)
  665. {
  666. struct raw3215_info *raw;
  667. ccw_device_set_offline(cdev);
  668. raw = cdev->dev.driver_data;
  669. if (raw) {
  670. cdev->dev.driver_data = NULL;
  671. kfree(raw->buffer);
  672. kfree(raw);
  673. }
  674. }
  675. static int
  676. raw3215_set_online (struct ccw_device *cdev)
  677. {
  678. struct raw3215_info *raw;
  679. raw = cdev->dev.driver_data;
  680. if (!raw)
  681. return -ENODEV;
  682. return raw3215_startup(raw);
  683. }
  684. static int
  685. raw3215_set_offline (struct ccw_device *cdev)
  686. {
  687. struct raw3215_info *raw;
  688. raw = cdev->dev.driver_data;
  689. if (!raw)
  690. return -ENODEV;
  691. raw3215_shutdown(raw);
  692. return 0;
  693. }
  694. static struct ccw_device_id raw3215_id[] = {
  695. { CCW_DEVICE(0x3215, 0) },
  696. { /* end of list */ },
  697. };
  698. static struct ccw_driver raw3215_ccw_driver = {
  699. .name = "3215",
  700. .owner = THIS_MODULE,
  701. .ids = raw3215_id,
  702. .probe = &raw3215_probe,
  703. .remove = &raw3215_remove,
  704. .set_online = &raw3215_set_online,
  705. .set_offline = &raw3215_set_offline,
  706. };
  707. #ifdef CONFIG_TN3215_CONSOLE
  708. /*
  709. * Write a string to the 3215 console
  710. */
  711. static void
  712. con3215_write(struct console *co, const char *str, unsigned int count)
  713. {
  714. struct raw3215_info *raw;
  715. int i;
  716. if (count <= 0)
  717. return;
  718. raw = raw3215[0]; /* console 3215 is the first one */
  719. while (count > 0) {
  720. for (i = 0; i < count; i++)
  721. if (str[i] == '\t' || str[i] == '\n')
  722. break;
  723. raw3215_write(raw, str, i);
  724. count -= i;
  725. str += i;
  726. if (count > 0) {
  727. raw3215_putchar(raw, *str);
  728. count--;
  729. str++;
  730. }
  731. }
  732. }
  733. static struct tty_driver *con3215_device(struct console *c, int *index)
  734. {
  735. *index = c->index;
  736. return tty3215_driver;
  737. }
  738. /*
  739. * panic() calls console_unblank before the system enters a
  740. * disabled, endless loop.
  741. */
  742. static void
  743. con3215_unblank(void)
  744. {
  745. struct raw3215_info *raw;
  746. unsigned long flags;
  747. raw = raw3215[0]; /* console 3215 is the first one */
  748. spin_lock_irqsave(raw->lock, flags);
  749. raw3215_make_room(raw, RAW3215_BUFFER_SIZE);
  750. spin_unlock_irqrestore(raw->lock, flags);
  751. }
  752. static int __init
  753. con3215_consetup(struct console *co, char *options)
  754. {
  755. return 0;
  756. }
  757. /*
  758. * The console structure for the 3215 console
  759. */
  760. static struct console con3215 = {
  761. .name = "ttyS",
  762. .write = con3215_write,
  763. .device = con3215_device,
  764. .unblank = con3215_unblank,
  765. .setup = con3215_consetup,
  766. .flags = CON_PRINTBUFFER,
  767. };
  768. /*
  769. * 3215 console initialization code called from console_init().
  770. * NOTE: This is called before kmalloc is available.
  771. */
  772. static int __init
  773. con3215_init(void)
  774. {
  775. struct ccw_device *cdev;
  776. struct raw3215_info *raw;
  777. struct raw3215_req *req;
  778. int i;
  779. /* Check if 3215 is to be the console */
  780. if (!CONSOLE_IS_3215)
  781. return -ENODEV;
  782. /* Set the console mode for VM */
  783. if (MACHINE_IS_VM) {
  784. cpcmd("TERM CONMODE 3215", NULL, 0, NULL);
  785. cpcmd("TERM AUTOCR OFF", NULL, 0, NULL);
  786. }
  787. /* allocate 3215 request structures */
  788. raw3215_freelist = NULL;
  789. spin_lock_init(&raw3215_freelist_lock);
  790. for (i = 0; i < NR_3215_REQ; i++) {
  791. req = (struct raw3215_req *) alloc_bootmem_low(sizeof(struct raw3215_req));
  792. req->next = raw3215_freelist;
  793. raw3215_freelist = req;
  794. }
  795. cdev = ccw_device_probe_console();
  796. if (!cdev)
  797. return -ENODEV;
  798. raw3215[0] = raw = (struct raw3215_info *)
  799. alloc_bootmem_low(sizeof(struct raw3215_info));
  800. memset(raw, 0, sizeof(struct raw3215_info));
  801. raw->buffer = (char *) alloc_bootmem_low(RAW3215_BUFFER_SIZE);
  802. raw->inbuf = (char *) alloc_bootmem_low(RAW3215_INBUF_SIZE);
  803. raw->cdev = cdev;
  804. raw->lock = get_ccwdev_lock(cdev);
  805. cdev->dev.driver_data = raw;
  806. cdev->handler = raw3215_irq;
  807. raw->flags |= RAW3215_FIXED;
  808. tasklet_init(&raw->tasklet,
  809. (void (*)(unsigned long)) raw3215_tasklet,
  810. (unsigned long) raw);
  811. init_waitqueue_head(&raw->empty_wait);
  812. /* Request the console irq */
  813. if (raw3215_startup(raw) != 0) {
  814. free_bootmem((unsigned long) raw->inbuf, RAW3215_INBUF_SIZE);
  815. free_bootmem((unsigned long) raw->buffer, RAW3215_BUFFER_SIZE);
  816. free_bootmem((unsigned long) raw, sizeof(struct raw3215_info));
  817. raw3215[0] = NULL;
  818. printk("Couldn't find a 3215 console device\n");
  819. return -ENODEV;
  820. }
  821. register_console(&con3215);
  822. return 0;
  823. }
  824. console_initcall(con3215_init);
  825. #endif
  826. /*
  827. * tty3215_open
  828. *
  829. * This routine is called whenever a 3215 tty is opened.
  830. */
  831. static int
  832. tty3215_open(struct tty_struct *tty, struct file * filp)
  833. {
  834. struct raw3215_info *raw;
  835. int retval, line;
  836. line = tty->index;
  837. if ((line < 0) || (line >= NR_3215))
  838. return -ENODEV;
  839. raw = raw3215[line];
  840. if (raw == NULL)
  841. return -ENODEV;
  842. tty->driver_data = raw;
  843. raw->tty = tty;
  844. tty->low_latency = 0; /* don't use bottom half for pushing chars */
  845. /*
  846. * Start up 3215 device
  847. */
  848. retval = raw3215_startup(raw);
  849. if (retval)
  850. return retval;
  851. return 0;
  852. }
  853. /*
  854. * tty3215_close()
  855. *
  856. * This routine is called when the 3215 tty is closed. We wait
  857. * for the remaining request to be completed. Then we clean up.
  858. */
  859. static void
  860. tty3215_close(struct tty_struct *tty, struct file * filp)
  861. {
  862. struct raw3215_info *raw;
  863. raw = (struct raw3215_info *) tty->driver_data;
  864. if (raw == NULL || tty->count > 1)
  865. return;
  866. tty->closing = 1;
  867. /* Shutdown the terminal */
  868. raw3215_shutdown(raw);
  869. tty->closing = 0;
  870. raw->tty = NULL;
  871. }
  872. /*
  873. * Returns the amount of free space in the output buffer.
  874. */
  875. static int
  876. tty3215_write_room(struct tty_struct *tty)
  877. {
  878. struct raw3215_info *raw;
  879. raw = (struct raw3215_info *) tty->driver_data;
  880. /* Subtract TAB_STOP_SIZE to allow for a tab, 8 <<< 64K */
  881. if ((RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE) >= 0)
  882. return RAW3215_BUFFER_SIZE - raw->count - TAB_STOP_SIZE;
  883. else
  884. return 0;
  885. }
  886. /*
  887. * String write routine for 3215 ttys
  888. */
  889. static int
  890. tty3215_write(struct tty_struct * tty,
  891. const unsigned char *buf, int count)
  892. {
  893. struct raw3215_info *raw;
  894. if (!tty)
  895. return 0;
  896. raw = (struct raw3215_info *) tty->driver_data;
  897. raw3215_write(raw, buf, count);
  898. return count;
  899. }
  900. /*
  901. * Put character routine for 3215 ttys
  902. */
  903. static void
  904. tty3215_put_char(struct tty_struct *tty, unsigned char ch)
  905. {
  906. struct raw3215_info *raw;
  907. if (!tty)
  908. return;
  909. raw = (struct raw3215_info *) tty->driver_data;
  910. raw3215_putchar(raw, ch);
  911. }
  912. static void
  913. tty3215_flush_chars(struct tty_struct *tty)
  914. {
  915. }
  916. /*
  917. * Returns the number of characters in the output buffer
  918. */
  919. static int
  920. tty3215_chars_in_buffer(struct tty_struct *tty)
  921. {
  922. struct raw3215_info *raw;
  923. raw = (struct raw3215_info *) tty->driver_data;
  924. return raw->count;
  925. }
  926. static void
  927. tty3215_flush_buffer(struct tty_struct *tty)
  928. {
  929. struct raw3215_info *raw;
  930. raw = (struct raw3215_info *) tty->driver_data;
  931. raw3215_flush_buffer(raw);
  932. tty_wakeup(tty);
  933. }
  934. /*
  935. * Currently we don't have any io controls for 3215 ttys
  936. */
  937. static int
  938. tty3215_ioctl(struct tty_struct *tty, struct file * file,
  939. unsigned int cmd, unsigned long arg)
  940. {
  941. if (tty->flags & (1 << TTY_IO_ERROR))
  942. return -EIO;
  943. switch (cmd) {
  944. default:
  945. return -ENOIOCTLCMD;
  946. }
  947. return 0;
  948. }
  949. /*
  950. * Disable reading from a 3215 tty
  951. */
  952. static void
  953. tty3215_throttle(struct tty_struct * tty)
  954. {
  955. struct raw3215_info *raw;
  956. raw = (struct raw3215_info *) tty->driver_data;
  957. raw->flags |= RAW3215_THROTTLED;
  958. }
  959. /*
  960. * Enable reading from a 3215 tty
  961. */
  962. static void
  963. tty3215_unthrottle(struct tty_struct * tty)
  964. {
  965. struct raw3215_info *raw;
  966. unsigned long flags;
  967. raw = (struct raw3215_info *) tty->driver_data;
  968. if (raw->flags & RAW3215_THROTTLED) {
  969. spin_lock_irqsave(raw->lock, flags);
  970. raw->flags &= ~RAW3215_THROTTLED;
  971. raw3215_try_io(raw);
  972. spin_unlock_irqrestore(raw->lock, flags);
  973. }
  974. }
  975. /*
  976. * Disable writing to a 3215 tty
  977. */
  978. static void
  979. tty3215_stop(struct tty_struct *tty)
  980. {
  981. struct raw3215_info *raw;
  982. raw = (struct raw3215_info *) tty->driver_data;
  983. raw->flags |= RAW3215_STOPPED;
  984. }
  985. /*
  986. * Enable writing to a 3215 tty
  987. */
  988. static void
  989. tty3215_start(struct tty_struct *tty)
  990. {
  991. struct raw3215_info *raw;
  992. unsigned long flags;
  993. raw = (struct raw3215_info *) tty->driver_data;
  994. if (raw->flags & RAW3215_STOPPED) {
  995. spin_lock_irqsave(raw->lock, flags);
  996. raw->flags &= ~RAW3215_STOPPED;
  997. raw3215_try_io(raw);
  998. spin_unlock_irqrestore(raw->lock, flags);
  999. }
  1000. }
  1001. static struct tty_operations tty3215_ops = {
  1002. .open = tty3215_open,
  1003. .close = tty3215_close,
  1004. .write = tty3215_write,
  1005. .put_char = tty3215_put_char,
  1006. .flush_chars = tty3215_flush_chars,
  1007. .write_room = tty3215_write_room,
  1008. .chars_in_buffer = tty3215_chars_in_buffer,
  1009. .flush_buffer = tty3215_flush_buffer,
  1010. .ioctl = tty3215_ioctl,
  1011. .throttle = tty3215_throttle,
  1012. .unthrottle = tty3215_unthrottle,
  1013. .stop = tty3215_stop,
  1014. .start = tty3215_start,
  1015. };
  1016. /*
  1017. * 3215 tty registration code called from tty_init().
  1018. * Most kernel services (incl. kmalloc) are available at this poimt.
  1019. */
  1020. int __init
  1021. tty3215_init(void)
  1022. {
  1023. struct tty_driver *driver;
  1024. int ret;
  1025. if (!CONSOLE_IS_3215)
  1026. return 0;
  1027. driver = alloc_tty_driver(NR_3215);
  1028. if (!driver)
  1029. return -ENOMEM;
  1030. ret = ccw_driver_register(&raw3215_ccw_driver);
  1031. if (ret) {
  1032. put_tty_driver(driver);
  1033. return ret;
  1034. }
  1035. /*
  1036. * Initialize the tty_driver structure
  1037. * Entries in tty3215_driver that are NOT initialized:
  1038. * proc_entry, set_termios, flush_buffer, set_ldisc, write_proc
  1039. */
  1040. driver->owner = THIS_MODULE;
  1041. driver->driver_name = "tty3215";
  1042. driver->name = "ttyS";
  1043. driver->major = TTY_MAJOR;
  1044. driver->minor_start = 64;
  1045. driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1046. driver->subtype = SYSTEM_TYPE_TTY;
  1047. driver->init_termios = tty_std_termios;
  1048. driver->init_termios.c_iflag = IGNBRK | IGNPAR;
  1049. driver->init_termios.c_oflag = ONLCR | XTABS;
  1050. driver->init_termios.c_lflag = ISIG;
  1051. driver->flags = TTY_DRIVER_REAL_RAW;
  1052. tty_set_operations(driver, &tty3215_ops);
  1053. ret = tty_register_driver(driver);
  1054. if (ret) {
  1055. printk("Couldn't register tty3215 driver\n");
  1056. put_tty_driver(driver);
  1057. return ret;
  1058. }
  1059. tty3215_driver = driver;
  1060. return 0;
  1061. }
  1062. static void __exit
  1063. tty3215_exit(void)
  1064. {
  1065. tty_unregister_driver(tty3215_driver);
  1066. put_tty_driver(tty3215_driver);
  1067. ccw_driver_unregister(&raw3215_ccw_driver);
  1068. }
  1069. module_init(tty3215_init);
  1070. module_exit(tty3215_exit);