isicom.c 40 KB

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