mos7840.c 76 KB

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