isicom.c 41 KB

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