mos7840.c 77 KB

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