mos7840.c 77 KB

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