raw3270.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. * drivers/s390/char/raw3270.c
  3. * IBM/3270 Driver - core functions.
  4. *
  5. * Author(s):
  6. * Original 3270 Code for 2.4 written by Richard Hitt (UTS Global)
  7. * Rewritten for 2.5 by Martin Schwidefsky <schwidefsky@de.ibm.com>
  8. * -- Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  9. */
  10. #include <linux/bootmem.h>
  11. #include <linux/module.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/list.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. #include <linux/wait.h>
  19. #include <asm/ccwdev.h>
  20. #include <asm/cio.h>
  21. #include <asm/ebcdic.h>
  22. #include <asm/diag.h>
  23. #include "raw3270.h"
  24. #include <linux/major.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/device.h>
  27. #include <linux/mutex.h>
  28. static struct class *class3270;
  29. /* The main 3270 data structure. */
  30. struct raw3270 {
  31. struct list_head list;
  32. struct ccw_device *cdev;
  33. int minor;
  34. short model, rows, cols;
  35. unsigned long flags;
  36. struct list_head req_queue; /* Request queue. */
  37. struct list_head view_list; /* List of available views. */
  38. struct raw3270_view *view; /* Active view. */
  39. struct timer_list timer; /* Device timer. */
  40. unsigned char *ascebc; /* ascii -> ebcdic table */
  41. struct device *clttydev; /* 3270-class tty device ptr */
  42. struct device *cltubdev; /* 3270-class tub device ptr */
  43. struct raw3270_request init_request;
  44. unsigned char init_data[256];
  45. };
  46. /* raw3270->flags */
  47. #define RAW3270_FLAGS_14BITADDR 0 /* 14-bit buffer addresses */
  48. #define RAW3270_FLAGS_BUSY 1 /* Device busy, leave it alone */
  49. #define RAW3270_FLAGS_ATTN 2 /* Device sent an ATTN interrupt */
  50. #define RAW3270_FLAGS_READY 4 /* Device is useable by views */
  51. #define RAW3270_FLAGS_CONSOLE 8 /* Device is the console. */
  52. /* Semaphore to protect global data of raw3270 (devices, views, etc). */
  53. static DEFINE_MUTEX(raw3270_mutex);
  54. /* List of 3270 devices. */
  55. static LIST_HEAD(raw3270_devices);
  56. /*
  57. * Flag to indicate if the driver has been registered. Some operations
  58. * like waiting for the end of i/o need to be done differently as long
  59. * as the kernel is still starting up (console support).
  60. */
  61. static int raw3270_registered;
  62. /* Module parameters */
  63. static int tubxcorrect = 0;
  64. module_param(tubxcorrect, bool, 0);
  65. /*
  66. * Wait queue for device init/delete, view delete.
  67. */
  68. DECLARE_WAIT_QUEUE_HEAD(raw3270_wait_queue);
  69. /*
  70. * Encode array for 12 bit 3270 addresses.
  71. */
  72. static unsigned char raw3270_ebcgraf[64] = {
  73. 0x40, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  74. 0xc8, 0xc9, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  75. 0x50, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  76. 0xd8, 0xd9, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  77. 0x60, 0x61, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  78. 0xe8, 0xe9, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  79. 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  80. 0xf8, 0xf9, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
  81. };
  82. void
  83. raw3270_buffer_address(struct raw3270 *rp, char *cp, unsigned short addr)
  84. {
  85. if (test_bit(RAW3270_FLAGS_14BITADDR, &rp->flags)) {
  86. cp[0] = (addr >> 8) & 0x3f;
  87. cp[1] = addr & 0xff;
  88. } else {
  89. cp[0] = raw3270_ebcgraf[(addr >> 6) & 0x3f];
  90. cp[1] = raw3270_ebcgraf[addr & 0x3f];
  91. }
  92. }
  93. /*
  94. * Allocate a new 3270 ccw request
  95. */
  96. struct raw3270_request *
  97. raw3270_request_alloc(size_t size)
  98. {
  99. struct raw3270_request *rq;
  100. /* Allocate request structure */
  101. rq = kzalloc(sizeof(struct raw3270_request), GFP_KERNEL | GFP_DMA);
  102. if (!rq)
  103. return ERR_PTR(-ENOMEM);
  104. /* alloc output buffer. */
  105. if (size > 0) {
  106. rq->buffer = kmalloc(size, GFP_KERNEL | GFP_DMA);
  107. if (!rq->buffer) {
  108. kfree(rq);
  109. return ERR_PTR(-ENOMEM);
  110. }
  111. }
  112. rq->size = size;
  113. INIT_LIST_HEAD(&rq->list);
  114. /*
  115. * Setup ccw.
  116. */
  117. rq->ccw.cda = __pa(rq->buffer);
  118. rq->ccw.flags = CCW_FLAG_SLI;
  119. return rq;
  120. }
  121. #ifdef CONFIG_TN3270_CONSOLE
  122. /*
  123. * Allocate a new 3270 ccw request from bootmem. Only works very
  124. * early in the boot process. Only con3270.c should be using this.
  125. */
  126. struct raw3270_request __init *raw3270_request_alloc_bootmem(size_t size)
  127. {
  128. struct raw3270_request *rq;
  129. rq = alloc_bootmem_low(sizeof(struct raw3270));
  130. /* alloc output buffer. */
  131. if (size > 0)
  132. rq->buffer = alloc_bootmem_low(size);
  133. rq->size = size;
  134. INIT_LIST_HEAD(&rq->list);
  135. /*
  136. * Setup ccw.
  137. */
  138. rq->ccw.cda = __pa(rq->buffer);
  139. rq->ccw.flags = CCW_FLAG_SLI;
  140. return rq;
  141. }
  142. #endif
  143. /*
  144. * Free 3270 ccw request
  145. */
  146. void
  147. raw3270_request_free (struct raw3270_request *rq)
  148. {
  149. kfree(rq->buffer);
  150. kfree(rq);
  151. }
  152. /*
  153. * Reset request to initial state.
  154. */
  155. void
  156. raw3270_request_reset(struct raw3270_request *rq)
  157. {
  158. BUG_ON(!list_empty(&rq->list));
  159. rq->ccw.cmd_code = 0;
  160. rq->ccw.count = 0;
  161. rq->ccw.cda = __pa(rq->buffer);
  162. rq->ccw.flags = CCW_FLAG_SLI;
  163. rq->rescnt = 0;
  164. rq->rc = 0;
  165. }
  166. /*
  167. * Set command code to ccw of a request.
  168. */
  169. void
  170. raw3270_request_set_cmd(struct raw3270_request *rq, u8 cmd)
  171. {
  172. rq->ccw.cmd_code = cmd;
  173. }
  174. /*
  175. * Add data fragment to output buffer.
  176. */
  177. int
  178. raw3270_request_add_data(struct raw3270_request *rq, void *data, size_t size)
  179. {
  180. if (size + rq->ccw.count > rq->size)
  181. return -E2BIG;
  182. memcpy(rq->buffer + rq->ccw.count, data, size);
  183. rq->ccw.count += size;
  184. return 0;
  185. }
  186. /*
  187. * Set address/length pair to ccw of a request.
  188. */
  189. void
  190. raw3270_request_set_data(struct raw3270_request *rq, void *data, size_t size)
  191. {
  192. rq->ccw.cda = __pa(data);
  193. rq->ccw.count = size;
  194. }
  195. /*
  196. * Set idal buffer to ccw of a request.
  197. */
  198. void
  199. raw3270_request_set_idal(struct raw3270_request *rq, struct idal_buffer *ib)
  200. {
  201. rq->ccw.cda = __pa(ib->data);
  202. rq->ccw.count = ib->size;
  203. rq->ccw.flags |= CCW_FLAG_IDA;
  204. }
  205. /*
  206. * Stop running ccw.
  207. */
  208. static int
  209. raw3270_halt_io_nolock(struct raw3270 *rp, struct raw3270_request *rq)
  210. {
  211. int retries;
  212. int rc;
  213. if (raw3270_request_final(rq))
  214. return 0;
  215. /* Check if interrupt has already been processed */
  216. for (retries = 0; retries < 5; retries++) {
  217. if (retries < 2)
  218. rc = ccw_device_halt(rp->cdev, (long) rq);
  219. else
  220. rc = ccw_device_clear(rp->cdev, (long) rq);
  221. if (rc == 0)
  222. break; /* termination successful */
  223. }
  224. return rc;
  225. }
  226. static int
  227. raw3270_halt_io(struct raw3270 *rp, struct raw3270_request *rq)
  228. {
  229. unsigned long flags;
  230. int rc;
  231. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  232. rc = raw3270_halt_io_nolock(rp, rq);
  233. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  234. return rc;
  235. }
  236. /*
  237. * Add the request to the request queue, try to start it if the
  238. * 3270 device is idle. Return without waiting for end of i/o.
  239. */
  240. static int
  241. __raw3270_start(struct raw3270 *rp, struct raw3270_view *view,
  242. struct raw3270_request *rq)
  243. {
  244. rq->view = view;
  245. raw3270_get_view(view);
  246. if (list_empty(&rp->req_queue) &&
  247. !test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  248. /* No other requests are on the queue. Start this one. */
  249. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  250. (unsigned long) rq, 0, 0);
  251. if (rq->rc) {
  252. raw3270_put_view(view);
  253. return rq->rc;
  254. }
  255. }
  256. list_add_tail(&rq->list, &rp->req_queue);
  257. return 0;
  258. }
  259. int
  260. raw3270_start(struct raw3270_view *view, struct raw3270_request *rq)
  261. {
  262. unsigned long flags;
  263. struct raw3270 *rp;
  264. int rc;
  265. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  266. rp = view->dev;
  267. if (!rp || rp->view != view)
  268. rc = -EACCES;
  269. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  270. rc = -ENODEV;
  271. else
  272. rc = __raw3270_start(rp, view, rq);
  273. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  274. return rc;
  275. }
  276. int
  277. raw3270_start_locked(struct raw3270_view *view, struct raw3270_request *rq)
  278. {
  279. struct raw3270 *rp;
  280. int rc;
  281. rp = view->dev;
  282. if (!rp || rp->view != view)
  283. rc = -EACCES;
  284. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  285. rc = -ENODEV;
  286. else
  287. rc = __raw3270_start(rp, view, rq);
  288. return rc;
  289. }
  290. int
  291. raw3270_start_irq(struct raw3270_view *view, struct raw3270_request *rq)
  292. {
  293. struct raw3270 *rp;
  294. rp = view->dev;
  295. rq->view = view;
  296. raw3270_get_view(view);
  297. list_add_tail(&rq->list, &rp->req_queue);
  298. return 0;
  299. }
  300. /*
  301. * 3270 interrupt routine, called from the ccw_device layer
  302. */
  303. static void
  304. raw3270_irq (struct ccw_device *cdev, unsigned long intparm, struct irb *irb)
  305. {
  306. struct raw3270 *rp;
  307. struct raw3270_view *view;
  308. struct raw3270_request *rq;
  309. int rc;
  310. rp = (struct raw3270 *) cdev->dev.driver_data;
  311. if (!rp)
  312. return;
  313. rq = (struct raw3270_request *) intparm;
  314. view = rq ? rq->view : rp->view;
  315. if (IS_ERR(irb))
  316. rc = RAW3270_IO_RETRY;
  317. else if (irb->scsw.cmd.fctl & SCSW_FCTL_HALT_FUNC) {
  318. rq->rc = -EIO;
  319. rc = RAW3270_IO_DONE;
  320. } else if (irb->scsw.cmd.dstat == (DEV_STAT_CHN_END | DEV_STAT_DEV_END |
  321. DEV_STAT_UNIT_EXCEP)) {
  322. /* Handle CE-DE-UE and subsequent UDE */
  323. set_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  324. rc = RAW3270_IO_BUSY;
  325. } else if (test_bit(RAW3270_FLAGS_BUSY, &rp->flags)) {
  326. /* Wait for UDE if busy flag is set. */
  327. if (irb->scsw.cmd.dstat & DEV_STAT_DEV_END) {
  328. clear_bit(RAW3270_FLAGS_BUSY, &rp->flags);
  329. /* Got it, now retry. */
  330. rc = RAW3270_IO_RETRY;
  331. } else
  332. rc = RAW3270_IO_BUSY;
  333. } else if (view)
  334. rc = view->fn->intv(view, rq, irb);
  335. else
  336. rc = RAW3270_IO_DONE;
  337. switch (rc) {
  338. case RAW3270_IO_DONE:
  339. break;
  340. case RAW3270_IO_BUSY:
  341. /*
  342. * Intervention required by the operator. We have to wait
  343. * for unsolicited device end.
  344. */
  345. return;
  346. case RAW3270_IO_RETRY:
  347. if (!rq)
  348. break;
  349. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  350. (unsigned long) rq, 0, 0);
  351. if (rq->rc == 0)
  352. return; /* Sucessfully restarted. */
  353. break;
  354. case RAW3270_IO_STOP:
  355. if (!rq)
  356. break;
  357. raw3270_halt_io_nolock(rp, rq);
  358. rq->rc = -EIO;
  359. break;
  360. default:
  361. BUG();
  362. }
  363. if (rq) {
  364. BUG_ON(list_empty(&rq->list));
  365. /* The request completed, remove from queue and do callback. */
  366. list_del_init(&rq->list);
  367. if (rq->callback)
  368. rq->callback(rq, rq->callback_data);
  369. /* Do put_device for get_device in raw3270_start. */
  370. raw3270_put_view(view);
  371. }
  372. /*
  373. * Try to start each request on request queue until one is
  374. * started successful.
  375. */
  376. while (!list_empty(&rp->req_queue)) {
  377. rq = list_entry(rp->req_queue.next,struct raw3270_request,list);
  378. rq->rc = ccw_device_start(rp->cdev, &rq->ccw,
  379. (unsigned long) rq, 0, 0);
  380. if (rq->rc == 0)
  381. break;
  382. /* Start failed. Remove request and do callback. */
  383. list_del_init(&rq->list);
  384. if (rq->callback)
  385. rq->callback(rq, rq->callback_data);
  386. /* Do put_device for get_device in raw3270_start. */
  387. raw3270_put_view(view);
  388. }
  389. }
  390. /*
  391. * Size sensing.
  392. */
  393. struct raw3270_ua { /* Query Reply structure for Usable Area */
  394. struct { /* Usable Area Query Reply Base */
  395. short l; /* Length of this structured field */
  396. char sfid; /* 0x81 if Query Reply */
  397. char qcode; /* 0x81 if Usable Area */
  398. char flags0;
  399. char flags1;
  400. short w; /* Width of usable area */
  401. short h; /* Heigth of usavle area */
  402. char units; /* 0x00:in; 0x01:mm */
  403. int xr;
  404. int yr;
  405. char aw;
  406. char ah;
  407. short buffsz; /* Character buffer size, bytes */
  408. char xmin;
  409. char ymin;
  410. char xmax;
  411. char ymax;
  412. } __attribute__ ((packed)) uab;
  413. struct { /* Alternate Usable Area Self-Defining Parameter */
  414. char l; /* Length of this Self-Defining Parm */
  415. char sdpid; /* 0x02 if Alternate Usable Area */
  416. char res;
  417. char auaid; /* 0x01 is Id for the A U A */
  418. short wauai; /* Width of AUAi */
  419. short hauai; /* Height of AUAi */
  420. char auaunits; /* 0x00:in, 0x01:mm */
  421. int auaxr;
  422. int auayr;
  423. char awauai;
  424. char ahauai;
  425. } __attribute__ ((packed)) aua;
  426. } __attribute__ ((packed));
  427. static struct diag210 raw3270_init_diag210;
  428. static DEFINE_MUTEX(raw3270_init_mutex);
  429. static int
  430. raw3270_init_irq(struct raw3270_view *view, struct raw3270_request *rq,
  431. struct irb *irb)
  432. {
  433. /*
  434. * Unit-Check Processing:
  435. * Expect Command Reject or Intervention Required.
  436. */
  437. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  438. /* Request finished abnormally. */
  439. if (irb->ecw[0] & SNS0_INTERVENTION_REQ) {
  440. set_bit(RAW3270_FLAGS_BUSY, &view->dev->flags);
  441. return RAW3270_IO_BUSY;
  442. }
  443. }
  444. if (rq) {
  445. if (irb->scsw.cmd.dstat & DEV_STAT_UNIT_CHECK) {
  446. if (irb->ecw[0] & SNS0_CMD_REJECT)
  447. rq->rc = -EOPNOTSUPP;
  448. else
  449. rq->rc = -EIO;
  450. } else
  451. /* Request finished normally. Copy residual count. */
  452. rq->rescnt = irb->scsw.cmd.count;
  453. }
  454. if (irb->scsw.cmd.dstat & DEV_STAT_ATTENTION) {
  455. set_bit(RAW3270_FLAGS_ATTN, &view->dev->flags);
  456. wake_up(&raw3270_wait_queue);
  457. }
  458. return RAW3270_IO_DONE;
  459. }
  460. static struct raw3270_fn raw3270_init_fn = {
  461. .intv = raw3270_init_irq
  462. };
  463. static struct raw3270_view raw3270_init_view = {
  464. .fn = &raw3270_init_fn
  465. };
  466. /*
  467. * raw3270_wait/raw3270_wait_interruptible/__raw3270_wakeup
  468. * Wait for end of request. The request must have been started
  469. * with raw3270_start, rc = 0. The device lock may NOT have been
  470. * released between calling raw3270_start and raw3270_wait.
  471. */
  472. static void
  473. raw3270_wake_init(struct raw3270_request *rq, void *data)
  474. {
  475. wake_up((wait_queue_head_t *) data);
  476. }
  477. /*
  478. * Special wait function that can cope with console initialization.
  479. */
  480. static int
  481. raw3270_start_init(struct raw3270 *rp, struct raw3270_view *view,
  482. struct raw3270_request *rq)
  483. {
  484. unsigned long flags;
  485. int rc;
  486. #ifdef CONFIG_TN3270_CONSOLE
  487. if (raw3270_registered == 0) {
  488. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  489. rq->callback = NULL;
  490. rc = __raw3270_start(rp, view, rq);
  491. if (rc == 0)
  492. while (!raw3270_request_final(rq)) {
  493. wait_cons_dev();
  494. barrier();
  495. }
  496. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  497. return rq->rc;
  498. }
  499. #endif
  500. rq->callback = raw3270_wake_init;
  501. rq->callback_data = &raw3270_wait_queue;
  502. spin_lock_irqsave(get_ccwdev_lock(view->dev->cdev), flags);
  503. rc = __raw3270_start(rp, view, rq);
  504. spin_unlock_irqrestore(get_ccwdev_lock(view->dev->cdev), flags);
  505. if (rc)
  506. return rc;
  507. /* Now wait for the completion. */
  508. rc = wait_event_interruptible(raw3270_wait_queue,
  509. raw3270_request_final(rq));
  510. if (rc == -ERESTARTSYS) { /* Interrupted by a signal. */
  511. raw3270_halt_io(view->dev, rq);
  512. /* No wait for the halt to complete. */
  513. wait_event(raw3270_wait_queue, raw3270_request_final(rq));
  514. return -ERESTARTSYS;
  515. }
  516. return rq->rc;
  517. }
  518. static int
  519. __raw3270_size_device_vm(struct raw3270 *rp)
  520. {
  521. int rc, model;
  522. struct ccw_dev_id dev_id;
  523. ccw_device_get_id(rp->cdev, &dev_id);
  524. raw3270_init_diag210.vrdcdvno = dev_id.devno;
  525. raw3270_init_diag210.vrdclen = sizeof(struct diag210);
  526. rc = diag210(&raw3270_init_diag210);
  527. if (rc)
  528. return rc;
  529. model = raw3270_init_diag210.vrdccrmd;
  530. switch (model) {
  531. case 2:
  532. rp->model = model;
  533. rp->rows = 24;
  534. rp->cols = 80;
  535. break;
  536. case 3:
  537. rp->model = model;
  538. rp->rows = 32;
  539. rp->cols = 80;
  540. break;
  541. case 4:
  542. rp->model = model;
  543. rp->rows = 43;
  544. rp->cols = 80;
  545. break;
  546. case 5:
  547. rp->model = model;
  548. rp->rows = 27;
  549. rp->cols = 132;
  550. break;
  551. default:
  552. rc = -EOPNOTSUPP;
  553. break;
  554. }
  555. return rc;
  556. }
  557. static int
  558. __raw3270_size_device(struct raw3270 *rp)
  559. {
  560. static const unsigned char wbuf[] =
  561. { 0x00, 0x07, 0x01, 0xff, 0x03, 0x00, 0x81 };
  562. struct raw3270_ua *uap;
  563. unsigned short count;
  564. int rc;
  565. /*
  566. * To determine the size of the 3270 device we need to do:
  567. * 1) send a 'read partition' data stream to the device
  568. * 2) wait for the attn interrupt that preceeds the query reply
  569. * 3) do a read modified to get the query reply
  570. * To make things worse we have to cope with intervention
  571. * required (3270 device switched to 'stand-by') and command
  572. * rejects (old devices that can't do 'read partition').
  573. */
  574. memset(&rp->init_request, 0, sizeof(rp->init_request));
  575. memset(&rp->init_data, 0, 256);
  576. /* Store 'read partition' data stream to init_data */
  577. memcpy(&rp->init_data, wbuf, sizeof(wbuf));
  578. INIT_LIST_HEAD(&rp->init_request.list);
  579. rp->init_request.ccw.cmd_code = TC_WRITESF;
  580. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  581. rp->init_request.ccw.count = sizeof(wbuf);
  582. rp->init_request.ccw.cda = (__u32) __pa(&rp->init_data);
  583. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  584. if (rc)
  585. /* Check error cases: -ERESTARTSYS, -EIO and -EOPNOTSUPP */
  586. return rc;
  587. /* Wait for attention interrupt. */
  588. #ifdef CONFIG_TN3270_CONSOLE
  589. if (raw3270_registered == 0) {
  590. unsigned long flags;
  591. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  592. while (!test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags))
  593. wait_cons_dev();
  594. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  595. } else
  596. #endif
  597. rc = wait_event_interruptible(raw3270_wait_queue,
  598. test_and_clear_bit(RAW3270_FLAGS_ATTN, &rp->flags));
  599. if (rc)
  600. return rc;
  601. /*
  602. * The device accepted the 'read partition' command. Now
  603. * set up a read ccw and issue it.
  604. */
  605. rp->init_request.ccw.cmd_code = TC_READMOD;
  606. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  607. rp->init_request.ccw.count = sizeof(rp->init_data);
  608. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  609. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  610. if (rc)
  611. return rc;
  612. /* Got a Query Reply */
  613. count = sizeof(rp->init_data) - rp->init_request.rescnt;
  614. uap = (struct raw3270_ua *) (rp->init_data + 1);
  615. /* Paranoia check. */
  616. if (rp->init_data[0] != 0x88 || uap->uab.qcode != 0x81)
  617. return -EOPNOTSUPP;
  618. /* Copy rows/columns of default Usable Area */
  619. rp->rows = uap->uab.h;
  620. rp->cols = uap->uab.w;
  621. /* Check for 14 bit addressing */
  622. if ((uap->uab.flags0 & 0x0d) == 0x01)
  623. set_bit(RAW3270_FLAGS_14BITADDR, &rp->flags);
  624. /* Check for Alternate Usable Area */
  625. if (uap->uab.l == sizeof(struct raw3270_ua) &&
  626. uap->aua.sdpid == 0x02) {
  627. rp->rows = uap->aua.hauai;
  628. rp->cols = uap->aua.wauai;
  629. }
  630. return 0;
  631. }
  632. static int
  633. raw3270_size_device(struct raw3270 *rp)
  634. {
  635. int rc;
  636. mutex_lock(&raw3270_init_mutex);
  637. rp->view = &raw3270_init_view;
  638. raw3270_init_view.dev = rp;
  639. if (MACHINE_IS_VM)
  640. rc = __raw3270_size_device_vm(rp);
  641. else
  642. rc = __raw3270_size_device(rp);
  643. raw3270_init_view.dev = NULL;
  644. rp->view = NULL;
  645. mutex_unlock(&raw3270_init_mutex);
  646. if (rc == 0) { /* Found something. */
  647. /* Try to find a model. */
  648. rp->model = 0;
  649. if (rp->rows == 24 && rp->cols == 80)
  650. rp->model = 2;
  651. if (rp->rows == 32 && rp->cols == 80)
  652. rp->model = 3;
  653. if (rp->rows == 43 && rp->cols == 80)
  654. rp->model = 4;
  655. if (rp->rows == 27 && rp->cols == 132)
  656. rp->model = 5;
  657. } else {
  658. /* Couldn't detect size. Use default model 2. */
  659. rp->model = 2;
  660. rp->rows = 24;
  661. rp->cols = 80;
  662. return 0;
  663. }
  664. return rc;
  665. }
  666. static int
  667. raw3270_reset_device(struct raw3270 *rp)
  668. {
  669. int rc;
  670. mutex_lock(&raw3270_init_mutex);
  671. memset(&rp->init_request, 0, sizeof(rp->init_request));
  672. memset(&rp->init_data, 0, sizeof(rp->init_data));
  673. /* Store reset data stream to init_data/init_request */
  674. rp->init_data[0] = TW_KR;
  675. INIT_LIST_HEAD(&rp->init_request.list);
  676. rp->init_request.ccw.cmd_code = TC_EWRITEA;
  677. rp->init_request.ccw.flags = CCW_FLAG_SLI;
  678. rp->init_request.ccw.count = 1;
  679. rp->init_request.ccw.cda = (__u32) __pa(rp->init_data);
  680. rp->view = &raw3270_init_view;
  681. raw3270_init_view.dev = rp;
  682. rc = raw3270_start_init(rp, &raw3270_init_view, &rp->init_request);
  683. raw3270_init_view.dev = NULL;
  684. rp->view = NULL;
  685. mutex_unlock(&raw3270_init_mutex);
  686. return rc;
  687. }
  688. int
  689. raw3270_reset(struct raw3270_view *view)
  690. {
  691. struct raw3270 *rp;
  692. int rc;
  693. rp = view->dev;
  694. if (!rp || rp->view != view)
  695. rc = -EACCES;
  696. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  697. rc = -ENODEV;
  698. else
  699. rc = raw3270_reset_device(view->dev);
  700. return rc;
  701. }
  702. /*
  703. * Setup new 3270 device.
  704. */
  705. static int
  706. raw3270_setup_device(struct ccw_device *cdev, struct raw3270 *rp, char *ascebc)
  707. {
  708. struct list_head *l;
  709. struct raw3270 *tmp;
  710. int minor;
  711. memset(rp, 0, sizeof(struct raw3270));
  712. /* Copy ebcdic -> ascii translation table. */
  713. memcpy(ascebc, _ascebc, 256);
  714. if (tubxcorrect) {
  715. /* correct brackets and circumflex */
  716. ascebc['['] = 0xad;
  717. ascebc[']'] = 0xbd;
  718. ascebc['^'] = 0xb0;
  719. }
  720. rp->ascebc = ascebc;
  721. /* Set defaults. */
  722. rp->rows = 24;
  723. rp->cols = 80;
  724. INIT_LIST_HEAD(&rp->req_queue);
  725. INIT_LIST_HEAD(&rp->view_list);
  726. /*
  727. * Add device to list and find the smallest unused minor
  728. * number for it. Note: there is no device with minor 0,
  729. * see special case for fs3270.c:fs3270_open().
  730. */
  731. mutex_lock(&raw3270_mutex);
  732. /* Keep the list sorted. */
  733. minor = RAW3270_FIRSTMINOR;
  734. rp->minor = -1;
  735. list_for_each(l, &raw3270_devices) {
  736. tmp = list_entry(l, struct raw3270, list);
  737. if (tmp->minor > minor) {
  738. rp->minor = minor;
  739. __list_add(&rp->list, l->prev, l);
  740. break;
  741. }
  742. minor++;
  743. }
  744. if (rp->minor == -1 && minor < RAW3270_MAXDEVS + RAW3270_FIRSTMINOR) {
  745. rp->minor = minor;
  746. list_add_tail(&rp->list, &raw3270_devices);
  747. }
  748. mutex_unlock(&raw3270_mutex);
  749. /* No free minor number? Then give up. */
  750. if (rp->minor == -1)
  751. return -EUSERS;
  752. rp->cdev = cdev;
  753. cdev->dev.driver_data = rp;
  754. cdev->handler = raw3270_irq;
  755. return 0;
  756. }
  757. #ifdef CONFIG_TN3270_CONSOLE
  758. /*
  759. * Setup 3270 device configured as console.
  760. */
  761. struct raw3270 __init *raw3270_setup_console(struct ccw_device *cdev)
  762. {
  763. struct raw3270 *rp;
  764. char *ascebc;
  765. int rc;
  766. rp = (struct raw3270 *) alloc_bootmem_low(sizeof(struct raw3270));
  767. ascebc = (char *) alloc_bootmem(256);
  768. rc = raw3270_setup_device(cdev, rp, ascebc);
  769. if (rc)
  770. return ERR_PTR(rc);
  771. set_bit(RAW3270_FLAGS_CONSOLE, &rp->flags);
  772. rc = raw3270_reset_device(rp);
  773. if (rc)
  774. return ERR_PTR(rc);
  775. rc = raw3270_size_device(rp);
  776. if (rc)
  777. return ERR_PTR(rc);
  778. rc = raw3270_reset_device(rp);
  779. if (rc)
  780. return ERR_PTR(rc);
  781. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  782. return rp;
  783. }
  784. void
  785. raw3270_wait_cons_dev(struct raw3270 *rp)
  786. {
  787. unsigned long flags;
  788. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  789. wait_cons_dev();
  790. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  791. }
  792. #endif
  793. /*
  794. * Create a 3270 device structure.
  795. */
  796. static struct raw3270 *
  797. raw3270_create_device(struct ccw_device *cdev)
  798. {
  799. struct raw3270 *rp;
  800. char *ascebc;
  801. int rc;
  802. rp = kmalloc(sizeof(struct raw3270), GFP_KERNEL | GFP_DMA);
  803. if (!rp)
  804. return ERR_PTR(-ENOMEM);
  805. ascebc = kmalloc(256, GFP_KERNEL);
  806. if (!ascebc) {
  807. kfree(rp);
  808. return ERR_PTR(-ENOMEM);
  809. }
  810. rc = raw3270_setup_device(cdev, rp, ascebc);
  811. if (rc) {
  812. kfree(rp->ascebc);
  813. kfree(rp);
  814. rp = ERR_PTR(rc);
  815. }
  816. /* Get reference to ccw_device structure. */
  817. get_device(&cdev->dev);
  818. return rp;
  819. }
  820. /*
  821. * Activate a view.
  822. */
  823. int
  824. raw3270_activate_view(struct raw3270_view *view)
  825. {
  826. struct raw3270 *rp;
  827. struct raw3270_view *oldview, *nv;
  828. unsigned long flags;
  829. int rc;
  830. rp = view->dev;
  831. if (!rp)
  832. return -ENODEV;
  833. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  834. if (rp->view == view)
  835. rc = 0;
  836. else if (!test_bit(RAW3270_FLAGS_READY, &rp->flags))
  837. rc = -ENODEV;
  838. else {
  839. oldview = NULL;
  840. if (rp->view) {
  841. oldview = rp->view;
  842. oldview->fn->deactivate(oldview);
  843. }
  844. rp->view = view;
  845. rc = view->fn->activate(view);
  846. if (rc) {
  847. /* Didn't work. Try to reactivate the old view. */
  848. rp->view = oldview;
  849. if (!oldview || oldview->fn->activate(oldview) != 0) {
  850. /* Didn't work as well. Try any other view. */
  851. list_for_each_entry(nv, &rp->view_list, list)
  852. if (nv != view && nv != oldview) {
  853. rp->view = nv;
  854. if (nv->fn->activate(nv) == 0)
  855. break;
  856. rp->view = NULL;
  857. }
  858. }
  859. }
  860. }
  861. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  862. return rc;
  863. }
  864. /*
  865. * Deactivate current view.
  866. */
  867. void
  868. raw3270_deactivate_view(struct raw3270_view *view)
  869. {
  870. unsigned long flags;
  871. struct raw3270 *rp;
  872. rp = view->dev;
  873. if (!rp)
  874. return;
  875. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  876. if (rp->view == view) {
  877. view->fn->deactivate(view);
  878. rp->view = NULL;
  879. /* Move deactivated view to end of list. */
  880. list_del_init(&view->list);
  881. list_add_tail(&view->list, &rp->view_list);
  882. /* Try to activate another view. */
  883. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  884. list_for_each_entry(view, &rp->view_list, list) {
  885. rp->view = view;
  886. if (view->fn->activate(view) == 0)
  887. break;
  888. rp->view = NULL;
  889. }
  890. }
  891. }
  892. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  893. }
  894. /*
  895. * Add view to device with minor "minor".
  896. */
  897. int
  898. raw3270_add_view(struct raw3270_view *view, struct raw3270_fn *fn, int minor)
  899. {
  900. unsigned long flags;
  901. struct raw3270 *rp;
  902. int rc;
  903. if (minor <= 0)
  904. return -ENODEV;
  905. mutex_lock(&raw3270_mutex);
  906. rc = -ENODEV;
  907. list_for_each_entry(rp, &raw3270_devices, list) {
  908. if (rp->minor != minor)
  909. continue;
  910. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  911. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  912. atomic_set(&view->ref_count, 2);
  913. view->dev = rp;
  914. view->fn = fn;
  915. view->model = rp->model;
  916. view->rows = rp->rows;
  917. view->cols = rp->cols;
  918. view->ascebc = rp->ascebc;
  919. spin_lock_init(&view->lock);
  920. list_add(&view->list, &rp->view_list);
  921. rc = 0;
  922. }
  923. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  924. break;
  925. }
  926. mutex_unlock(&raw3270_mutex);
  927. return rc;
  928. }
  929. /*
  930. * Find specific view of device with minor "minor".
  931. */
  932. struct raw3270_view *
  933. raw3270_find_view(struct raw3270_fn *fn, int minor)
  934. {
  935. struct raw3270 *rp;
  936. struct raw3270_view *view, *tmp;
  937. unsigned long flags;
  938. mutex_lock(&raw3270_mutex);
  939. view = ERR_PTR(-ENODEV);
  940. list_for_each_entry(rp, &raw3270_devices, list) {
  941. if (rp->minor != minor)
  942. continue;
  943. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  944. if (test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  945. view = ERR_PTR(-ENOENT);
  946. list_for_each_entry(tmp, &rp->view_list, list) {
  947. if (tmp->fn == fn) {
  948. raw3270_get_view(tmp);
  949. view = tmp;
  950. break;
  951. }
  952. }
  953. }
  954. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  955. break;
  956. }
  957. mutex_unlock(&raw3270_mutex);
  958. return view;
  959. }
  960. /*
  961. * Remove view from device and free view structure via call to view->fn->free.
  962. */
  963. void
  964. raw3270_del_view(struct raw3270_view *view)
  965. {
  966. unsigned long flags;
  967. struct raw3270 *rp;
  968. struct raw3270_view *nv;
  969. rp = view->dev;
  970. spin_lock_irqsave(get_ccwdev_lock(rp->cdev), flags);
  971. if (rp->view == view) {
  972. view->fn->deactivate(view);
  973. rp->view = NULL;
  974. }
  975. list_del_init(&view->list);
  976. if (!rp->view && test_bit(RAW3270_FLAGS_READY, &rp->flags)) {
  977. /* Try to activate another view. */
  978. list_for_each_entry(nv, &rp->view_list, list) {
  979. if (nv->fn->activate(nv) == 0) {
  980. rp->view = nv;
  981. break;
  982. }
  983. }
  984. }
  985. spin_unlock_irqrestore(get_ccwdev_lock(rp->cdev), flags);
  986. /* Wait for reference counter to drop to zero. */
  987. atomic_dec(&view->ref_count);
  988. wait_event(raw3270_wait_queue, atomic_read(&view->ref_count) == 0);
  989. if (view->fn->free)
  990. view->fn->free(view);
  991. }
  992. /*
  993. * Remove a 3270 device structure.
  994. */
  995. static void
  996. raw3270_delete_device(struct raw3270 *rp)
  997. {
  998. struct ccw_device *cdev;
  999. /* Remove from device chain. */
  1000. mutex_lock(&raw3270_mutex);
  1001. if (rp->clttydev && !IS_ERR(rp->clttydev))
  1002. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1003. if (rp->cltubdev && !IS_ERR(rp->cltubdev))
  1004. device_destroy(class3270, MKDEV(IBM_FS3270_MAJOR, rp->minor));
  1005. list_del_init(&rp->list);
  1006. mutex_unlock(&raw3270_mutex);
  1007. /* Disconnect from ccw_device. */
  1008. cdev = rp->cdev;
  1009. rp->cdev = NULL;
  1010. cdev->dev.driver_data = NULL;
  1011. cdev->handler = NULL;
  1012. /* Put ccw_device structure. */
  1013. put_device(&cdev->dev);
  1014. /* Now free raw3270 structure. */
  1015. kfree(rp->ascebc);
  1016. kfree(rp);
  1017. }
  1018. static int
  1019. raw3270_probe (struct ccw_device *cdev)
  1020. {
  1021. return 0;
  1022. }
  1023. /*
  1024. * Additional attributes for a 3270 device
  1025. */
  1026. static ssize_t
  1027. raw3270_model_show(struct device *dev, struct device_attribute *attr, char *buf)
  1028. {
  1029. return snprintf(buf, PAGE_SIZE, "%i\n",
  1030. ((struct raw3270 *) dev->driver_data)->model);
  1031. }
  1032. static DEVICE_ATTR(model, 0444, raw3270_model_show, NULL);
  1033. static ssize_t
  1034. raw3270_rows_show(struct device *dev, struct device_attribute *attr, char *buf)
  1035. {
  1036. return snprintf(buf, PAGE_SIZE, "%i\n",
  1037. ((struct raw3270 *) dev->driver_data)->rows);
  1038. }
  1039. static DEVICE_ATTR(rows, 0444, raw3270_rows_show, NULL);
  1040. static ssize_t
  1041. raw3270_columns_show(struct device *dev, struct device_attribute *attr, char *buf)
  1042. {
  1043. return snprintf(buf, PAGE_SIZE, "%i\n",
  1044. ((struct raw3270 *) dev->driver_data)->cols);
  1045. }
  1046. static DEVICE_ATTR(columns, 0444, raw3270_columns_show, NULL);
  1047. static struct attribute * raw3270_attrs[] = {
  1048. &dev_attr_model.attr,
  1049. &dev_attr_rows.attr,
  1050. &dev_attr_columns.attr,
  1051. NULL,
  1052. };
  1053. static struct attribute_group raw3270_attr_group = {
  1054. .attrs = raw3270_attrs,
  1055. };
  1056. static int raw3270_create_attributes(struct raw3270 *rp)
  1057. {
  1058. int rc;
  1059. rc = sysfs_create_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1060. if (rc)
  1061. goto out;
  1062. rp->clttydev = device_create(class3270, &rp->cdev->dev,
  1063. MKDEV(IBM_TTY3270_MAJOR, rp->minor), NULL,
  1064. "tty%s", dev_name(&rp->cdev->dev));
  1065. if (IS_ERR(rp->clttydev)) {
  1066. rc = PTR_ERR(rp->clttydev);
  1067. goto out_ttydev;
  1068. }
  1069. rp->cltubdev = device_create(class3270, &rp->cdev->dev,
  1070. MKDEV(IBM_FS3270_MAJOR, rp->minor), NULL,
  1071. "tub%s", dev_name(&rp->cdev->dev));
  1072. if (!IS_ERR(rp->cltubdev))
  1073. goto out;
  1074. rc = PTR_ERR(rp->cltubdev);
  1075. device_destroy(class3270, MKDEV(IBM_TTY3270_MAJOR, rp->minor));
  1076. out_ttydev:
  1077. sysfs_remove_group(&rp->cdev->dev.kobj, &raw3270_attr_group);
  1078. out:
  1079. return rc;
  1080. }
  1081. /*
  1082. * Notifier for device addition/removal
  1083. */
  1084. struct raw3270_notifier {
  1085. struct list_head list;
  1086. void (*notifier)(int, int);
  1087. };
  1088. static LIST_HEAD(raw3270_notifier);
  1089. int raw3270_register_notifier(void (*notifier)(int, int))
  1090. {
  1091. struct raw3270_notifier *np;
  1092. struct raw3270 *rp;
  1093. np = kmalloc(sizeof(struct raw3270_notifier), GFP_KERNEL);
  1094. if (!np)
  1095. return -ENOMEM;
  1096. np->notifier = notifier;
  1097. mutex_lock(&raw3270_mutex);
  1098. list_add_tail(&np->list, &raw3270_notifier);
  1099. list_for_each_entry(rp, &raw3270_devices, list) {
  1100. get_device(&rp->cdev->dev);
  1101. notifier(rp->minor, 1);
  1102. }
  1103. mutex_unlock(&raw3270_mutex);
  1104. return 0;
  1105. }
  1106. void raw3270_unregister_notifier(void (*notifier)(int, int))
  1107. {
  1108. struct raw3270_notifier *np;
  1109. mutex_lock(&raw3270_mutex);
  1110. list_for_each_entry(np, &raw3270_notifier, list)
  1111. if (np->notifier == notifier) {
  1112. list_del(&np->list);
  1113. kfree(np);
  1114. break;
  1115. }
  1116. mutex_unlock(&raw3270_mutex);
  1117. }
  1118. /*
  1119. * Set 3270 device online.
  1120. */
  1121. static int
  1122. raw3270_set_online (struct ccw_device *cdev)
  1123. {
  1124. struct raw3270 *rp;
  1125. struct raw3270_notifier *np;
  1126. int rc;
  1127. rp = raw3270_create_device(cdev);
  1128. if (IS_ERR(rp))
  1129. return PTR_ERR(rp);
  1130. rc = raw3270_reset_device(rp);
  1131. if (rc)
  1132. goto failure;
  1133. rc = raw3270_size_device(rp);
  1134. if (rc)
  1135. goto failure;
  1136. rc = raw3270_reset_device(rp);
  1137. if (rc)
  1138. goto failure;
  1139. rc = raw3270_create_attributes(rp);
  1140. if (rc)
  1141. goto failure;
  1142. set_bit(RAW3270_FLAGS_READY, &rp->flags);
  1143. mutex_lock(&raw3270_mutex);
  1144. list_for_each_entry(np, &raw3270_notifier, list)
  1145. np->notifier(rp->minor, 1);
  1146. mutex_unlock(&raw3270_mutex);
  1147. return 0;
  1148. failure:
  1149. raw3270_delete_device(rp);
  1150. return rc;
  1151. }
  1152. /*
  1153. * Remove 3270 device structure.
  1154. */
  1155. static void
  1156. raw3270_remove (struct ccw_device *cdev)
  1157. {
  1158. unsigned long flags;
  1159. struct raw3270 *rp;
  1160. struct raw3270_view *v;
  1161. struct raw3270_notifier *np;
  1162. rp = cdev->dev.driver_data;
  1163. /*
  1164. * _remove is the opposite of _probe; it's probe that
  1165. * should set up rp. raw3270_remove gets entered for
  1166. * devices even if they haven't been varied online.
  1167. * Thus, rp may validly be NULL here.
  1168. */
  1169. if (rp == NULL)
  1170. return;
  1171. clear_bit(RAW3270_FLAGS_READY, &rp->flags);
  1172. sysfs_remove_group(&cdev->dev.kobj, &raw3270_attr_group);
  1173. /* Deactivate current view and remove all views. */
  1174. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1175. if (rp->view) {
  1176. rp->view->fn->deactivate(rp->view);
  1177. rp->view = NULL;
  1178. }
  1179. while (!list_empty(&rp->view_list)) {
  1180. v = list_entry(rp->view_list.next, struct raw3270_view, list);
  1181. if (v->fn->release)
  1182. v->fn->release(v);
  1183. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1184. raw3270_del_view(v);
  1185. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1186. }
  1187. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1188. mutex_lock(&raw3270_mutex);
  1189. list_for_each_entry(np, &raw3270_notifier, list)
  1190. np->notifier(rp->minor, 0);
  1191. mutex_unlock(&raw3270_mutex);
  1192. /* Reset 3270 device. */
  1193. raw3270_reset_device(rp);
  1194. /* And finally remove it. */
  1195. raw3270_delete_device(rp);
  1196. }
  1197. /*
  1198. * Set 3270 device offline.
  1199. */
  1200. static int
  1201. raw3270_set_offline (struct ccw_device *cdev)
  1202. {
  1203. struct raw3270 *rp;
  1204. rp = cdev->dev.driver_data;
  1205. if (test_bit(RAW3270_FLAGS_CONSOLE, &rp->flags))
  1206. return -EBUSY;
  1207. raw3270_remove(cdev);
  1208. return 0;
  1209. }
  1210. static struct ccw_device_id raw3270_id[] = {
  1211. { CCW_DEVICE(0x3270, 0) },
  1212. { CCW_DEVICE(0x3271, 0) },
  1213. { CCW_DEVICE(0x3272, 0) },
  1214. { CCW_DEVICE(0x3273, 0) },
  1215. { CCW_DEVICE(0x3274, 0) },
  1216. { CCW_DEVICE(0x3275, 0) },
  1217. { CCW_DEVICE(0x3276, 0) },
  1218. { CCW_DEVICE(0x3277, 0) },
  1219. { CCW_DEVICE(0x3278, 0) },
  1220. { CCW_DEVICE(0x3279, 0) },
  1221. { CCW_DEVICE(0x3174, 0) },
  1222. { /* end of list */ },
  1223. };
  1224. static struct ccw_driver raw3270_ccw_driver = {
  1225. .name = "3270",
  1226. .owner = THIS_MODULE,
  1227. .ids = raw3270_id,
  1228. .probe = &raw3270_probe,
  1229. .remove = &raw3270_remove,
  1230. .set_online = &raw3270_set_online,
  1231. .set_offline = &raw3270_set_offline,
  1232. };
  1233. static int
  1234. raw3270_init(void)
  1235. {
  1236. struct raw3270 *rp;
  1237. int rc;
  1238. if (raw3270_registered)
  1239. return 0;
  1240. raw3270_registered = 1;
  1241. rc = ccw_driver_register(&raw3270_ccw_driver);
  1242. if (rc == 0) {
  1243. /* Create attributes for early (= console) device. */
  1244. mutex_lock(&raw3270_mutex);
  1245. class3270 = class_create(THIS_MODULE, "3270");
  1246. list_for_each_entry(rp, &raw3270_devices, list) {
  1247. get_device(&rp->cdev->dev);
  1248. raw3270_create_attributes(rp);
  1249. }
  1250. mutex_unlock(&raw3270_mutex);
  1251. }
  1252. return rc;
  1253. }
  1254. static void
  1255. raw3270_exit(void)
  1256. {
  1257. ccw_driver_unregister(&raw3270_ccw_driver);
  1258. class_destroy(class3270);
  1259. }
  1260. MODULE_LICENSE("GPL");
  1261. module_init(raw3270_init);
  1262. module_exit(raw3270_exit);
  1263. EXPORT_SYMBOL(raw3270_request_alloc);
  1264. EXPORT_SYMBOL(raw3270_request_free);
  1265. EXPORT_SYMBOL(raw3270_request_reset);
  1266. EXPORT_SYMBOL(raw3270_request_set_cmd);
  1267. EXPORT_SYMBOL(raw3270_request_add_data);
  1268. EXPORT_SYMBOL(raw3270_request_set_data);
  1269. EXPORT_SYMBOL(raw3270_request_set_idal);
  1270. EXPORT_SYMBOL(raw3270_buffer_address);
  1271. EXPORT_SYMBOL(raw3270_add_view);
  1272. EXPORT_SYMBOL(raw3270_del_view);
  1273. EXPORT_SYMBOL(raw3270_find_view);
  1274. EXPORT_SYMBOL(raw3270_activate_view);
  1275. EXPORT_SYMBOL(raw3270_deactivate_view);
  1276. EXPORT_SYMBOL(raw3270_start);
  1277. EXPORT_SYMBOL(raw3270_start_locked);
  1278. EXPORT_SYMBOL(raw3270_start_irq);
  1279. EXPORT_SYMBOL(raw3270_reset);
  1280. EXPORT_SYMBOL(raw3270_register_notifier);
  1281. EXPORT_SYMBOL(raw3270_unregister_notifier);
  1282. EXPORT_SYMBOL(raw3270_wait_queue);