nozomi.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909
  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 = dev_get_drvdata(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 = dev_get_drvdata(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. int ret;
  1143. struct nozomi *dc = NULL;
  1144. int ndev_idx;
  1145. int i;
  1146. dev_dbg(&pdev->dev, "Init, new card found\n");
  1147. for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
  1148. if (!ndevs[ndev_idx])
  1149. break;
  1150. if (ndev_idx >= ARRAY_SIZE(ndevs)) {
  1151. dev_err(&pdev->dev, "no free tty range for this card left\n");
  1152. ret = -EIO;
  1153. goto err;
  1154. }
  1155. dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
  1156. if (unlikely(!dc)) {
  1157. dev_err(&pdev->dev, "Could not allocate memory\n");
  1158. ret = -ENOMEM;
  1159. goto err_free;
  1160. }
  1161. dc->pdev = pdev;
  1162. ret = pci_enable_device(dc->pdev);
  1163. if (ret) {
  1164. dev_err(&pdev->dev, "Failed to enable PCI Device\n");
  1165. goto err_free;
  1166. }
  1167. ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
  1168. if (ret) {
  1169. dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
  1170. (int) /* nozomi_private.io_addr */ 0);
  1171. goto err_disable_device;
  1172. }
  1173. /* Find out what card type it is */
  1174. nozomi_get_card_type(dc);
  1175. dc->base_addr = pci_iomap(dc->pdev, 0, dc->card_type);
  1176. if (!dc->base_addr) {
  1177. dev_err(&pdev->dev, "Unable to map card MMIO\n");
  1178. ret = -ENODEV;
  1179. goto err_rel_regs;
  1180. }
  1181. dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
  1182. if (!dc->send_buf) {
  1183. dev_err(&pdev->dev, "Could not allocate send buffer?\n");
  1184. ret = -ENOMEM;
  1185. goto err_free_sbuf;
  1186. }
  1187. for (i = PORT_MDM; i < MAX_PORT; i++) {
  1188. if (kfifo_alloc(&dc->port[i].fifo_ul, FIFO_BUFFER_SIZE_UL,
  1189. GFP_KERNEL)) {
  1190. dev_err(&pdev->dev,
  1191. "Could not allocate kfifo buffer\n");
  1192. ret = -ENOMEM;
  1193. goto err_free_kfifo;
  1194. }
  1195. }
  1196. spin_lock_init(&dc->spin_mutex);
  1197. nozomi_setup_private_data(dc);
  1198. /* Disable all interrupts */
  1199. dc->last_ier = 0;
  1200. writew(dc->last_ier, dc->reg_ier);
  1201. ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
  1202. NOZOMI_NAME, dc);
  1203. if (unlikely(ret)) {
  1204. dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
  1205. goto err_free_all_kfifo;
  1206. }
  1207. DBG1("base_addr: %p", dc->base_addr);
  1208. make_sysfs_files(dc);
  1209. dc->index_start = ndev_idx * MAX_PORT;
  1210. ndevs[ndev_idx] = dc;
  1211. pci_set_drvdata(pdev, dc);
  1212. /* Enable RESET interrupt */
  1213. dc->last_ier = RESET;
  1214. iowrite16(dc->last_ier, dc->reg_ier);
  1215. dc->state = NOZOMI_STATE_ENABLED;
  1216. for (i = 0; i < MAX_PORT; i++) {
  1217. struct device *tty_dev;
  1218. struct port *port = &dc->port[i];
  1219. port->dc = dc;
  1220. tty_port_init(&port->port);
  1221. port->port.ops = &noz_tty_port_ops;
  1222. tty_dev = tty_port_register_device(&port->port, ntty_driver,
  1223. dc->index_start + i, &pdev->dev);
  1224. if (IS_ERR(tty_dev)) {
  1225. ret = PTR_ERR(tty_dev);
  1226. dev_err(&pdev->dev, "Could not allocate tty?\n");
  1227. tty_port_destroy(&port->port);
  1228. goto err_free_tty;
  1229. }
  1230. }
  1231. return 0;
  1232. err_free_tty:
  1233. for (i--; i >= 0; i--) {
  1234. tty_unregister_device(ntty_driver, dc->index_start + i);
  1235. tty_port_destroy(&dc->port[i].port);
  1236. }
  1237. free_irq(pdev->irq, dc);
  1238. err_free_all_kfifo:
  1239. i = MAX_PORT;
  1240. err_free_kfifo:
  1241. for (i--; i >= PORT_MDM; i--)
  1242. kfifo_free(&dc->port[i].fifo_ul);
  1243. err_free_sbuf:
  1244. kfree(dc->send_buf);
  1245. iounmap(dc->base_addr);
  1246. err_rel_regs:
  1247. pci_release_regions(pdev);
  1248. err_disable_device:
  1249. pci_disable_device(pdev);
  1250. err_free:
  1251. kfree(dc);
  1252. err:
  1253. return ret;
  1254. }
  1255. static void tty_exit(struct nozomi *dc)
  1256. {
  1257. unsigned int i;
  1258. DBG1(" ");
  1259. for (i = 0; i < MAX_PORT; ++i)
  1260. tty_port_tty_hangup(&dc->port[i].port, false);
  1261. /* Racy below - surely should wait for scheduled work to be done or
  1262. complete off a hangup method ? */
  1263. while (dc->open_ttys)
  1264. msleep(1);
  1265. for (i = 0; i < MAX_PORT; ++i) {
  1266. tty_unregister_device(ntty_driver, dc->index_start + i);
  1267. tty_port_destroy(&dc->port[i].port);
  1268. }
  1269. }
  1270. /* Deallocate memory for one device */
  1271. static void nozomi_card_exit(struct pci_dev *pdev)
  1272. {
  1273. int i;
  1274. struct ctrl_ul ctrl;
  1275. struct nozomi *dc = pci_get_drvdata(pdev);
  1276. /* Disable all interrupts */
  1277. dc->last_ier = 0;
  1278. writew(dc->last_ier, dc->reg_ier);
  1279. tty_exit(dc);
  1280. /* Send 0x0001, command card to resend the reset token. */
  1281. /* This is to get the reset when the module is reloaded. */
  1282. ctrl.port = 0x00;
  1283. ctrl.reserved = 0;
  1284. ctrl.RTS = 0;
  1285. ctrl.DTR = 1;
  1286. DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
  1287. /* Setup dc->reg addresses to we can use defines here */
  1288. write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
  1289. writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
  1290. remove_sysfs_files(dc);
  1291. free_irq(pdev->irq, dc);
  1292. for (i = 0; i < MAX_PORT; i++)
  1293. kfifo_free(&dc->port[i].fifo_ul);
  1294. kfree(dc->send_buf);
  1295. iounmap(dc->base_addr);
  1296. pci_release_regions(pdev);
  1297. pci_disable_device(pdev);
  1298. ndevs[dc->index_start / MAX_PORT] = NULL;
  1299. kfree(dc);
  1300. }
  1301. static void set_rts(const struct tty_struct *tty, int rts)
  1302. {
  1303. struct port *port = get_port_by_tty(tty);
  1304. port->ctrl_ul.RTS = rts;
  1305. port->update_flow_control = 1;
  1306. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1307. }
  1308. static void set_dtr(const struct tty_struct *tty, int dtr)
  1309. {
  1310. struct port *port = get_port_by_tty(tty);
  1311. DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
  1312. port->ctrl_ul.DTR = dtr;
  1313. port->update_flow_control = 1;
  1314. enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
  1315. }
  1316. /*
  1317. * ----------------------------------------------------------------------------
  1318. * TTY code
  1319. * ----------------------------------------------------------------------------
  1320. */
  1321. static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
  1322. {
  1323. struct port *port = get_port_by_tty(tty);
  1324. struct nozomi *dc = get_dc_by_tty(tty);
  1325. int ret;
  1326. if (!port || !dc || dc->state != NOZOMI_STATE_READY)
  1327. return -ENODEV;
  1328. ret = tty_standard_install(driver, tty);
  1329. if (ret == 0)
  1330. tty->driver_data = port;
  1331. return ret;
  1332. }
  1333. static void ntty_cleanup(struct tty_struct *tty)
  1334. {
  1335. tty->driver_data = NULL;
  1336. }
  1337. static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
  1338. {
  1339. struct port *port = container_of(tport, struct port, port);
  1340. struct nozomi *dc = port->dc;
  1341. unsigned long flags;
  1342. DBG1("open: %d", port->token_dl);
  1343. spin_lock_irqsave(&dc->spin_mutex, flags);
  1344. dc->last_ier = dc->last_ier | port->token_dl;
  1345. writew(dc->last_ier, dc->reg_ier);
  1346. dc->open_ttys++;
  1347. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1348. printk("noz: activated %d: %p\n", tty->index, tport);
  1349. return 0;
  1350. }
  1351. static int ntty_open(struct tty_struct *tty, struct file *filp)
  1352. {
  1353. struct port *port = tty->driver_data;
  1354. return tty_port_open(&port->port, tty, filp);
  1355. }
  1356. static void ntty_shutdown(struct tty_port *tport)
  1357. {
  1358. struct port *port = container_of(tport, struct port, port);
  1359. struct nozomi *dc = port->dc;
  1360. unsigned long flags;
  1361. DBG1("close: %d", port->token_dl);
  1362. spin_lock_irqsave(&dc->spin_mutex, flags);
  1363. dc->last_ier &= ~(port->token_dl);
  1364. writew(dc->last_ier, dc->reg_ier);
  1365. dc->open_ttys--;
  1366. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1367. printk("noz: shutdown %p\n", tport);
  1368. }
  1369. static void ntty_close(struct tty_struct *tty, struct file *filp)
  1370. {
  1371. struct port *port = tty->driver_data;
  1372. if (port)
  1373. tty_port_close(&port->port, tty, filp);
  1374. }
  1375. static void ntty_hangup(struct tty_struct *tty)
  1376. {
  1377. struct port *port = tty->driver_data;
  1378. tty_port_hangup(&port->port);
  1379. }
  1380. /*
  1381. * called when the userspace process writes to the tty (/dev/noz*).
  1382. * Data is inserted into a fifo, which is then read and transferred to the modem.
  1383. */
  1384. static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
  1385. int count)
  1386. {
  1387. int rval = -EINVAL;
  1388. struct nozomi *dc = get_dc_by_tty(tty);
  1389. struct port *port = tty->driver_data;
  1390. unsigned long flags;
  1391. /* DBG1( "WRITEx: %d, index = %d", count, index); */
  1392. if (!dc || !port)
  1393. return -ENODEV;
  1394. rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
  1395. spin_lock_irqsave(&dc->spin_mutex, flags);
  1396. /* CTS is only valid on the modem channel */
  1397. if (port == &(dc->port[PORT_MDM])) {
  1398. if (port->ctrl_dl.CTS) {
  1399. DBG4("Enable interrupt");
  1400. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1401. } else {
  1402. dev_err(&dc->pdev->dev,
  1403. "CTS not active on modem port?\n");
  1404. }
  1405. } else {
  1406. enable_transmit_ul(tty->index % MAX_PORT, dc);
  1407. }
  1408. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1409. return rval;
  1410. }
  1411. /*
  1412. * Calculate how much is left in device
  1413. * This method is called by the upper tty layer.
  1414. * #according to sources N_TTY.c it expects a value >= 0 and
  1415. * does not check for negative values.
  1416. *
  1417. * If the port is unplugged report lots of room and let the bits
  1418. * dribble away so we don't block anything.
  1419. */
  1420. static int ntty_write_room(struct tty_struct *tty)
  1421. {
  1422. struct port *port = tty->driver_data;
  1423. int room = 4096;
  1424. const struct nozomi *dc = get_dc_by_tty(tty);
  1425. if (dc)
  1426. room = kfifo_avail(&port->fifo_ul);
  1427. return room;
  1428. }
  1429. /* Gets io control parameters */
  1430. static int ntty_tiocmget(struct tty_struct *tty)
  1431. {
  1432. const struct port *port = tty->driver_data;
  1433. const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
  1434. const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
  1435. /* Note: these could change under us but it is not clear this
  1436. matters if so */
  1437. return (ctrl_ul->RTS ? TIOCM_RTS : 0)
  1438. | (ctrl_ul->DTR ? TIOCM_DTR : 0)
  1439. | (ctrl_dl->DCD ? TIOCM_CAR : 0)
  1440. | (ctrl_dl->RI ? TIOCM_RNG : 0)
  1441. | (ctrl_dl->DSR ? TIOCM_DSR : 0)
  1442. | (ctrl_dl->CTS ? TIOCM_CTS : 0);
  1443. }
  1444. /* Sets io controls parameters */
  1445. static int ntty_tiocmset(struct tty_struct *tty,
  1446. unsigned int set, unsigned int clear)
  1447. {
  1448. struct nozomi *dc = get_dc_by_tty(tty);
  1449. unsigned long flags;
  1450. spin_lock_irqsave(&dc->spin_mutex, flags);
  1451. if (set & TIOCM_RTS)
  1452. set_rts(tty, 1);
  1453. else if (clear & TIOCM_RTS)
  1454. set_rts(tty, 0);
  1455. if (set & TIOCM_DTR)
  1456. set_dtr(tty, 1);
  1457. else if (clear & TIOCM_DTR)
  1458. set_dtr(tty, 0);
  1459. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1460. return 0;
  1461. }
  1462. static int ntty_cflags_changed(struct port *port, unsigned long flags,
  1463. struct async_icount *cprev)
  1464. {
  1465. const struct async_icount cnow = port->tty_icount;
  1466. int ret;
  1467. ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng))
  1468. || ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr))
  1469. || ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd))
  1470. || ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
  1471. *cprev = cnow;
  1472. return ret;
  1473. }
  1474. static int ntty_tiocgicount(struct tty_struct *tty,
  1475. struct serial_icounter_struct *icount)
  1476. {
  1477. struct port *port = tty->driver_data;
  1478. const struct async_icount cnow = port->tty_icount;
  1479. icount->cts = cnow.cts;
  1480. icount->dsr = cnow.dsr;
  1481. icount->rng = cnow.rng;
  1482. icount->dcd = cnow.dcd;
  1483. icount->rx = cnow.rx;
  1484. icount->tx = cnow.tx;
  1485. icount->frame = cnow.frame;
  1486. icount->overrun = cnow.overrun;
  1487. icount->parity = cnow.parity;
  1488. icount->brk = cnow.brk;
  1489. icount->buf_overrun = cnow.buf_overrun;
  1490. return 0;
  1491. }
  1492. static int ntty_ioctl(struct tty_struct *tty,
  1493. unsigned int cmd, unsigned long arg)
  1494. {
  1495. struct port *port = tty->driver_data;
  1496. int rval = -ENOIOCTLCMD;
  1497. DBG1("******** IOCTL, cmd: %d", cmd);
  1498. switch (cmd) {
  1499. case TIOCMIWAIT: {
  1500. struct async_icount cprev = port->tty_icount;
  1501. rval = wait_event_interruptible(port->tty_wait,
  1502. ntty_cflags_changed(port, arg, &cprev));
  1503. break;
  1504. }
  1505. default:
  1506. DBG1("ERR: 0x%08X, %d", cmd, cmd);
  1507. break;
  1508. }
  1509. return rval;
  1510. }
  1511. /*
  1512. * Called by the upper tty layer when tty buffers are ready
  1513. * to receive data again after a call to throttle.
  1514. */
  1515. static void ntty_unthrottle(struct tty_struct *tty)
  1516. {
  1517. struct nozomi *dc = get_dc_by_tty(tty);
  1518. unsigned long flags;
  1519. DBG1("UNTHROTTLE");
  1520. spin_lock_irqsave(&dc->spin_mutex, flags);
  1521. enable_transmit_dl(tty->index % MAX_PORT, dc);
  1522. set_rts(tty, 1);
  1523. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1524. }
  1525. /*
  1526. * Called by the upper tty layer when the tty buffers are almost full.
  1527. * The driver should stop send more data.
  1528. */
  1529. static void ntty_throttle(struct tty_struct *tty)
  1530. {
  1531. struct nozomi *dc = get_dc_by_tty(tty);
  1532. unsigned long flags;
  1533. DBG1("THROTTLE");
  1534. spin_lock_irqsave(&dc->spin_mutex, flags);
  1535. set_rts(tty, 0);
  1536. spin_unlock_irqrestore(&dc->spin_mutex, flags);
  1537. }
  1538. /* Returns number of chars in buffer, called by tty layer */
  1539. static s32 ntty_chars_in_buffer(struct tty_struct *tty)
  1540. {
  1541. struct port *port = tty->driver_data;
  1542. struct nozomi *dc = get_dc_by_tty(tty);
  1543. s32 rval = 0;
  1544. if (unlikely(!dc || !port)) {
  1545. goto exit_in_buffer;
  1546. }
  1547. rval = kfifo_len(&port->fifo_ul);
  1548. exit_in_buffer:
  1549. return rval;
  1550. }
  1551. static const struct tty_port_operations noz_tty_port_ops = {
  1552. .activate = ntty_activate,
  1553. .shutdown = ntty_shutdown,
  1554. };
  1555. static const struct tty_operations tty_ops = {
  1556. .ioctl = ntty_ioctl,
  1557. .open = ntty_open,
  1558. .close = ntty_close,
  1559. .hangup = ntty_hangup,
  1560. .write = ntty_write,
  1561. .write_room = ntty_write_room,
  1562. .unthrottle = ntty_unthrottle,
  1563. .throttle = ntty_throttle,
  1564. .chars_in_buffer = ntty_chars_in_buffer,
  1565. .tiocmget = ntty_tiocmget,
  1566. .tiocmset = ntty_tiocmset,
  1567. .get_icount = ntty_tiocgicount,
  1568. .install = ntty_install,
  1569. .cleanup = ntty_cleanup,
  1570. };
  1571. /* Module initialization */
  1572. static struct pci_driver nozomi_driver = {
  1573. .name = NOZOMI_NAME,
  1574. .id_table = nozomi_pci_tbl,
  1575. .probe = nozomi_card_init,
  1576. .remove = nozomi_card_exit,
  1577. };
  1578. static __init int nozomi_init(void)
  1579. {
  1580. int ret;
  1581. printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
  1582. ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
  1583. if (!ntty_driver)
  1584. return -ENOMEM;
  1585. ntty_driver->driver_name = NOZOMI_NAME_TTY;
  1586. ntty_driver->name = "noz";
  1587. ntty_driver->major = 0;
  1588. ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1589. ntty_driver->subtype = SERIAL_TYPE_NORMAL;
  1590. ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1591. ntty_driver->init_termios = tty_std_termios;
  1592. ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
  1593. HUPCL | CLOCAL;
  1594. ntty_driver->init_termios.c_ispeed = 115200;
  1595. ntty_driver->init_termios.c_ospeed = 115200;
  1596. tty_set_operations(ntty_driver, &tty_ops);
  1597. ret = tty_register_driver(ntty_driver);
  1598. if (ret) {
  1599. printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
  1600. goto free_tty;
  1601. }
  1602. ret = pci_register_driver(&nozomi_driver);
  1603. if (ret) {
  1604. printk(KERN_ERR "Nozomi: can't register pci driver\n");
  1605. goto unr_tty;
  1606. }
  1607. return 0;
  1608. unr_tty:
  1609. tty_unregister_driver(ntty_driver);
  1610. free_tty:
  1611. put_tty_driver(ntty_driver);
  1612. return ret;
  1613. }
  1614. static __exit void nozomi_exit(void)
  1615. {
  1616. printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
  1617. pci_unregister_driver(&nozomi_driver);
  1618. tty_unregister_driver(ntty_driver);
  1619. put_tty_driver(ntty_driver);
  1620. }
  1621. module_init(nozomi_init);
  1622. module_exit(nozomi_exit);
  1623. MODULE_LICENSE("Dual BSD/GPL");
  1624. MODULE_DESCRIPTION(DRIVER_DESC);