con3215.c 30 KB

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