isicom.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Original driver code supplied by Multi-Tech
  4. *
  5. * Changes
  6. * 1/9/98 alan@lxorguk.ukuu.org.uk
  7. * Merge to 2.0.x kernel tree
  8. * Obtain and use official major/minors
  9. * Loader switched to a misc device
  10. * (fixed range check bug as a side effect)
  11. * Printk clean up
  12. * 9/12/98 alan@lxorguk.ukuu.org.uk
  13. * Rough port to 2.1.x
  14. *
  15. * 10/6/99 sameer Merged the ISA and PCI drivers to
  16. * a new unified driver.
  17. *
  18. * 3/9/99 sameer Added support for ISI4616 cards.
  19. *
  20. * 16/9/99 sameer We do not force RTS low anymore.
  21. * This is to prevent the firmware
  22. * from getting confused.
  23. *
  24. * 26/10/99 sameer Cosmetic changes:The driver now
  25. * dumps the Port Count information
  26. * along with I/O address and IRQ.
  27. *
  28. * 13/12/99 sameer Fixed the problem with IRQ sharing.
  29. *
  30. * 10/5/00 sameer Fixed isicom_shutdown_board()
  31. * to not lower DTR on all the ports
  32. * when the last port on the card is
  33. * closed.
  34. *
  35. * 10/5/00 sameer Signal mask setup command added
  36. * to isicom_setup_port and
  37. * isicom_shutdown_port.
  38. *
  39. * 24/5/00 sameer The driver is now SMP aware.
  40. *
  41. *
  42. * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
  43. *
  44. *
  45. * 03/01/01 anil .s Added support for resetting the
  46. * internal modems on ISI cards.
  47. *
  48. * 08/02/01 anil .s Upgraded the driver for kernel
  49. * 2.4.x
  50. *
  51. * 11/04/01 Kevin Fixed firmware load problem with
  52. * ISIHP-4X card
  53. *
  54. * 30/04/01 anil .s Fixed the remote login through
  55. * ISI port problem. Now the link
  56. * does not go down before password
  57. * prompt.
  58. *
  59. * 03/05/01 anil .s Fixed the problem with IRQ sharing
  60. * among ISI-PCI cards.
  61. *
  62. * 03/05/01 anil .s Added support to display the version
  63. * info during insmod as well as module
  64. * listing by lsmod.
  65. *
  66. * 10/05/01 anil .s Done the modifications to the source
  67. * file and Install script so that the
  68. * same installation can be used for
  69. * 2.2.x and 2.4.x kernel.
  70. *
  71. * 06/06/01 anil .s Now we drop both dtr and rts during
  72. * shutdown_port as well as raise them
  73. * during isicom_config_port.
  74. *
  75. * 09/06/01 acme@conectiva.com.br use capable, not suser, do
  76. * restore_flags on failure in
  77. * isicom_send_break, verify put_user
  78. * result
  79. *
  80. * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
  81. * Baud index extended to 21
  82. *
  83. * 20/03/03 ranjeeth Made to work for Linux Advanced server.
  84. * Taken care of license warning.
  85. *
  86. * 10/12/03 Ravindra Made to work for Fedora Core 1 of
  87. * Red Hat Distribution
  88. *
  89. * 06/01/05 Alan Cox Merged the ISI and base kernel strands
  90. * into a single 2.6 driver
  91. *
  92. * ***********************************************************
  93. *
  94. * To use this driver you also need the support package. You
  95. * can find this in RPM format on
  96. * ftp://ftp.linux.org.uk/pub/linux/alan
  97. *
  98. * You can find the original tools for this direct from Multitech
  99. * ftp://ftp.multitech.com/ISI-Cards/
  100. *
  101. * Having installed the cards the module options (/etc/modprobe.d/)
  102. *
  103. * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
  104. *
  105. * Omit those entries for boards you don't have installed.
  106. *
  107. * TODO
  108. * Merge testing
  109. * 64-bit verification
  110. */
  111. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  112. #include <linux/module.h>
  113. #include <linux/firmware.h>
  114. #include <linux/kernel.h>
  115. #include <linux/tty.h>
  116. #include <linux/tty_flip.h>
  117. #include <linux/termios.h>
  118. #include <linux/fs.h>
  119. #include <linux/sched.h>
  120. #include <linux/serial.h>
  121. #include <linux/mm.h>
  122. #include <linux/interrupt.h>
  123. #include <linux/timer.h>
  124. #include <linux/delay.h>
  125. #include <linux/ioport.h>
  126. #include <linux/slab.h>
  127. #include <linux/uaccess.h>
  128. #include <linux/io.h>
  129. #include <linux/pci.h>
  130. #include <linux/isicom.h>
  131. #define InterruptTheCard(base) outw(0, (base) + 0xc)
  132. #define ClearInterrupt(base) inw((base) + 0x0a)
  133. #ifdef DEBUG
  134. #define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
  135. #else
  136. #define isicom_paranoia_check(a, b, c) 0
  137. #endif
  138. static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
  139. static void isicom_remove(struct pci_dev *);
  140. static const struct pci_device_id isicom_pci_tbl[] = {
  141. { PCI_DEVICE(VENDOR_ID, 0x2028) },
  142. { PCI_DEVICE(VENDOR_ID, 0x2051) },
  143. { PCI_DEVICE(VENDOR_ID, 0x2052) },
  144. { PCI_DEVICE(VENDOR_ID, 0x2053) },
  145. { PCI_DEVICE(VENDOR_ID, 0x2054) },
  146. { PCI_DEVICE(VENDOR_ID, 0x2055) },
  147. { PCI_DEVICE(VENDOR_ID, 0x2056) },
  148. { PCI_DEVICE(VENDOR_ID, 0x2057) },
  149. { PCI_DEVICE(VENDOR_ID, 0x2058) },
  150. { 0 }
  151. };
  152. MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
  153. static struct pci_driver isicom_driver = {
  154. .name = "isicom",
  155. .id_table = isicom_pci_tbl,
  156. .probe = isicom_probe,
  157. .remove = isicom_remove
  158. };
  159. static int prev_card = 3; /* start servicing isi_card[0] */
  160. static struct tty_driver *isicom_normal;
  161. static void isicom_tx(struct timer_list *unused);
  162. static void isicom_start(struct tty_struct *tty);
  163. static DEFINE_TIMER(tx, isicom_tx);
  164. /* baud index mappings from linux defns to isi */
  165. static signed char linuxb_to_isib[] = {
  166. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
  167. };
  168. struct isi_board {
  169. unsigned long base;
  170. int irq;
  171. unsigned char port_count;
  172. unsigned short status;
  173. unsigned short port_status; /* each bit for each port */
  174. unsigned short shift_count;
  175. struct isi_port *ports;
  176. signed char count;
  177. spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
  178. unsigned long flags;
  179. unsigned int index;
  180. };
  181. struct isi_port {
  182. unsigned short magic;
  183. struct tty_port port;
  184. u16 channel;
  185. u16 status;
  186. struct isi_board *card;
  187. unsigned char *xmit_buf;
  188. int xmit_head;
  189. int xmit_tail;
  190. int xmit_cnt;
  191. };
  192. static struct isi_board isi_card[BOARD_COUNT];
  193. static struct isi_port isi_ports[PORT_COUNT];
  194. /*
  195. * Locking functions for card level locking. We need to own both
  196. * the kernel lock for the card and have the card in a position that
  197. * it wants to talk.
  198. */
  199. static int WaitTillCardIsFree(unsigned long base)
  200. {
  201. unsigned int count = 0;
  202. unsigned int a = in_atomic(); /* do we run under spinlock? */
  203. while (!(inw(base + 0xe) & 0x1) && count++ < 100)
  204. if (a)
  205. mdelay(1);
  206. else
  207. msleep(1);
  208. return !(inw(base + 0xe) & 0x1);
  209. }
  210. static int lock_card(struct isi_board *card)
  211. {
  212. unsigned long base = card->base;
  213. unsigned int retries, a;
  214. for (retries = 0; retries < 10; retries++) {
  215. spin_lock_irqsave(&card->card_lock, card->flags);
  216. for (a = 0; a < 10; a++) {
  217. if (inw(base + 0xe) & 0x1)
  218. return 1;
  219. udelay(10);
  220. }
  221. spin_unlock_irqrestore(&card->card_lock, card->flags);
  222. msleep(10);
  223. }
  224. pr_warn("Failed to lock Card (0x%lx)\n", card->base);
  225. return 0; /* Failed to acquire the card! */
  226. }
  227. static void unlock_card(struct isi_board *card)
  228. {
  229. spin_unlock_irqrestore(&card->card_lock, card->flags);
  230. }
  231. /*
  232. * ISI Card specific ops ...
  233. */
  234. /* card->lock HAS to be held */
  235. static void raise_dtr(struct isi_port *port)
  236. {
  237. struct isi_board *card = port->card;
  238. unsigned long base = card->base;
  239. u16 channel = port->channel;
  240. if (WaitTillCardIsFree(base))
  241. return;
  242. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  243. outw(0x0504, base);
  244. InterruptTheCard(base);
  245. port->status |= ISI_DTR;
  246. }
  247. /* card->lock HAS to be held */
  248. static void drop_dtr(struct isi_port *port)
  249. {
  250. struct isi_board *card = port->card;
  251. unsigned long base = card->base;
  252. u16 channel = port->channel;
  253. if (WaitTillCardIsFree(base))
  254. return;
  255. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  256. outw(0x0404, base);
  257. InterruptTheCard(base);
  258. port->status &= ~ISI_DTR;
  259. }
  260. /* card->lock HAS to be held */
  261. static inline void raise_rts(struct isi_port *port)
  262. {
  263. struct isi_board *card = port->card;
  264. unsigned long base = card->base;
  265. u16 channel = port->channel;
  266. if (WaitTillCardIsFree(base))
  267. return;
  268. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  269. outw(0x0a04, base);
  270. InterruptTheCard(base);
  271. port->status |= ISI_RTS;
  272. }
  273. /* card->lock HAS to be held */
  274. static inline void drop_rts(struct isi_port *port)
  275. {
  276. struct isi_board *card = port->card;
  277. unsigned long base = card->base;
  278. u16 channel = port->channel;
  279. if (WaitTillCardIsFree(base))
  280. return;
  281. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  282. outw(0x0804, base);
  283. InterruptTheCard(base);
  284. port->status &= ~ISI_RTS;
  285. }
  286. /* card->lock MUST NOT be held */
  287. static void isicom_dtr_rts(struct tty_port *port, int on)
  288. {
  289. struct isi_port *ip = container_of(port, struct isi_port, port);
  290. struct isi_board *card = ip->card;
  291. unsigned long base = card->base;
  292. u16 channel = ip->channel;
  293. if (!lock_card(card))
  294. return;
  295. if (on) {
  296. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  297. outw(0x0f04, base);
  298. InterruptTheCard(base);
  299. ip->status |= (ISI_DTR | ISI_RTS);
  300. } else {
  301. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  302. outw(0x0C04, base);
  303. InterruptTheCard(base);
  304. ip->status &= ~(ISI_DTR | ISI_RTS);
  305. }
  306. unlock_card(card);
  307. }
  308. /* card->lock HAS to be held */
  309. static void drop_dtr_rts(struct isi_port *port)
  310. {
  311. struct isi_board *card = port->card;
  312. unsigned long base = card->base;
  313. u16 channel = port->channel;
  314. if (WaitTillCardIsFree(base))
  315. return;
  316. outw(0x8000 | (channel << card->shift_count) | 0x02, base);
  317. outw(0x0c04, base);
  318. InterruptTheCard(base);
  319. port->status &= ~(ISI_RTS | ISI_DTR);
  320. }
  321. /*
  322. * ISICOM Driver specific routines ...
  323. *
  324. */
  325. static inline int __isicom_paranoia_check(struct isi_port const *port,
  326. char *name, const char *routine)
  327. {
  328. if (!port) {
  329. pr_warn("Warning: bad isicom magic for dev %s in %s\n",
  330. name, routine);
  331. return 1;
  332. }
  333. if (port->magic != ISICOM_MAGIC) {
  334. pr_warn("Warning: NULL isicom port for dev %s in %s\n",
  335. name, routine);
  336. return 1;
  337. }
  338. return 0;
  339. }
  340. /*
  341. * Transmitter.
  342. *
  343. * We shovel data into the card buffers on a regular basis. The card
  344. * will do the rest of the work for us.
  345. */
  346. static void isicom_tx(struct timer_list *unused)
  347. {
  348. unsigned long flags, base;
  349. unsigned int retries;
  350. short count = (BOARD_COUNT-1), card;
  351. short txcount, wrd, residue, word_count, cnt;
  352. struct isi_port *port;
  353. struct tty_struct *tty;
  354. /* find next active board */
  355. card = (prev_card + 1) & 0x0003;
  356. while (count-- > 0) {
  357. if (isi_card[card].status & BOARD_ACTIVE)
  358. break;
  359. card = (card + 1) & 0x0003;
  360. }
  361. if (!(isi_card[card].status & BOARD_ACTIVE))
  362. goto sched_again;
  363. prev_card = card;
  364. count = isi_card[card].port_count;
  365. port = isi_card[card].ports;
  366. base = isi_card[card].base;
  367. spin_lock_irqsave(&isi_card[card].card_lock, flags);
  368. for (retries = 0; retries < 100; retries++) {
  369. if (inw(base + 0xe) & 0x1)
  370. break;
  371. udelay(2);
  372. }
  373. if (retries >= 100)
  374. goto unlock;
  375. tty = tty_port_tty_get(&port->port);
  376. if (tty == NULL)
  377. goto put_unlock;
  378. for (; count > 0; count--, port++) {
  379. /* port not active or tx disabled to force flow control */
  380. if (!tty_port_initialized(&port->port) ||
  381. !(port->status & ISI_TXOK))
  382. continue;
  383. txcount = min_t(short, TX_SIZE, port->xmit_cnt);
  384. if (txcount <= 0 || tty->stopped || tty->hw_stopped)
  385. continue;
  386. if (!(inw(base + 0x02) & (1 << port->channel)))
  387. continue;
  388. pr_debug("txing %d bytes, port%d.\n",
  389. txcount, port->channel + 1);
  390. outw((port->channel << isi_card[card].shift_count) | txcount,
  391. base);
  392. residue = NO;
  393. wrd = 0;
  394. while (1) {
  395. cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
  396. - port->xmit_tail));
  397. if (residue == YES) {
  398. residue = NO;
  399. if (cnt > 0) {
  400. wrd |= (port->port.xmit_buf[port->xmit_tail]
  401. << 8);
  402. port->xmit_tail = (port->xmit_tail + 1)
  403. & (SERIAL_XMIT_SIZE - 1);
  404. port->xmit_cnt--;
  405. txcount--;
  406. cnt--;
  407. outw(wrd, base);
  408. } else {
  409. outw(wrd, base);
  410. break;
  411. }
  412. }
  413. if (cnt <= 0)
  414. break;
  415. word_count = cnt >> 1;
  416. outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
  417. port->xmit_tail = (port->xmit_tail
  418. + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
  419. txcount -= (word_count << 1);
  420. port->xmit_cnt -= (word_count << 1);
  421. if (cnt & 0x0001) {
  422. residue = YES;
  423. wrd = port->port.xmit_buf[port->xmit_tail];
  424. port->xmit_tail = (port->xmit_tail + 1)
  425. & (SERIAL_XMIT_SIZE - 1);
  426. port->xmit_cnt--;
  427. txcount--;
  428. }
  429. }
  430. InterruptTheCard(base);
  431. if (port->xmit_cnt <= 0)
  432. port->status &= ~ISI_TXOK;
  433. if (port->xmit_cnt <= WAKEUP_CHARS)
  434. tty_wakeup(tty);
  435. }
  436. put_unlock:
  437. tty_kref_put(tty);
  438. unlock:
  439. spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
  440. /* schedule another tx for hopefully in about 10ms */
  441. sched_again:
  442. mod_timer(&tx, jiffies + msecs_to_jiffies(10));
  443. }
  444. /*
  445. * Main interrupt handler routine
  446. */
  447. static irqreturn_t isicom_interrupt(int irq, void *dev_id)
  448. {
  449. struct isi_board *card = dev_id;
  450. struct isi_port *port;
  451. struct tty_struct *tty;
  452. unsigned long base;
  453. u16 header, word_count, count, channel;
  454. short byte_count;
  455. unsigned char *rp;
  456. if (!card || !(card->status & FIRMWARE_LOADED))
  457. return IRQ_NONE;
  458. base = card->base;
  459. /* did the card interrupt us? */
  460. if (!(inw(base + 0x0e) & 0x02))
  461. return IRQ_NONE;
  462. spin_lock(&card->card_lock);
  463. /*
  464. * disable any interrupts from the PCI card and lower the
  465. * interrupt line
  466. */
  467. outw(0x8000, base+0x04);
  468. ClearInterrupt(base);
  469. inw(base); /* get the dummy word out */
  470. header = inw(base);
  471. channel = (header & 0x7800) >> card->shift_count;
  472. byte_count = header & 0xff;
  473. if (channel + 1 > card->port_count) {
  474. pr_warn("%s(0x%lx): %d(channel) > port_count\n",
  475. __func__, base, channel + 1);
  476. outw(0x0000, base+0x04); /* enable interrupts */
  477. spin_unlock(&card->card_lock);
  478. return IRQ_HANDLED;
  479. }
  480. port = card->ports + channel;
  481. if (!tty_port_initialized(&port->port)) {
  482. outw(0x0000, base+0x04); /* enable interrupts */
  483. spin_unlock(&card->card_lock);
  484. return IRQ_HANDLED;
  485. }
  486. tty = tty_port_tty_get(&port->port);
  487. if (tty == NULL) {
  488. word_count = byte_count >> 1;
  489. while (byte_count > 1) {
  490. inw(base);
  491. byte_count -= 2;
  492. }
  493. if (byte_count & 0x01)
  494. inw(base);
  495. outw(0x0000, base+0x04); /* enable interrupts */
  496. spin_unlock(&card->card_lock);
  497. return IRQ_HANDLED;
  498. }
  499. if (header & 0x8000) { /* Status Packet */
  500. header = inw(base);
  501. switch (header & 0xff) {
  502. case 0: /* Change in EIA signals */
  503. if (tty_port_check_carrier(&port->port)) {
  504. if (port->status & ISI_DCD) {
  505. if (!(header & ISI_DCD)) {
  506. /* Carrier has been lost */
  507. pr_debug("%s: DCD->low.\n",
  508. __func__);
  509. port->status &= ~ISI_DCD;
  510. tty_hangup(tty);
  511. }
  512. } else if (header & ISI_DCD) {
  513. /* Carrier has been detected */
  514. pr_debug("%s: DCD->high.\n",
  515. __func__);
  516. port->status |= ISI_DCD;
  517. wake_up_interruptible(&port->port.open_wait);
  518. }
  519. } else {
  520. if (header & ISI_DCD)
  521. port->status |= ISI_DCD;
  522. else
  523. port->status &= ~ISI_DCD;
  524. }
  525. if (tty_port_cts_enabled(&port->port)) {
  526. if (tty->hw_stopped) {
  527. if (header & ISI_CTS) {
  528. tty->hw_stopped = 0;
  529. /* start tx ing */
  530. port->status |= (ISI_TXOK
  531. | ISI_CTS);
  532. tty_wakeup(tty);
  533. }
  534. } else if (!(header & ISI_CTS)) {
  535. tty->hw_stopped = 1;
  536. /* stop tx ing */
  537. port->status &= ~(ISI_TXOK | ISI_CTS);
  538. }
  539. } else {
  540. if (header & ISI_CTS)
  541. port->status |= ISI_CTS;
  542. else
  543. port->status &= ~ISI_CTS;
  544. }
  545. if (header & ISI_DSR)
  546. port->status |= ISI_DSR;
  547. else
  548. port->status &= ~ISI_DSR;
  549. if (header & ISI_RI)
  550. port->status |= ISI_RI;
  551. else
  552. port->status &= ~ISI_RI;
  553. break;
  554. case 1: /* Received Break !!! */
  555. tty_insert_flip_char(&port->port, 0, TTY_BREAK);
  556. if (port->port.flags & ASYNC_SAK)
  557. do_SAK(tty);
  558. tty_flip_buffer_push(&port->port);
  559. break;
  560. case 2: /* Statistics */
  561. pr_debug("%s: stats!!!\n", __func__);
  562. break;
  563. default:
  564. pr_debug("%s: Unknown code in status packet.\n",
  565. __func__);
  566. break;
  567. }
  568. } else { /* Data Packet */
  569. count = tty_prepare_flip_string(&port->port, &rp,
  570. byte_count & ~1);
  571. pr_debug("%s: Can rx %d of %d bytes.\n",
  572. __func__, count, byte_count);
  573. word_count = count >> 1;
  574. insw(base, rp, word_count);
  575. byte_count -= (word_count << 1);
  576. if (count & 0x0001) {
  577. tty_insert_flip_char(&port->port, inw(base) & 0xff,
  578. TTY_NORMAL);
  579. byte_count -= 2;
  580. }
  581. if (byte_count > 0) {
  582. pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
  583. __func__, base, channel + 1);
  584. /* drain out unread xtra data */
  585. while (byte_count > 0) {
  586. inw(base);
  587. byte_count -= 2;
  588. }
  589. }
  590. tty_flip_buffer_push(&port->port);
  591. }
  592. outw(0x0000, base+0x04); /* enable interrupts */
  593. spin_unlock(&card->card_lock);
  594. tty_kref_put(tty);
  595. return IRQ_HANDLED;
  596. }
  597. static void isicom_config_port(struct tty_struct *tty)
  598. {
  599. struct isi_port *port = tty->driver_data;
  600. struct isi_board *card = port->card;
  601. unsigned long baud;
  602. unsigned long base = card->base;
  603. u16 channel_setup, channel = port->channel,
  604. shift_count = card->shift_count;
  605. unsigned char flow_ctrl;
  606. /* FIXME: Switch to new tty baud API */
  607. baud = C_BAUD(tty);
  608. if (baud & CBAUDEX) {
  609. baud &= ~CBAUDEX;
  610. /* if CBAUDEX bit is on and the baud is set to either 50 or 75
  611. * then the card is programmed for 57.6Kbps or 115Kbps
  612. * respectively.
  613. */
  614. /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
  615. if (baud < 1 || baud > 4)
  616. tty->termios.c_cflag &= ~CBAUDEX;
  617. else
  618. baud += 15;
  619. }
  620. if (baud == 15) {
  621. /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
  622. * by the set_serial_info ioctl ... this is done by
  623. * the 'setserial' utility.
  624. */
  625. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  626. baud++; /* 57.6 Kbps */
  627. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  628. baud += 2; /* 115 Kbps */
  629. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  630. baud += 3; /* 230 kbps*/
  631. if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  632. baud += 4; /* 460 kbps*/
  633. }
  634. if (linuxb_to_isib[baud] == -1) {
  635. /* hang up */
  636. drop_dtr(port);
  637. return;
  638. } else
  639. raise_dtr(port);
  640. if (WaitTillCardIsFree(base) == 0) {
  641. outw(0x8000 | (channel << shift_count) | 0x03, base);
  642. outw(linuxb_to_isib[baud] << 8 | 0x03, base);
  643. channel_setup = 0;
  644. switch (C_CSIZE(tty)) {
  645. case CS5:
  646. channel_setup |= ISICOM_CS5;
  647. break;
  648. case CS6:
  649. channel_setup |= ISICOM_CS6;
  650. break;
  651. case CS7:
  652. channel_setup |= ISICOM_CS7;
  653. break;
  654. case CS8:
  655. channel_setup |= ISICOM_CS8;
  656. break;
  657. }
  658. if (C_CSTOPB(tty))
  659. channel_setup |= ISICOM_2SB;
  660. if (C_PARENB(tty)) {
  661. channel_setup |= ISICOM_EVPAR;
  662. if (C_PARODD(tty))
  663. channel_setup |= ISICOM_ODPAR;
  664. }
  665. outw(channel_setup, base);
  666. InterruptTheCard(base);
  667. }
  668. tty_port_set_check_carrier(&port->port, !C_CLOCAL(tty));
  669. /* flow control settings ...*/
  670. flow_ctrl = 0;
  671. tty_port_set_cts_flow(&port->port, C_CRTSCTS(tty));
  672. if (C_CRTSCTS(tty))
  673. flow_ctrl |= ISICOM_CTSRTS;
  674. if (I_IXON(tty))
  675. flow_ctrl |= ISICOM_RESPOND_XONXOFF;
  676. if (I_IXOFF(tty))
  677. flow_ctrl |= ISICOM_INITIATE_XONXOFF;
  678. if (WaitTillCardIsFree(base) == 0) {
  679. outw(0x8000 | (channel << shift_count) | 0x04, base);
  680. outw(flow_ctrl << 8 | 0x05, base);
  681. outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
  682. InterruptTheCard(base);
  683. }
  684. /* rx enabled -> enable port for rx on the card */
  685. if (C_CREAD(tty)) {
  686. card->port_status |= (1 << channel);
  687. outw(card->port_status, base + 0x02);
  688. }
  689. }
  690. /* open et all */
  691. static inline void isicom_setup_board(struct isi_board *bp)
  692. {
  693. int channel;
  694. struct isi_port *port;
  695. bp->count++;
  696. if (!(bp->status & BOARD_INIT)) {
  697. port = bp->ports;
  698. for (channel = 0; channel < bp->port_count; channel++, port++)
  699. drop_dtr_rts(port);
  700. }
  701. bp->status |= BOARD_ACTIVE | BOARD_INIT;
  702. }
  703. /* Activate and thus setup board are protected from races against shutdown
  704. by the tty_port mutex */
  705. static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
  706. {
  707. struct isi_port *port = container_of(tport, struct isi_port, port);
  708. struct isi_board *card = port->card;
  709. unsigned long flags;
  710. if (tty_port_alloc_xmit_buf(tport) < 0)
  711. return -ENOMEM;
  712. spin_lock_irqsave(&card->card_lock, flags);
  713. isicom_setup_board(card);
  714. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  715. /* discard any residual data */
  716. if (WaitTillCardIsFree(card->base) == 0) {
  717. outw(0x8000 | (port->channel << card->shift_count) | 0x02,
  718. card->base);
  719. outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
  720. InterruptTheCard(card->base);
  721. }
  722. isicom_config_port(tty);
  723. spin_unlock_irqrestore(&card->card_lock, flags);
  724. return 0;
  725. }
  726. static int isicom_carrier_raised(struct tty_port *port)
  727. {
  728. struct isi_port *ip = container_of(port, struct isi_port, port);
  729. return (ip->status & ISI_DCD)?1 : 0;
  730. }
  731. static struct tty_port *isicom_find_port(struct tty_struct *tty)
  732. {
  733. struct isi_port *port;
  734. struct isi_board *card;
  735. unsigned int board;
  736. int line = tty->index;
  737. board = BOARD(line);
  738. card = &isi_card[board];
  739. if (!(card->status & FIRMWARE_LOADED))
  740. return NULL;
  741. /* open on a port greater than the port count for the card !!! */
  742. if (line > ((board * 16) + card->port_count - 1))
  743. return NULL;
  744. port = &isi_ports[line];
  745. if (isicom_paranoia_check(port, tty->name, "isicom_open"))
  746. return NULL;
  747. return &port->port;
  748. }
  749. static int isicom_open(struct tty_struct *tty, struct file *filp)
  750. {
  751. struct isi_port *port;
  752. struct tty_port *tport;
  753. tport = isicom_find_port(tty);
  754. if (tport == NULL)
  755. return -ENODEV;
  756. port = container_of(tport, struct isi_port, port);
  757. tty->driver_data = port;
  758. return tty_port_open(tport, tty, filp);
  759. }
  760. /* close et all */
  761. /* card->lock HAS to be held */
  762. static void isicom_shutdown_port(struct isi_port *port)
  763. {
  764. struct isi_board *card = port->card;
  765. if (--card->count < 0) {
  766. pr_debug("%s: bad board(0x%lx) count %d.\n",
  767. __func__, card->base, card->count);
  768. card->count = 0;
  769. }
  770. /* last port was closed, shutdown that board too */
  771. if (!card->count)
  772. card->status &= BOARD_ACTIVE;
  773. }
  774. static void isicom_flush_buffer(struct tty_struct *tty)
  775. {
  776. struct isi_port *port = tty->driver_data;
  777. struct isi_board *card = port->card;
  778. unsigned long flags;
  779. if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
  780. return;
  781. spin_lock_irqsave(&card->card_lock, flags);
  782. port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
  783. spin_unlock_irqrestore(&card->card_lock, flags);
  784. tty_wakeup(tty);
  785. }
  786. static void isicom_shutdown(struct tty_port *port)
  787. {
  788. struct isi_port *ip = container_of(port, struct isi_port, port);
  789. struct isi_board *card = ip->card;
  790. unsigned long flags;
  791. /* indicate to the card that no more data can be received
  792. on this port */
  793. spin_lock_irqsave(&card->card_lock, flags);
  794. card->port_status &= ~(1 << ip->channel);
  795. outw(card->port_status, card->base + 0x02);
  796. isicom_shutdown_port(ip);
  797. spin_unlock_irqrestore(&card->card_lock, flags);
  798. tty_port_free_xmit_buf(port);
  799. }
  800. static void isicom_close(struct tty_struct *tty, struct file *filp)
  801. {
  802. struct isi_port *ip = tty->driver_data;
  803. struct tty_port *port;
  804. if (ip == NULL)
  805. return;
  806. port = &ip->port;
  807. if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
  808. return;
  809. tty_port_close(port, tty, filp);
  810. }
  811. /* write et all */
  812. static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
  813. int count)
  814. {
  815. struct isi_port *port = tty->driver_data;
  816. struct isi_board *card = port->card;
  817. unsigned long flags;
  818. int cnt, total = 0;
  819. if (isicom_paranoia_check(port, tty->name, "isicom_write"))
  820. return 0;
  821. spin_lock_irqsave(&card->card_lock, flags);
  822. while (1) {
  823. cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
  824. - 1, SERIAL_XMIT_SIZE - port->xmit_head));
  825. if (cnt <= 0)
  826. break;
  827. memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
  828. port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
  829. - 1);
  830. port->xmit_cnt += cnt;
  831. buf += cnt;
  832. count -= cnt;
  833. total += cnt;
  834. }
  835. if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
  836. port->status |= ISI_TXOK;
  837. spin_unlock_irqrestore(&card->card_lock, flags);
  838. return total;
  839. }
  840. /* put_char et all */
  841. static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
  842. {
  843. struct isi_port *port = tty->driver_data;
  844. struct isi_board *card = port->card;
  845. unsigned long flags;
  846. if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
  847. return 0;
  848. spin_lock_irqsave(&card->card_lock, flags);
  849. if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
  850. spin_unlock_irqrestore(&card->card_lock, flags);
  851. return 0;
  852. }
  853. port->port.xmit_buf[port->xmit_head++] = ch;
  854. port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
  855. port->xmit_cnt++;
  856. spin_unlock_irqrestore(&card->card_lock, flags);
  857. return 1;
  858. }
  859. /* flush_chars et all */
  860. static void isicom_flush_chars(struct tty_struct *tty)
  861. {
  862. struct isi_port *port = tty->driver_data;
  863. if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
  864. return;
  865. if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  866. !port->port.xmit_buf)
  867. return;
  868. /* this tells the transmitter to consider this port for
  869. data output to the card ... that's the best we can do. */
  870. port->status |= ISI_TXOK;
  871. }
  872. /* write_room et all */
  873. static int isicom_write_room(struct tty_struct *tty)
  874. {
  875. struct isi_port *port = tty->driver_data;
  876. int free;
  877. if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
  878. return 0;
  879. free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
  880. if (free < 0)
  881. free = 0;
  882. return free;
  883. }
  884. /* chars_in_buffer et all */
  885. static int isicom_chars_in_buffer(struct tty_struct *tty)
  886. {
  887. struct isi_port *port = tty->driver_data;
  888. if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
  889. return 0;
  890. return port->xmit_cnt;
  891. }
  892. /* ioctl et all */
  893. static int isicom_send_break(struct tty_struct *tty, int length)
  894. {
  895. struct isi_port *port = tty->driver_data;
  896. struct isi_board *card = port->card;
  897. unsigned long base = card->base;
  898. if (length == -1)
  899. return -EOPNOTSUPP;
  900. if (!lock_card(card))
  901. return -EINVAL;
  902. outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
  903. outw((length & 0xff) << 8 | 0x00, base);
  904. outw((length & 0xff00u), base);
  905. InterruptTheCard(base);
  906. unlock_card(card);
  907. return 0;
  908. }
  909. static int isicom_tiocmget(struct tty_struct *tty)
  910. {
  911. struct isi_port *port = tty->driver_data;
  912. /* just send the port status */
  913. u16 status = port->status;
  914. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  915. return -ENODEV;
  916. return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
  917. ((status & ISI_DTR) ? TIOCM_DTR : 0) |
  918. ((status & ISI_DCD) ? TIOCM_CAR : 0) |
  919. ((status & ISI_DSR) ? TIOCM_DSR : 0) |
  920. ((status & ISI_CTS) ? TIOCM_CTS : 0) |
  921. ((status & ISI_RI ) ? TIOCM_RI : 0);
  922. }
  923. static int isicom_tiocmset(struct tty_struct *tty,
  924. unsigned int set, unsigned int clear)
  925. {
  926. struct isi_port *port = tty->driver_data;
  927. unsigned long flags;
  928. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  929. return -ENODEV;
  930. spin_lock_irqsave(&port->card->card_lock, flags);
  931. if (set & TIOCM_RTS)
  932. raise_rts(port);
  933. if (set & TIOCM_DTR)
  934. raise_dtr(port);
  935. if (clear & TIOCM_RTS)
  936. drop_rts(port);
  937. if (clear & TIOCM_DTR)
  938. drop_dtr(port);
  939. spin_unlock_irqrestore(&port->card->card_lock, flags);
  940. return 0;
  941. }
  942. static int isicom_set_serial_info(struct tty_struct *tty,
  943. struct serial_struct __user *info)
  944. {
  945. struct isi_port *port = tty->driver_data;
  946. struct serial_struct newinfo;
  947. int reconfig_port;
  948. if (copy_from_user(&newinfo, info, sizeof(newinfo)))
  949. return -EFAULT;
  950. mutex_lock(&port->port.mutex);
  951. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  952. (newinfo.flags & ASYNC_SPD_MASK));
  953. if (!capable(CAP_SYS_ADMIN)) {
  954. if ((newinfo.close_delay != port->port.close_delay) ||
  955. (newinfo.closing_wait != port->port.closing_wait) ||
  956. ((newinfo.flags & ~ASYNC_USR_MASK) !=
  957. (port->port.flags & ~ASYNC_USR_MASK))) {
  958. mutex_unlock(&port->port.mutex);
  959. return -EPERM;
  960. }
  961. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  962. (newinfo.flags & ASYNC_USR_MASK));
  963. } else {
  964. port->port.close_delay = newinfo.close_delay;
  965. port->port.closing_wait = newinfo.closing_wait;
  966. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  967. (newinfo.flags & ASYNC_FLAGS));
  968. }
  969. if (reconfig_port) {
  970. unsigned long flags;
  971. spin_lock_irqsave(&port->card->card_lock, flags);
  972. isicom_config_port(tty);
  973. spin_unlock_irqrestore(&port->card->card_lock, flags);
  974. }
  975. mutex_unlock(&port->port.mutex);
  976. return 0;
  977. }
  978. static int isicom_get_serial_info(struct isi_port *port,
  979. struct serial_struct __user *info)
  980. {
  981. struct serial_struct out_info;
  982. mutex_lock(&port->port.mutex);
  983. memset(&out_info, 0, sizeof(out_info));
  984. /* out_info.type = ? */
  985. out_info.line = port - isi_ports;
  986. out_info.port = port->card->base;
  987. out_info.irq = port->card->irq;
  988. out_info.flags = port->port.flags;
  989. /* out_info.baud_base = ? */
  990. out_info.close_delay = port->port.close_delay;
  991. out_info.closing_wait = port->port.closing_wait;
  992. mutex_unlock(&port->port.mutex);
  993. if (copy_to_user(info, &out_info, sizeof(out_info)))
  994. return -EFAULT;
  995. return 0;
  996. }
  997. static int isicom_ioctl(struct tty_struct *tty,
  998. unsigned int cmd, unsigned long arg)
  999. {
  1000. struct isi_port *port = tty->driver_data;
  1001. void __user *argp = (void __user *)arg;
  1002. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  1003. return -ENODEV;
  1004. switch (cmd) {
  1005. case TIOCGSERIAL:
  1006. return isicom_get_serial_info(port, argp);
  1007. case TIOCSSERIAL:
  1008. return isicom_set_serial_info(tty, argp);
  1009. default:
  1010. return -ENOIOCTLCMD;
  1011. }
  1012. return 0;
  1013. }
  1014. /* set_termios et all */
  1015. static void isicom_set_termios(struct tty_struct *tty,
  1016. struct ktermios *old_termios)
  1017. {
  1018. struct isi_port *port = tty->driver_data;
  1019. unsigned long flags;
  1020. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  1021. return;
  1022. if (tty->termios.c_cflag == old_termios->c_cflag &&
  1023. tty->termios.c_iflag == old_termios->c_iflag)
  1024. return;
  1025. spin_lock_irqsave(&port->card->card_lock, flags);
  1026. isicom_config_port(tty);
  1027. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1028. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  1029. tty->hw_stopped = 0;
  1030. isicom_start(tty);
  1031. }
  1032. }
  1033. /* throttle et all */
  1034. static void isicom_throttle(struct tty_struct *tty)
  1035. {
  1036. struct isi_port *port = tty->driver_data;
  1037. struct isi_board *card = port->card;
  1038. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1039. return;
  1040. /* tell the card that this port cannot handle any more data for now */
  1041. card->port_status &= ~(1 << port->channel);
  1042. outw(card->port_status, card->base + 0x02);
  1043. }
  1044. /* unthrottle et all */
  1045. static void isicom_unthrottle(struct tty_struct *tty)
  1046. {
  1047. struct isi_port *port = tty->driver_data;
  1048. struct isi_board *card = port->card;
  1049. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1050. return;
  1051. /* tell the card that this port is ready to accept more data */
  1052. card->port_status |= (1 << port->channel);
  1053. outw(card->port_status, card->base + 0x02);
  1054. }
  1055. /* stop et all */
  1056. static void isicom_stop(struct tty_struct *tty)
  1057. {
  1058. struct isi_port *port = tty->driver_data;
  1059. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1060. return;
  1061. /* this tells the transmitter not to consider this port for
  1062. data output to the card. */
  1063. port->status &= ~ISI_TXOK;
  1064. }
  1065. /* start et all */
  1066. static void isicom_start(struct tty_struct *tty)
  1067. {
  1068. struct isi_port *port = tty->driver_data;
  1069. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1070. return;
  1071. /* this tells the transmitter to consider this port for
  1072. data output to the card. */
  1073. port->status |= ISI_TXOK;
  1074. }
  1075. static void isicom_hangup(struct tty_struct *tty)
  1076. {
  1077. struct isi_port *port = tty->driver_data;
  1078. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1079. return;
  1080. tty_port_hangup(&port->port);
  1081. }
  1082. /*
  1083. * Driver init and deinit functions
  1084. */
  1085. static const struct tty_operations isicom_ops = {
  1086. .open = isicom_open,
  1087. .close = isicom_close,
  1088. .write = isicom_write,
  1089. .put_char = isicom_put_char,
  1090. .flush_chars = isicom_flush_chars,
  1091. .write_room = isicom_write_room,
  1092. .chars_in_buffer = isicom_chars_in_buffer,
  1093. .ioctl = isicom_ioctl,
  1094. .set_termios = isicom_set_termios,
  1095. .throttle = isicom_throttle,
  1096. .unthrottle = isicom_unthrottle,
  1097. .stop = isicom_stop,
  1098. .start = isicom_start,
  1099. .hangup = isicom_hangup,
  1100. .flush_buffer = isicom_flush_buffer,
  1101. .tiocmget = isicom_tiocmget,
  1102. .tiocmset = isicom_tiocmset,
  1103. .break_ctl = isicom_send_break,
  1104. };
  1105. static const struct tty_port_operations isicom_port_ops = {
  1106. .carrier_raised = isicom_carrier_raised,
  1107. .dtr_rts = isicom_dtr_rts,
  1108. .activate = isicom_activate,
  1109. .shutdown = isicom_shutdown,
  1110. };
  1111. static int reset_card(struct pci_dev *pdev,
  1112. const unsigned int card, unsigned int *signature)
  1113. {
  1114. struct isi_board *board = pci_get_drvdata(pdev);
  1115. unsigned long base = board->base;
  1116. unsigned int sig, portcount = 0;
  1117. int retval = 0;
  1118. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1119. base);
  1120. inw(base + 0x8);
  1121. msleep(10);
  1122. outw(0, base + 0x8); /* Reset */
  1123. msleep(1000);
  1124. sig = inw(base + 0x4) & 0xff;
  1125. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1126. sig != 0xee) {
  1127. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1128. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1129. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1130. retval = -EIO;
  1131. goto end;
  1132. }
  1133. msleep(10);
  1134. portcount = inw(base + 0x2);
  1135. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1136. portcount != 8 && portcount != 16)) {
  1137. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1138. card + 1);
  1139. retval = -EIO;
  1140. goto end;
  1141. }
  1142. switch (sig) {
  1143. case 0xa5:
  1144. case 0xbb:
  1145. case 0xdd:
  1146. board->port_count = (portcount == 4) ? 4 : 8;
  1147. board->shift_count = 12;
  1148. break;
  1149. case 0xcc:
  1150. case 0xee:
  1151. board->port_count = 16;
  1152. board->shift_count = 11;
  1153. break;
  1154. }
  1155. dev_info(&pdev->dev, "-Done\n");
  1156. *signature = sig;
  1157. end:
  1158. return retval;
  1159. }
  1160. static int load_firmware(struct pci_dev *pdev,
  1161. const unsigned int index, const unsigned int signature)
  1162. {
  1163. struct isi_board *board = pci_get_drvdata(pdev);
  1164. const struct firmware *fw;
  1165. unsigned long base = board->base;
  1166. unsigned int a;
  1167. u16 word_count, status;
  1168. int retval = -EIO;
  1169. char *name;
  1170. u8 *data;
  1171. struct stframe {
  1172. u16 addr;
  1173. u16 count;
  1174. u8 data[0];
  1175. } *frame;
  1176. switch (signature) {
  1177. case 0xa5:
  1178. name = "isi608.bin";
  1179. break;
  1180. case 0xbb:
  1181. name = "isi608em.bin";
  1182. break;
  1183. case 0xcc:
  1184. name = "isi616em.bin";
  1185. break;
  1186. case 0xdd:
  1187. name = "isi4608.bin";
  1188. break;
  1189. case 0xee:
  1190. name = "isi4616.bin";
  1191. break;
  1192. default:
  1193. dev_err(&pdev->dev, "Unknown signature.\n");
  1194. goto end;
  1195. }
  1196. retval = request_firmware(&fw, name, &pdev->dev);
  1197. if (retval)
  1198. goto end;
  1199. retval = -EIO;
  1200. for (frame = (struct stframe *)fw->data;
  1201. frame < (struct stframe *)(fw->data + fw->size);
  1202. frame = (struct stframe *)((u8 *)(frame + 1) +
  1203. frame->count)) {
  1204. if (WaitTillCardIsFree(base))
  1205. goto errrelfw;
  1206. outw(0xf0, base); /* start upload sequence */
  1207. outw(0x00, base);
  1208. outw(frame->addr, base); /* lsb of address */
  1209. word_count = frame->count / 2 + frame->count % 2;
  1210. outw(word_count, base);
  1211. InterruptTheCard(base);
  1212. udelay(100); /* 0x2f */
  1213. if (WaitTillCardIsFree(base))
  1214. goto errrelfw;
  1215. status = inw(base + 0x4);
  1216. if (status != 0) {
  1217. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1218. "Address:0x%x\n"
  1219. "Count:0x%x\n"
  1220. "Status:0x%x\n",
  1221. index + 1, frame->addr, frame->count, status);
  1222. goto errrelfw;
  1223. }
  1224. outsw(base, frame->data, word_count);
  1225. InterruptTheCard(base);
  1226. udelay(50); /* 0x0f */
  1227. if (WaitTillCardIsFree(base))
  1228. goto errrelfw;
  1229. status = inw(base + 0x4);
  1230. if (status != 0) {
  1231. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1232. "Status:0x%x\n", index + 1, status);
  1233. goto errrelfw;
  1234. }
  1235. }
  1236. /* XXX: should we test it by reading it back and comparing with original like
  1237. * in load firmware package? */
  1238. for (frame = (struct stframe *)fw->data;
  1239. frame < (struct stframe *)(fw->data + fw->size);
  1240. frame = (struct stframe *)((u8 *)(frame + 1) +
  1241. frame->count)) {
  1242. if (WaitTillCardIsFree(base))
  1243. goto errrelfw;
  1244. outw(0xf1, base); /* start download sequence */
  1245. outw(0x00, base);
  1246. outw(frame->addr, base); /* lsb of address */
  1247. word_count = (frame->count >> 1) + frame->count % 2;
  1248. outw(word_count + 1, base);
  1249. InterruptTheCard(base);
  1250. udelay(50); /* 0xf */
  1251. if (WaitTillCardIsFree(base))
  1252. goto errrelfw;
  1253. status = inw(base + 0x4);
  1254. if (status != 0) {
  1255. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1256. "Address:0x%x\n"
  1257. "Count:0x%x\n"
  1258. "Status: 0x%x\n",
  1259. index + 1, frame->addr, frame->count, status);
  1260. goto errrelfw;
  1261. }
  1262. data = kmalloc(word_count * 2, GFP_KERNEL);
  1263. if (data == NULL) {
  1264. dev_err(&pdev->dev, "Card%d, firmware upload "
  1265. "failed, not enough memory\n", index + 1);
  1266. goto errrelfw;
  1267. }
  1268. inw(base);
  1269. insw(base, data, word_count);
  1270. InterruptTheCard(base);
  1271. for (a = 0; a < frame->count; a++)
  1272. if (data[a] != frame->data[a]) {
  1273. kfree(data);
  1274. dev_err(&pdev->dev, "Card%d, firmware upload "
  1275. "failed\n", index + 1);
  1276. goto errrelfw;
  1277. }
  1278. kfree(data);
  1279. udelay(50); /* 0xf */
  1280. if (WaitTillCardIsFree(base))
  1281. goto errrelfw;
  1282. status = inw(base + 0x4);
  1283. if (status != 0) {
  1284. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1285. "Card Status:0x%x\n", index + 1, status);
  1286. goto errrelfw;
  1287. }
  1288. }
  1289. /* xfer ctrl */
  1290. if (WaitTillCardIsFree(base))
  1291. goto errrelfw;
  1292. outw(0xf2, base);
  1293. outw(0x800, base);
  1294. outw(0x0, base);
  1295. outw(0x0, base);
  1296. InterruptTheCard(base);
  1297. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1298. board->status |= FIRMWARE_LOADED;
  1299. retval = 0;
  1300. errrelfw:
  1301. release_firmware(fw);
  1302. end:
  1303. return retval;
  1304. }
  1305. /*
  1306. * Insmod can set static symbols so keep these static
  1307. */
  1308. static unsigned int card_count;
  1309. static int isicom_probe(struct pci_dev *pdev,
  1310. const struct pci_device_id *ent)
  1311. {
  1312. unsigned int uninitialized_var(signature), index;
  1313. int retval = -EPERM;
  1314. struct isi_board *board = NULL;
  1315. if (card_count >= BOARD_COUNT)
  1316. goto err;
  1317. retval = pci_enable_device(pdev);
  1318. if (retval) {
  1319. dev_err(&pdev->dev, "failed to enable\n");
  1320. goto err;
  1321. }
  1322. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1323. /* allot the first empty slot in the array */
  1324. for (index = 0; index < BOARD_COUNT; index++) {
  1325. if (isi_card[index].base == 0) {
  1326. board = &isi_card[index];
  1327. break;
  1328. }
  1329. }
  1330. if (index == BOARD_COUNT) {
  1331. retval = -ENODEV;
  1332. goto err_disable;
  1333. }
  1334. board->index = index;
  1335. board->base = pci_resource_start(pdev, 3);
  1336. board->irq = pdev->irq;
  1337. card_count++;
  1338. pci_set_drvdata(pdev, board);
  1339. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1340. if (retval) {
  1341. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1342. "will be disabled.\n", board->base, board->base + 15,
  1343. index + 1);
  1344. retval = -EBUSY;
  1345. goto errdec;
  1346. }
  1347. retval = request_irq(board->irq, isicom_interrupt,
  1348. IRQF_SHARED, ISICOM_NAME, board);
  1349. if (retval < 0) {
  1350. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1351. "Card%d will be disabled.\n", board->irq, index + 1);
  1352. goto errunrr;
  1353. }
  1354. retval = reset_card(pdev, index, &signature);
  1355. if (retval < 0)
  1356. goto errunri;
  1357. retval = load_firmware(pdev, index, signature);
  1358. if (retval < 0)
  1359. goto errunri;
  1360. for (index = 0; index < board->port_count; index++) {
  1361. struct tty_port *tport = &board->ports[index].port;
  1362. tty_port_init(tport);
  1363. tport->ops = &isicom_port_ops;
  1364. tport->close_delay = 50 * HZ/100;
  1365. tport->closing_wait = 3000 * HZ/100;
  1366. tty_port_register_device(tport, isicom_normal,
  1367. board->index * 16 + index, &pdev->dev);
  1368. }
  1369. return 0;
  1370. errunri:
  1371. free_irq(board->irq, board);
  1372. errunrr:
  1373. pci_release_region(pdev, 3);
  1374. errdec:
  1375. board->base = 0;
  1376. card_count--;
  1377. err_disable:
  1378. pci_disable_device(pdev);
  1379. err:
  1380. return retval;
  1381. }
  1382. static void isicom_remove(struct pci_dev *pdev)
  1383. {
  1384. struct isi_board *board = pci_get_drvdata(pdev);
  1385. unsigned int i;
  1386. for (i = 0; i < board->port_count; i++) {
  1387. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1388. tty_port_destroy(&board->ports[i].port);
  1389. }
  1390. free_irq(board->irq, board);
  1391. pci_release_region(pdev, 3);
  1392. board->base = 0;
  1393. card_count--;
  1394. pci_disable_device(pdev);
  1395. }
  1396. static int __init isicom_init(void)
  1397. {
  1398. int retval, idx, channel;
  1399. struct isi_port *port;
  1400. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1401. port = &isi_ports[idx * 16];
  1402. isi_card[idx].ports = port;
  1403. spin_lock_init(&isi_card[idx].card_lock);
  1404. for (channel = 0; channel < 16; channel++, port++) {
  1405. port->magic = ISICOM_MAGIC;
  1406. port->card = &isi_card[idx];
  1407. port->channel = channel;
  1408. port->status = 0;
  1409. /* . . . */
  1410. }
  1411. isi_card[idx].base = 0;
  1412. isi_card[idx].irq = 0;
  1413. }
  1414. /* tty driver structure initialization */
  1415. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1416. if (!isicom_normal) {
  1417. retval = -ENOMEM;
  1418. goto error;
  1419. }
  1420. isicom_normal->name = "ttyM";
  1421. isicom_normal->major = ISICOM_NMAJOR;
  1422. isicom_normal->minor_start = 0;
  1423. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1424. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1425. isicom_normal->init_termios = tty_std_termios;
  1426. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1427. CLOCAL;
  1428. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1429. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1430. tty_set_operations(isicom_normal, &isicom_ops);
  1431. retval = tty_register_driver(isicom_normal);
  1432. if (retval) {
  1433. pr_debug("Couldn't register the dialin driver\n");
  1434. goto err_puttty;
  1435. }
  1436. retval = pci_register_driver(&isicom_driver);
  1437. if (retval < 0) {
  1438. pr_err("Unable to register pci driver.\n");
  1439. goto err_unrtty;
  1440. }
  1441. mod_timer(&tx, jiffies + 1);
  1442. return 0;
  1443. err_unrtty:
  1444. tty_unregister_driver(isicom_normal);
  1445. err_puttty:
  1446. put_tty_driver(isicom_normal);
  1447. error:
  1448. return retval;
  1449. }
  1450. static void __exit isicom_exit(void)
  1451. {
  1452. del_timer_sync(&tx);
  1453. pci_unregister_driver(&isicom_driver);
  1454. tty_unregister_driver(isicom_normal);
  1455. put_tty_driver(isicom_normal);
  1456. }
  1457. module_init(isicom_init);
  1458. module_exit(isicom_exit);
  1459. MODULE_AUTHOR("MultiTech");
  1460. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1461. MODULE_LICENSE("GPL");
  1462. MODULE_FIRMWARE("isi608.bin");
  1463. MODULE_FIRMWARE("isi608em.bin");
  1464. MODULE_FIRMWARE("isi616em.bin");
  1465. MODULE_FIRMWARE("isi4608.bin");
  1466. MODULE_FIRMWARE("isi4616.bin");