con3215.c 29 KB

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