mos7840.c 73 KB

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