nozomi.c 49 KB

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