isicom.c 41 KB

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