isicom.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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 *ss)
  940. {
  941. struct isi_port *port = tty->driver_data;
  942. int reconfig_port;
  943. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  944. return -ENODEV;
  945. mutex_lock(&port->port.mutex);
  946. reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
  947. (ss->flags & ASYNC_SPD_MASK));
  948. if (!capable(CAP_SYS_ADMIN)) {
  949. if ((ss->close_delay != port->port.close_delay) ||
  950. (ss->closing_wait != port->port.closing_wait) ||
  951. ((ss->flags & ~ASYNC_USR_MASK) !=
  952. (port->port.flags & ~ASYNC_USR_MASK))) {
  953. mutex_unlock(&port->port.mutex);
  954. return -EPERM;
  955. }
  956. port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
  957. (ss->flags & ASYNC_USR_MASK));
  958. } else {
  959. port->port.close_delay = ss->close_delay;
  960. port->port.closing_wait = ss->closing_wait;
  961. port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
  962. (ss->flags & ASYNC_FLAGS));
  963. }
  964. if (reconfig_port) {
  965. unsigned long flags;
  966. spin_lock_irqsave(&port->card->card_lock, flags);
  967. isicom_config_port(tty);
  968. spin_unlock_irqrestore(&port->card->card_lock, flags);
  969. }
  970. mutex_unlock(&port->port.mutex);
  971. return 0;
  972. }
  973. static int isicom_get_serial_info(struct tty_struct *tty,
  974. struct serial_struct *ss)
  975. {
  976. struct isi_port *port = tty->driver_data;
  977. if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
  978. return -ENODEV;
  979. mutex_lock(&port->port.mutex);
  980. /* ss->type = ? */
  981. ss->line = port - isi_ports;
  982. ss->port = port->card->base;
  983. ss->irq = port->card->irq;
  984. ss->flags = port->port.flags;
  985. /* ss->baud_base = ? */
  986. ss->close_delay = port->port.close_delay;
  987. ss->closing_wait = port->port.closing_wait;
  988. mutex_unlock(&port->port.mutex);
  989. return 0;
  990. }
  991. /* set_termios et all */
  992. static void isicom_set_termios(struct tty_struct *tty,
  993. struct ktermios *old_termios)
  994. {
  995. struct isi_port *port = tty->driver_data;
  996. unsigned long flags;
  997. if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
  998. return;
  999. if (tty->termios.c_cflag == old_termios->c_cflag &&
  1000. tty->termios.c_iflag == old_termios->c_iflag)
  1001. return;
  1002. spin_lock_irqsave(&port->card->card_lock, flags);
  1003. isicom_config_port(tty);
  1004. spin_unlock_irqrestore(&port->card->card_lock, flags);
  1005. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  1006. tty->hw_stopped = 0;
  1007. isicom_start(tty);
  1008. }
  1009. }
  1010. /* throttle et all */
  1011. static void isicom_throttle(struct tty_struct *tty)
  1012. {
  1013. struct isi_port *port = tty->driver_data;
  1014. struct isi_board *card = port->card;
  1015. if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
  1016. return;
  1017. /* tell the card that this port cannot handle any more data for now */
  1018. card->port_status &= ~(1 << port->channel);
  1019. outw(card->port_status, card->base + 0x02);
  1020. }
  1021. /* unthrottle et all */
  1022. static void isicom_unthrottle(struct tty_struct *tty)
  1023. {
  1024. struct isi_port *port = tty->driver_data;
  1025. struct isi_board *card = port->card;
  1026. if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
  1027. return;
  1028. /* tell the card that this port is ready to accept more data */
  1029. card->port_status |= (1 << port->channel);
  1030. outw(card->port_status, card->base + 0x02);
  1031. }
  1032. /* stop et all */
  1033. static void isicom_stop(struct tty_struct *tty)
  1034. {
  1035. struct isi_port *port = tty->driver_data;
  1036. if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
  1037. return;
  1038. /* this tells the transmitter not to consider this port for
  1039. data output to the card. */
  1040. port->status &= ~ISI_TXOK;
  1041. }
  1042. /* start et all */
  1043. static void isicom_start(struct tty_struct *tty)
  1044. {
  1045. struct isi_port *port = tty->driver_data;
  1046. if (isicom_paranoia_check(port, tty->name, "isicom_start"))
  1047. return;
  1048. /* this tells the transmitter to consider this port for
  1049. data output to the card. */
  1050. port->status |= ISI_TXOK;
  1051. }
  1052. static void isicom_hangup(struct tty_struct *tty)
  1053. {
  1054. struct isi_port *port = tty->driver_data;
  1055. if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
  1056. return;
  1057. tty_port_hangup(&port->port);
  1058. }
  1059. /*
  1060. * Driver init and deinit functions
  1061. */
  1062. static const struct tty_operations isicom_ops = {
  1063. .open = isicom_open,
  1064. .close = isicom_close,
  1065. .write = isicom_write,
  1066. .put_char = isicom_put_char,
  1067. .flush_chars = isicom_flush_chars,
  1068. .write_room = isicom_write_room,
  1069. .chars_in_buffer = isicom_chars_in_buffer,
  1070. .set_termios = isicom_set_termios,
  1071. .throttle = isicom_throttle,
  1072. .unthrottle = isicom_unthrottle,
  1073. .stop = isicom_stop,
  1074. .start = isicom_start,
  1075. .hangup = isicom_hangup,
  1076. .flush_buffer = isicom_flush_buffer,
  1077. .tiocmget = isicom_tiocmget,
  1078. .tiocmset = isicom_tiocmset,
  1079. .break_ctl = isicom_send_break,
  1080. .get_serial = isicom_get_serial_info,
  1081. .set_serial = isicom_set_serial_info,
  1082. };
  1083. static const struct tty_port_operations isicom_port_ops = {
  1084. .carrier_raised = isicom_carrier_raised,
  1085. .dtr_rts = isicom_dtr_rts,
  1086. .activate = isicom_activate,
  1087. .shutdown = isicom_shutdown,
  1088. };
  1089. static int reset_card(struct pci_dev *pdev,
  1090. const unsigned int card, unsigned int *signature)
  1091. {
  1092. struct isi_board *board = pci_get_drvdata(pdev);
  1093. unsigned long base = board->base;
  1094. unsigned int sig, portcount = 0;
  1095. int retval = 0;
  1096. dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
  1097. base);
  1098. inw(base + 0x8);
  1099. msleep(10);
  1100. outw(0, base + 0x8); /* Reset */
  1101. msleep(1000);
  1102. sig = inw(base + 0x4) & 0xff;
  1103. if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
  1104. sig != 0xee) {
  1105. dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
  1106. "bad I/O Port Address 0x%lx).\n", card + 1, base);
  1107. dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
  1108. retval = -EIO;
  1109. goto end;
  1110. }
  1111. msleep(10);
  1112. portcount = inw(base + 0x2);
  1113. if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
  1114. portcount != 8 && portcount != 16)) {
  1115. dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
  1116. card + 1);
  1117. retval = -EIO;
  1118. goto end;
  1119. }
  1120. switch (sig) {
  1121. case 0xa5:
  1122. case 0xbb:
  1123. case 0xdd:
  1124. board->port_count = (portcount == 4) ? 4 : 8;
  1125. board->shift_count = 12;
  1126. break;
  1127. case 0xcc:
  1128. case 0xee:
  1129. board->port_count = 16;
  1130. board->shift_count = 11;
  1131. break;
  1132. }
  1133. dev_info(&pdev->dev, "-Done\n");
  1134. *signature = sig;
  1135. end:
  1136. return retval;
  1137. }
  1138. static int load_firmware(struct pci_dev *pdev,
  1139. const unsigned int index, const unsigned int signature)
  1140. {
  1141. struct isi_board *board = pci_get_drvdata(pdev);
  1142. const struct firmware *fw;
  1143. unsigned long base = board->base;
  1144. unsigned int a;
  1145. u16 word_count, status;
  1146. int retval = -EIO;
  1147. char *name;
  1148. u8 *data;
  1149. struct stframe {
  1150. u16 addr;
  1151. u16 count;
  1152. u8 data[0];
  1153. } *frame;
  1154. switch (signature) {
  1155. case 0xa5:
  1156. name = "isi608.bin";
  1157. break;
  1158. case 0xbb:
  1159. name = "isi608em.bin";
  1160. break;
  1161. case 0xcc:
  1162. name = "isi616em.bin";
  1163. break;
  1164. case 0xdd:
  1165. name = "isi4608.bin";
  1166. break;
  1167. case 0xee:
  1168. name = "isi4616.bin";
  1169. break;
  1170. default:
  1171. dev_err(&pdev->dev, "Unknown signature.\n");
  1172. goto end;
  1173. }
  1174. retval = request_firmware(&fw, name, &pdev->dev);
  1175. if (retval)
  1176. goto end;
  1177. retval = -EIO;
  1178. for (frame = (struct stframe *)fw->data;
  1179. frame < (struct stframe *)(fw->data + fw->size);
  1180. frame = (struct stframe *)((u8 *)(frame + 1) +
  1181. frame->count)) {
  1182. if (WaitTillCardIsFree(base))
  1183. goto errrelfw;
  1184. outw(0xf0, base); /* start upload sequence */
  1185. outw(0x00, base);
  1186. outw(frame->addr, base); /* lsb of address */
  1187. word_count = frame->count / 2 + frame->count % 2;
  1188. outw(word_count, base);
  1189. InterruptTheCard(base);
  1190. udelay(100); /* 0x2f */
  1191. if (WaitTillCardIsFree(base))
  1192. goto errrelfw;
  1193. status = inw(base + 0x4);
  1194. if (status != 0) {
  1195. dev_warn(&pdev->dev, "Card%d rejected load header:\n"
  1196. "Address:0x%x\n"
  1197. "Count:0x%x\n"
  1198. "Status:0x%x\n",
  1199. index + 1, frame->addr, frame->count, status);
  1200. goto errrelfw;
  1201. }
  1202. outsw(base, frame->data, word_count);
  1203. InterruptTheCard(base);
  1204. udelay(50); /* 0x0f */
  1205. if (WaitTillCardIsFree(base))
  1206. goto errrelfw;
  1207. status = inw(base + 0x4);
  1208. if (status != 0) {
  1209. dev_err(&pdev->dev, "Card%d got out of sync.Card "
  1210. "Status:0x%x\n", index + 1, status);
  1211. goto errrelfw;
  1212. }
  1213. }
  1214. /* XXX: should we test it by reading it back and comparing with original like
  1215. * in load firmware package? */
  1216. for (frame = (struct stframe *)fw->data;
  1217. frame < (struct stframe *)(fw->data + fw->size);
  1218. frame = (struct stframe *)((u8 *)(frame + 1) +
  1219. frame->count)) {
  1220. if (WaitTillCardIsFree(base))
  1221. goto errrelfw;
  1222. outw(0xf1, base); /* start download sequence */
  1223. outw(0x00, base);
  1224. outw(frame->addr, base); /* lsb of address */
  1225. word_count = (frame->count >> 1) + frame->count % 2;
  1226. outw(word_count + 1, base);
  1227. InterruptTheCard(base);
  1228. udelay(50); /* 0xf */
  1229. if (WaitTillCardIsFree(base))
  1230. goto errrelfw;
  1231. status = inw(base + 0x4);
  1232. if (status != 0) {
  1233. dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
  1234. "Address:0x%x\n"
  1235. "Count:0x%x\n"
  1236. "Status: 0x%x\n",
  1237. index + 1, frame->addr, frame->count, status);
  1238. goto errrelfw;
  1239. }
  1240. data = kmalloc_array(word_count, 2, GFP_KERNEL);
  1241. if (data == NULL) {
  1242. dev_err(&pdev->dev, "Card%d, firmware upload "
  1243. "failed, not enough memory\n", index + 1);
  1244. goto errrelfw;
  1245. }
  1246. inw(base);
  1247. insw(base, data, word_count);
  1248. InterruptTheCard(base);
  1249. for (a = 0; a < frame->count; a++)
  1250. if (data[a] != frame->data[a]) {
  1251. kfree(data);
  1252. dev_err(&pdev->dev, "Card%d, firmware upload "
  1253. "failed\n", index + 1);
  1254. goto errrelfw;
  1255. }
  1256. kfree(data);
  1257. udelay(50); /* 0xf */
  1258. if (WaitTillCardIsFree(base))
  1259. goto errrelfw;
  1260. status = inw(base + 0x4);
  1261. if (status != 0) {
  1262. dev_err(&pdev->dev, "Card%d verify got out of sync. "
  1263. "Card Status:0x%x\n", index + 1, status);
  1264. goto errrelfw;
  1265. }
  1266. }
  1267. /* xfer ctrl */
  1268. if (WaitTillCardIsFree(base))
  1269. goto errrelfw;
  1270. outw(0xf2, base);
  1271. outw(0x800, base);
  1272. outw(0x0, base);
  1273. outw(0x0, base);
  1274. InterruptTheCard(base);
  1275. outw(0x0, base + 0x4); /* for ISI4608 cards */
  1276. board->status |= FIRMWARE_LOADED;
  1277. retval = 0;
  1278. errrelfw:
  1279. release_firmware(fw);
  1280. end:
  1281. return retval;
  1282. }
  1283. /*
  1284. * Insmod can set static symbols so keep these static
  1285. */
  1286. static unsigned int card_count;
  1287. static int isicom_probe(struct pci_dev *pdev,
  1288. const struct pci_device_id *ent)
  1289. {
  1290. unsigned int uninitialized_var(signature), index;
  1291. int retval = -EPERM;
  1292. struct isi_board *board = NULL;
  1293. if (card_count >= BOARD_COUNT)
  1294. goto err;
  1295. retval = pci_enable_device(pdev);
  1296. if (retval) {
  1297. dev_err(&pdev->dev, "failed to enable\n");
  1298. goto err;
  1299. }
  1300. dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
  1301. /* allot the first empty slot in the array */
  1302. for (index = 0; index < BOARD_COUNT; index++) {
  1303. if (isi_card[index].base == 0) {
  1304. board = &isi_card[index];
  1305. break;
  1306. }
  1307. }
  1308. if (index == BOARD_COUNT) {
  1309. retval = -ENODEV;
  1310. goto err_disable;
  1311. }
  1312. board->index = index;
  1313. board->base = pci_resource_start(pdev, 3);
  1314. board->irq = pdev->irq;
  1315. card_count++;
  1316. pci_set_drvdata(pdev, board);
  1317. retval = pci_request_region(pdev, 3, ISICOM_NAME);
  1318. if (retval) {
  1319. dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
  1320. "will be disabled.\n", board->base, board->base + 15,
  1321. index + 1);
  1322. retval = -EBUSY;
  1323. goto errdec;
  1324. }
  1325. retval = request_irq(board->irq, isicom_interrupt,
  1326. IRQF_SHARED, ISICOM_NAME, board);
  1327. if (retval < 0) {
  1328. dev_err(&pdev->dev, "Could not install handler at Irq %d. "
  1329. "Card%d will be disabled.\n", board->irq, index + 1);
  1330. goto errunrr;
  1331. }
  1332. retval = reset_card(pdev, index, &signature);
  1333. if (retval < 0)
  1334. goto errunri;
  1335. retval = load_firmware(pdev, index, signature);
  1336. if (retval < 0)
  1337. goto errunri;
  1338. for (index = 0; index < board->port_count; index++) {
  1339. struct tty_port *tport = &board->ports[index].port;
  1340. tty_port_init(tport);
  1341. tport->ops = &isicom_port_ops;
  1342. tport->close_delay = 50 * HZ/100;
  1343. tport->closing_wait = 3000 * HZ/100;
  1344. tty_port_register_device(tport, isicom_normal,
  1345. board->index * 16 + index, &pdev->dev);
  1346. }
  1347. return 0;
  1348. errunri:
  1349. free_irq(board->irq, board);
  1350. errunrr:
  1351. pci_release_region(pdev, 3);
  1352. errdec:
  1353. board->base = 0;
  1354. card_count--;
  1355. err_disable:
  1356. pci_disable_device(pdev);
  1357. err:
  1358. return retval;
  1359. }
  1360. static void isicom_remove(struct pci_dev *pdev)
  1361. {
  1362. struct isi_board *board = pci_get_drvdata(pdev);
  1363. unsigned int i;
  1364. for (i = 0; i < board->port_count; i++) {
  1365. tty_unregister_device(isicom_normal, board->index * 16 + i);
  1366. tty_port_destroy(&board->ports[i].port);
  1367. }
  1368. free_irq(board->irq, board);
  1369. pci_release_region(pdev, 3);
  1370. board->base = 0;
  1371. card_count--;
  1372. pci_disable_device(pdev);
  1373. }
  1374. static int __init isicom_init(void)
  1375. {
  1376. int retval, idx, channel;
  1377. struct isi_port *port;
  1378. for (idx = 0; idx < BOARD_COUNT; idx++) {
  1379. port = &isi_ports[idx * 16];
  1380. isi_card[idx].ports = port;
  1381. spin_lock_init(&isi_card[idx].card_lock);
  1382. for (channel = 0; channel < 16; channel++, port++) {
  1383. port->magic = ISICOM_MAGIC;
  1384. port->card = &isi_card[idx];
  1385. port->channel = channel;
  1386. port->status = 0;
  1387. /* . . . */
  1388. }
  1389. isi_card[idx].base = 0;
  1390. isi_card[idx].irq = 0;
  1391. }
  1392. /* tty driver structure initialization */
  1393. isicom_normal = alloc_tty_driver(PORT_COUNT);
  1394. if (!isicom_normal) {
  1395. retval = -ENOMEM;
  1396. goto error;
  1397. }
  1398. isicom_normal->name = "ttyM";
  1399. isicom_normal->major = ISICOM_NMAJOR;
  1400. isicom_normal->minor_start = 0;
  1401. isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
  1402. isicom_normal->subtype = SERIAL_TYPE_NORMAL;
  1403. isicom_normal->init_termios = tty_std_termios;
  1404. isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
  1405. CLOCAL;
  1406. isicom_normal->flags = TTY_DRIVER_REAL_RAW |
  1407. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
  1408. tty_set_operations(isicom_normal, &isicom_ops);
  1409. retval = tty_register_driver(isicom_normal);
  1410. if (retval) {
  1411. pr_debug("Couldn't register the dialin driver\n");
  1412. goto err_puttty;
  1413. }
  1414. retval = pci_register_driver(&isicom_driver);
  1415. if (retval < 0) {
  1416. pr_err("Unable to register pci driver.\n");
  1417. goto err_unrtty;
  1418. }
  1419. mod_timer(&tx, jiffies + 1);
  1420. return 0;
  1421. err_unrtty:
  1422. tty_unregister_driver(isicom_normal);
  1423. err_puttty:
  1424. put_tty_driver(isicom_normal);
  1425. error:
  1426. return retval;
  1427. }
  1428. static void __exit isicom_exit(void)
  1429. {
  1430. del_timer_sync(&tx);
  1431. pci_unregister_driver(&isicom_driver);
  1432. tty_unregister_driver(isicom_normal);
  1433. put_tty_driver(isicom_normal);
  1434. }
  1435. module_init(isicom_init);
  1436. module_exit(isicom_exit);
  1437. MODULE_AUTHOR("MultiTech");
  1438. MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
  1439. MODULE_LICENSE("GPL");
  1440. MODULE_FIRMWARE("isi608.bin");
  1441. MODULE_FIRMWARE("isi608em.bin");
  1442. MODULE_FIRMWARE("isi616em.bin");
  1443. MODULE_FIRMWARE("isi4608.bin");
  1444. MODULE_FIRMWARE("isi4616.bin");