mos7840.c 78 KB

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