con3215.c 29 KB

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