mos7840.c 76 KB

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