mos7840.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Clean ups from Moschip version and a few ioctl implementations by:
  17. * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/serial.h>
  35. #include <asm/uaccess.h>
  36. /*
  37. * Version Information
  38. */
  39. #define DRIVER_VERSION "1.3.1"
  40. #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  41. /*
  42. * 16C50 UART register defines
  43. */
  44. #define LCR_BITS_5 0x00 /* 5 bits/char */
  45. #define LCR_BITS_6 0x01 /* 6 bits/char */
  46. #define LCR_BITS_7 0x02 /* 7 bits/char */
  47. #define LCR_BITS_8 0x03 /* 8 bits/char */
  48. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  49. #define LCR_STOP_1 0x00 /* 1 stop bit */
  50. #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
  51. #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
  52. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  53. #define LCR_PAR_NONE 0x00 /* No parity */
  54. #define LCR_PAR_ODD 0x08 /* Odd parity */
  55. #define LCR_PAR_EVEN 0x18 /* Even parity */
  56. #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
  57. #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
  58. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  59. #define LCR_SET_BREAK 0x40 /* Set Break condition */
  60. #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
  61. #define MCR_DTR 0x01 /* Assert DTR */
  62. #define MCR_RTS 0x02 /* Assert RTS */
  63. #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
  64. #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
  65. #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
  66. #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
  67. #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
  68. #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
  69. #define MOS7840_MSR_RI 0x40 /* Current state of RI */
  70. #define MOS7840_MSR_CD 0x80 /* Current state of CD */
  71. /*
  72. * Defines used for sending commands to port
  73. */
  74. #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
  75. #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
  76. #define MOS_PORT1 0x0200
  77. #define MOS_PORT2 0x0300
  78. #define MOS_VENREG 0x0000
  79. #define MOS_MAX_PORT 0x02
  80. #define MOS_WRITE 0x0E
  81. #define MOS_READ 0x0D
  82. /* Requests */
  83. #define MCS_RD_RTYPE 0xC0
  84. #define MCS_WR_RTYPE 0x40
  85. #define MCS_RDREQ 0x0D
  86. #define MCS_WRREQ 0x0E
  87. #define MCS_CTRL_TIMEOUT 500
  88. #define VENDOR_READ_LENGTH (0x01)
  89. #define MAX_NAME_LEN 64
  90. #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
  91. #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
  92. /* For higher baud Rates use TIOCEXBAUD */
  93. #define TIOCEXBAUD 0x5462
  94. /* vendor id and device id defines */
  95. /* The native mos7840/7820 component */
  96. #define USB_VENDOR_ID_MOSCHIP 0x9710
  97. #define MOSCHIP_DEVICE_ID_7840 0x7840
  98. #define MOSCHIP_DEVICE_ID_7820 0x7820
  99. /* The native component can have its vendor/device id's overridden
  100. * in vendor-specific implementations. Such devices can be handled
  101. * by making a change here, in moschip_port_id_table, and in
  102. * moschip_id_table_combined
  103. */
  104. #define USB_VENDOR_ID_BANDB 0x0856
  105. #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
  106. #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
  107. /* Interrupt Routine Defines */
  108. #define SERIAL_IIR_RLS 0x06
  109. #define SERIAL_IIR_MS 0x00
  110. /*
  111. * Emulation of the bit mask on the LINE STATUS REGISTER.
  112. */
  113. #define SERIAL_LSR_DR 0x0001
  114. #define SERIAL_LSR_OE 0x0002
  115. #define SERIAL_LSR_PE 0x0004
  116. #define SERIAL_LSR_FE 0x0008
  117. #define SERIAL_LSR_BI 0x0010
  118. #define MOS_MSR_DELTA_CTS 0x10
  119. #define MOS_MSR_DELTA_DSR 0x20
  120. #define MOS_MSR_DELTA_RI 0x40
  121. #define MOS_MSR_DELTA_CD 0x80
  122. // Serial Port register Address
  123. #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
  124. #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
  125. #define LINE_CONTROL_REGISTER ((__u16)(0x03))
  126. #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
  127. #define LINE_STATUS_REGISTER ((__u16)(0x05))
  128. #define MODEM_STATUS_REGISTER ((__u16)(0x06))
  129. #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
  130. #define DIVISOR_LATCH_LSB ((__u16)(0x00))
  131. #define DIVISOR_LATCH_MSB ((__u16)(0x01))
  132. #define CLK_MULTI_REGISTER ((__u16)(0x02))
  133. #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
  134. #define SERIAL_LCR_DLAB ((__u16)(0x0080))
  135. /*
  136. * URB POOL related defines
  137. */
  138. #define NUM_URBS 16 /* URB Count */
  139. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  140. static struct usb_device_id moschip_port_id_table[] = {
  141. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  142. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  143. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
  144. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
  145. {} /* terminating entry */
  146. };
  147. static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
  148. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
  149. {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
  150. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
  151. {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
  152. {} /* terminating entry */
  153. };
  154. MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
  155. /* This structure holds all of the local port information */
  156. struct moschip_port {
  157. int port_num; /*Actual port number in the device(1,2,etc) */
  158. struct urb *write_urb; /* write URB for this port */
  159. struct urb *read_urb; /* read URB for this port */
  160. struct urb *int_urb;
  161. __u8 shadowLCR; /* last LCR value received */
  162. __u8 shadowMCR; /* last MCR value received */
  163. char open;
  164. char open_ports;
  165. char zombie;
  166. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  167. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  168. int delta_msr_cond;
  169. struct async_icount icount;
  170. struct usb_serial_port *port; /* loop back to the owner of this object */
  171. /*Offsets */
  172. __u8 SpRegOffset;
  173. __u8 ControlRegOffset;
  174. __u8 DcrRegOffset;
  175. //for processing control URBS in interrupt context
  176. struct urb *control_urb;
  177. struct usb_ctrlrequest *dr;
  178. char *ctrl_buf;
  179. int MsrLsr;
  180. spinlock_t pool_lock;
  181. struct urb *write_urb_pool[NUM_URBS];
  182. char busy[NUM_URBS];
  183. };
  184. static int debug;
  185. /*
  186. * mos7840_set_reg_sync
  187. * To set the Control register by calling usb_fill_control_urb function
  188. * by passing usb_sndctrlpipe function as parameter.
  189. */
  190. static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
  191. __u16 val)
  192. {
  193. struct usb_device *dev = port->serial->dev;
  194. val = val & 0x00ff;
  195. dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
  196. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  197. MCS_WR_RTYPE, val, reg, NULL, 0,
  198. MOS_WDR_TIMEOUT);
  199. }
  200. /*
  201. * mos7840_get_reg_sync
  202. * To set the Uart register by calling usb_fill_control_urb function by
  203. * passing usb_rcvctrlpipe function as parameter.
  204. */
  205. static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
  206. __u16 * val)
  207. {
  208. struct usb_device *dev = port->serial->dev;
  209. int ret = 0;
  210. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  211. MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
  212. MOS_WDR_TIMEOUT);
  213. dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
  214. *val = (*val) & 0x00ff;
  215. return ret;
  216. }
  217. /*
  218. * mos7840_set_uart_reg
  219. * To set the Uart register by calling usb_fill_control_urb function by
  220. * passing usb_sndctrlpipe function as parameter.
  221. */
  222. static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
  223. __u16 val)
  224. {
  225. struct usb_device *dev = port->serial->dev;
  226. val = val & 0x00ff;
  227. // For the UART control registers, the application number need to be Or'ed
  228. if (port->serial->num_ports == 4) {
  229. val |=
  230. (((__u16) port->number - (__u16) (port->serial->minor)) +
  231. 1) << 8;
  232. dbg("mos7840_set_uart_reg application number is %x\n", val);
  233. } else {
  234. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  235. val |=
  236. (((__u16) port->number -
  237. (__u16) (port->serial->minor)) + 1) << 8;
  238. dbg("mos7840_set_uart_reg application number is %x\n",
  239. val);
  240. } else {
  241. val |=
  242. (((__u16) port->number -
  243. (__u16) (port->serial->minor)) + 2) << 8;
  244. dbg("mos7840_set_uart_reg application number is %x\n",
  245. val);
  246. }
  247. }
  248. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
  249. MCS_WR_RTYPE, val, reg, NULL, 0,
  250. MOS_WDR_TIMEOUT);
  251. }
  252. /*
  253. * mos7840_get_uart_reg
  254. * To set the Control register by calling usb_fill_control_urb function
  255. * by passing usb_rcvctrlpipe function as parameter.
  256. */
  257. static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
  258. __u16 * val)
  259. {
  260. struct usb_device *dev = port->serial->dev;
  261. int ret = 0;
  262. __u16 Wval;
  263. //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
  264. /*Wval is same as application number */
  265. if (port->serial->num_ports == 4) {
  266. Wval =
  267. (((__u16) port->number - (__u16) (port->serial->minor)) +
  268. 1) << 8;
  269. dbg("mos7840_get_uart_reg application number is %x\n", Wval);
  270. } else {
  271. if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
  272. Wval =
  273. (((__u16) port->number -
  274. (__u16) (port->serial->minor)) + 1) << 8;
  275. dbg("mos7840_get_uart_reg application number is %x\n",
  276. Wval);
  277. } else {
  278. Wval =
  279. (((__u16) port->number -
  280. (__u16) (port->serial->minor)) + 2) << 8;
  281. dbg("mos7840_get_uart_reg application number is %x\n",
  282. Wval);
  283. }
  284. }
  285. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
  286. MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
  287. MOS_WDR_TIMEOUT);
  288. *val = (*val) & 0x00ff;
  289. return ret;
  290. }
  291. static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
  292. {
  293. dbg("***************************************\n");
  294. dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
  295. dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
  296. dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
  297. dbg("***************************************\n");
  298. }
  299. /************************************************************************/
  300. /************************************************************************/
  301. /* I N T E R F A C E F U N C T I O N S */
  302. /* I N T E R F A C E F U N C T I O N S */
  303. /************************************************************************/
  304. /************************************************************************/
  305. static inline void mos7840_set_port_private(struct usb_serial_port *port,
  306. struct moschip_port *data)
  307. {
  308. usb_set_serial_port_data(port, (void *)data);
  309. }
  310. static inline struct moschip_port *mos7840_get_port_private(struct
  311. usb_serial_port
  312. *port)
  313. {
  314. return (struct moschip_port *)usb_get_serial_port_data(port);
  315. }
  316. static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
  317. {
  318. struct moschip_port *mos7840_port;
  319. struct async_icount *icount;
  320. mos7840_port = port;
  321. icount = &mos7840_port->icount;
  322. if (new_msr &
  323. (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
  324. MOS_MSR_DELTA_CD)) {
  325. icount = &mos7840_port->icount;
  326. /* update input line counters */
  327. if (new_msr & MOS_MSR_DELTA_CTS) {
  328. icount->cts++;
  329. smp_wmb();
  330. }
  331. if (new_msr & MOS_MSR_DELTA_DSR) {
  332. icount->dsr++;
  333. smp_wmb();
  334. }
  335. if (new_msr & MOS_MSR_DELTA_CD) {
  336. icount->dcd++;
  337. smp_wmb();
  338. }
  339. if (new_msr & MOS_MSR_DELTA_RI) {
  340. icount->rng++;
  341. smp_wmb();
  342. }
  343. }
  344. }
  345. static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
  346. {
  347. struct async_icount *icount;
  348. dbg("%s - %02x", __func__, new_lsr);
  349. if (new_lsr & SERIAL_LSR_BI) {
  350. //
  351. // Parity and Framing errors only count if they
  352. // occur exclusive of a break being
  353. // received.
  354. //
  355. new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
  356. }
  357. /* update input line counters */
  358. icount = &port->icount;
  359. if (new_lsr & SERIAL_LSR_BI) {
  360. icount->brk++;
  361. smp_wmb();
  362. }
  363. if (new_lsr & SERIAL_LSR_OE) {
  364. icount->overrun++;
  365. smp_wmb();
  366. }
  367. if (new_lsr & SERIAL_LSR_PE) {
  368. icount->parity++;
  369. smp_wmb();
  370. }
  371. if (new_lsr & SERIAL_LSR_FE) {
  372. icount->frame++;
  373. smp_wmb();
  374. }
  375. }
  376. /************************************************************************/
  377. /************************************************************************/
  378. /* U S B C A L L B A C K F U N C T I O N S */
  379. /* U S B C A L L B A C K F U N C T I O N S */
  380. /************************************************************************/
  381. /************************************************************************/
  382. static void mos7840_control_callback(struct urb *urb)
  383. {
  384. unsigned char *data;
  385. struct moschip_port *mos7840_port;
  386. __u8 regval = 0x0;
  387. int result = 0;
  388. int status = urb->status;
  389. mos7840_port = urb->context;
  390. switch (status) {
  391. case 0:
  392. /* success */
  393. break;
  394. case -ECONNRESET:
  395. case -ENOENT:
  396. case -ESHUTDOWN:
  397. /* this urb is terminated, clean up */
  398. dbg("%s - urb shutting down with status: %d", __func__,
  399. status);
  400. return;
  401. default:
  402. dbg("%s - nonzero urb status received: %d", __func__,
  403. status);
  404. goto exit;
  405. }
  406. dbg("%s urb buffer size is %d\n", __func__, urb->actual_length);
  407. dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__,
  408. mos7840_port->MsrLsr, mos7840_port->port_num);
  409. data = urb->transfer_buffer;
  410. regval = (__u8) data[0];
  411. dbg("%s data is %x\n", __func__, regval);
  412. if (mos7840_port->MsrLsr == 0)
  413. mos7840_handle_new_msr(mos7840_port, regval);
  414. else if (mos7840_port->MsrLsr == 1)
  415. mos7840_handle_new_lsr(mos7840_port, regval);
  416. exit:
  417. spin_lock(&mos7840_port->pool_lock);
  418. if (!mos7840_port->zombie)
  419. result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
  420. spin_unlock(&mos7840_port->pool_lock);
  421. if (result) {
  422. dev_err(&urb->dev->dev,
  423. "%s - Error %d submitting interrupt urb\n",
  424. __func__, result);
  425. }
  426. }
  427. static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
  428. __u16 * val)
  429. {
  430. struct usb_device *dev = mcs->port->serial->dev;
  431. struct usb_ctrlrequest *dr = mcs->dr;
  432. unsigned char *buffer = mcs->ctrl_buf;
  433. int ret;
  434. dr->bRequestType = MCS_RD_RTYPE;
  435. dr->bRequest = MCS_RDREQ;
  436. dr->wValue = cpu_to_le16(Wval); //0;
  437. dr->wIndex = cpu_to_le16(reg);
  438. dr->wLength = cpu_to_le16(2);
  439. usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
  440. (unsigned char *)dr, buffer, 2,
  441. mos7840_control_callback, mcs);
  442. mcs->control_urb->transfer_buffer_length = 2;
  443. ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
  444. return ret;
  445. }
  446. /*****************************************************************************
  447. * mos7840_interrupt_callback
  448. * this is the callback function for when we have received data on the
  449. * interrupt endpoint.
  450. *****************************************************************************/
  451. static void mos7840_interrupt_callback(struct urb *urb)
  452. {
  453. int result;
  454. int length;
  455. struct moschip_port *mos7840_port;
  456. struct usb_serial *serial;
  457. __u16 Data;
  458. unsigned char *data;
  459. __u8 sp[5], st;
  460. int i, rv = 0;
  461. __u16 wval, wreg = 0;
  462. int status = urb->status;
  463. dbg("%s", " : Entering\n");
  464. switch (status) {
  465. case 0:
  466. /* success */
  467. break;
  468. case -ECONNRESET:
  469. case -ENOENT:
  470. case -ESHUTDOWN:
  471. /* this urb is terminated, clean up */
  472. dbg("%s - urb shutting down with status: %d", __func__,
  473. status);
  474. return;
  475. default:
  476. dbg("%s - nonzero urb status received: %d", __func__,
  477. status);
  478. goto exit;
  479. }
  480. length = urb->actual_length;
  481. data = urb->transfer_buffer;
  482. serial = urb->context;
  483. /* Moschip get 5 bytes
  484. * Byte 1 IIR Port 1 (port.number is 0)
  485. * Byte 2 IIR Port 2 (port.number is 1)
  486. * Byte 3 IIR Port 3 (port.number is 2)
  487. * Byte 4 IIR Port 4 (port.number is 3)
  488. * Byte 5 FIFO status for both */
  489. if (length && length > 5) {
  490. dbg("%s \n", "Wrong data !!!");
  491. return;
  492. }
  493. sp[0] = (__u8) data[0];
  494. sp[1] = (__u8) data[1];
  495. sp[2] = (__u8) data[2];
  496. sp[3] = (__u8) data[3];
  497. st = (__u8) data[4];
  498. for (i = 0; i < serial->num_ports; i++) {
  499. mos7840_port = mos7840_get_port_private(serial->port[i]);
  500. wval =
  501. (((__u16) serial->port[i]->number -
  502. (__u16) (serial->minor)) + 1) << 8;
  503. if (mos7840_port->open) {
  504. if (sp[i] & 0x01) {
  505. dbg("SP%d No Interrupt !!!\n", i);
  506. } else {
  507. switch (sp[i] & 0x0f) {
  508. case SERIAL_IIR_RLS:
  509. dbg("Serial Port %d: Receiver status error or ", i);
  510. dbg("address bit detected in 9-bit mode\n");
  511. mos7840_port->MsrLsr = 1;
  512. wreg = LINE_STATUS_REGISTER;
  513. break;
  514. case SERIAL_IIR_MS:
  515. dbg("Serial Port %d: Modem status change\n", i);
  516. mos7840_port->MsrLsr = 0;
  517. wreg = MODEM_STATUS_REGISTER;
  518. break;
  519. }
  520. spin_lock(&mos7840_port->pool_lock);
  521. if (!mos7840_port->zombie) {
  522. rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
  523. } else {
  524. spin_unlock(&mos7840_port->pool_lock);
  525. return;
  526. }
  527. spin_unlock(&mos7840_port->pool_lock);
  528. }
  529. }
  530. }
  531. if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
  532. return;
  533. exit:
  534. result = usb_submit_urb(urb, GFP_ATOMIC);
  535. if (result) {
  536. dev_err(&urb->dev->dev,
  537. "%s - Error %d submitting interrupt urb\n",
  538. __func__, result);
  539. }
  540. }
  541. static int mos7840_port_paranoia_check(struct usb_serial_port *port,
  542. const char *function)
  543. {
  544. if (!port) {
  545. dbg("%s - port == NULL", function);
  546. return -1;
  547. }
  548. if (!port->serial) {
  549. dbg("%s - port->serial == NULL", function);
  550. return -1;
  551. }
  552. return 0;
  553. }
  554. /* Inline functions to check the sanity of a pointer that is passed to us */
  555. static int mos7840_serial_paranoia_check(struct usb_serial *serial,
  556. const char *function)
  557. {
  558. if (!serial) {
  559. dbg("%s - serial == NULL", function);
  560. return -1;
  561. }
  562. if (!serial->type) {
  563. dbg("%s - serial->type == NULL!", function);
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
  569. const char *function)
  570. {
  571. /* if no port was specified, or it fails a paranoia check */
  572. if (!port ||
  573. mos7840_port_paranoia_check(port, function) ||
  574. mos7840_serial_paranoia_check(port->serial, function)) {
  575. /* then say that we don't have a valid usb_serial thing, which will
  576. * end up genrating -ENODEV return values */
  577. return NULL;
  578. }
  579. return port->serial;
  580. }
  581. /*****************************************************************************
  582. * mos7840_bulk_in_callback
  583. * this is the callback function for when we have received data on the
  584. * bulk in endpoint.
  585. *****************************************************************************/
  586. static void mos7840_bulk_in_callback(struct urb *urb)
  587. {
  588. int retval;
  589. unsigned char *data;
  590. struct usb_serial *serial;
  591. struct usb_serial_port *port;
  592. struct moschip_port *mos7840_port;
  593. struct tty_struct *tty;
  594. int status = urb->status;
  595. if (status) {
  596. dbg("nonzero read bulk status received: %d", status);
  597. return;
  598. }
  599. mos7840_port = urb->context;
  600. if (!mos7840_port) {
  601. dbg("%s", "NULL mos7840_port pointer \n");
  602. return;
  603. }
  604. port = (struct usb_serial_port *)mos7840_port->port;
  605. if (mos7840_port_paranoia_check(port, __func__)) {
  606. dbg("%s", "Port Paranoia failed \n");
  607. return;
  608. }
  609. serial = mos7840_get_usb_serial(port, __func__);
  610. if (!serial) {
  611. dbg("%s\n", "Bad serial pointer ");
  612. return;
  613. }
  614. dbg("%s\n", "Entering... \n");
  615. data = urb->transfer_buffer;
  616. dbg("%s", "Entering ........... \n");
  617. if (urb->actual_length) {
  618. tty = mos7840_port->port->port.tty;
  619. if (tty) {
  620. tty_buffer_request_room(tty, urb->actual_length);
  621. tty_insert_flip_string(tty, data, urb->actual_length);
  622. dbg(" %s \n", data);
  623. tty_flip_buffer_push(tty);
  624. }
  625. mos7840_port->icount.rx += urb->actual_length;
  626. smp_wmb();
  627. dbg("mos7840_port->icount.rx is %d:\n",
  628. mos7840_port->icount.rx);
  629. }
  630. if (!mos7840_port->read_urb) {
  631. dbg("%s", "URB KILLED !!!\n");
  632. return;
  633. }
  634. mos7840_port->read_urb->dev = serial->dev;
  635. retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  636. if (retval) {
  637. dbg(" usb_submit_urb(read bulk) failed, retval = %d",
  638. retval);
  639. }
  640. }
  641. /*****************************************************************************
  642. * mos7840_bulk_out_data_callback
  643. * this is the callback function for when we have finished sending serial data
  644. * on the bulk out endpoint.
  645. *****************************************************************************/
  646. static void mos7840_bulk_out_data_callback(struct urb *urb)
  647. {
  648. struct moschip_port *mos7840_port;
  649. struct tty_struct *tty;
  650. int status = urb->status;
  651. int i;
  652. mos7840_port = urb->context;
  653. spin_lock(&mos7840_port->pool_lock);
  654. for (i = 0; i < NUM_URBS; i++) {
  655. if (urb == mos7840_port->write_urb_pool[i]) {
  656. mos7840_port->busy[i] = 0;
  657. break;
  658. }
  659. }
  660. spin_unlock(&mos7840_port->pool_lock);
  661. if (status) {
  662. dbg("nonzero write bulk status received:%d\n", status);
  663. return;
  664. }
  665. if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
  666. dbg("%s", "Port Paranoia failed \n");
  667. return;
  668. }
  669. dbg("%s \n", "Entering .........");
  670. tty = mos7840_port->port->port.tty;
  671. if (tty && mos7840_port->open)
  672. tty_wakeup(tty);
  673. }
  674. /************************************************************************/
  675. /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
  676. /************************************************************************/
  677. #ifdef MCSSerialProbe
  678. static int mos7840_serial_probe(struct usb_serial *serial,
  679. const struct usb_device_id *id)
  680. {
  681. /*need to implement the mode_reg reading and updating\
  682. structures usb_serial_ device_type\
  683. (i.e num_ports, num_bulkin,bulkout etc) */
  684. /* Also we can update the changes attach */
  685. return 1;
  686. }
  687. #endif
  688. /*****************************************************************************
  689. * mos7840_open
  690. * this function is called by the tty driver when a port is opened
  691. * If successful, we return 0
  692. * Otherwise we return a negative error number.
  693. *****************************************************************************/
  694. static int mos7840_open(struct tty_struct *tty,
  695. struct usb_serial_port *port, struct file *filp)
  696. {
  697. int response;
  698. int j;
  699. struct usb_serial *serial;
  700. struct urb *urb;
  701. __u16 Data;
  702. int status;
  703. struct moschip_port *mos7840_port;
  704. struct moschip_port *port0;
  705. if (mos7840_port_paranoia_check(port, __func__)) {
  706. dbg("%s", "Port Paranoia failed \n");
  707. return -ENODEV;
  708. }
  709. serial = port->serial;
  710. if (mos7840_serial_paranoia_check(serial, __func__)) {
  711. dbg("%s", "Serial Paranoia failed \n");
  712. return -ENODEV;
  713. }
  714. mos7840_port = mos7840_get_port_private(port);
  715. port0 = mos7840_get_port_private(serial->port[0]);
  716. if (mos7840_port == NULL || port0 == NULL)
  717. return -ENODEV;
  718. usb_clear_halt(serial->dev, port->write_urb->pipe);
  719. usb_clear_halt(serial->dev, port->read_urb->pipe);
  720. port0->open_ports++;
  721. /* Initialising the write urb pool */
  722. for (j = 0; j < NUM_URBS; ++j) {
  723. urb = usb_alloc_urb(0, GFP_KERNEL);
  724. mos7840_port->write_urb_pool[j] = urb;
  725. if (urb == NULL) {
  726. err("No more urbs???");
  727. continue;
  728. }
  729. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  730. if (!urb->transfer_buffer) {
  731. usb_free_urb(urb);
  732. mos7840_port->write_urb_pool[j] = NULL;
  733. err("%s-out of memory for urb buffers.", __func__);
  734. continue;
  735. }
  736. }
  737. /*****************************************************************************
  738. * Initialize MCS7840 -- Write Init values to corresponding Registers
  739. *
  740. * Register Index
  741. * 1 : IER
  742. * 2 : FCR
  743. * 3 : LCR
  744. * 4 : MCR
  745. *
  746. * 0x08 : SP1/2 Control Reg
  747. *****************************************************************************/
  748. //NEED to check the following Block
  749. status = 0;
  750. Data = 0x0;
  751. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  752. if (status < 0) {
  753. dbg("Reading Spreg failed\n");
  754. return -1;
  755. }
  756. Data |= 0x80;
  757. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  758. if (status < 0) {
  759. dbg("writing Spreg failed\n");
  760. return -1;
  761. }
  762. Data &= ~0x80;
  763. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  764. if (status < 0) {
  765. dbg("writing Spreg failed\n");
  766. return -1;
  767. }
  768. //End of block to be checked
  769. status = 0;
  770. Data = 0x0;
  771. status =
  772. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  773. if (status < 0) {
  774. dbg("Reading Controlreg failed\n");
  775. return -1;
  776. }
  777. Data |= 0x08; //Driver done bit
  778. Data |= 0x20; //rx_disable
  779. status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  780. if (status < 0) {
  781. dbg("writing Controlreg failed\n");
  782. return -1;
  783. }
  784. //do register settings here
  785. // Set all regs to the device default values.
  786. ////////////////////////////////////
  787. // First Disable all interrupts.
  788. ////////////////////////////////////
  789. Data = 0x00;
  790. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  791. if (status < 0) {
  792. dbg("disableing interrupts failed\n");
  793. return -1;
  794. }
  795. // Set FIFO_CONTROL_REGISTER to the default value
  796. Data = 0x00;
  797. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  798. if (status < 0) {
  799. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  800. return -1;
  801. }
  802. Data = 0xcf;
  803. status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  804. if (status < 0) {
  805. dbg("Writing FIFO_CONTROL_REGISTER failed\n");
  806. return -1;
  807. }
  808. Data = 0x03;
  809. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  810. mos7840_port->shadowLCR = Data;
  811. Data = 0x0b;
  812. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  813. mos7840_port->shadowMCR = Data;
  814. Data = 0x00;
  815. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  816. mos7840_port->shadowLCR = Data;
  817. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  818. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  819. Data = 0x0c;
  820. status = 0;
  821. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  822. Data = 0x0;
  823. status = 0;
  824. status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  825. Data = 0x00;
  826. status = 0;
  827. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  828. Data = Data & ~SERIAL_LCR_DLAB;
  829. status = 0;
  830. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  831. mos7840_port->shadowLCR = Data;
  832. //clearing Bulkin and Bulkout Fifo
  833. Data = 0x0;
  834. status = 0;
  835. status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
  836. Data = Data | 0x0c;
  837. status = 0;
  838. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  839. Data = Data & ~0x0c;
  840. status = 0;
  841. status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  842. //Finally enable all interrupts
  843. Data = 0x0;
  844. Data = 0x0c;
  845. status = 0;
  846. status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  847. //clearing rx_disable
  848. Data = 0x0;
  849. status = 0;
  850. status =
  851. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  852. Data = Data & ~0x20;
  853. status = 0;
  854. status =
  855. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  856. // rx_negate
  857. Data = 0x0;
  858. status = 0;
  859. status =
  860. mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
  861. Data = Data | 0x10;
  862. status = 0;
  863. status =
  864. mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
  865. /* force low_latency on so that our tty_push actually forces *
  866. * the data through,otherwise it is scheduled, and with *
  867. * high data rates (like with OHCI) data can get lost. */
  868. if (tty)
  869. tty->low_latency = 1;
  870. /* Check to see if we've set up our endpoint info yet *
  871. * (can't set it up in mos7840_startup as the structures *
  872. * were not set up at that time.) */
  873. if (port0->open_ports == 1) {
  874. if (serial->port[0]->interrupt_in_buffer == NULL) {
  875. /* set up interrupt urb */
  876. usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
  877. serial->dev,
  878. usb_rcvintpipe(serial->dev,
  879. serial->port[0]->
  880. interrupt_in_endpointAddress),
  881. serial->port[0]->interrupt_in_buffer,
  882. serial->port[0]->interrupt_in_urb->
  883. transfer_buffer_length,
  884. mos7840_interrupt_callback,
  885. serial,
  886. serial->port[0]->interrupt_in_urb->
  887. interval);
  888. /* start interrupt read for mos7840 *
  889. * will continue as long as mos7840 is connected */
  890. response =
  891. usb_submit_urb(serial->port[0]->interrupt_in_urb,
  892. GFP_KERNEL);
  893. if (response) {
  894. err("%s - Error %d submitting interrupt urb",
  895. __func__, response);
  896. }
  897. }
  898. }
  899. /* see if we've set up our endpoint info yet *
  900. * (can't set it up in mos7840_startup as the *
  901. * structures were not set up at that time.) */
  902. dbg("port number is %d \n", port->number);
  903. dbg("serial number is %d \n", port->serial->minor);
  904. dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
  905. dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
  906. dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
  907. dbg("port's number in the device is %d\n", mos7840_port->port_num);
  908. mos7840_port->read_urb = port->read_urb;
  909. /* set up our bulk in urb */
  910. usb_fill_bulk_urb(mos7840_port->read_urb,
  911. serial->dev,
  912. usb_rcvbulkpipe(serial->dev,
  913. port->bulk_in_endpointAddress),
  914. port->bulk_in_buffer,
  915. mos7840_port->read_urb->transfer_buffer_length,
  916. mos7840_bulk_in_callback, mos7840_port);
  917. dbg("mos7840_open: bulkin endpoint is %d\n",
  918. port->bulk_in_endpointAddress);
  919. response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
  920. if (response) {
  921. err("%s - Error %d submitting control urb", __func__,
  922. response);
  923. }
  924. /* initialize our wait queues */
  925. init_waitqueue_head(&mos7840_port->wait_chase);
  926. init_waitqueue_head(&mos7840_port->delta_msr_wait);
  927. /* initialize our icount structure */
  928. memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
  929. /* initialize our port settings */
  930. mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
  931. /* send a open port command */
  932. mos7840_port->open = 1;
  933. //mos7840_change_port_settings(mos7840_port,old_termios);
  934. mos7840_port->icount.tx = 0;
  935. mos7840_port->icount.rx = 0;
  936. dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
  937. return 0;
  938. }
  939. /*****************************************************************************
  940. * mos7840_chars_in_buffer
  941. * this function is called by the tty driver when it wants to know how many
  942. * bytes of data we currently have outstanding in the port (data that has
  943. * been written, but hasn't made it out the port yet)
  944. * If successful, we return the number of bytes left to be written in the
  945. * system,
  946. * Otherwise we return zero.
  947. *****************************************************************************/
  948. static int mos7840_chars_in_buffer(struct tty_struct *tty)
  949. {
  950. struct usb_serial_port *port = tty->driver_data;
  951. int i;
  952. int chars = 0;
  953. unsigned long flags;
  954. struct moschip_port *mos7840_port;
  955. dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
  956. if (mos7840_port_paranoia_check(port, __func__)) {
  957. dbg("%s", "Invalid port \n");
  958. return 0;
  959. }
  960. mos7840_port = mos7840_get_port_private(port);
  961. if (mos7840_port == NULL) {
  962. dbg("%s \n", "mos7840_break:leaving ...........");
  963. return 0;
  964. }
  965. spin_lock_irqsave(&mos7840_port->pool_lock,flags);
  966. for (i = 0; i < NUM_URBS; ++i)
  967. if (mos7840_port->busy[i])
  968. chars += URB_TRANSFER_BUFFER_SIZE;
  969. spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
  970. dbg("%s - returns %d", __func__, chars);
  971. return chars;
  972. }
  973. /************************************************************************
  974. *
  975. * mos7840_block_until_tx_empty
  976. *
  977. * This function will block the close until one of the following:
  978. * 1. TX count are 0
  979. * 2. The mos7840 has stopped
  980. * 3. A timeout of 3 seconds without activity has expired
  981. *
  982. ************************************************************************/
  983. static void mos7840_block_until_tx_empty(struct tty_struct *tty,
  984. struct moschip_port *mos7840_port)
  985. {
  986. int timeout = HZ / 10;
  987. int wait = 30;
  988. int count;
  989. while (1) {
  990. count = mos7840_chars_in_buffer(tty);
  991. /* Check for Buffer status */
  992. if (count <= 0) {
  993. return;
  994. }
  995. /* Block the thread for a while */
  996. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  997. timeout);
  998. /* No activity.. count down section */
  999. wait--;
  1000. if (wait == 0) {
  1001. dbg("%s - TIMEOUT", __func__);
  1002. return;
  1003. } else {
  1004. /* Reset timeout value back to seconds */
  1005. wait = 30;
  1006. }
  1007. }
  1008. }
  1009. /*****************************************************************************
  1010. * mos7840_close
  1011. * this function is called by the tty driver when a port is closed
  1012. *****************************************************************************/
  1013. static void mos7840_close(struct tty_struct *tty,
  1014. struct usb_serial_port *port, struct file *filp)
  1015. {
  1016. struct usb_serial *serial;
  1017. struct moschip_port *mos7840_port;
  1018. struct moschip_port *port0;
  1019. int j;
  1020. __u16 Data;
  1021. dbg("%s\n", "mos7840_close:entering...");
  1022. if (mos7840_port_paranoia_check(port, __func__)) {
  1023. dbg("%s", "Port Paranoia failed \n");
  1024. return;
  1025. }
  1026. serial = mos7840_get_usb_serial(port, __func__);
  1027. if (!serial) {
  1028. dbg("%s", "Serial Paranoia failed \n");
  1029. return;
  1030. }
  1031. mos7840_port = mos7840_get_port_private(port);
  1032. port0 = mos7840_get_port_private(serial->port[0]);
  1033. if (mos7840_port == NULL || port0 == NULL)
  1034. return;
  1035. for (j = 0; j < NUM_URBS; ++j)
  1036. usb_kill_urb(mos7840_port->write_urb_pool[j]);
  1037. /* Freeing Write URBs */
  1038. for (j = 0; j < NUM_URBS; ++j) {
  1039. if (mos7840_port->write_urb_pool[j]) {
  1040. if (mos7840_port->write_urb_pool[j]->transfer_buffer)
  1041. kfree(mos7840_port->write_urb_pool[j]->
  1042. transfer_buffer);
  1043. usb_free_urb(mos7840_port->write_urb_pool[j]);
  1044. }
  1045. }
  1046. if (serial->dev)
  1047. /* flush and block until tx is empty */
  1048. mos7840_block_until_tx_empty(tty, mos7840_port);
  1049. /* While closing port, shutdown all bulk read, write *
  1050. * and interrupt read if they exists */
  1051. if (serial->dev) {
  1052. if (mos7840_port->write_urb) {
  1053. dbg("%s", "Shutdown bulk write\n");
  1054. usb_kill_urb(mos7840_port->write_urb);
  1055. }
  1056. if (mos7840_port->read_urb) {
  1057. dbg("%s", "Shutdown bulk read\n");
  1058. usb_kill_urb(mos7840_port->read_urb);
  1059. }
  1060. if ((&mos7840_port->control_urb)) {
  1061. dbg("%s", "Shutdown control read\n");
  1062. // usb_kill_urb (mos7840_port->control_urb);
  1063. }
  1064. }
  1065. // if(mos7840_port->ctrl_buf != NULL)
  1066. // kfree(mos7840_port->ctrl_buf);
  1067. port0->open_ports--;
  1068. dbg("mos7840_num_open_ports in close%d:in port%d\n",
  1069. port0->open_ports, port->number);
  1070. if (port0->open_ports == 0) {
  1071. if (serial->port[0]->interrupt_in_urb) {
  1072. dbg("%s", "Shutdown interrupt_in_urb\n");
  1073. usb_kill_urb(serial->port[0]->interrupt_in_urb);
  1074. }
  1075. }
  1076. if (mos7840_port->write_urb) {
  1077. /* if this urb had a transfer buffer already (old tx) free it */
  1078. if (mos7840_port->write_urb->transfer_buffer != NULL) {
  1079. kfree(mos7840_port->write_urb->transfer_buffer);
  1080. }
  1081. usb_free_urb(mos7840_port->write_urb);
  1082. }
  1083. Data = 0x0;
  1084. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1085. Data = 0x00;
  1086. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1087. mos7840_port->open = 0;
  1088. dbg("%s \n", "Leaving ............");
  1089. }
  1090. /************************************************************************
  1091. *
  1092. * mos7840_block_until_chase_response
  1093. *
  1094. * This function will block the close until one of the following:
  1095. * 1. Response to our Chase comes from mos7840
  1096. * 2. A timeout of 10 seconds without activity has expired
  1097. * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
  1098. *
  1099. ************************************************************************/
  1100. static void mos7840_block_until_chase_response(struct tty_struct *tty,
  1101. struct moschip_port *mos7840_port)
  1102. {
  1103. int timeout = 1 * HZ;
  1104. int wait = 10;
  1105. int count;
  1106. while (1) {
  1107. count = mos7840_chars_in_buffer(tty);
  1108. /* Check for Buffer status */
  1109. if (count <= 0) {
  1110. return;
  1111. }
  1112. /* Block the thread for a while */
  1113. interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
  1114. timeout);
  1115. /* No activity.. count down section */
  1116. wait--;
  1117. if (wait == 0) {
  1118. dbg("%s - TIMEOUT", __func__);
  1119. return;
  1120. } else {
  1121. /* Reset timeout value back to seconds */
  1122. wait = 10;
  1123. }
  1124. }
  1125. }
  1126. /*****************************************************************************
  1127. * mos7840_break
  1128. * this function sends a break to the port
  1129. *****************************************************************************/
  1130. static void mos7840_break(struct tty_struct *tty, int break_state)
  1131. {
  1132. struct usb_serial_port *port = tty->driver_data;
  1133. unsigned char data;
  1134. struct usb_serial *serial;
  1135. struct moschip_port *mos7840_port;
  1136. dbg("%s \n", "Entering ...........");
  1137. dbg("mos7840_break: Start\n");
  1138. if (mos7840_port_paranoia_check(port, __func__)) {
  1139. dbg("%s", "Port Paranoia failed \n");
  1140. return;
  1141. }
  1142. serial = mos7840_get_usb_serial(port, __func__);
  1143. if (!serial) {
  1144. dbg("%s", "Serial Paranoia failed \n");
  1145. return;
  1146. }
  1147. mos7840_port = mos7840_get_port_private(port);
  1148. if (mos7840_port == NULL) {
  1149. return;
  1150. }
  1151. if (serial->dev)
  1152. /* flush and block until tx is empty */
  1153. mos7840_block_until_chase_response(tty, mos7840_port);
  1154. if (break_state == -1)
  1155. data = mos7840_port->shadowLCR | LCR_SET_BREAK;
  1156. else
  1157. data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
  1158. mos7840_port->shadowLCR = data;
  1159. dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
  1160. mos7840_port->shadowLCR);
  1161. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
  1162. mos7840_port->shadowLCR);
  1163. return;
  1164. }
  1165. /*****************************************************************************
  1166. * mos7840_write_room
  1167. * this function is called by the tty driver when it wants to know how many
  1168. * bytes of data we can accept for a specific port.
  1169. * If successful, we return the amount of room that we have for this port
  1170. * Otherwise we return a negative error number.
  1171. *****************************************************************************/
  1172. static int mos7840_write_room(struct tty_struct *tty)
  1173. {
  1174. struct usb_serial_port *port = tty->driver_data;
  1175. int i;
  1176. int room = 0;
  1177. unsigned long flags;
  1178. struct moschip_port *mos7840_port;
  1179. dbg("%s \n", " mos7840_write_room:entering ...........");
  1180. if (mos7840_port_paranoia_check(port, __func__)) {
  1181. dbg("%s", "Invalid port \n");
  1182. dbg("%s \n", " mos7840_write_room:leaving ...........");
  1183. return -1;
  1184. }
  1185. mos7840_port = mos7840_get_port_private(port);
  1186. if (mos7840_port == NULL) {
  1187. dbg("%s \n", "mos7840_break:leaving ...........");
  1188. return -1;
  1189. }
  1190. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1191. for (i = 0; i < NUM_URBS; ++i) {
  1192. if (!mos7840_port->busy[i]) {
  1193. room += URB_TRANSFER_BUFFER_SIZE;
  1194. }
  1195. }
  1196. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1197. room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
  1198. dbg("%s - returns %d", __func__, room);
  1199. return room;
  1200. }
  1201. /*****************************************************************************
  1202. * mos7840_write
  1203. * this function is called by the tty driver when data should be written to
  1204. * the port.
  1205. * If successful, we return the number of bytes written, otherwise we
  1206. * return a negative error number.
  1207. *****************************************************************************/
  1208. static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
  1209. const unsigned char *data, int count)
  1210. {
  1211. int status;
  1212. int i;
  1213. int bytes_sent = 0;
  1214. int transfer_size;
  1215. unsigned long flags;
  1216. struct moschip_port *mos7840_port;
  1217. struct usb_serial *serial;
  1218. struct urb *urb;
  1219. //__u16 Data;
  1220. const unsigned char *current_position = data;
  1221. unsigned char *data1;
  1222. dbg("%s \n", "entering ...........");
  1223. //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
  1224. #ifdef NOTMOS7840
  1225. Data = 0x00;
  1226. status = 0;
  1227. status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
  1228. mos7840_port->shadowLCR = Data;
  1229. dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
  1230. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1231. mos7840_port->shadowLCR);
  1232. //Data = 0x03;
  1233. //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
  1234. //mos7840_port->shadowLCR=Data;//Need to add later
  1235. Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
  1236. status = 0;
  1237. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1238. //Data = 0x0c;
  1239. //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
  1240. Data = 0x00;
  1241. status = 0;
  1242. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
  1243. dbg("mos7840_write:DLL value is %x\n", Data);
  1244. Data = 0x0;
  1245. status = 0;
  1246. status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
  1247. dbg("mos7840_write:DLM value is %x\n", Data);
  1248. Data = Data & ~SERIAL_LCR_DLAB;
  1249. dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
  1250. mos7840_port->shadowLCR);
  1251. status = 0;
  1252. status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1253. #endif
  1254. if (mos7840_port_paranoia_check(port, __func__)) {
  1255. dbg("%s", "Port Paranoia failed \n");
  1256. return -1;
  1257. }
  1258. serial = port->serial;
  1259. if (mos7840_serial_paranoia_check(serial, __func__)) {
  1260. dbg("%s", "Serial Paranoia failed \n");
  1261. return -1;
  1262. }
  1263. mos7840_port = mos7840_get_port_private(port);
  1264. if (mos7840_port == NULL) {
  1265. dbg("%s", "mos7840_port is NULL\n");
  1266. return -1;
  1267. }
  1268. /* try to find a free urb in the list */
  1269. urb = NULL;
  1270. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  1271. for (i = 0; i < NUM_URBS; ++i) {
  1272. if (!mos7840_port->busy[i]) {
  1273. mos7840_port->busy[i] = 1;
  1274. urb = mos7840_port->write_urb_pool[i];
  1275. dbg("\nURB:%d", i);
  1276. break;
  1277. }
  1278. }
  1279. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  1280. if (urb == NULL) {
  1281. dbg("%s - no more free urbs", __func__);
  1282. goto exit;
  1283. }
  1284. if (urb->transfer_buffer == NULL) {
  1285. urb->transfer_buffer =
  1286. kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
  1287. if (urb->transfer_buffer == NULL) {
  1288. err("%s no more kernel memory...", __func__);
  1289. goto exit;
  1290. }
  1291. }
  1292. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1293. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1294. /* fill urb with data and submit */
  1295. usb_fill_bulk_urb(urb,
  1296. serial->dev,
  1297. usb_sndbulkpipe(serial->dev,
  1298. port->bulk_out_endpointAddress),
  1299. urb->transfer_buffer,
  1300. transfer_size,
  1301. mos7840_bulk_out_data_callback, mos7840_port);
  1302. data1 = urb->transfer_buffer;
  1303. dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
  1304. /* send it down the pipe */
  1305. status = usb_submit_urb(urb, GFP_ATOMIC);
  1306. if (status) {
  1307. mos7840_port->busy[i] = 0;
  1308. err("%s - usb_submit_urb(write bulk) failed with status = %d",
  1309. __func__, status);
  1310. bytes_sent = status;
  1311. goto exit;
  1312. }
  1313. bytes_sent = transfer_size;
  1314. mos7840_port->icount.tx += transfer_size;
  1315. smp_wmb();
  1316. dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
  1317. exit:
  1318. return bytes_sent;
  1319. }
  1320. /*****************************************************************************
  1321. * mos7840_throttle
  1322. * this function is called by the tty driver when it wants to stop the data
  1323. * being read from the port.
  1324. *****************************************************************************/
  1325. static void mos7840_throttle(struct tty_struct *tty)
  1326. {
  1327. struct usb_serial_port *port = tty->driver_data;
  1328. struct moschip_port *mos7840_port;
  1329. int status;
  1330. if (mos7840_port_paranoia_check(port, __func__)) {
  1331. dbg("%s", "Invalid port \n");
  1332. return;
  1333. }
  1334. dbg("- port %d\n", port->number);
  1335. mos7840_port = mos7840_get_port_private(port);
  1336. if (mos7840_port == NULL)
  1337. return;
  1338. if (!mos7840_port->open) {
  1339. dbg("%s\n", "port not opened");
  1340. return;
  1341. }
  1342. dbg("%s", "Entering .......... \n");
  1343. /* if we are implementing XON/XOFF, send the stop character */
  1344. if (I_IXOFF(tty)) {
  1345. unsigned char stop_char = STOP_CHAR(tty);
  1346. status = mos7840_write(tty, port, &stop_char, 1);
  1347. if (status <= 0)
  1348. return;
  1349. }
  1350. /* if we are implementing RTS/CTS, toggle that line */
  1351. if (tty->termios->c_cflag & CRTSCTS) {
  1352. mos7840_port->shadowMCR &= ~MCR_RTS;
  1353. status = 0;
  1354. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1355. mos7840_port->shadowMCR);
  1356. if (status < 0)
  1357. return;
  1358. }
  1359. return;
  1360. }
  1361. /*****************************************************************************
  1362. * mos7840_unthrottle
  1363. * this function is called by the tty driver when it wants to resume the data
  1364. * being read from the port (called after SerialThrottle is called)
  1365. *****************************************************************************/
  1366. static void mos7840_unthrottle(struct tty_struct *tty)
  1367. {
  1368. struct usb_serial_port *port = tty->driver_data;
  1369. int status;
  1370. struct moschip_port *mos7840_port = mos7840_get_port_private(port);
  1371. if (mos7840_port_paranoia_check(port, __func__)) {
  1372. dbg("%s", "Invalid port \n");
  1373. return;
  1374. }
  1375. if (mos7840_port == NULL)
  1376. return;
  1377. if (!mos7840_port->open) {
  1378. dbg("%s - port not opened", __func__);
  1379. return;
  1380. }
  1381. dbg("%s", "Entering .......... \n");
  1382. /* if we are implementing XON/XOFF, send the start character */
  1383. if (I_IXOFF(tty)) {
  1384. unsigned char start_char = START_CHAR(tty);
  1385. status = mos7840_write(tty, port, &start_char, 1);
  1386. if (status <= 0)
  1387. return;
  1388. }
  1389. /* if we are implementing RTS/CTS, toggle that line */
  1390. if (tty->termios->c_cflag & CRTSCTS) {
  1391. mos7840_port->shadowMCR |= MCR_RTS;
  1392. status = 0;
  1393. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
  1394. mos7840_port->shadowMCR);
  1395. if (status < 0)
  1396. return;
  1397. }
  1398. }
  1399. static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
  1400. {
  1401. struct usb_serial_port *port = tty->driver_data;
  1402. struct moschip_port *mos7840_port;
  1403. unsigned int result;
  1404. __u16 msr;
  1405. __u16 mcr;
  1406. int status = 0;
  1407. mos7840_port = mos7840_get_port_private(port);
  1408. dbg("%s - port %d", __func__, port->number);
  1409. if (mos7840_port == NULL)
  1410. return -ENODEV;
  1411. status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
  1412. status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
  1413. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  1414. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  1415. | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
  1416. | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
  1417. | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
  1418. | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
  1419. | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
  1420. dbg("%s - 0x%04X", __func__, result);
  1421. return result;
  1422. }
  1423. static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
  1424. unsigned int set, unsigned int clear)
  1425. {
  1426. struct usb_serial_port *port = tty->driver_data;
  1427. struct moschip_port *mos7840_port;
  1428. unsigned int mcr;
  1429. int status;
  1430. dbg("%s - port %d", __func__, port->number);
  1431. mos7840_port = mos7840_get_port_private(port);
  1432. if (mos7840_port == NULL)
  1433. return -ENODEV;
  1434. /* FIXME: What locks the port registers ? */
  1435. mcr = mos7840_port->shadowMCR;
  1436. if (clear & TIOCM_RTS)
  1437. mcr &= ~MCR_RTS;
  1438. if (clear & TIOCM_DTR)
  1439. mcr &= ~MCR_DTR;
  1440. if (clear & TIOCM_LOOP)
  1441. mcr &= ~MCR_LOOPBACK;
  1442. if (set & TIOCM_RTS)
  1443. mcr |= MCR_RTS;
  1444. if (set & TIOCM_DTR)
  1445. mcr |= MCR_DTR;
  1446. if (set & TIOCM_LOOP)
  1447. mcr |= MCR_LOOPBACK;
  1448. mos7840_port->shadowMCR = mcr;
  1449. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
  1450. if (status < 0) {
  1451. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1452. return status;
  1453. }
  1454. return 0;
  1455. }
  1456. /*****************************************************************************
  1457. * mos7840_calc_baud_rate_divisor
  1458. * this function calculates the proper baud rate divisor for the specified
  1459. * baud rate.
  1460. *****************************************************************************/
  1461. static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
  1462. __u16 * clk_sel_val)
  1463. {
  1464. dbg("%s - %d", __func__, baudRate);
  1465. if (baudRate <= 115200) {
  1466. *divisor = 115200 / baudRate;
  1467. *clk_sel_val = 0x0;
  1468. }
  1469. if ((baudRate > 115200) && (baudRate <= 230400)) {
  1470. *divisor = 230400 / baudRate;
  1471. *clk_sel_val = 0x10;
  1472. } else if ((baudRate > 230400) && (baudRate <= 403200)) {
  1473. *divisor = 403200 / baudRate;
  1474. *clk_sel_val = 0x20;
  1475. } else if ((baudRate > 403200) && (baudRate <= 460800)) {
  1476. *divisor = 460800 / baudRate;
  1477. *clk_sel_val = 0x30;
  1478. } else if ((baudRate > 460800) && (baudRate <= 806400)) {
  1479. *divisor = 806400 / baudRate;
  1480. *clk_sel_val = 0x40;
  1481. } else if ((baudRate > 806400) && (baudRate <= 921600)) {
  1482. *divisor = 921600 / baudRate;
  1483. *clk_sel_val = 0x50;
  1484. } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
  1485. *divisor = 1572864 / baudRate;
  1486. *clk_sel_val = 0x60;
  1487. } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
  1488. *divisor = 3145728 / baudRate;
  1489. *clk_sel_val = 0x70;
  1490. }
  1491. return 0;
  1492. #ifdef NOTMCS7840
  1493. for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
  1494. if (mos7840_divisor_table[i].BaudRate == baudrate) {
  1495. *divisor = mos7840_divisor_table[i].Divisor;
  1496. return 0;
  1497. }
  1498. }
  1499. /* After trying for all the standard baud rates *
  1500. * Try calculating the divisor for this baud rate */
  1501. if (baudrate > 75 && baudrate < 230400) {
  1502. /* get the divisor */
  1503. custom = (__u16) (230400L / baudrate);
  1504. /* Check for round off */
  1505. round1 = (__u16) (2304000L / baudrate);
  1506. round = (__u16) (round1 - (custom * 10));
  1507. if (round > 4) {
  1508. custom++;
  1509. }
  1510. *divisor = custom;
  1511. dbg(" Baud %d = %d\n", baudrate, custom);
  1512. return 0;
  1513. }
  1514. dbg("%s\n", " Baud calculation Failed...");
  1515. return -1;
  1516. #endif
  1517. }
  1518. /*****************************************************************************
  1519. * mos7840_send_cmd_write_baud_rate
  1520. * this function sends the proper command to change the baud rate of the
  1521. * specified port.
  1522. *****************************************************************************/
  1523. static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
  1524. int baudRate)
  1525. {
  1526. int divisor = 0;
  1527. int status;
  1528. __u16 Data;
  1529. unsigned char number;
  1530. __u16 clk_sel_val;
  1531. struct usb_serial_port *port;
  1532. if (mos7840_port == NULL)
  1533. return -1;
  1534. port = (struct usb_serial_port *)mos7840_port->port;
  1535. if (mos7840_port_paranoia_check(port, __func__)) {
  1536. dbg("%s", "Invalid port \n");
  1537. return -1;
  1538. }
  1539. if (mos7840_serial_paranoia_check(port->serial, __func__)) {
  1540. dbg("%s", "Invalid Serial \n");
  1541. return -1;
  1542. }
  1543. dbg("%s", "Entering .......... \n");
  1544. number = mos7840_port->port->number - mos7840_port->port->serial->minor;
  1545. dbg("%s - port = %d, baud = %d", __func__,
  1546. mos7840_port->port->number, baudRate);
  1547. //reset clk_uart_sel in spregOffset
  1548. if (baudRate > 115200) {
  1549. #ifdef HW_flow_control
  1550. //NOTE: need to see the pther register to modify
  1551. //setting h/w flow control bit to 1;
  1552. status = 0;
  1553. Data = 0x2b;
  1554. mos7840_port->shadowMCR = Data;
  1555. status =
  1556. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1557. if (status < 0) {
  1558. dbg("Writing spreg failed in set_serial_baud\n");
  1559. return -1;
  1560. }
  1561. #endif
  1562. } else {
  1563. #ifdef HW_flow_control
  1564. //setting h/w flow control bit to 0;
  1565. status = 0;
  1566. Data = 0xb;
  1567. mos7840_port->shadowMCR = Data;
  1568. status =
  1569. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1570. if (status < 0) {
  1571. dbg("Writing spreg failed in set_serial_baud\n");
  1572. return -1;
  1573. }
  1574. #endif
  1575. }
  1576. if (1) //baudRate <= 115200)
  1577. {
  1578. clk_sel_val = 0x0;
  1579. Data = 0x0;
  1580. status = 0;
  1581. status =
  1582. mos7840_calc_baud_rate_divisor(baudRate, &divisor,
  1583. &clk_sel_val);
  1584. status =
  1585. mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
  1586. &Data);
  1587. if (status < 0) {
  1588. dbg("reading spreg failed in set_serial_baud\n");
  1589. return -1;
  1590. }
  1591. Data = (Data & 0x8f) | clk_sel_val;
  1592. status = 0;
  1593. status =
  1594. mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
  1595. if (status < 0) {
  1596. dbg("Writing spreg failed in set_serial_baud\n");
  1597. return -1;
  1598. }
  1599. /* Calculate the Divisor */
  1600. if (status) {
  1601. err("%s - bad baud rate", __func__);
  1602. dbg("%s\n", "bad baud rate");
  1603. return status;
  1604. }
  1605. /* Enable access to divisor latch */
  1606. Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
  1607. mos7840_port->shadowLCR = Data;
  1608. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1609. /* Write the divisor */
  1610. Data = (unsigned char)(divisor & 0xff);
  1611. dbg("set_serial_baud Value to write DLL is %x\n", Data);
  1612. mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
  1613. Data = (unsigned char)((divisor & 0xff00) >> 8);
  1614. dbg("set_serial_baud Value to write DLM is %x\n", Data);
  1615. mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
  1616. /* Disable access to divisor latch */
  1617. Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
  1618. mos7840_port->shadowLCR = Data;
  1619. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1620. }
  1621. return status;
  1622. }
  1623. /*****************************************************************************
  1624. * mos7840_change_port_settings
  1625. * This routine is called to set the UART on the device to match
  1626. * the specified new settings.
  1627. *****************************************************************************/
  1628. static void mos7840_change_port_settings(struct tty_struct *tty,
  1629. struct moschip_port *mos7840_port, struct ktermios *old_termios)
  1630. {
  1631. int baud;
  1632. unsigned cflag;
  1633. unsigned iflag;
  1634. __u8 lData;
  1635. __u8 lParity;
  1636. __u8 lStop;
  1637. int status;
  1638. __u16 Data;
  1639. struct usb_serial_port *port;
  1640. struct usb_serial *serial;
  1641. if (mos7840_port == NULL)
  1642. return;
  1643. port = (struct usb_serial_port *)mos7840_port->port;
  1644. if (mos7840_port_paranoia_check(port, __func__)) {
  1645. dbg("%s", "Invalid port \n");
  1646. return;
  1647. }
  1648. if (mos7840_serial_paranoia_check(port->serial, __func__)) {
  1649. dbg("%s", "Invalid Serial \n");
  1650. return;
  1651. }
  1652. serial = port->serial;
  1653. dbg("%s - port %d", __func__, mos7840_port->port->number);
  1654. if (!mos7840_port->open) {
  1655. dbg("%s - port not opened", __func__);
  1656. return;
  1657. }
  1658. dbg("%s", "Entering .......... \n");
  1659. lData = LCR_BITS_8;
  1660. lStop = LCR_STOP_1;
  1661. lParity = LCR_PAR_NONE;
  1662. cflag = tty->termios->c_cflag;
  1663. iflag = tty->termios->c_iflag;
  1664. /* Change the number of bits */
  1665. if (cflag & CSIZE) {
  1666. switch (cflag & CSIZE) {
  1667. case CS5:
  1668. lData = LCR_BITS_5;
  1669. break;
  1670. case CS6:
  1671. lData = LCR_BITS_6;
  1672. break;
  1673. case CS7:
  1674. lData = LCR_BITS_7;
  1675. break;
  1676. default:
  1677. case CS8:
  1678. lData = LCR_BITS_8;
  1679. break;
  1680. }
  1681. }
  1682. /* Change the Parity bit */
  1683. if (cflag & PARENB) {
  1684. if (cflag & PARODD) {
  1685. lParity = LCR_PAR_ODD;
  1686. dbg("%s - parity = odd", __func__);
  1687. } else {
  1688. lParity = LCR_PAR_EVEN;
  1689. dbg("%s - parity = even", __func__);
  1690. }
  1691. } else {
  1692. dbg("%s - parity = none", __func__);
  1693. }
  1694. if (cflag & CMSPAR) {
  1695. lParity = lParity | 0x20;
  1696. }
  1697. /* Change the Stop bit */
  1698. if (cflag & CSTOPB) {
  1699. lStop = LCR_STOP_2;
  1700. dbg("%s - stop bits = 2", __func__);
  1701. } else {
  1702. lStop = LCR_STOP_1;
  1703. dbg("%s - stop bits = 1", __func__);
  1704. }
  1705. /* Update the LCR with the correct value */
  1706. mos7840_port->shadowLCR &=
  1707. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1708. mos7840_port->shadowLCR |= (lData | lParity | lStop);
  1709. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
  1710. mos7840_port->shadowLCR);
  1711. /* Disable Interrupts */
  1712. Data = 0x00;
  1713. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1714. Data = 0x00;
  1715. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1716. Data = 0xcf;
  1717. mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
  1718. /* Send the updated LCR value to the mos7840 */
  1719. Data = mos7840_port->shadowLCR;
  1720. mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
  1721. Data = 0x00b;
  1722. mos7840_port->shadowMCR = Data;
  1723. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1724. Data = 0x00b;
  1725. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1726. /* set up the MCR register and send it to the mos7840 */
  1727. mos7840_port->shadowMCR = MCR_MASTER_IE;
  1728. if (cflag & CBAUD) {
  1729. mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  1730. }
  1731. if (cflag & CRTSCTS) {
  1732. mos7840_port->shadowMCR |= (MCR_XON_ANY);
  1733. } else {
  1734. mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
  1735. }
  1736. Data = mos7840_port->shadowMCR;
  1737. mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1738. /* Determine divisor based on baud rate */
  1739. baud = tty_get_baud_rate(tty);
  1740. if (!baud) {
  1741. /* pick a default, any default... */
  1742. dbg("%s\n", "Picked default baud...");
  1743. baud = 9600;
  1744. }
  1745. dbg("%s - baud rate = %d", __func__, baud);
  1746. status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
  1747. /* Enable Interrupts */
  1748. Data = 0x0c;
  1749. mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
  1750. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1751. mos7840_port->read_urb->dev = serial->dev;
  1752. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1753. if (status) {
  1754. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1755. status);
  1756. }
  1757. }
  1758. wake_up(&mos7840_port->delta_msr_wait);
  1759. mos7840_port->delta_msr_cond = 1;
  1760. dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
  1761. mos7840_port->shadowLCR);
  1762. return;
  1763. }
  1764. /*****************************************************************************
  1765. * mos7840_set_termios
  1766. * this function is called by the tty driver when it wants to change
  1767. * the termios structure
  1768. *****************************************************************************/
  1769. static void mos7840_set_termios(struct tty_struct *tty,
  1770. struct usb_serial_port *port,
  1771. struct ktermios *old_termios)
  1772. {
  1773. int status;
  1774. unsigned int cflag;
  1775. struct usb_serial *serial;
  1776. struct moschip_port *mos7840_port;
  1777. dbg("mos7840_set_termios: START\n");
  1778. if (mos7840_port_paranoia_check(port, __func__)) {
  1779. dbg("%s", "Invalid port \n");
  1780. return;
  1781. }
  1782. serial = port->serial;
  1783. if (mos7840_serial_paranoia_check(serial, __func__)) {
  1784. dbg("%s", "Invalid Serial \n");
  1785. return;
  1786. }
  1787. mos7840_port = mos7840_get_port_private(port);
  1788. if (mos7840_port == NULL)
  1789. return;
  1790. if (!mos7840_port->open) {
  1791. dbg("%s - port not opened", __func__);
  1792. return;
  1793. }
  1794. dbg("%s\n", "setting termios - ");
  1795. cflag = tty->termios->c_cflag;
  1796. dbg("%s - clfag %08x iflag %08x", __func__,
  1797. tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
  1798. dbg("%s - old clfag %08x old iflag %08x", __func__,
  1799. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1800. dbg("%s - port %d", __func__, port->number);
  1801. /* change the port settings to the new ones specified */
  1802. mos7840_change_port_settings(tty, mos7840_port, old_termios);
  1803. if (!mos7840_port->read_urb) {
  1804. dbg("%s", "URB KILLED !!!!!\n");
  1805. return;
  1806. }
  1807. if (mos7840_port->read_urb->status != -EINPROGRESS) {
  1808. mos7840_port->read_urb->dev = serial->dev;
  1809. status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
  1810. if (status) {
  1811. dbg(" usb_submit_urb(read bulk) failed, status = %d",
  1812. status);
  1813. }
  1814. }
  1815. return;
  1816. }
  1817. /*****************************************************************************
  1818. * mos7840_get_lsr_info - get line status register info
  1819. *
  1820. * Purpose: Let user call ioctl() to get info when the UART physically
  1821. * is emptied. On bus types like RS485, the transmitter must
  1822. * release the bus after transmitting. This must be done when
  1823. * the transmit shift register is empty, not be done when the
  1824. * transmit holding register is empty. This functionality
  1825. * allows an RS485 driver to be written in user space.
  1826. *****************************************************************************/
  1827. static int mos7840_get_lsr_info(struct tty_struct *tty,
  1828. unsigned int __user *value)
  1829. {
  1830. int count;
  1831. unsigned int result = 0;
  1832. count = mos7840_chars_in_buffer(tty);
  1833. if (count == 0) {
  1834. dbg("%s -- Empty", __func__);
  1835. result = TIOCSER_TEMT;
  1836. }
  1837. if (copy_to_user(value, &result, sizeof(int)))
  1838. return -EFAULT;
  1839. return 0;
  1840. }
  1841. /*****************************************************************************
  1842. * mos7840_set_modem_info
  1843. * function to set modem info
  1844. *****************************************************************************/
  1845. /* FIXME: Should be using the model control hooks */
  1846. static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
  1847. unsigned int cmd, unsigned int __user *value)
  1848. {
  1849. unsigned int mcr;
  1850. unsigned int arg;
  1851. __u16 Data;
  1852. int status;
  1853. struct usb_serial_port *port;
  1854. if (mos7840_port == NULL)
  1855. return -1;
  1856. port = (struct usb_serial_port *)mos7840_port->port;
  1857. if (mos7840_port_paranoia_check(port, __func__)) {
  1858. dbg("%s", "Invalid port \n");
  1859. return -1;
  1860. }
  1861. mcr = mos7840_port->shadowMCR;
  1862. if (copy_from_user(&arg, value, sizeof(int)))
  1863. return -EFAULT;
  1864. switch (cmd) {
  1865. case TIOCMBIS:
  1866. if (arg & TIOCM_RTS)
  1867. mcr |= MCR_RTS;
  1868. if (arg & TIOCM_DTR)
  1869. mcr |= MCR_RTS;
  1870. if (arg & TIOCM_LOOP)
  1871. mcr |= MCR_LOOPBACK;
  1872. break;
  1873. case TIOCMBIC:
  1874. if (arg & TIOCM_RTS)
  1875. mcr &= ~MCR_RTS;
  1876. if (arg & TIOCM_DTR)
  1877. mcr &= ~MCR_RTS;
  1878. if (arg & TIOCM_LOOP)
  1879. mcr &= ~MCR_LOOPBACK;
  1880. break;
  1881. case TIOCMSET:
  1882. /* turn off the RTS and DTR and LOOPBACK
  1883. * and then only turn on what was asked to */
  1884. mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
  1885. mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
  1886. mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
  1887. mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
  1888. break;
  1889. }
  1890. mos7840_port->shadowMCR = mcr;
  1891. Data = mos7840_port->shadowMCR;
  1892. status = 0;
  1893. status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
  1894. if (status < 0) {
  1895. dbg("setting MODEM_CONTROL_REGISTER Failed\n");
  1896. return -1;
  1897. }
  1898. return 0;
  1899. }
  1900. /*****************************************************************************
  1901. * mos7840_get_modem_info
  1902. * function to get modem info
  1903. *****************************************************************************/
  1904. static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
  1905. unsigned int __user *value)
  1906. {
  1907. unsigned int result = 0;
  1908. __u16 msr;
  1909. unsigned int mcr = mos7840_port->shadowMCR;
  1910. int status = 0;
  1911. status = mos7840_get_uart_reg(mos7840_port->port,
  1912. MODEM_STATUS_REGISTER, &msr);
  1913. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1914. |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1915. |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1916. |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
  1917. |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1918. |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1919. dbg("%s -- %x", __func__, result);
  1920. if (copy_to_user(value, &result, sizeof(int)))
  1921. return -EFAULT;
  1922. return 0;
  1923. }
  1924. /*****************************************************************************
  1925. * mos7840_get_serial_info
  1926. * function to get information about serial port
  1927. *****************************************************************************/
  1928. static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
  1929. struct serial_struct __user *retinfo)
  1930. {
  1931. struct serial_struct tmp;
  1932. if (mos7840_port == NULL)
  1933. return -1;
  1934. if (!retinfo)
  1935. return -EFAULT;
  1936. memset(&tmp, 0, sizeof(tmp));
  1937. tmp.type = PORT_16550A;
  1938. tmp.line = mos7840_port->port->serial->minor;
  1939. tmp.port = mos7840_port->port->number;
  1940. tmp.irq = 0;
  1941. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1942. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1943. tmp.baud_base = 9600;
  1944. tmp.close_delay = 5 * HZ;
  1945. tmp.closing_wait = 30 * HZ;
  1946. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1947. return -EFAULT;
  1948. return 0;
  1949. }
  1950. /*****************************************************************************
  1951. * SerialIoctl
  1952. * this function handles any ioctl calls to the driver
  1953. *****************************************************************************/
  1954. static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
  1955. unsigned int cmd, unsigned long arg)
  1956. {
  1957. struct usb_serial_port *port = tty->driver_data;
  1958. void __user *argp = (void __user *)arg;
  1959. struct moschip_port *mos7840_port;
  1960. struct async_icount cnow;
  1961. struct async_icount cprev;
  1962. struct serial_icounter_struct icount;
  1963. int mosret = 0;
  1964. if (mos7840_port_paranoia_check(port, __func__)) {
  1965. dbg("%s", "Invalid port \n");
  1966. return -1;
  1967. }
  1968. mos7840_port = mos7840_get_port_private(port);
  1969. if (mos7840_port == NULL)
  1970. return -1;
  1971. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1972. switch (cmd) {
  1973. /* return number of bytes available */
  1974. case TIOCSERGETLSR:
  1975. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1976. return mos7840_get_lsr_info(tty, argp);
  1977. return 0;
  1978. /* FIXME: use the modem hooks and remove this */
  1979. case TIOCMBIS:
  1980. case TIOCMBIC:
  1981. case TIOCMSET:
  1982. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
  1983. port->number);
  1984. mosret =
  1985. mos7840_set_modem_info(mos7840_port, cmd, argp);
  1986. return mosret;
  1987. case TIOCMGET:
  1988. dbg("%s (%d) TIOCMGET", __func__, port->number);
  1989. return mos7840_get_modem_info(mos7840_port, argp);
  1990. case TIOCGSERIAL:
  1991. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1992. return mos7840_get_serial_info(mos7840_port, argp);
  1993. case TIOCSSERIAL:
  1994. dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
  1995. break;
  1996. case TIOCMIWAIT:
  1997. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1998. cprev = mos7840_port->icount;
  1999. while (1) {
  2000. //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
  2001. mos7840_port->delta_msr_cond = 0;
  2002. wait_event_interruptible(mos7840_port->delta_msr_wait,
  2003. (mos7840_port->
  2004. delta_msr_cond == 1));
  2005. /* see if a signal did it */
  2006. if (signal_pending(current))
  2007. return -ERESTARTSYS;
  2008. cnow = mos7840_port->icount;
  2009. smp_rmb();
  2010. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2011. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  2012. return -EIO; /* no change => error */
  2013. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  2014. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  2015. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  2016. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  2017. return 0;
  2018. }
  2019. cprev = cnow;
  2020. }
  2021. /* NOTREACHED */
  2022. break;
  2023. case TIOCGICOUNT:
  2024. cnow = mos7840_port->icount;
  2025. smp_rmb();
  2026. icount.cts = cnow.cts;
  2027. icount.dsr = cnow.dsr;
  2028. icount.rng = cnow.rng;
  2029. icount.dcd = cnow.dcd;
  2030. icount.rx = cnow.rx;
  2031. icount.tx = cnow.tx;
  2032. icount.frame = cnow.frame;
  2033. icount.overrun = cnow.overrun;
  2034. icount.parity = cnow.parity;
  2035. icount.brk = cnow.brk;
  2036. icount.buf_overrun = cnow.buf_overrun;
  2037. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  2038. port->number, icount.rx, icount.tx);
  2039. if (copy_to_user(argp, &icount, sizeof(icount)))
  2040. return -EFAULT;
  2041. return 0;
  2042. default:
  2043. break;
  2044. }
  2045. return -ENOIOCTLCMD;
  2046. }
  2047. static int mos7840_calc_num_ports(struct usb_serial *serial)
  2048. {
  2049. int mos7840_num_ports = 0;
  2050. dbg("numberofendpoints: %d \n",
  2051. (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
  2052. dbg("numberofendpoints: %d \n",
  2053. (int)serial->interface->altsetting->desc.bNumEndpoints);
  2054. if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
  2055. mos7840_num_ports = serial->num_ports = 2;
  2056. } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
  2057. serial->num_bulk_in = 4;
  2058. serial->num_bulk_out = 4;
  2059. mos7840_num_ports = serial->num_ports = 4;
  2060. }
  2061. return mos7840_num_ports;
  2062. }
  2063. /****************************************************************************
  2064. * mos7840_startup
  2065. ****************************************************************************/
  2066. static int mos7840_startup(struct usb_serial *serial)
  2067. {
  2068. struct moschip_port *mos7840_port;
  2069. struct usb_device *dev;
  2070. int i, status;
  2071. __u16 Data;
  2072. dbg("%s \n", " mos7840_startup :entering..........");
  2073. if (!serial) {
  2074. dbg("%s\n", "Invalid Handler");
  2075. return -1;
  2076. }
  2077. dev = serial->dev;
  2078. dbg("%s\n", "Entering...");
  2079. /* we set up the pointers to the endpoints in the mos7840_open *
  2080. * function, as the structures aren't created yet. */
  2081. /* set up port private structures */
  2082. for (i = 0; i < serial->num_ports; ++i) {
  2083. mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  2084. if (mos7840_port == NULL) {
  2085. err("%s - Out of memory", __func__);
  2086. status = -ENOMEM;
  2087. i--; /* don't follow NULL pointer cleaning up */
  2088. goto error;
  2089. }
  2090. /* Initialize all port interrupt end point to port 0 int endpoint *
  2091. * Our device has only one interrupt end point comman to all port */
  2092. mos7840_port->port = serial->port[i];
  2093. mos7840_set_port_private(serial->port[i], mos7840_port);
  2094. spin_lock_init(&mos7840_port->pool_lock);
  2095. mos7840_port->port_num = ((serial->port[i]->number -
  2096. (serial->port[i]->serial->minor)) +
  2097. 1);
  2098. if (mos7840_port->port_num == 1) {
  2099. mos7840_port->SpRegOffset = 0x0;
  2100. mos7840_port->ControlRegOffset = 0x1;
  2101. mos7840_port->DcrRegOffset = 0x4;
  2102. } else if ((mos7840_port->port_num == 2)
  2103. && (serial->num_ports == 4)) {
  2104. mos7840_port->SpRegOffset = 0x8;
  2105. mos7840_port->ControlRegOffset = 0x9;
  2106. mos7840_port->DcrRegOffset = 0x16;
  2107. } else if ((mos7840_port->port_num == 2)
  2108. && (serial->num_ports == 2)) {
  2109. mos7840_port->SpRegOffset = 0xa;
  2110. mos7840_port->ControlRegOffset = 0xb;
  2111. mos7840_port->DcrRegOffset = 0x19;
  2112. } else if ((mos7840_port->port_num == 3)
  2113. && (serial->num_ports == 4)) {
  2114. mos7840_port->SpRegOffset = 0xa;
  2115. mos7840_port->ControlRegOffset = 0xb;
  2116. mos7840_port->DcrRegOffset = 0x19;
  2117. } else if ((mos7840_port->port_num == 4)
  2118. && (serial->num_ports == 4)) {
  2119. mos7840_port->SpRegOffset = 0xc;
  2120. mos7840_port->ControlRegOffset = 0xd;
  2121. mos7840_port->DcrRegOffset = 0x1c;
  2122. }
  2123. mos7840_dump_serial_port(mos7840_port);
  2124. mos7840_set_port_private(serial->port[i], mos7840_port);
  2125. //enable rx_disable bit in control register
  2126. status =
  2127. mos7840_get_reg_sync(serial->port[i],
  2128. mos7840_port->ControlRegOffset, &Data);
  2129. if (status < 0) {
  2130. dbg("Reading ControlReg failed status-0x%x\n", status);
  2131. break;
  2132. } else
  2133. dbg("ControlReg Reading success val is %x, status%d\n",
  2134. Data, status);
  2135. Data |= 0x08; //setting driver done bit
  2136. Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
  2137. //Data |= 0x20; //rx_disable bit
  2138. status = 0;
  2139. status =
  2140. mos7840_set_reg_sync(serial->port[i],
  2141. mos7840_port->ControlRegOffset, Data);
  2142. if (status < 0) {
  2143. dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
  2144. break;
  2145. } else
  2146. dbg("ControlReg Writing success(rx_disable) status%d\n",
  2147. status);
  2148. //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
  2149. Data = 0x01;
  2150. status = 0;
  2151. status =
  2152. mos7840_set_reg_sync(serial->port[i],
  2153. (__u16) (mos7840_port->DcrRegOffset +
  2154. 0), Data);
  2155. if (status < 0) {
  2156. dbg("Writing DCR0 failed status-0x%x\n", status);
  2157. break;
  2158. } else
  2159. dbg("DCR0 Writing success status%d\n", status);
  2160. Data = 0x05;
  2161. status = 0;
  2162. status =
  2163. mos7840_set_reg_sync(serial->port[i],
  2164. (__u16) (mos7840_port->DcrRegOffset +
  2165. 1), Data);
  2166. if (status < 0) {
  2167. dbg("Writing DCR1 failed status-0x%x\n", status);
  2168. break;
  2169. } else
  2170. dbg("DCR1 Writing success status%d\n", status);
  2171. Data = 0x24;
  2172. status = 0;
  2173. status =
  2174. mos7840_set_reg_sync(serial->port[i],
  2175. (__u16) (mos7840_port->DcrRegOffset +
  2176. 2), Data);
  2177. if (status < 0) {
  2178. dbg("Writing DCR2 failed status-0x%x\n", status);
  2179. break;
  2180. } else
  2181. dbg("DCR2 Writing success status%d\n", status);
  2182. // write values in clkstart0x0 and clkmulti 0x20
  2183. Data = 0x0;
  2184. status = 0;
  2185. status =
  2186. mos7840_set_reg_sync(serial->port[i],
  2187. CLK_START_VALUE_REGISTER, Data);
  2188. if (status < 0) {
  2189. dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
  2190. break;
  2191. } else
  2192. dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
  2193. Data = 0x20;
  2194. status =
  2195. mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
  2196. Data);
  2197. if (status < 0) {
  2198. dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
  2199. status);
  2200. goto error;
  2201. } else
  2202. dbg("CLK_MULTI_REGISTER Writing success status%d\n",
  2203. status);
  2204. //write value 0x0 to scratchpad register
  2205. Data = 0x00;
  2206. status =
  2207. mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
  2208. Data);
  2209. if (status < 0) {
  2210. dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
  2211. status);
  2212. break;
  2213. } else
  2214. dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
  2215. status);
  2216. //Zero Length flag register
  2217. if ((mos7840_port->port_num != 1)
  2218. && (serial->num_ports == 2)) {
  2219. Data = 0xff;
  2220. status = 0;
  2221. status = mos7840_set_reg_sync(serial->port[i],
  2222. (__u16) (ZLP_REG1 +
  2223. ((__u16)
  2224. mos7840_port->
  2225. port_num)),
  2226. Data);
  2227. dbg("ZLIP offset%x\n",
  2228. (__u16) (ZLP_REG1 +
  2229. ((__u16) mos7840_port->port_num)));
  2230. if (status < 0) {
  2231. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2232. i + 2, status);
  2233. break;
  2234. } else
  2235. dbg("ZLP_REG%d Writing success status%d\n",
  2236. i + 2, status);
  2237. } else {
  2238. Data = 0xff;
  2239. status = 0;
  2240. status = mos7840_set_reg_sync(serial->port[i],
  2241. (__u16) (ZLP_REG1 +
  2242. ((__u16)
  2243. mos7840_port->
  2244. port_num) -
  2245. 0x1), Data);
  2246. dbg("ZLIP offset%x\n",
  2247. (__u16) (ZLP_REG1 +
  2248. ((__u16) mos7840_port->port_num) - 0x1));
  2249. if (status < 0) {
  2250. dbg("Writing ZLP_REG%d failed status-0x%x\n",
  2251. i + 1, status);
  2252. break;
  2253. } else
  2254. dbg("ZLP_REG%d Writing success status%d\n",
  2255. i + 1, status);
  2256. }
  2257. mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  2258. mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
  2259. mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  2260. if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
  2261. status = -ENOMEM;
  2262. goto error;
  2263. }
  2264. }
  2265. //Zero Length flag enable
  2266. Data = 0x0f;
  2267. status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
  2268. if (status < 0) {
  2269. dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
  2270. goto error;
  2271. } else
  2272. dbg("ZLP_REG5 Writing success status%d\n", status);
  2273. /* setting configuration feature to one */
  2274. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  2275. (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
  2276. return 0;
  2277. error:
  2278. for (/* nothing */; i >= 0; i--) {
  2279. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2280. kfree(mos7840_port->dr);
  2281. kfree(mos7840_port->ctrl_buf);
  2282. usb_free_urb(mos7840_port->control_urb);
  2283. kfree(mos7840_port);
  2284. serial->port[i] = NULL;
  2285. }
  2286. return status;
  2287. }
  2288. /****************************************************************************
  2289. * mos7840_shutdown
  2290. * This function is called whenever the device is removed from the usb bus.
  2291. ****************************************************************************/
  2292. static void mos7840_shutdown(struct usb_serial *serial)
  2293. {
  2294. int i;
  2295. unsigned long flags;
  2296. struct moschip_port *mos7840_port;
  2297. dbg("%s \n", " shutdown :entering..........");
  2298. if (!serial) {
  2299. dbg("%s", "Invalid Handler \n");
  2300. return;
  2301. }
  2302. /* check for the ports to be closed,close the ports and disconnect */
  2303. /* free private structure allocated for serial port *
  2304. * stop reads and writes on all ports */
  2305. for (i = 0; i < serial->num_ports; ++i) {
  2306. mos7840_port = mos7840_get_port_private(serial->port[i]);
  2307. spin_lock_irqsave(&mos7840_port->pool_lock, flags);
  2308. mos7840_port->zombie = 1;
  2309. spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
  2310. usb_kill_urb(mos7840_port->control_urb);
  2311. kfree(mos7840_port->ctrl_buf);
  2312. kfree(mos7840_port->dr);
  2313. kfree(mos7840_port);
  2314. mos7840_set_port_private(serial->port[i], NULL);
  2315. }
  2316. dbg("%s\n", "Thank u :: ");
  2317. }
  2318. static struct usb_driver io_driver = {
  2319. .name = "mos7840",
  2320. .probe = usb_serial_probe,
  2321. .disconnect = usb_serial_disconnect,
  2322. .id_table = moschip_id_table_combined,
  2323. .no_dynamic_id = 1,
  2324. };
  2325. static struct usb_serial_driver moschip7840_4port_device = {
  2326. .driver = {
  2327. .owner = THIS_MODULE,
  2328. .name = "mos7840",
  2329. },
  2330. .description = DRIVER_DESC,
  2331. .usb_driver = &io_driver,
  2332. .id_table = moschip_port_id_table,
  2333. .num_ports = 4,
  2334. .open = mos7840_open,
  2335. .close = mos7840_close,
  2336. .write = mos7840_write,
  2337. .write_room = mos7840_write_room,
  2338. .chars_in_buffer = mos7840_chars_in_buffer,
  2339. .throttle = mos7840_throttle,
  2340. .unthrottle = mos7840_unthrottle,
  2341. .calc_num_ports = mos7840_calc_num_ports,
  2342. #ifdef MCSSerialProbe
  2343. .probe = mos7840_serial_probe,
  2344. #endif
  2345. .ioctl = mos7840_ioctl,
  2346. .set_termios = mos7840_set_termios,
  2347. .break_ctl = mos7840_break,
  2348. .tiocmget = mos7840_tiocmget,
  2349. .tiocmset = mos7840_tiocmset,
  2350. .attach = mos7840_startup,
  2351. .shutdown = mos7840_shutdown,
  2352. .read_bulk_callback = mos7840_bulk_in_callback,
  2353. .read_int_callback = mos7840_interrupt_callback,
  2354. };
  2355. /****************************************************************************
  2356. * moschip7840_init
  2357. * This is called by the module subsystem, or on startup to initialize us
  2358. ****************************************************************************/
  2359. static int __init moschip7840_init(void)
  2360. {
  2361. int retval;
  2362. dbg("%s \n", " mos7840_init :entering..........");
  2363. /* Register with the usb serial */
  2364. retval = usb_serial_register(&moschip7840_4port_device);
  2365. if (retval)
  2366. goto failed_port_device_register;
  2367. dbg("%s\n", "Entring...");
  2368. info(DRIVER_DESC " " DRIVER_VERSION);
  2369. /* Register with the usb */
  2370. retval = usb_register(&io_driver);
  2371. if (retval)
  2372. goto failed_usb_register;
  2373. if (retval == 0) {
  2374. dbg("%s\n", "Leaving...");
  2375. return 0;
  2376. }
  2377. failed_usb_register:
  2378. usb_serial_deregister(&moschip7840_4port_device);
  2379. failed_port_device_register:
  2380. return retval;
  2381. }
  2382. /****************************************************************************
  2383. * moschip7840_exit
  2384. * Called when the driver is about to be unloaded.
  2385. ****************************************************************************/
  2386. static void __exit moschip7840_exit(void)
  2387. {
  2388. dbg("%s \n", " mos7840_exit :entering..........");
  2389. usb_deregister(&io_driver);
  2390. usb_serial_deregister(&moschip7840_4port_device);
  2391. dbg("%s\n", "Entring...");
  2392. }
  2393. module_init(moschip7840_init);
  2394. module_exit(moschip7840_exit);
  2395. /* Module information */
  2396. MODULE_DESCRIPTION(DRIVER_DESC);
  2397. MODULE_LICENSE("GPL");
  2398. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2399. MODULE_PARM_DESC(debug, "Debug enabled or not");