raw3270.c 33 KB

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