mos7840.c 78 KB

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