nozomi.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
  4. *
  5. * Written by: Ulf Jakobsson,
  6. * Jan Åkerfeldt,
  7. * Stefan Thomasson,
  8. *
  9. * Maintained by: Paul Hardwick (p.hardwick@option.com)
  10. *
  11. * Patches:
  12. * Locking code changes for Vodafone by Sphere Systems Ltd,
  13. * Andrew Bird (ajb@spheresystems.co.uk )
  14. * & Phil Sanderson
  15. *
  16. * Source has been ported from an implementation made by Filip Aben @ Option
  17. *
  18. * --------------------------------------------------------------------------
  19. *
  20. * Copyright (c) 2005,2006 Option Wireless Sweden AB
  21. * Copyright (c) 2006 Sphere Systems Ltd
  22. * Copyright (c) 2006 Option Wireless n/v
  23. * All rights Reserved.
  24. *
  25. * --------------------------------------------------------------------------
  26. */
  27. /* Enable this to have a lot of debug printouts */
  28. #define DEBUG
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/ioport.h>
  33. #include <linux/tty.h>
  34. #include <linux/tty_driver.h>
  35. #include <linux/tty_flip.h>
  36. #include <linux/sched.h>
  37. #include <linux/serial.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/kmod.h>
  40. #include <linux/init.h>
  41. #include <linux/kfifo.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/slab.h>
  44. #include <asm/byteorder.h>
  45. #include <linux/delay.h>
  46. #define VERSION_STRING DRIVER_DESC " 2.1d"
  47. /* Default debug printout level */
  48. #define NOZOMI_DEBUG_LEVEL 0x00
  49. static int debug = NOZOMI_DEBUG_LEVEL;
  50. module_param(debug, int, S_IRUGO | S_IWUSR);
  51. /* Macros definitions */
  52. #define DBG_(lvl, fmt, args...) \
  53. do { \
  54. if (lvl & debug) \
  55. pr_debug("[%d] %s(): " fmt "\n", \
  56. __LINE__, __func__, ##args); \
  57. } while (0)
  58. #define DBG1(args...) DBG_(0x01, ##args)
  59. #define DBG2(args...) DBG_(0x02, ##args)
  60. #define DBG3(args...) DBG_(0x04, ##args)
  61. #define DBG4(args...) DBG_(0x08, ##args)
  62. /* TODO: rewrite to optimize macros... */
  63. #define TMP_BUF_MAX 256
  64. #define DUMP(buf__, len__) \
  65. do { \
  66. char tbuf[TMP_BUF_MAX] = {0}; \
  67. if (len__ > 1) { \
  68. u32 data_len = min_t(u32, len__, TMP_BUF_MAX); \
  69. strscpy(tbuf, buf__, data_len); \
  70. if (tbuf[data_len - 2] == '\r') \
  71. tbuf[data_len - 2] = 'r'; \
  72. DBG1("SENDING: '%s' (%d+n)", tbuf, len__); \
  73. } else { \
  74. DBG1("SENDING: '%s' (%d)", tbuf, len__); \
  75. } \
  76. } while (0)
  77. /* Defines */
  78. #define NOZOMI_NAME "nozomi"
  79. #define NOZOMI_NAME_TTY "nozomi_tty"
  80. #define DRIVER_DESC "Nozomi driver"
  81. #define NTTY_TTY_MAXMINORS 256
  82. #define NTTY_FIFO_BUFFER_SIZE 8192
  83. /* Must be power of 2 */
  84. #define FIFO_BUFFER_SIZE_UL 8192
  85. /* Size of tmp send buffer to card */
  86. #define SEND_BUF_MAX 1024
  87. #define RECEIVE_BUF_MAX 4
  88. #define R_IIR 0x0000 /* Interrupt Identity Register */
  89. #define R_FCR 0x0000 /* Flow Control Register */
  90. #define R_IER 0x0004 /* Interrupt Enable Register */
  91. #define NOZOMI_CONFIG_MAGIC 0xEFEFFEFE
  92. #define TOGGLE_VALID 0x0000
  93. /* Definition of interrupt tokens */
  94. #define MDM_DL1 0x0001
  95. #define MDM_UL1 0x0002
  96. #define MDM_DL2 0x0004
  97. #define MDM_UL2 0x0008
  98. #define DIAG_DL1 0x0010
  99. #define DIAG_DL2 0x0020
  100. #define DIAG_UL 0x0040
  101. #define APP1_DL 0x0080
  102. #define APP1_UL 0x0100
  103. #define APP2_DL 0x0200
  104. #define APP2_UL 0x0400
  105. #define CTRL_DL 0x0800
  106. #define CTRL_UL 0x1000
  107. #define RESET 0x8000
  108. #define MDM_DL (MDM_DL1 | MDM_DL2)
  109. #define MDM_UL (MDM_UL1 | MDM_UL2)
  110. #define DIAG_DL (DIAG_DL1 | DIAG_DL2)
  111. /* modem signal definition */
  112. #define CTRL_DSR 0x0001
  113. #define CTRL_DCD 0x0002
  114. #define CTRL_RI 0x0004
  115. #define CTRL_CTS 0x0008
  116. #define CTRL_DTR 0x0001
  117. #define CTRL_RTS 0x0002
  118. #define MAX_PORT 4
  119. #define NOZOMI_MAX_PORTS 5
  120. #define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
  121. /* Type definitions */
  122. /*
  123. * There are two types of nozomi cards,
  124. * one with 2048 memory and with 8192 memory
  125. */
  126. enum card_type {
  127. F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
  128. F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
  129. };
  130. /* Initialization states a card can be in */
  131. enum card_state {
  132. NOZOMI_STATE_UNKNOWN = 0,
  133. NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
  134. NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
  135. NOZOMI_STATE_READY = 3, /* flowcontrols received */
  136. };
  137. /* Two different toggle channels exist */
  138. enum channel_type {
  139. CH_A = 0,
  140. CH_B = 1,
  141. };
  142. /* Port definition for the card regarding flow control */
  143. enum ctrl_port_type {
  144. CTRL_CMD = 0,
  145. CTRL_MDM = 1,
  146. CTRL_DIAG = 2,
  147. CTRL_APP1 = 3,
  148. CTRL_APP2 = 4,
  149. CTRL_ERROR = -1,
  150. };
  151. /* Ports that the nozomi has */
  152. enum port_type {
  153. PORT_MDM = 0,
  154. PORT_DIAG = 1,
  155. PORT_APP1 = 2,
  156. PORT_APP2 = 3,
  157. PORT_CTRL = 4,
  158. PORT_ERROR = -1,
  159. };
  160. #ifdef __BIG_ENDIAN
  161. /* Big endian */
  162. struct toggles {
  163. unsigned int enabled:5; /*
  164. * Toggle fields are valid if enabled is 0,
  165. * else A-channels must always be used.
  166. */
  167. unsigned int diag_dl:1;
  168. unsigned int mdm_dl:1;
  169. unsigned int mdm_ul:1;
  170. } __attribute__ ((packed));
  171. /* Configuration table to read at startup of card */
  172. /* Is for now only needed during initialization phase */
  173. struct config_table {
  174. u32 signature;
  175. u16 product_information;
  176. u16 version;
  177. u8 pad3[3];
  178. struct toggles toggle;
  179. u8 pad1[4];
  180. u16 dl_mdm_len1; /*
  181. * If this is 64, it can hold
  182. * 60 bytes + 4 that is length field
  183. */
  184. u16 dl_start;
  185. u16 dl_diag_len1;
  186. u16 dl_mdm_len2; /*
  187. * If this is 64, it can hold
  188. * 60 bytes + 4 that is length field
  189. */
  190. u16 dl_app1_len;
  191. u16 dl_diag_len2;
  192. u16 dl_ctrl_len;
  193. u16 dl_app2_len;
  194. u8 pad2[16];
  195. u16 ul_mdm_len1;
  196. u16 ul_start;
  197. u16 ul_diag_len;
  198. u16 ul_mdm_len2;
  199. u16 ul_app1_len;
  200. u16 ul_app2_len;
  201. u16 ul_ctrl_len;
  202. } __attribute__ ((packed));
  203. /* This stores all control downlink flags */
  204. struct ctrl_dl {
  205. u8 port;
  206. unsigned int reserved:4;
  207. unsigned int CTS:1;
  208. unsigned int RI:1;
  209. unsigned int DCD:1;
  210. unsigned int DSR:1;
  211. } __attribute__ ((packed));
  212. /* This stores all control uplink flags */
  213. struct ctrl_ul {
  214. u8 port;
  215. unsigned int reserved:6;
  216. unsigned int RTS:1;
  217. unsigned int DTR:1;
  218. } __attribute__ ((packed));
  219. #else
  220. /* Little endian */
  221. /* This represents the toggle information */
  222. struct toggles {
  223. unsigned int mdm_ul:1;
  224. unsigned int mdm_dl:1;
  225. unsigned int diag_dl:1;
  226. unsigned int enabled:5; /*
  227. * Toggle fields are valid if enabled is 0,
  228. * else A-channels must always be used.
  229. */
  230. } __attribute__ ((packed));
  231. /* Configuration table to read at startup of card */
  232. struct config_table {
  233. u32 signature;
  234. u16 version;
  235. u16 product_information;
  236. struct toggles toggle;
  237. u8 pad1[7];
  238. u16 dl_start;
  239. u16 dl_mdm_len1; /*
  240. * If this is 64, it can hold
  241. * 60 bytes + 4 that is length field
  242. */
  243. u16 dl_mdm_len2;
  244. u16 dl_diag_len1;
  245. u16 dl_diag_len2;
  246. u16 dl_app1_len;
  247. u16 dl_app2_len;
  248. u16 dl_ctrl_len;
  249. u8 pad2[16];
  250. u16 ul_start;
  251. u16 ul_mdm_len2;
  252. u16 ul_mdm_len1;
  253. u16 ul_diag_len;
  254. u16 ul_app1_len;
  255. u16 ul_app2_len;
  256. u16 ul_ctrl_len;
  257. } __attribute__ ((packed));
  258. /* This stores all control downlink flags */
  259. struct ctrl_dl {
  260. unsigned int DSR:1;
  261. unsigned int DCD:1;
  262. unsigned int RI:1;
  263. unsigned int CTS:1;
  264. unsigned int reserverd:4;
  265. u8 port;
  266. } __attribute__ ((packed));
  267. /* This stores all control uplink flags */
  268. struct ctrl_ul {
  269. unsigned int DTR:1;
  270. unsigned int RTS:1;
  271. unsigned int reserved:6;
  272. u8 port;
  273. } __attribute__ ((packed));
  274. #endif
  275. /* This holds all information that is needed regarding a port */
  276. struct port {
  277. struct tty_port port;
  278. u8 update_flow_control;
  279. struct ctrl_ul ctrl_ul;
  280. struct ctrl_dl ctrl_dl;
  281. struct kfifo fifo_ul;
  282. void __iomem *dl_addr[2];
  283. u32 dl_size[2];
  284. u8 toggle_dl;
  285. void __iomem *ul_addr[2];
  286. u32 ul_size[2];
  287. u8 toggle_ul;
  288. u16 token_dl;
  289. wait_queue_head_t tty_wait;
  290. struct async_icount tty_icount;
  291. struct nozomi *dc;
  292. };
  293. /* Private data one for each card in the system */
  294. struct nozomi {
  295. void __iomem *base_addr;
  296. unsigned long flip;
  297. /* Pointers to registers */
  298. void __iomem *reg_iir;
  299. void __iomem *reg_fcr;
  300. void __iomem *reg_ier;
  301. u16 last_ier;
  302. enum card_type card_type;
  303. struct config_table config_table; /* Configuration table */
  304. struct pci_dev *pdev;
  305. struct port port[NOZOMI_MAX_PORTS];
  306. u8 *send_buf;
  307. spinlock_t spin_mutex; /* secures access to registers and tty */
  308. unsigned int index_start;
  309. enum card_state state;
  310. u32 open_ttys;
  311. };
  312. /* This is a data packet that is read or written to/from card */
  313. struct buffer {
  314. u32 size; /* size is the length of the data buffer */
  315. u8 *data;
  316. } __attribute__ ((packed));
  317. /* Global variables */
  318. static const struct pci_device_id nozomi_pci_tbl[] = {
  319. {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
  320. {},
  321. };
  322. MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
  323. static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
  324. static struct tty_driver *ntty_driver;
  325. static const struct tty_port_operations noz_tty_port_ops;
  326. /*
  327. * find card by tty_index
  328. */
  329. static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
  330. {
  331. return tty ? ndevs[tty->index / MAX_PORT] : NULL;
  332. }
  333. static inline struct port *get_port_by_tty(const struct tty_struct *tty)
  334. {
  335. struct nozomi *ndev = get_dc_by_tty(tty);
  336. return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
  337. }
  338. /*
  339. * TODO:
  340. * -Optimize
  341. * -Rewrite cleaner
  342. */
  343. static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
  344. u32 size_bytes)
  345. {
  346. u32 i = 0;
  347. const u32 __iomem *ptr = mem_addr_start;
  348. u16 *buf16;
  349. if (unlikely(!ptr || !buf))
  350. goto out;
  351. /* shortcut for extremely often used cases */
  352. switch (size_bytes) {
  353. case 2: /* 2 bytes */
  354. buf16 = (u16 *) buf;
  355. *buf16 = __le16_to_cpu(readw(ptr));
  356. goto out;
  357. break;
  358. case 4: /* 4 bytes */
  359. *(buf) = __le32_to_cpu(readl(ptr));
  360. goto out;
  361. break;
  362. }
  363. while (i < size_bytes) {
  364. if (size_bytes - i == 2) {
  365. /* Handle 2 bytes in the end */
  366. buf16 = (u16 *) buf;
  367. *(buf16) = __le16_to_cpu(readw(ptr));
  368. i += 2;
  369. } else {
  370. /* Read 4 bytes */
  371. *(buf) = __le32_to_cpu(readl(ptr));
  372. i += 4;
  373. }
  374. buf++;
  375. ptr++;
  376. }
  377. out:
  378. return;
  379. }
  380. /*
  381. * TODO:
  382. * -Optimize
  383. * -Rewrite cleaner
  384. */
  385. static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
  386. u32 size_bytes)
  387. {
  388. u32 i = 0;
  389. u32 __iomem *ptr = mem_addr_start;
  390. const u16 *buf16;
  391. if (unlikely(!ptr || !buf))
  392. return 0;
  393. /* shortcut for extremely often used cases */
  394. switch (size_bytes) {
  395. case 2: /* 2 bytes */
  396. buf16 = (const u16 *)buf;
  397. writew(__cpu_to_le16(*buf16), ptr);
  398. return 2;
  399. break;
  400. case 1: /*
  401. * also needs to write 4 bytes in this case
  402. * so falling through..
  403. */
  404. case 4: /* 4 bytes */
  405. writel(__cpu_to_le32(*buf), ptr);
  406. return 4;
  407. break;
  408. }
  409. while (i < size_bytes) {
  410. if (size_bytes - i == 2) {
  411. /* 2 bytes */
  412. buf16 = (const u16 *)buf;
  413. writew(__cpu_to_le16(*buf16), ptr);
  414. i += 2;
  415. } else {
  416. /* 4 bytes */
  417. writel(__cpu_to_le32(*buf), ptr);
  418. i += 4;
  419. }
  420. buf++;
  421. ptr++;
  422. }
  423. return i;
  424. }
  425. /* Setup pointers to different channels and also setup buffer sizes. */
  426. static void nozomi_setup_memory(struct nozomi *dc)
  427. {
  428. void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
  429. /* The length reported is including the length field of 4 bytes,
  430. * hence subtract with 4.
  431. */
  432. const u16 buff_offset = 4;
  433. /* Modem port dl configuration */
  434. dc->port[PORT_MDM].dl_addr[CH_A] = offset;
  435. dc->port[PORT_MDM].dl_addr[CH_B] =
  436. (offset += dc->config_table.dl_mdm_len1);
  437. dc->port[PORT_MDM].dl_size[CH_A] =
  438. dc->config_table.dl_mdm_len1 - buff_offset;
  439. dc->port[PORT_MDM].dl_size[CH_B] =
  440. dc->config_table.dl_mdm_len2 - buff_offset;
  441. /* Diag port dl configuration */
  442. dc->port[PORT_DIAG].dl_addr[CH_A] =
  443. (offset += dc->config_table.dl_mdm_len2);
  444. dc->port[PORT_DIAG].dl_size[CH_A] =
  445. dc->config_table.dl_diag_len1 - buff_offset;
  446. dc->port[PORT_DIAG].dl_addr[CH_B] =
  447. (offset += dc->config_table.dl_diag_len1);
  448. dc->port[PORT_DIAG].dl_size[CH_B] =
  449. dc->config_table.dl_diag_len2 - buff_offset;
  450. /* App1 port dl configuration */
  451. dc->port[PORT_APP1].dl_addr[CH_A] =
  452. (offset += dc->config_table.dl_diag_len2);
  453. dc->port[PORT_APP1].dl_size[CH_A] =
  454. dc->config_table.dl_app1_len - buff_offset;
  455. /* App2 port dl configuration */
  456. dc->port[PORT_APP2].dl_addr[CH_A] =
  457. (offset += dc->config_table.dl_app1_len);
  458. dc->port[PORT_APP2].dl_size[CH_A] =
  459. dc->config_table.dl_app2_len - buff_offset;
  460. /* Ctrl dl configuration */
  461. dc->port[PORT_CTRL].dl_addr[CH_A] =
  462. (offset += dc->config_table.dl_app2_len);
  463. dc->port[PORT_CTRL].dl_size[CH_A] =
  464. dc->config_table.dl_ctrl_len - buff_offset;
  465. offset = dc->base_addr + dc->config_table.ul_start;
  466. /* Modem Port ul configuration */
  467. dc->port[PORT_MDM].ul_addr[CH_A] = offset;
  468. dc->port[PORT_MDM].ul_size[CH_A] =
  469. dc->config_table.ul_mdm_len1 - buff_offset;
  470. dc->port[PORT_MDM].ul_addr[CH_B] =
  471. (offset += dc->config_table.ul_mdm_len1);
  472. dc->port[PORT_MDM].ul_size[CH_B] =
  473. dc->config_table.ul_mdm_len2 - buff_offset;
  474. /* Diag port ul configuration */
  475. dc->port[PORT_DIAG].ul_addr[CH_A] =
  476. (offset += dc->config_table.ul_mdm_len2);
  477. dc->port[PORT_DIAG].ul_size[CH_A] =
  478. dc->config_table.ul_diag_len - buff_offset;
  479. /* App1 port ul configuration */
  480. dc->port[PORT_APP1].ul_addr[CH_A] =
  481. (offset += dc->config_table.ul_diag_len);
  482. dc->port[PORT_APP1].ul_size[CH_A] =
  483. dc->config_table.ul_app1_len - buff_offset;
  484. /* App2 port ul configuration */
  485. dc->port[PORT_APP2].ul_addr[CH_A] =
  486. (offset += dc->config_table.ul_app1_len);
  487. dc->port[PORT_APP2].ul_size[CH_A] =
  488. dc->config_table.ul_app2_len - buff_offset;
  489. /* Ctrl ul configuration */
  490. dc->port[PORT_CTRL].ul_addr[CH_A] =
  491. (offset += dc->config_table.ul_app2_len);
  492. dc->port[PORT_CTRL].ul_size[CH_A] =
  493. dc->config_table.ul_ctrl_len - buff_offset;
  494. }
  495. /* Dump config table under initalization phase */
  496. #ifdef DEBUG
  497. static void dump_table(const struct nozomi *dc)
  498. {
  499. DBG3("signature: 0x%08X", dc->config_table.signature);
  500. DBG3("version: 0x%04X", dc->config_table.version);
  501. DBG3("product_information: 0x%04X", \
  502. dc->config_table.product_information);
  503. DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
  504. DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
  505. DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
  506. DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
  507. DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
  508. DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
  509. dc->config_table.dl_mdm_len1);
  510. DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
  511. dc->config_table.dl_mdm_len2);
  512. DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
  513. dc->config_table.dl_diag_len1);
  514. DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
  515. dc->config_table.dl_diag_len2);
  516. DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
  517. dc->config_table.dl_app1_len);
  518. DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
  519. dc->config_table.dl_app2_len);
  520. DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
  521. dc->config_table.dl_ctrl_len);
  522. DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
  523. dc->config_table.ul_start);
  524. DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
  525. dc->config_table.ul_mdm_len1);
  526. DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
  527. dc->config_table.ul_mdm_len2);
  528. DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
  529. dc->config_table.ul_diag_len);
  530. DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
  531. dc->config_table.ul_app1_len);
  532. DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
  533. dc->config_table.ul_app2_len);
  534. DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
  535. dc->config_table.ul_ctrl_len);
  536. }
  537. #else
  538. static inline void dump_table(const struct nozomi *dc) { }
  539. #endif
  540. /*
  541. * Read configuration table from card under intalization phase
  542. * Returns 1 if ok, else 0
  543. */
  544. static int nozomi_read_config_table(struct nozomi *dc)
  545. {
  546. read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
  547. sizeof(struct config_table));
  548. if (dc->config_table.signature != NOZOMI_CONFIG_MAGIC) {
  549. dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
  550. dc->config_table.signature, NOZOMI_CONFIG_MAGIC);
  551. return 0;
  552. }
  553. if ((dc->config_table.version == 0)
  554. || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
  555. int i;
  556. DBG1("Second phase, configuring card");
  557. nozomi_setup_memory(dc);
  558. dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
  559. dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
  560. dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
  561. DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
  562. dc->port[PORT_MDM].toggle_ul,
  563. dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
  564. dump_table(dc);
  565. for (i = PORT_MDM; i < MAX_PORT; i++) {
  566. memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
  567. memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
  568. }
  569. /* Enable control channel */
  570. dc->last_ier = dc->last_ier | CTRL_DL;
  571. writew(dc->last_ier, dc->reg_ier);
  572. dc->state = NOZOMI_STATE_ALLOCATED;
  573. dev_info(&dc->pdev->dev, "Initialization OK!\n");
  574. return 1;
  575. }
  576. if ((dc->config_table.version > 0)
  577. && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
  578. u32 offset = 0;
  579. DBG1("First phase: pushing upload buffers, clearing download");
  580. dev_info(&dc->pdev->dev, "Version of card: %d\n",
  581. dc->config_table.version);
  582. /* Here we should disable all I/O over F32. */
  583. nozomi_setup_memory(dc);
  584. /*
  585. * We should send ALL channel pair tokens back along
  586. * with reset token
  587. */
  588. /* push upload modem buffers */
  589. write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
  590. (u32 *) &offset, 4);
  591. write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
  592. (u32 *) &offset, 4);
  593. writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
  594. DBG1("First phase done");
  595. }
  596. return 1;
  597. }
  598. /* Enable uplink interrupts */
  599. static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
  600. {
  601. static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
  602. if (port < NOZOMI_MAX_PORTS) {
  603. dc->last_ier |= mask[port];
  604. writew(dc->last_ier, dc->reg_ier);
  605. } else {
  606. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  607. }
  608. }
  609. /* Disable uplink interrupts */
  610. static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
  611. {
  612. static const u16 mask[] =
  613. {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
  614. if (port < NOZOMI_MAX_PORTS) {
  615. dc->last_ier &= mask[port];
  616. writew(dc->last_ier, dc->reg_ier);
  617. } else {
  618. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  619. }
  620. }
  621. /* Enable downlink interrupts */
  622. static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
  623. {
  624. static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
  625. if (port < NOZOMI_MAX_PORTS) {
  626. dc->last_ier |= mask[port];
  627. writew(dc->last_ier, dc->reg_ier);
  628. } else {
  629. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  630. }
  631. }
  632. /* Disable downlink interrupts */
  633. static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
  634. {
  635. static const u16 mask[] =
  636. {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
  637. if (port < NOZOMI_MAX_PORTS) {
  638. dc->last_ier &= mask[port];
  639. writew(dc->last_ier, dc->reg_ier);
  640. } else {
  641. dev_err(&dc->pdev->dev, "Called with wrong port?\n");
  642. }
  643. }
  644. /*
  645. * Return 1 - send buffer to card and ack.
  646. * Return 0 - don't ack, don't send buffer to card.
  647. */
  648. static int send_data(enum port_type index, struct nozomi *dc)
  649. {
  650. u32 size = 0;
  651. struct port *port = &dc->port[index];
  652. const u8 toggle = port->toggle_ul;
  653. void __iomem *addr = port->ul_addr[toggle];
  654. const u32 ul_size = port->ul_size[toggle];
  655. /* Get data from tty and place in buf for now */
  656. size = kfifo_out(&port->fifo_ul, dc->send_buf,
  657. ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
  658. if (size == 0) {
  659. DBG4("No more data to send, disable link:");
  660. return 0;
  661. }
  662. /* DUMP(buf, size); */
  663. /* Write length + data */
  664. write_mem32(addr, (u32 *) &size, 4);
  665. write_mem32(addr + 4, (u32 *) dc->send_buf, size);
  666. tty_port_tty_wakeup(&port->port);
  667. return 1;
  668. }
  669. /* If all data has been read, return 1, else 0 */
  670. static int receive_data(enum port_type index, struct nozomi *dc)
  671. {
  672. u8 buf[RECEIVE_BUF_MAX] = { 0 };
  673. int size;
  674. u32 offset = 4;
  675. struct port *port = &dc->port[index];
  676. void __iomem *addr = port->dl_addr[port->toggle_dl];
  677. struct tty_struct *tty = tty_port_tty_get(&port->port);
  678. int i, ret;
  679. size = __le32_to_cpu(readl(addr));
  680. /* DBG1( "%d bytes port: %d", size, index); */
  681. if (tty && tty_throttled(tty)) {
  682. DBG1("No room in tty, don't read data, don't ack interrupt, "
  683. "disable interrupt");
  684. /* disable interrupt in downlink... */
  685. disable_transmit_dl(index, dc);
  686. ret = 0;
  687. goto put;
  688. }
  689. if (unlikely(size == 0)) {
  690. dev_err(&dc->pdev->dev, "size == 0?\n");
  691. ret = 1;
  692. goto put;
  693. }
  694. while (size > 0) {
  695. read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
  696. if (size == 1) {
  697. tty_insert_flip_char(&port->port, buf[0], TTY_NORMAL);
  698. size = 0;
  699. } else if (size < RECEIVE_BUF_MAX) {
  700. size -= tty_insert_flip_string(&port->port,
  701. (char *)buf, size);
  702. } else {
  703. i = tty_insert_flip_string(&port->port,
  704. (char *)buf, RECEIVE_BUF_MAX);
  705. size -= i;
  706. offset += i;
  707. }
  708. }
  709. set_bit(index, &dc->flip);
  710. ret = 1;
  711. put:
  712. tty_kref_put(tty);
  713. return ret;
  714. }
  715. /* Debug for interrupts */
  716. #ifdef DEBUG
  717. static char *interrupt2str(u16 interrupt)
  718. {
  719. static char buf[TMP_BUF_MAX];
  720. char *p = buf;
  721. interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
  722. interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  723. "MDM_DL2 ") : NULL;
  724. interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  725. "MDM_UL1 ") : NULL;
  726. interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  727. "MDM_UL2 ") : NULL;
  728. interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  729. "DIAG_DL1 ") : NULL;
  730. interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  731. "DIAG_DL2 ") : NULL;
  732. interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  733. "DIAG_UL ") : NULL;
  734. interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  735. "APP1_DL ") : NULL;
  736. interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  737. "APP2_DL ") : NULL;
  738. interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  739. "APP1_UL ") : NULL;
  740. interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  741. "APP2_UL ") : NULL;
  742. interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  743. "CTRL_DL ") : NULL;
  744. interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  745. "CTRL_UL ") : NULL;
  746. interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
  747. "RESET ") : NULL;
  748. return buf;
  749. }
  750. #endif
  751. /*
  752. * Receive flow control
  753. * Return 1 - If ok, else 0
  754. */
  755. static int receive_flow_control(struct nozomi *dc)
  756. {
  757. enum port_type port = PORT_MDM;
  758. struct ctrl_dl ctrl_dl;
  759. struct ctrl_dl old_ctrl;
  760. u16 enable_ier = 0;
  761. read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
  762. switch (ctrl_dl.port) {
  763. case CTRL_CMD:
  764. DBG1("The Base Band sends this value as a response to a "
  765. "request for IMSI detach sent over the control "
  766. "channel uplink (see section 7.6.1).");
  767. break;
  768. case CTRL_MDM:
  769. port = PORT_MDM;
  770. enable_ier = MDM_DL;
  771. break;
  772. case CTRL_DIAG:
  773. port = PORT_DIAG;
  774. enable_ier = DIAG_DL;
  775. break;
  776. case CTRL_APP1:
  777. port = PORT_APP1;
  778. enable_ier = APP1_DL;
  779. break;
  780. case CTRL_APP2:
  781. port = PORT_APP2;
  782. enable_ier = APP2_DL;
  783. if (dc->state == NOZOMI_STATE_ALLOCATED) {
  784. /*
  785. * After card initialization the flow control
  786. * received for APP2 is always the last
  787. */
  788. dc->state = NOZOMI_STATE_READY;
  789. dev_info(&dc->pdev->dev, "Device READY!\n");
  790. }
  791. break;
  792. default:
  793. dev_err(&dc->pdev->dev,
  794. "ERROR: flow control received for non-existing port\n");
  795. return 0;
  796. }
  797. DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
  798. *((u16 *)&ctrl_dl));
  799. old_ctrl = dc->port[port].ctrl_dl;
  800. dc->port[port].ctrl_dl = ctrl_dl;
  801. if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
  802. DBG1("Disable interrupt (0x%04X) on port: %d",
  803. enable_ier, port);
  804. disable_transmit_ul(port, dc);
  805. } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
  806. if (kfifo_len(&dc->port[port].fifo_ul)) {
  807. DBG1("Enable interrupt (0x%04X) on port: %d",
  808. enable_ier, port);
  809. DBG1("Data in buffer [%d], enable transmit! ",
  810. kfifo_len(&dc->port[port].fifo_ul));
  811. enable_transmit_ul(port, dc);
  812. } else {
  813. DBG1("No data in buffer...");
  814. }
  815. }
  816. if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
  817. DBG1(" No change in mctrl");
  818. return 1;
  819. }
  820. /* Update statistics */
  821. if (old_ctrl.CTS != ctrl_dl.CTS)
  822. dc->port[port].tty_icount.cts++;
  823. if (old_ctrl.DSR != ctrl_dl.DSR)
  824. dc->port[port].tty_icount.dsr++;
  825. if (old_ctrl.RI != ctrl_dl.RI)
  826. dc->port[port].tty_icount.rng++;
  827. if (old_ctrl.DCD != ctrl_dl.DCD)
  828. dc->port[port].tty_icount.dcd++;
  829. wake_up_interruptible(&dc->port[port].tty_wait);
  830. DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
  831. port,
  832. dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
  833. dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
  834. return 1;
  835. }
  836. static enum ctrl_port_type port2ctrl(enum port_type port,
  837. const struct nozomi *dc)
  838. {
  839. switch (port) {
  840. case PORT_MDM:
  841. return CTRL_MDM;
  842. case PORT_DIAG:
  843. return CTRL_DIAG;
  844. case PORT_APP1:
  845. return CTRL_APP1;
  846. case PORT_APP2:
  847. return CTRL_APP2;
  848. default:
  849. dev_err(&dc->pdev->dev,
  850. "ERROR: send flow control " \
  851. "received for non-existing port\n");
  852. }
  853. return CTRL_ERROR;
  854. }
  855. /*
  856. * Send flow control, can only update one channel at a time
  857. * Return 0 - If we have updated all flow control
  858. * Return 1 - If we need to update more flow control, ack current enable more
  859. */
  860. static int send_flow_control(struct nozomi *dc)
  861. {
  862. u32 i, more_flow_control_to_be_updated = 0;
  863. u16 *ctrl;
  864. for (i = PORT_MDM; i < MAX_PORT; i++) {
  865. if (dc->port[i].update_flow_control) {
  866. if (more_flow_control_to_be_updated) {
  867. /* We have more flow control to be updated */
  868. return 1;
  869. }
  870. dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
  871. ctrl = (u16 *)&dc->port[i].ctrl_ul;
  872. write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
  873. (u32 *) ctrl, 2);
  874. dc->port[i].update_flow_control = 0;
  875. more_flow_control_to_be_updated = 1;
  876. }
  877. }
  878. return 0;
  879. }
  880. /*
  881. * Handle downlink data, ports that are handled are modem and diagnostics
  882. * Return 1 - ok
  883. * Return 0 - toggle fields are out of sync
  884. */
  885. static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
  886. u16 read_iir, u16 mask1, u16 mask2)
  887. {
  888. if (*toggle == 0 && read_iir & mask1) {
  889. if (receive_data(port, dc)) {
  890. writew(mask1, dc->reg_fcr);
  891. *toggle = !(*toggle);
  892. }
  893. if (read_iir & mask2) {
  894. if (receive_data(port, dc)) {
  895. writew(mask2, dc->reg_fcr);
  896. *toggle = !(*toggle);
  897. }
  898. }
  899. } else if (*toggle == 1 && read_iir & mask2) {
  900. if (receive_data(port, dc)) {
  901. writew(mask2, dc->reg_fcr);
  902. *toggle = !(*toggle);
  903. }
  904. if (read_iir & mask1) {
  905. if (receive_data(port, dc)) {
  906. writew(mask1, dc->reg_fcr);
  907. *toggle = !(*toggle);
  908. }
  909. }
  910. } else {
  911. dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
  912. *toggle);
  913. return 0;
  914. }
  915. return 1;
  916. }
  917. /*
  918. * Handle uplink data, this is currently for the modem port
  919. * Return 1 - ok
  920. * Return 0 - toggle field are out of sync
  921. */
  922. static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
  923. {
  924. u8 *toggle = &(dc->port[port].toggle_ul);
  925. if (*toggle == 0 && read_iir & MDM_UL1) {
  926. dc->last_ier &= ~MDM_UL;
  927. writew(dc->last_ier, dc->reg_ier);
  928. if (send_data(port, dc)) {
  929. writew(MDM_UL1, dc->reg_fcr);
  930. dc->last_ier = dc->last_ier | MDM_UL;
  931. writew(dc->last_ier, dc->reg_ier);
  932. *toggle = !*toggle;
  933. }
  934. if (read_iir & MDM_UL2) {
  935. dc->last_ier &= ~MDM_UL;
  936. writew(dc->last_ier, dc->reg_ier);
  937. if (send_data(port, dc)) {
  938. writew(MDM_UL2, dc->reg_fcr);
  939. dc->last_ier = dc->last_ier | MDM_UL;
  940. writew(dc->last_ier, dc->reg_ier);
  941. *toggle = !*toggle;
  942. }
  943. }
  944. } else if (*toggle == 1 && read_iir & MDM_UL2) {
  945. dc->last_ier &= ~MDM_UL;
  946. writew(dc->last_ier, dc->reg_ier);
  947. if (send_data(port, dc)) {
  948. writew(MDM_UL2, dc->reg_fcr);
  949. dc->last_ier = dc->last_ier | MDM_UL;
  950. writew(dc->last_ier, dc->reg_ier);
  951. *toggle = !*toggle;
  952. }
  953. if (read_iir & MDM_UL1) {
  954. dc->last_ier &= ~MDM_UL;
  955. writew(dc->last_ier, dc->reg_ier);
  956. if (send_data(port, dc)) {
  957. writew(MDM_UL1, dc->reg_fcr);
  958. dc->last_ier = dc->last_ier | MDM_UL;
  959. writew(dc->last_ier, dc->reg_ier);
  960. *toggle = !*toggle;
  961. }
  962. }
  963. } else {
  964. writew(read_iir & MDM_UL, dc->reg_fcr);
  965. dev_err(&dc->pdev->dev, "port out of sync!\n");
  966. return 0;
  967. }
  968. return 1;
  969. }
  970. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  971. {
  972. struct nozomi *dc = dev_id;
  973. unsigned int a;
  974. u16 read_iir;
  975. if (!dc)
  976. return IRQ_NONE;
  977. spin_lock(&dc->spin_mutex);
  978. read_iir = readw(dc->reg_iir);
  979. /* Card removed */
  980. if (read_iir == (u16)-1)
  981. goto none;
  982. /*
  983. * Just handle interrupt enabled in IER
  984. * (by masking with dc->last_ier)
  985. */
  986. read_iir &= dc->last_ier;
  987. if (read_iir == 0)
  988. goto none;
  989. DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
  990. dc->last_ier);
  991. if (read_iir & RESET) {
  992. if (unlikely(!nozomi_read_config_table(dc))) {
  993. dc->last_ier = 0x0;
  994. writew(dc->last_ier, dc->reg_ier);
  995. dev_err(&dc->pdev->dev, "Could not read status from "
  996. "card, we should disable interface\n");
  997. } else {
  998. writew(RESET, dc->reg_fcr);
  999. }
  1000. /* No more useful info if this was the reset interrupt. */
  1001. goto exit_handler;
  1002. }
  1003. if (read_iir & CTRL_UL) {
  1004. DBG1("CTRL_UL");
  1005. dc->last_ier &= ~CTRL_UL;
  1006. writew(dc->last_ier, dc->reg_ier);
  1007. if (send_flow_control(dc)) {
  1008. writew(CTRL_UL, dc->reg_fcr);
  1009. dc->last_ier = dc->last_ier | CTRL_UL;
  1010. writew(dc->last_ier, dc->reg_ier);
  1011. }
  1012. }
  1013. if (read_iir & CTRL_DL) {
  1014. receive_flow_control(dc);
  1015. writew(CTRL_DL, dc->reg_fcr);
  1016. }
  1017. if (read_iir & MDM_DL) {
  1018. if (!handle_data_dl(dc, PORT_MDM,
  1019. &(dc->port[PORT_MDM].toggle_dl), read_iir,
  1020. MDM_DL1, MDM_DL2)) {
  1021. dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
  1022. goto exit_handler;
  1023. }
  1024. }
  1025. if (read_iir & MDM_UL) {
  1026. if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
  1027. dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
  1028. goto exit_handler;
  1029. }
  1030. }
  1031. if (read_iir & DIAG_DL) {
  1032. if (!handle_data_dl(dc, PORT_DIAG,
  1033. &(dc->port[PORT_DIAG].toggle_dl), read_iir,
  1034. DIAG_DL1, DIAG_DL2)) {
  1035. dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
  1036. goto exit_handler;
  1037. }
  1038. }
  1039. if (read_iir & DIAG_UL) {
  1040. dc->last_ier &= ~DIAG_UL;
  1041. writew(dc->last_ier, dc->reg_ier);
  1042. if (send_data(PORT_DIAG, dc)) {
  1043. writew(DIAG_UL, dc->reg_fcr);
  1044. dc->last_ier = dc->last_ier | DIAG_UL;
  1045. writew(dc->last_ier, dc->reg_ier);
  1046. }
  1047. }
  1048. if (read_iir & APP1_DL) {
  1049. if (receive_data(PORT_APP1, dc))
  1050. writew(APP1_DL, dc->reg_fcr);
  1051. }
  1052. if (read_iir & APP1_UL) {
  1053. dc->last_ier &= ~APP1_UL;
  1054. writew(dc->last_ier, dc->reg_ier);
  1055. if (send_data(PORT_APP1, dc)) {
  1056. writew(APP1_UL, dc->reg_fcr);
  1057. dc->last_ier = dc->last_ier | APP1_UL;
  1058. writew(dc->last_ier, dc->reg_ier);
  1059. }
  1060. }
  1061. if (read_iir & APP2_DL) {
  1062. if (receive_data(PORT_APP2, dc))
  1063. writew(APP2_DL, dc->reg_fcr);
  1064. }
  1065. if (read_iir & APP2_UL) {
  1066. dc->last_ier &= ~APP2_UL;
  1067. writew(dc->last_ier, dc->reg_ier);
  1068. if (send_data(PORT_APP2, dc)) {
  1069. writew(APP2_UL, dc->reg_fcr);
  1070. dc->last_ier = dc->last_ier | APP2_UL;
  1071. writew(dc->last_ier, dc->reg_ier);
  1072. }
  1073. }
  1074. exit_handler:
  1075. spin_unlock(&dc->spin_mutex);
  1076. for (a = 0; a < NOZOMI_MAX_PORTS; a++)
  1077. if (test_and_clear_bit(a, &dc->flip))
  1078. tty_flip_buffer_push(&dc->port[a].port);
  1079. return IRQ_HANDLED;
  1080. none:
  1081. spin_unlock(&dc->spin_mutex);
  1082. return IRQ_NONE;
  1083. }
  1084. static void nozomi_get_card_type(struct nozomi *dc)
  1085. {
  1086. int i;
  1087. u32 size = 0;
  1088. for (i = 0; i < 6; i++)
  1089. size += pci_resource_len(dc->pdev, i);
  1090. /* Assume card type F32_8 if no match */
  1091. dc->card_type = size == 2048 ? F32_2 : F32_8;
  1092. dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
  1093. }
  1094. static void nozomi_setup_private_data(struct nozomi *dc)
  1095. {
  1096. void __iomem *offset = dc->base_addr + dc->card_type / 2;
  1097. unsigned int i;
  1098. dc->reg_fcr = (void __iomem *)(offset + R_FCR);
  1099. dc->reg_iir = (void __iomem *)(offset + R_IIR);
  1100. dc->reg_ier = (void __iomem *)(offset + R_IER);
  1101. dc->last_ier = 0;
  1102. dc->flip = 0;
  1103. dc->port[PORT_MDM].token_dl = MDM_DL;
  1104. dc->port[PORT_DIAG].token_dl = DIAG_DL;
  1105. dc->port[PORT_APP1].token_dl = APP1_DL;
  1106. dc->port[PORT_APP2].token_dl = APP2_DL;
  1107. for (i = 0; i < MAX_PORT; i++)
  1108. init_waitqueue_head(&dc->port[i].tty_wait);
  1109. }
  1110. static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
  1111. char *buf)
  1112. {
  1113. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1114. return sprintf(buf, "%d\n", dc->card_type);
  1115. }
  1116. static DEVICE_ATTR_RO(card_type);
  1117. static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
  1118. char *buf)
  1119. {
  1120. const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
  1121. return sprintf(buf, "%u\n", dc->open_ttys);
  1122. }
  1123. static DEVICE_ATTR_RO(open_ttys);
  1124. static void make_sysfs_files(struct nozomi *dc)
  1125. {
  1126. if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
  1127. dev_err(&dc->pdev->dev,
  1128. "Could not create sysfs file for card_type\n");
  1129. if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
  1130. dev_err(&dc->pdev->dev,
  1131. "Could not create sysfs file for open_ttys\n");
  1132. }
  1133. static void remove_sysfs_files(struct nozomi *dc)
  1134. {
  1135. device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
  1136. device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
  1137. }
  1138. /* Allocate memory for one device */
  1139. static int nozomi_card_init(struct pci_dev *pdev,
  1140. const struct pci_device_id *ent)
  1141. {
  1142. resource_size_t start;
  1143. int ret;
  1144. struct nozomi *dc = NULL;
  1145. int ndev_idx;
  1146. int i;
  1147. dev_dbg(&pdev->dev, "Init, new card found\n");
  1148. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1149. if (!ndevs[ndev_idx])
  1150. break;
  1151. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1152. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1153. ret = -EIO;
  1154. goto err;
  1155. }
  1156. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1157. if (unlikely(!dc)) {
  1158. dev_err(&pdev->dev, "Could not allocate memory\n");
  1159. ret = -ENOMEM;
  1160. goto err_free;
  1161. }
  1162. dc->pdev = pdev;
  1163. ret = pci_enable_device(dc->pdev);
  1164. if (ret) {
  1165. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1166. goto err_free;
  1167. }
  1168. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1169. if (ret) {
  1170. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1171. (int) /* nozomi_private.io_addr */ 0);
  1172. goto err_disable_device;
  1173. }
  1174. start = pci_resource_start(dc->pdev, 0);
  1175. if (start == 0) {
  1176. dev_err(&pdev->dev, "No I/O address for card detected\n");
  1177. ret = -ENODEV;
  1178. goto err_rel_regs;
  1179. }
  1180. /* Find out what card type it is */
  1181. nozomi_get_card_type(dc);
  1182. dc->base_addr = ioremap_nocache(start, dc->card_type);
  1183. if (!dc->base_addr) {
  1184. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1185. ret = -ENODEV;
  1186. goto err_rel_regs;
  1187. }
  1188. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1189. if (!dc->send_buf) {
  1190. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1191. ret = -ENOMEM;
  1192. goto err_free_sbuf;
  1193. }
  1194. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1195. if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  1196. GFP_KERNEL)) {
  1197. dev_err(&pdev->dev,
  1198. "Could not allocate kfifo buffer\n");
  1199. ret = -ENOMEM;
  1200. goto err_free_kfifo;
  1201. }
  1202. }
  1203. spin_lock_init(&dc->spin_mutex);
  1204. nozomi_setup_private_data(dc);
  1205. /* Disable all interrupts */
  1206. dc->last_ier = 0;
  1207. writew(dc->last_ier, dc->reg_ier);
  1208. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1209. NOZOMI_NAME, dc);
  1210. if (unlikely(ret)) {
  1211. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1212. goto err_free_kfifo;
  1213. }
  1214. DBG1("base_addr: %p", dc->base_addr);
  1215. make_sysfs_files(dc);
  1216. dc->index_start = ndev_idx * MAX_PORT;
  1217. ndevs[ndev_idx] = dc;
  1218. pci_set_drvdata(pdev, dc);
  1219. /* Enable RESET interrupt */
  1220. dc->last_ier = RESET;
  1221. iowrite16(dc->last_ier, dc->reg_ier);
  1222. dc->state = NOZOMI_STATE_ENABLED;
  1223. for (i = 0; i < MAX_PORT; i++) {
  1224. struct device *tty_dev;
  1225. struct port *port = &dc->port[i];
  1226. port->dc = dc;
  1227. tty_port_init(&port->port);
  1228. port->port.ops = &noz_tty_port_ops;
  1229. tty_dev = tty_port_register_device(&port->port, ntty_driver,
  1230. dc->index_start + i, &pdev->dev);
  1231. if (IS_ERR(tty_dev)) {
  1232. ret = PTR_ERR(tty_dev);
  1233. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1234. tty_port_destroy(&port->port);
  1235. goto err_free_tty;
  1236. }
  1237. }
  1238. return 0;
  1239. err_free_tty:
  1240. for (i = 0; i < MAX_PORT; ++i) {
  1241. tty_unregister_device(ntty_driver, dc->index_start + i);
  1242. tty_port_destroy(&dc->port[i].port);
  1243. }
  1244. err_free_kfifo:
  1245. for (i = 0; i < MAX_PORT; i++)
  1246. kfifo_free(&dc->port[i].fifo_ul);
  1247. err_free_sbuf:
  1248. kfree(dc->send_buf);
  1249. iounmap(dc->base_addr);
  1250. err_rel_regs:
  1251. pci_release_regions(pdev);
  1252. err_disable_device:
  1253. pci_disable_device(pdev);
  1254. err_free:
  1255. kfree(dc);
  1256. err:
  1257. return ret;
  1258. }
  1259. static void tty_exit(struct nozomi *dc)
  1260. {
  1261. unsigned int i;
  1262. DBG1(" ");
  1263. for (i = 0; i < MAX_PORT; ++i)
  1264. tty_port_tty_hangup(&dc->port[i].port, false);
  1265. /* Racy below - surely should wait for scheduled work to be done or
  1266. complete off a hangup method ? */
  1267. while (dc->open_ttys)
  1268. msleep(1);
  1269. for (i = 0; i < MAX_PORT; ++i) {
  1270. tty_unregister_device(ntty_driver, dc->index_start + i);
  1271. tty_port_destroy(&dc->port[i].port);
  1272. }
  1273. }
  1274. /* Deallocate memory for one device */
  1275. static void nozomi_card_exit(struct pci_dev *pdev)
  1276. {
  1277. int i;
  1278. struct ctrl_ul ctrl;
  1279. struct nozomi *dc = pci_get_drvdata(pdev);
  1280. /* Disable all interrupts */
  1281. dc->last_ier = 0;
  1282. writew(dc->last_ier, dc->reg_ier);
  1283. tty_exit(dc);
  1284. /* Send 0x0001, command card to resend the reset token. */
  1285. /* This is to get the reset when the module is reloaded. */
  1286. ctrl.port = 0x00;
  1287. ctrl.reserved = 0;
  1288. ctrl.RTS = 0;
  1289. ctrl.DTR = 1;
  1290. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1291. /* Setup dc->reg addresses to we can use defines here */
  1292. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1293. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1294. remove_sysfs_files(dc);
  1295. free_irq(pdev->irq, dc);
  1296. for (i = 0; i < MAX_PORT; i++)
  1297. kfifo_free(&dc->port[i].fifo_ul);
  1298. kfree(dc->send_buf);
  1299. iounmap(dc->base_addr);
  1300. pci_release_regions(pdev);
  1301. pci_disable_device(pdev);
  1302. ndevs[dc->index_start / MAX_PORT] = NULL;
  1303. kfree(dc);
  1304. }
  1305. static void set_rts(const struct tty_struct *tty, int rts)
  1306. {
  1307. struct port *port = get_port_by_tty(tty);
  1308. port->ctrl_ul.RTS = rts;
  1309. port->update_flow_control = 1;
  1310. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1311. }
  1312. static void set_dtr(const struct tty_struct *tty, int dtr)
  1313. {
  1314. struct port *port = get_port_by_tty(tty);
  1315. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1316. port->ctrl_ul.DTR = dtr;
  1317. port->update_flow_control = 1;
  1318. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1319. }
  1320. /*
  1321. * ----------------------------------------------------------------------------
  1322. * TTY code
  1323. * ----------------------------------------------------------------------------
  1324. */
  1325. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1326. {
  1327. struct port *port = get_port_by_tty(tty);
  1328. struct nozomi *dc = get_dc_by_tty(tty);
  1329. int ret;
  1330. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1331. return -ENODEV;
  1332. ret = tty_standard_install(driver, tty);
  1333. if (ret == 0)
  1334. tty->driver_data = port;
  1335. return ret;
  1336. }
  1337. static void ntty_cleanup(struct tty_struct *tty)
  1338. {
  1339. tty->driver_data = NULL;
  1340. }
  1341. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1342. {
  1343. struct port *port = container_of(tport, struct port, port);
  1344. struct nozomi *dc = port->dc;
  1345. unsigned long flags;
  1346. DBG1("open: %d", port->token_dl);
  1347. spin_lock_irqsave(&dc->spin_mutex, flags);
  1348. dc->last_ier = dc->last_ier | port->token_dl;
  1349. writew(dc->last_ier, dc->reg_ier);
  1350. dc->open_ttys++;
  1351. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1352. printk("noz: activated %d: %p\n", tty->index, tport);
  1353. return 0;
  1354. }
  1355. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1356. {
  1357. struct port *port = tty->driver_data;
  1358. return tty_port_open(&port->port, tty, filp);
  1359. }
  1360. static void ntty_shutdown(struct tty_port *tport)
  1361. {
  1362. struct port *port = container_of(tport, struct port, port);
  1363. struct nozomi *dc = port->dc;
  1364. unsigned long flags;
  1365. DBG1("close: %d", port->token_dl);
  1366. spin_lock_irqsave(&dc->spin_mutex, flags);
  1367. dc->last_ier &= ~(port->token_dl);
  1368. writew(dc->last_ier, dc->reg_ier);
  1369. dc->open_ttys--;
  1370. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1371. printk("noz: shutdown %p\n", tport);
  1372. }
  1373. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1374. {
  1375. struct port *port = tty->driver_data;
  1376. if (port)
  1377. tty_port_close(&port->port, tty, filp);
  1378. }
  1379. static void ntty_hangup(struct tty_struct *tty)
  1380. {
  1381. struct port *port = tty->driver_data;
  1382. tty_port_hangup(&port->port);
  1383. }
  1384. /*
  1385. * called when the userspace process writes to the tty (/dev/noz*).
  1386. * Data is inserted into a fifo, which is then read and transferred to the modem.
  1387. */
  1388. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1389. int count)
  1390. {
  1391. int rval = -EINVAL;
  1392. struct nozomi *dc = get_dc_by_tty(tty);
  1393. struct port *port = tty->driver_data;
  1394. unsigned long flags;
  1395. /* DBG1( "WRITEx: %d, index = %d", count, index); */
  1396. if (!dc || !port)
  1397. return -ENODEV;
  1398. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1399. spin_lock_irqsave(&dc->spin_mutex, flags);
  1400. /* CTS is only valid on the modem channel */
  1401. if (port == &(dc->port[PORT_MDM])) {
  1402. if (port->ctrl_dl.CTS) {
  1403. DBG4("Enable interrupt");
  1404. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1405. } else {
  1406. dev_err(&dc->pdev->dev,
  1407. "CTS not active on modem port?\n");
  1408. }
  1409. } else {
  1410. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1411. }
  1412. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1413. return rval;
  1414. }
  1415. /*
  1416. * Calculate how much is left in device
  1417. * This method is called by the upper tty layer.
  1418. * #according to sources N_TTY.c it expects a value >= 0 and
  1419. * does not check for negative values.
  1420. *
  1421. * If the port is unplugged report lots of room and let the bits
  1422. * dribble away so we don't block anything.
  1423. */
  1424. static int ntty_write_room(struct tty_struct *tty)
  1425. {
  1426. struct port *port = tty->driver_data;
  1427. int room = 4096;
  1428. const struct nozomi *dc = get_dc_by_tty(tty);
  1429. if (dc)
  1430. room = kfifo_avail(&port->fifo_ul);
  1431. return room;
  1432. }
  1433. /* Gets io control parameters */
  1434. static int ntty_tiocmget(struct tty_struct *tty)
  1435. {
  1436. const struct port *port = tty->driver_data;
  1437. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1438. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1439. /* Note: these could change under us but it is not clear this
  1440. matters if so */
  1441. return (ctrl_ul->RTS ? TIOCM_RTS : 0)
  1442. | (ctrl_ul->DTR ? TIOCM_DTR : 0)
  1443. | (ctrl_dl->DCD ? TIOCM_CAR : 0)
  1444. | (ctrl_dl->RI ? TIOCM_RNG : 0)
  1445. | (ctrl_dl->DSR ? TIOCM_DSR : 0)
  1446. | (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1447. }
  1448. /* Sets io controls parameters */
  1449. static int ntty_tiocmset(struct tty_struct *tty,
  1450. unsigned int set, unsigned int clear)
  1451. {
  1452. struct nozomi *dc = get_dc_by_tty(tty);
  1453. unsigned long flags;
  1454. spin_lock_irqsave(&dc->spin_mutex, flags);
  1455. if (set & TIOCM_RTS)
  1456. set_rts(tty, 1);
  1457. else if (clear & TIOCM_RTS)
  1458. set_rts(tty, 0);
  1459. if (set & TIOCM_DTR)
  1460. set_dtr(tty, 1);
  1461. else if (clear & TIOCM_DTR)
  1462. set_dtr(tty, 0);
  1463. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1464. return 0;
  1465. }
  1466. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1467. struct async_icount *cprev)
  1468. {
  1469. const struct async_icount cnow = port->tty_icount;
  1470. int ret;
  1471. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng))
  1472. || ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr))
  1473. || ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd))
  1474. || ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1475. *cprev = cnow;
  1476. return ret;
  1477. }
  1478. static int ntty_tiocgicount(struct tty_struct *tty,
  1479. struct serial_icounter_struct *icount)
  1480. {
  1481. struct port *port = tty->driver_data;
  1482. const struct async_icount cnow = port->tty_icount;
  1483. icount->cts = cnow.cts;
  1484. icount->dsr = cnow.dsr;
  1485. icount->rng = cnow.rng;
  1486. icount->dcd = cnow.dcd;
  1487. icount->rx = cnow.rx;
  1488. icount->tx = cnow.tx;
  1489. icount->frame = cnow.frame;
  1490. icount->overrun = cnow.overrun;
  1491. icount->parity = cnow.parity;
  1492. icount->brk = cnow.brk;
  1493. icount->buf_overrun = cnow.buf_overrun;
  1494. return 0;
  1495. }
  1496. static int ntty_ioctl(struct tty_struct *tty,
  1497. unsigned int cmd, unsigned long arg)
  1498. {
  1499. struct port *port = tty->driver_data;
  1500. int rval = -ENOIOCTLCMD;
  1501. DBG1("******** IOCTL, cmd: %d", cmd);
  1502. switch (cmd) {
  1503. case TIOCMIWAIT: {
  1504. struct async_icount cprev = port->tty_icount;
  1505. rval = wait_event_interruptible(port->tty_wait,
  1506. ntty_cflags_changed(port, arg, &cprev));
  1507. break;
  1508. }
  1509. default:
  1510. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1511. break;
  1512. }
  1513. return rval;
  1514. }
  1515. /*
  1516. * Called by the upper tty layer when tty buffers are ready
  1517. * to receive data again after a call to throttle.
  1518. */
  1519. static void ntty_unthrottle(struct tty_struct *tty)
  1520. {
  1521. struct nozomi *dc = get_dc_by_tty(tty);
  1522. unsigned long flags;
  1523. DBG1("UNTHROTTLE");
  1524. spin_lock_irqsave(&dc->spin_mutex, flags);
  1525. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1526. set_rts(tty, 1);
  1527. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1528. }
  1529. /*
  1530. * Called by the upper tty layer when the tty buffers are almost full.
  1531. * The driver should stop send more data.
  1532. */
  1533. static void ntty_throttle(struct tty_struct *tty)
  1534. {
  1535. struct nozomi *dc = get_dc_by_tty(tty);
  1536. unsigned long flags;
  1537. DBG1("THROTTLE");
  1538. spin_lock_irqsave(&dc->spin_mutex, flags);
  1539. set_rts(tty, 0);
  1540. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1541. }
  1542. /* Returns number of chars in buffer, called by tty layer */
  1543. static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  1544. {
  1545. struct port *port = tty->driver_data;
  1546. struct nozomi *dc = get_dc_by_tty(tty);
  1547. s32 rval = 0;
  1548. if (unlikely(!dc || !port)) {
  1549. goto exit_in_buffer;
  1550. }
  1551. rval = kfifo_len(&port->fifo_ul);
  1552. exit_in_buffer:
  1553. return rval;
  1554. }
  1555. static const struct tty_port_operations noz_tty_port_ops = {
  1556. .activate = ntty_activate,
  1557. .shutdown = ntty_shutdown,
  1558. };
  1559. static const struct tty_operations tty_ops = {
  1560. .ioctl = ntty_ioctl,
  1561. .open = ntty_open,
  1562. .close = ntty_close,
  1563. .hangup = ntty_hangup,
  1564. .write = ntty_write,
  1565. .write_room = ntty_write_room,
  1566. .unthrottle = ntty_unthrottle,
  1567. .throttle = ntty_throttle,
  1568. .chars_in_buffer = ntty_chars_in_buffer,
  1569. .tiocmget = ntty_tiocmget,
  1570. .tiocmset = ntty_tiocmset,
  1571. .get_icount = ntty_tiocgicount,
  1572. .install = ntty_install,
  1573. .cleanup = ntty_cleanup,
  1574. };
  1575. /* Module initialization */
  1576. static struct pci_driver nozomi_driver = {
  1577. .name = NOZOMI_NAME,
  1578. .id_table = nozomi_pci_tbl,
  1579. .probe = nozomi_card_init,
  1580. .remove = nozomi_card_exit,
  1581. };
  1582. static __init int nozomi_init(void)
  1583. {
  1584. int ret;
  1585. printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
  1586. ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  1587. if (!ntty_driver)
  1588. return -ENOMEM;
  1589. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1590. ntty_driver->name = "noz";
  1591. ntty_driver->major = 0;
  1592. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1593. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1594. ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1595. ntty_driver->init_termios = tty_std_termios;
  1596. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1597. HUPCL | CLOCAL;
  1598. ntty_driver->init_termios.c_ispeed = 115200;
  1599. ntty_driver->init_termios.c_ospeed = 115200;
  1600. tty_set_operations(ntty_driver, &tty_ops);
  1601. ret = tty_register_driver(ntty_driver);
  1602. if (ret) {
  1603. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1604. goto free_tty;
  1605. }
  1606. ret = pci_register_driver(&nozomi_driver);
  1607. if (ret) {
  1608. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1609. goto unr_tty;
  1610. }
  1611. return 0;
  1612. unr_tty:
  1613. tty_unregister_driver(ntty_driver);
  1614. free_tty:
  1615. put_tty_driver(ntty_driver);
  1616. return ret;
  1617. }
  1618. static __exit void nozomi_exit(void)
  1619. {
  1620. printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
  1621. pci_unregister_driver(&nozomi_driver);
  1622. tty_unregister_driver(ntty_driver);
  1623. put_tty_driver(ntty_driver);
  1624. }
  1625. module_init(nozomi_init);
  1626. module_exit(nozomi_exit);
  1627. MODULE_LICENSE("Dual BSD/GPL");
  1628. MODULE_DESCRIPTION(DRIVER_DESC);