mos7720.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  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. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/parport.h>
  38. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  39. #define DRIVER_DESC "Moschip USB Serial Driver"
  40. /* default urb timeout */
  41. #define MOS_WDR_TIMEOUT (HZ * 5)
  42. #define MOS_MAX_PORT 0x02
  43. #define MOS_WRITE 0x0E
  44. #define MOS_READ 0x0D
  45. /* Interrupt Rotinue Defines */
  46. #define SERIAL_IIR_RLS 0x06
  47. #define SERIAL_IIR_RDA 0x04
  48. #define SERIAL_IIR_CTI 0x0c
  49. #define SERIAL_IIR_THR 0x02
  50. #define SERIAL_IIR_MS 0x00
  51. #define NUM_URBS 16 /* URB Count */
  52. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  53. /* This structure holds all of the local serial port information */
  54. struct moschip_port {
  55. __u8 shadowLCR; /* last LCR value received */
  56. __u8 shadowMCR; /* last MCR value received */
  57. __u8 shadowMSR; /* last MSR value received */
  58. char open;
  59. struct async_icount icount;
  60. struct usb_serial_port *port; /* loop back to the owner */
  61. struct urb *write_urb_pool[NUM_URBS];
  62. };
  63. static struct usb_serial_driver moschip7720_2port_driver;
  64. #define USB_VENDOR_ID_MOSCHIP 0x9710
  65. #define MOSCHIP_DEVICE_ID_7720 0x7720
  66. #define MOSCHIP_DEVICE_ID_7715 0x7715
  67. static const struct usb_device_id id_table[] = {
  68. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  69. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  70. { } /* terminating entry */
  71. };
  72. MODULE_DEVICE_TABLE(usb, id_table);
  73. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  74. /* initial values for parport regs */
  75. #define DCR_INIT_VAL 0x0c /* SLCTIN, nINIT */
  76. #define ECR_INIT_VAL 0x00 /* SPP mode */
  77. struct urbtracker {
  78. struct mos7715_parport *mos_parport;
  79. struct list_head urblist_entry;
  80. struct kref ref_count;
  81. struct urb *urb;
  82. };
  83. enum mos7715_pp_modes {
  84. SPP = 0<<5,
  85. PS2 = 1<<5, /* moschip calls this 'NIBBLE' mode */
  86. PPF = 2<<5, /* moschip calls this 'CB-FIFO mode */
  87. };
  88. struct mos7715_parport {
  89. struct parport *pp; /* back to containing struct */
  90. struct kref ref_count; /* to instance of this struct */
  91. struct list_head deferred_urbs; /* list deferred async urbs */
  92. struct list_head active_urbs; /* list async urbs in flight */
  93. spinlock_t listlock; /* protects list access */
  94. bool msg_pending; /* usb sync call pending */
  95. struct completion syncmsg_compl; /* usb sync call completed */
  96. struct tasklet_struct urb_tasklet; /* for sending deferred urbs */
  97. struct usb_serial *serial; /* back to containing struct */
  98. __u8 shadowECR; /* parallel port regs... */
  99. __u8 shadowDCR;
  100. atomic_t shadowDSR; /* updated in int-in callback */
  101. };
  102. /* lock guards against dereferencing NULL ptr in parport ops callbacks */
  103. static DEFINE_SPINLOCK(release_lock);
  104. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  105. static const unsigned int dummy; /* for clarity in register access fns */
  106. enum mos_regs {
  107. THR, /* serial port regs */
  108. RHR,
  109. IER,
  110. FCR,
  111. ISR,
  112. LCR,
  113. MCR,
  114. LSR,
  115. MSR,
  116. SPR,
  117. DLL,
  118. DLM,
  119. DPR, /* parallel port regs */
  120. DSR,
  121. DCR,
  122. ECR,
  123. SP1_REG, /* device control regs */
  124. SP2_REG, /* serial port 2 (7720 only) */
  125. PP_REG,
  126. SP_CONTROL_REG,
  127. };
  128. /*
  129. * Return the correct value for the Windex field of the setup packet
  130. * for a control endpoint message. See the 7715 datasheet.
  131. */
  132. static inline __u16 get_reg_index(enum mos_regs reg)
  133. {
  134. static const __u16 mos7715_index_lookup_table[] = {
  135. 0x00, /* THR */
  136. 0x00, /* RHR */
  137. 0x01, /* IER */
  138. 0x02, /* FCR */
  139. 0x02, /* ISR */
  140. 0x03, /* LCR */
  141. 0x04, /* MCR */
  142. 0x05, /* LSR */
  143. 0x06, /* MSR */
  144. 0x07, /* SPR */
  145. 0x00, /* DLL */
  146. 0x01, /* DLM */
  147. 0x00, /* DPR */
  148. 0x01, /* DSR */
  149. 0x02, /* DCR */
  150. 0x0a, /* ECR */
  151. 0x01, /* SP1_REG */
  152. 0x02, /* SP2_REG (7720 only) */
  153. 0x04, /* PP_REG (7715 only) */
  154. 0x08, /* SP_CONTROL_REG */
  155. };
  156. return mos7715_index_lookup_table[reg];
  157. }
  158. /*
  159. * Return the correct value for the upper byte of the Wvalue field of
  160. * the setup packet for a control endpoint message.
  161. */
  162. static inline __u16 get_reg_value(enum mos_regs reg,
  163. unsigned int serial_portnum)
  164. {
  165. if (reg >= SP1_REG) /* control reg */
  166. return 0x0000;
  167. else if (reg >= DPR) /* parallel port reg (7715 only) */
  168. return 0x0100;
  169. else /* serial port reg */
  170. return (serial_portnum + 2) << 8;
  171. }
  172. /*
  173. * Write data byte to the specified device register. The data is embedded in
  174. * the value field of the setup packet. serial_portnum is ignored for registers
  175. * not specific to a particular serial port.
  176. */
  177. static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  178. enum mos_regs reg, __u8 data)
  179. {
  180. struct usb_device *usbdev = serial->dev;
  181. unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
  182. __u8 request = (__u8)0x0e;
  183. __u8 requesttype = (__u8)0x40;
  184. __u16 index = get_reg_index(reg);
  185. __u16 value = get_reg_value(reg, serial_portnum) + data;
  186. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  187. index, NULL, 0, MOS_WDR_TIMEOUT);
  188. if (status < 0)
  189. dev_err(&usbdev->dev,
  190. "mos7720: usb_control_msg() failed: %d", status);
  191. return status;
  192. }
  193. /*
  194. * Read data byte from the specified device register. The data returned by the
  195. * device is embedded in the value field of the setup packet. serial_portnum is
  196. * ignored for registers that are not specific to a particular serial port.
  197. */
  198. static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
  199. enum mos_regs reg, __u8 *data)
  200. {
  201. struct usb_device *usbdev = serial->dev;
  202. unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
  203. __u8 request = (__u8)0x0d;
  204. __u8 requesttype = (__u8)0xc0;
  205. __u16 index = get_reg_index(reg);
  206. __u16 value = get_reg_value(reg, serial_portnum);
  207. int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
  208. index, data, 1, MOS_WDR_TIMEOUT);
  209. if (status < 0)
  210. dev_err(&usbdev->dev,
  211. "mos7720: usb_control_msg() failed: %d", status);
  212. return status;
  213. }
  214. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  215. static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
  216. enum mos7715_pp_modes mode)
  217. {
  218. mos_parport->shadowECR = mode;
  219. write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
  220. return 0;
  221. }
  222. static void destroy_mos_parport(struct kref *kref)
  223. {
  224. struct mos7715_parport *mos_parport =
  225. container_of(kref, struct mos7715_parport, ref_count);
  226. kfree(mos_parport);
  227. }
  228. static void destroy_urbtracker(struct kref *kref)
  229. {
  230. struct urbtracker *urbtrack =
  231. container_of(kref, struct urbtracker, ref_count);
  232. struct mos7715_parport *mos_parport = urbtrack->mos_parport;
  233. usb_free_urb(urbtrack->urb);
  234. kfree(urbtrack);
  235. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  236. }
  237. /*
  238. * This runs as a tasklet when sending an urb in a non-blocking parallel
  239. * port callback had to be deferred because the disconnect mutex could not be
  240. * obtained at the time.
  241. */
  242. static void send_deferred_urbs(unsigned long _mos_parport)
  243. {
  244. int ret_val;
  245. unsigned long flags;
  246. struct mos7715_parport *mos_parport = (void *)_mos_parport;
  247. struct urbtracker *urbtrack, *tmp;
  248. struct list_head *cursor, *next;
  249. struct device *dev;
  250. /* if release function ran, game over */
  251. if (unlikely(mos_parport->serial == NULL))
  252. return;
  253. dev = &mos_parport->serial->dev->dev;
  254. /* try again to get the mutex */
  255. if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
  256. dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
  257. tasklet_schedule(&mos_parport->urb_tasklet);
  258. return;
  259. }
  260. /* if device disconnected, game over */
  261. if (unlikely(mos_parport->serial->disconnected)) {
  262. mutex_unlock(&mos_parport->serial->disc_mutex);
  263. return;
  264. }
  265. spin_lock_irqsave(&mos_parport->listlock, flags);
  266. if (list_empty(&mos_parport->deferred_urbs)) {
  267. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  268. mutex_unlock(&mos_parport->serial->disc_mutex);
  269. dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
  270. return;
  271. }
  272. /* move contents of deferred_urbs list to active_urbs list and submit */
  273. list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
  274. list_move_tail(cursor, &mos_parport->active_urbs);
  275. list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
  276. urblist_entry) {
  277. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  278. dev_dbg(dev, "%s: urb submitted\n", __func__);
  279. if (ret_val) {
  280. dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
  281. list_del(&urbtrack->urblist_entry);
  282. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  283. }
  284. }
  285. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  286. mutex_unlock(&mos_parport->serial->disc_mutex);
  287. }
  288. /* callback for parallel port control urbs submitted asynchronously */
  289. static void async_complete(struct urb *urb)
  290. {
  291. struct urbtracker *urbtrack = urb->context;
  292. int status = urb->status;
  293. if (unlikely(status))
  294. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
  295. /* remove the urbtracker from the active_urbs list */
  296. spin_lock(&urbtrack->mos_parport->listlock);
  297. list_del(&urbtrack->urblist_entry);
  298. spin_unlock(&urbtrack->mos_parport->listlock);
  299. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  300. }
  301. static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
  302. enum mos_regs reg, __u8 data)
  303. {
  304. struct urbtracker *urbtrack;
  305. int ret_val;
  306. unsigned long flags;
  307. struct usb_ctrlrequest setup;
  308. struct usb_serial *serial = mos_parport->serial;
  309. struct usb_device *usbdev = serial->dev;
  310. /* create and initialize the control urb and containing urbtracker */
  311. urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
  312. if (urbtrack == NULL) {
  313. dev_err(&usbdev->dev, "out of memory");
  314. return -ENOMEM;
  315. }
  316. kref_get(&mos_parport->ref_count);
  317. urbtrack->mos_parport = mos_parport;
  318. urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
  319. if (urbtrack->urb == NULL) {
  320. dev_err(&usbdev->dev, "out of urbs");
  321. kfree(urbtrack);
  322. return -ENOMEM;
  323. }
  324. setup.bRequestType = (__u8)0x40;
  325. setup.bRequest = (__u8)0x0e;
  326. setup.wValue = get_reg_value(reg, dummy);
  327. setup.wIndex = get_reg_index(reg);
  328. setup.wLength = 0;
  329. usb_fill_control_urb(urbtrack->urb, usbdev,
  330. usb_sndctrlpipe(usbdev, 0),
  331. (unsigned char *)&setup,
  332. NULL, 0, async_complete, urbtrack);
  333. kref_init(&urbtrack->ref_count);
  334. INIT_LIST_HEAD(&urbtrack->urblist_entry);
  335. /*
  336. * get the disconnect mutex, or add tracker to the deferred_urbs list
  337. * and schedule a tasklet to try again later
  338. */
  339. if (!mutex_trylock(&serial->disc_mutex)) {
  340. spin_lock_irqsave(&mos_parport->listlock, flags);
  341. list_add_tail(&urbtrack->urblist_entry,
  342. &mos_parport->deferred_urbs);
  343. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  344. tasklet_schedule(&mos_parport->urb_tasklet);
  345. dev_dbg(&usbdev->dev, "tasklet scheduled");
  346. return 0;
  347. }
  348. /* bail if device disconnected */
  349. if (serial->disconnected) {
  350. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  351. mutex_unlock(&serial->disc_mutex);
  352. return -ENODEV;
  353. }
  354. /* add the tracker to the active_urbs list and submit */
  355. spin_lock_irqsave(&mos_parport->listlock, flags);
  356. list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
  357. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  358. ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
  359. mutex_unlock(&serial->disc_mutex);
  360. if (ret_val) {
  361. dev_err(&usbdev->dev,
  362. "%s: submit_urb() failed: %d", __func__, ret_val);
  363. spin_lock_irqsave(&mos_parport->listlock, flags);
  364. list_del(&urbtrack->urblist_entry);
  365. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  366. kref_put(&urbtrack->ref_count, destroy_urbtracker);
  367. return ret_val;
  368. }
  369. return 0;
  370. }
  371. /*
  372. * This is the the common top part of all parallel port callback operations that
  373. * send synchronous messages to the device. This implements convoluted locking
  374. * that avoids two scenarios: (1) a port operation is called after usbserial
  375. * has called our release function, at which point struct mos7715_parport has
  376. * been destroyed, and (2) the device has been disconnected, but usbserial has
  377. * not called the release function yet because someone has a serial port open.
  378. * The shared release_lock prevents the first, and the mutex and disconnected
  379. * flag maintained by usbserial covers the second. We also use the msg_pending
  380. * flag to ensure that all synchronous usb messgage calls have completed before
  381. * our release function can return.
  382. */
  383. static int parport_prologue(struct parport *pp)
  384. {
  385. struct mos7715_parport *mos_parport;
  386. spin_lock(&release_lock);
  387. mos_parport = pp->private_data;
  388. if (unlikely(mos_parport == NULL)) {
  389. /* release fn called, port struct destroyed */
  390. spin_unlock(&release_lock);
  391. return -1;
  392. }
  393. mos_parport->msg_pending = true; /* synch usb call pending */
  394. INIT_COMPLETION(mos_parport->syncmsg_compl);
  395. spin_unlock(&release_lock);
  396. mutex_lock(&mos_parport->serial->disc_mutex);
  397. if (mos_parport->serial->disconnected) {
  398. /* device disconnected */
  399. mutex_unlock(&mos_parport->serial->disc_mutex);
  400. mos_parport->msg_pending = false;
  401. complete(&mos_parport->syncmsg_compl);
  402. return -1;
  403. }
  404. return 0;
  405. }
  406. /*
  407. * This is the the common bottom part of all parallel port functions that send
  408. * synchronous messages to the device.
  409. */
  410. static inline void parport_epilogue(struct parport *pp)
  411. {
  412. struct mos7715_parport *mos_parport = pp->private_data;
  413. mutex_unlock(&mos_parport->serial->disc_mutex);
  414. mos_parport->msg_pending = false;
  415. complete(&mos_parport->syncmsg_compl);
  416. }
  417. static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
  418. {
  419. struct mos7715_parport *mos_parport = pp->private_data;
  420. if (parport_prologue(pp) < 0)
  421. return;
  422. mos7715_change_mode(mos_parport, SPP);
  423. write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
  424. parport_epilogue(pp);
  425. }
  426. static unsigned char parport_mos7715_read_data(struct parport *pp)
  427. {
  428. struct mos7715_parport *mos_parport = pp->private_data;
  429. unsigned char d;
  430. if (parport_prologue(pp) < 0)
  431. return 0;
  432. read_mos_reg(mos_parport->serial, dummy, DPR, &d);
  433. parport_epilogue(pp);
  434. return d;
  435. }
  436. static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
  437. {
  438. struct mos7715_parport *mos_parport = pp->private_data;
  439. __u8 data;
  440. if (parport_prologue(pp) < 0)
  441. return;
  442. data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
  443. write_mos_reg(mos_parport->serial, dummy, DCR, data);
  444. mos_parport->shadowDCR = data;
  445. parport_epilogue(pp);
  446. }
  447. static unsigned char parport_mos7715_read_control(struct parport *pp)
  448. {
  449. struct mos7715_parport *mos_parport = pp->private_data;
  450. __u8 dcr;
  451. spin_lock(&release_lock);
  452. mos_parport = pp->private_data;
  453. if (unlikely(mos_parport == NULL)) {
  454. spin_unlock(&release_lock);
  455. return 0;
  456. }
  457. dcr = mos_parport->shadowDCR & 0x0f;
  458. spin_unlock(&release_lock);
  459. return dcr;
  460. }
  461. static unsigned char parport_mos7715_frob_control(struct parport *pp,
  462. unsigned char mask,
  463. unsigned char val)
  464. {
  465. struct mos7715_parport *mos_parport = pp->private_data;
  466. __u8 dcr;
  467. mask &= 0x0f;
  468. val &= 0x0f;
  469. if (parport_prologue(pp) < 0)
  470. return 0;
  471. mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
  472. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  473. dcr = mos_parport->shadowDCR & 0x0f;
  474. parport_epilogue(pp);
  475. return dcr;
  476. }
  477. static unsigned char parport_mos7715_read_status(struct parport *pp)
  478. {
  479. unsigned char status;
  480. struct mos7715_parport *mos_parport = pp->private_data;
  481. spin_lock(&release_lock);
  482. mos_parport = pp->private_data;
  483. if (unlikely(mos_parport == NULL)) { /* release called */
  484. spin_unlock(&release_lock);
  485. return 0;
  486. }
  487. status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
  488. spin_unlock(&release_lock);
  489. return status;
  490. }
  491. static void parport_mos7715_enable_irq(struct parport *pp)
  492. {
  493. }
  494. static void parport_mos7715_disable_irq(struct parport *pp)
  495. {
  496. }
  497. static void parport_mos7715_data_forward(struct parport *pp)
  498. {
  499. struct mos7715_parport *mos_parport = pp->private_data;
  500. if (parport_prologue(pp) < 0)
  501. return;
  502. mos7715_change_mode(mos_parport, PS2);
  503. mos_parport->shadowDCR &= ~0x20;
  504. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  505. parport_epilogue(pp);
  506. }
  507. static void parport_mos7715_data_reverse(struct parport *pp)
  508. {
  509. struct mos7715_parport *mos_parport = pp->private_data;
  510. if (parport_prologue(pp) < 0)
  511. return;
  512. mos7715_change_mode(mos_parport, PS2);
  513. mos_parport->shadowDCR |= 0x20;
  514. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  515. parport_epilogue(pp);
  516. }
  517. static void parport_mos7715_init_state(struct pardevice *dev,
  518. struct parport_state *s)
  519. {
  520. s->u.pc.ctr = DCR_INIT_VAL;
  521. s->u.pc.ecr = ECR_INIT_VAL;
  522. }
  523. /* N.B. Parport core code requires that this function not block */
  524. static void parport_mos7715_save_state(struct parport *pp,
  525. struct parport_state *s)
  526. {
  527. struct mos7715_parport *mos_parport;
  528. spin_lock(&release_lock);
  529. mos_parport = pp->private_data;
  530. if (unlikely(mos_parport == NULL)) { /* release called */
  531. spin_unlock(&release_lock);
  532. return;
  533. }
  534. s->u.pc.ctr = mos_parport->shadowDCR;
  535. s->u.pc.ecr = mos_parport->shadowECR;
  536. spin_unlock(&release_lock);
  537. }
  538. /* N.B. Parport core code requires that this function not block */
  539. static void parport_mos7715_restore_state(struct parport *pp,
  540. struct parport_state *s)
  541. {
  542. struct mos7715_parport *mos_parport;
  543. spin_lock(&release_lock);
  544. mos_parport = pp->private_data;
  545. if (unlikely(mos_parport == NULL)) { /* release called */
  546. spin_unlock(&release_lock);
  547. return;
  548. }
  549. write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
  550. write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
  551. spin_unlock(&release_lock);
  552. }
  553. static size_t parport_mos7715_write_compat(struct parport *pp,
  554. const void *buffer,
  555. size_t len, int flags)
  556. {
  557. int retval;
  558. struct mos7715_parport *mos_parport = pp->private_data;
  559. int actual_len;
  560. if (parport_prologue(pp) < 0)
  561. return 0;
  562. mos7715_change_mode(mos_parport, PPF);
  563. retval = usb_bulk_msg(mos_parport->serial->dev,
  564. usb_sndbulkpipe(mos_parport->serial->dev, 2),
  565. (void *)buffer, len, &actual_len,
  566. MOS_WDR_TIMEOUT);
  567. parport_epilogue(pp);
  568. if (retval) {
  569. dev_err(&mos_parport->serial->dev->dev,
  570. "mos7720: usb_bulk_msg() failed: %d", retval);
  571. return 0;
  572. }
  573. return actual_len;
  574. }
  575. static struct parport_operations parport_mos7715_ops = {
  576. .owner = THIS_MODULE,
  577. .write_data = parport_mos7715_write_data,
  578. .read_data = parport_mos7715_read_data,
  579. .write_control = parport_mos7715_write_control,
  580. .read_control = parport_mos7715_read_control,
  581. .frob_control = parport_mos7715_frob_control,
  582. .read_status = parport_mos7715_read_status,
  583. .enable_irq = parport_mos7715_enable_irq,
  584. .disable_irq = parport_mos7715_disable_irq,
  585. .data_forward = parport_mos7715_data_forward,
  586. .data_reverse = parport_mos7715_data_reverse,
  587. .init_state = parport_mos7715_init_state,
  588. .save_state = parport_mos7715_save_state,
  589. .restore_state = parport_mos7715_restore_state,
  590. .compat_write_data = parport_mos7715_write_compat,
  591. .nibble_read_data = parport_ieee1284_read_nibble,
  592. .byte_read_data = parport_ieee1284_read_byte,
  593. };
  594. /*
  595. * Allocate and initialize parallel port control struct, initialize
  596. * the parallel port hardware device, and register with the parport subsystem.
  597. */
  598. static int mos7715_parport_init(struct usb_serial *serial)
  599. {
  600. struct mos7715_parport *mos_parport;
  601. /* allocate and initialize parallel port control struct */
  602. mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
  603. if (mos_parport == NULL) {
  604. dev_dbg(&serial->dev->dev, "%s: kzalloc failed\n", __func__);
  605. return -ENOMEM;
  606. }
  607. mos_parport->msg_pending = false;
  608. kref_init(&mos_parport->ref_count);
  609. spin_lock_init(&mos_parport->listlock);
  610. INIT_LIST_HEAD(&mos_parport->active_urbs);
  611. INIT_LIST_HEAD(&mos_parport->deferred_urbs);
  612. usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
  613. mos_parport->serial = serial;
  614. tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
  615. (unsigned long) mos_parport);
  616. init_completion(&mos_parport->syncmsg_compl);
  617. /* cycle parallel port reset bit */
  618. write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
  619. write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
  620. /* initialize device registers */
  621. mos_parport->shadowDCR = DCR_INIT_VAL;
  622. write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
  623. mos_parport->shadowECR = ECR_INIT_VAL;
  624. write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
  625. /* register with parport core */
  626. mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
  627. PARPORT_DMA_NONE,
  628. &parport_mos7715_ops);
  629. if (mos_parport->pp == NULL) {
  630. dev_err(&serial->interface->dev,
  631. "Could not register parport\n");
  632. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  633. return -EIO;
  634. }
  635. mos_parport->pp->private_data = mos_parport;
  636. mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
  637. mos_parport->pp->dev = &serial->interface->dev;
  638. parport_announce_port(mos_parport->pp);
  639. return 0;
  640. }
  641. #endif /* CONFIG_USB_SERIAL_MOS7715_PARPORT */
  642. /*
  643. * mos7720_interrupt_callback
  644. * this is the callback function for when we have received data on the
  645. * interrupt endpoint.
  646. */
  647. static void mos7720_interrupt_callback(struct urb *urb)
  648. {
  649. int result;
  650. int length;
  651. int status = urb->status;
  652. struct device *dev = &urb->dev->dev;
  653. __u8 *data;
  654. __u8 sp1;
  655. __u8 sp2;
  656. switch (status) {
  657. case 0:
  658. /* success */
  659. break;
  660. case -ECONNRESET:
  661. case -ENOENT:
  662. case -ESHUTDOWN:
  663. /* this urb is terminated, clean up */
  664. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  665. return;
  666. default:
  667. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  668. goto exit;
  669. }
  670. length = urb->actual_length;
  671. data = urb->transfer_buffer;
  672. /* Moschip get 4 bytes
  673. * Byte 1 IIR Port 1 (port.number is 0)
  674. * Byte 2 IIR Port 2 (port.number is 1)
  675. * Byte 3 --------------
  676. * Byte 4 FIFO status for both */
  677. /* the above description is inverted
  678. * oneukum 2007-03-14 */
  679. if (unlikely(length != 4)) {
  680. dev_dbg(dev, "Wrong data !!!\n");
  681. return;
  682. }
  683. sp1 = data[3];
  684. sp2 = data[2];
  685. if ((sp1 | sp2) & 0x01) {
  686. /* No Interrupt Pending in both the ports */
  687. dev_dbg(dev, "No Interrupt !!!\n");
  688. } else {
  689. switch (sp1 & 0x0f) {
  690. case SERIAL_IIR_RLS:
  691. dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
  692. break;
  693. case SERIAL_IIR_CTI:
  694. dev_dbg(dev, "Serial Port 1: Receiver time out\n");
  695. break;
  696. case SERIAL_IIR_MS:
  697. /* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
  698. break;
  699. }
  700. switch (sp2 & 0x0f) {
  701. case SERIAL_IIR_RLS:
  702. dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
  703. break;
  704. case SERIAL_IIR_CTI:
  705. dev_dbg(dev, "Serial Port 2: Receiver time out\n");
  706. break;
  707. case SERIAL_IIR_MS:
  708. /* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
  709. break;
  710. }
  711. }
  712. exit:
  713. result = usb_submit_urb(urb, GFP_ATOMIC);
  714. if (result)
  715. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  716. }
  717. /*
  718. * mos7715_interrupt_callback
  719. * this is the 7715's callback function for when we have received data on
  720. * the interrupt endpoint.
  721. */
  722. static void mos7715_interrupt_callback(struct urb *urb)
  723. {
  724. int result;
  725. int length;
  726. int status = urb->status;
  727. struct device *dev = &urb->dev->dev;
  728. __u8 *data;
  729. __u8 iir;
  730. switch (status) {
  731. case 0:
  732. /* success */
  733. break;
  734. case -ECONNRESET:
  735. case -ENOENT:
  736. case -ESHUTDOWN:
  737. case -ENODEV:
  738. /* this urb is terminated, clean up */
  739. dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
  740. return;
  741. default:
  742. dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
  743. goto exit;
  744. }
  745. length = urb->actual_length;
  746. data = urb->transfer_buffer;
  747. /* Structure of data from 7715 device:
  748. * Byte 1: IIR serial Port
  749. * Byte 2: unused
  750. * Byte 2: DSR parallel port
  751. * Byte 4: FIFO status for both */
  752. if (unlikely(length != 4)) {
  753. dev_dbg(dev, "Wrong data !!!\n");
  754. return;
  755. }
  756. iir = data[0];
  757. if (!(iir & 0x01)) { /* serial port interrupt pending */
  758. switch (iir & 0x0f) {
  759. case SERIAL_IIR_RLS:
  760. dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n\n");
  761. break;
  762. case SERIAL_IIR_CTI:
  763. dev_dbg(dev, "Serial Port: Receiver time out\n");
  764. break;
  765. case SERIAL_IIR_MS:
  766. /* dev_dbg(dev, "Serial Port: Modem status change\n"); */
  767. break;
  768. }
  769. }
  770. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  771. { /* update local copy of DSR reg */
  772. struct usb_serial_port *port = urb->context;
  773. struct mos7715_parport *mos_parport = port->serial->private;
  774. if (unlikely(mos_parport == NULL))
  775. return;
  776. atomic_set(&mos_parport->shadowDSR, data[2]);
  777. }
  778. #endif
  779. exit:
  780. result = usb_submit_urb(urb, GFP_ATOMIC);
  781. if (result)
  782. dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
  783. }
  784. /*
  785. * mos7720_bulk_in_callback
  786. * this is the callback function for when we have received data on the
  787. * bulk in endpoint.
  788. */
  789. static void mos7720_bulk_in_callback(struct urb *urb)
  790. {
  791. int retval;
  792. unsigned char *data ;
  793. struct usb_serial_port *port;
  794. struct tty_struct *tty;
  795. int status = urb->status;
  796. if (status) {
  797. dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
  798. return;
  799. }
  800. port = urb->context;
  801. dev_dbg(&port->dev, "Entering...%s\n", __func__);
  802. data = urb->transfer_buffer;
  803. tty = tty_port_tty_get(&port->port);
  804. if (tty && urb->actual_length) {
  805. tty_insert_flip_string(tty, data, urb->actual_length);
  806. tty_flip_buffer_push(tty);
  807. }
  808. tty_kref_put(tty);
  809. if (port->read_urb->status != -EINPROGRESS) {
  810. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  811. if (retval)
  812. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
  813. }
  814. }
  815. /*
  816. * mos7720_bulk_out_data_callback
  817. * this is the callback function for when we have finished sending serial
  818. * data on the bulk out endpoint.
  819. */
  820. static void mos7720_bulk_out_data_callback(struct urb *urb)
  821. {
  822. struct moschip_port *mos7720_port;
  823. struct tty_struct *tty;
  824. int status = urb->status;
  825. if (status) {
  826. dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
  827. return;
  828. }
  829. mos7720_port = urb->context;
  830. if (!mos7720_port) {
  831. dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
  832. return ;
  833. }
  834. tty = tty_port_tty_get(&mos7720_port->port->port);
  835. if (tty && mos7720_port->open)
  836. tty_wakeup(tty);
  837. tty_kref_put(tty);
  838. }
  839. /*
  840. * mos77xx_probe
  841. * this function installs the appropriate read interrupt endpoint callback
  842. * depending on whether the device is a 7720 or 7715, thus avoiding costly
  843. * run-time checks in the high-frequency callback routine itself.
  844. */
  845. static int mos77xx_probe(struct usb_serial *serial,
  846. const struct usb_device_id *id)
  847. {
  848. if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
  849. moschip7720_2port_driver.read_int_callback =
  850. mos7715_interrupt_callback;
  851. else
  852. moschip7720_2port_driver.read_int_callback =
  853. mos7720_interrupt_callback;
  854. return 0;
  855. }
  856. static int mos77xx_calc_num_ports(struct usb_serial *serial)
  857. {
  858. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  859. if (product == MOSCHIP_DEVICE_ID_7715)
  860. return 1;
  861. return 2;
  862. }
  863. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  864. {
  865. struct usb_serial *serial;
  866. struct urb *urb;
  867. struct moschip_port *mos7720_port;
  868. int response;
  869. int port_number;
  870. __u8 data;
  871. int allocated_urbs = 0;
  872. int j;
  873. serial = port->serial;
  874. mos7720_port = usb_get_serial_port_data(port);
  875. if (mos7720_port == NULL)
  876. return -ENODEV;
  877. usb_clear_halt(serial->dev, port->write_urb->pipe);
  878. usb_clear_halt(serial->dev, port->read_urb->pipe);
  879. /* Initialising the write urb pool */
  880. for (j = 0; j < NUM_URBS; ++j) {
  881. urb = usb_alloc_urb(0, GFP_KERNEL);
  882. mos7720_port->write_urb_pool[j] = urb;
  883. if (urb == NULL) {
  884. dev_err(&port->dev, "No more urbs???\n");
  885. continue;
  886. }
  887. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  888. GFP_KERNEL);
  889. if (!urb->transfer_buffer) {
  890. dev_err(&port->dev,
  891. "%s-out of memory for urb buffers.\n",
  892. __func__);
  893. usb_free_urb(mos7720_port->write_urb_pool[j]);
  894. mos7720_port->write_urb_pool[j] = NULL;
  895. continue;
  896. }
  897. allocated_urbs++;
  898. }
  899. if (!allocated_urbs)
  900. return -ENOMEM;
  901. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  902. *
  903. * Register Index
  904. * 0 : THR/RHR
  905. * 1 : IER
  906. * 2 : FCR
  907. * 3 : LCR
  908. * 4 : MCR
  909. * 5 : LSR
  910. * 6 : MSR
  911. * 7 : SPR
  912. *
  913. * 0x08 : SP1/2 Control Reg
  914. */
  915. port_number = port->number - port->serial->minor;
  916. read_mos_reg(serial, port_number, LSR, &data);
  917. dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
  918. write_mos_reg(serial, dummy, SP1_REG, 0x02);
  919. write_mos_reg(serial, dummy, SP2_REG, 0x02);
  920. write_mos_reg(serial, port_number, IER, 0x00);
  921. write_mos_reg(serial, port_number, FCR, 0x00);
  922. write_mos_reg(serial, port_number, FCR, 0xcf);
  923. mos7720_port->shadowLCR = 0x03;
  924. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  925. mos7720_port->shadowMCR = 0x0b;
  926. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  927. write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
  928. read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
  929. data = data | (port->number - port->serial->minor + 1);
  930. write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
  931. mos7720_port->shadowLCR = 0x83;
  932. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  933. write_mos_reg(serial, port_number, THR, 0x0c);
  934. write_mos_reg(serial, port_number, IER, 0x00);
  935. mos7720_port->shadowLCR = 0x03;
  936. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  937. write_mos_reg(serial, port_number, IER, 0x0c);
  938. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  939. if (response)
  940. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  941. __func__, response);
  942. /* initialize our icount structure */
  943. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  944. /* initialize our port settings */
  945. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  946. /* send a open port command */
  947. mos7720_port->open = 1;
  948. return 0;
  949. }
  950. /*
  951. * mos7720_chars_in_buffer
  952. * this function is called by the tty driver when it wants to know how many
  953. * bytes of data we currently have outstanding in the port (data that has
  954. * been written, but hasn't made it out the port yet)
  955. * If successful, we return the number of bytes left to be written in the
  956. * system,
  957. * Otherwise we return a negative error number.
  958. */
  959. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  960. {
  961. struct usb_serial_port *port = tty->driver_data;
  962. int i;
  963. int chars = 0;
  964. struct moschip_port *mos7720_port;
  965. mos7720_port = usb_get_serial_port_data(port);
  966. if (mos7720_port == NULL)
  967. return 0;
  968. for (i = 0; i < NUM_URBS; ++i) {
  969. if (mos7720_port->write_urb_pool[i] &&
  970. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  971. chars += URB_TRANSFER_BUFFER_SIZE;
  972. }
  973. dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
  974. return chars;
  975. }
  976. static void mos7720_close(struct usb_serial_port *port)
  977. {
  978. struct usb_serial *serial;
  979. struct moschip_port *mos7720_port;
  980. int j;
  981. serial = port->serial;
  982. mos7720_port = usb_get_serial_port_data(port);
  983. if (mos7720_port == NULL)
  984. return;
  985. for (j = 0; j < NUM_URBS; ++j)
  986. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  987. /* Freeing Write URBs */
  988. for (j = 0; j < NUM_URBS; ++j) {
  989. if (mos7720_port->write_urb_pool[j]) {
  990. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  991. usb_free_urb(mos7720_port->write_urb_pool[j]);
  992. }
  993. }
  994. /* While closing port, shutdown all bulk read, write *
  995. * and interrupt read if they exists, otherwise nop */
  996. usb_kill_urb(port->write_urb);
  997. usb_kill_urb(port->read_urb);
  998. mutex_lock(&serial->disc_mutex);
  999. /* these commands must not be issued if the device has
  1000. * been disconnected */
  1001. if (!serial->disconnected) {
  1002. write_mos_reg(serial, port->number - port->serial->minor,
  1003. MCR, 0x00);
  1004. write_mos_reg(serial, port->number - port->serial->minor,
  1005. IER, 0x00);
  1006. }
  1007. mutex_unlock(&serial->disc_mutex);
  1008. mos7720_port->open = 0;
  1009. }
  1010. static void mos7720_break(struct tty_struct *tty, int break_state)
  1011. {
  1012. struct usb_serial_port *port = tty->driver_data;
  1013. unsigned char data;
  1014. struct usb_serial *serial;
  1015. struct moschip_port *mos7720_port;
  1016. serial = port->serial;
  1017. mos7720_port = usb_get_serial_port_data(port);
  1018. if (mos7720_port == NULL)
  1019. return;
  1020. if (break_state == -1)
  1021. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  1022. else
  1023. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  1024. mos7720_port->shadowLCR = data;
  1025. write_mos_reg(serial, port->number - port->serial->minor,
  1026. LCR, mos7720_port->shadowLCR);
  1027. }
  1028. /*
  1029. * mos7720_write_room
  1030. * this function is called by the tty driver when it wants to know how many
  1031. * bytes of data we can accept for a specific port.
  1032. * If successful, we return the amount of room that we have for this port
  1033. * Otherwise we return a negative error number.
  1034. */
  1035. static int mos7720_write_room(struct tty_struct *tty)
  1036. {
  1037. struct usb_serial_port *port = tty->driver_data;
  1038. struct moschip_port *mos7720_port;
  1039. int room = 0;
  1040. int i;
  1041. mos7720_port = usb_get_serial_port_data(port);
  1042. if (mos7720_port == NULL)
  1043. return -ENODEV;
  1044. /* FIXME: Locking */
  1045. for (i = 0; i < NUM_URBS; ++i) {
  1046. if (mos7720_port->write_urb_pool[i] &&
  1047. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  1048. room += URB_TRANSFER_BUFFER_SIZE;
  1049. }
  1050. dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
  1051. return room;
  1052. }
  1053. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  1054. const unsigned char *data, int count)
  1055. {
  1056. int status;
  1057. int i;
  1058. int bytes_sent = 0;
  1059. int transfer_size;
  1060. struct moschip_port *mos7720_port;
  1061. struct usb_serial *serial;
  1062. struct urb *urb;
  1063. const unsigned char *current_position = data;
  1064. serial = port->serial;
  1065. mos7720_port = usb_get_serial_port_data(port);
  1066. if (mos7720_port == NULL)
  1067. return -ENODEV;
  1068. /* try to find a free urb in the list */
  1069. urb = NULL;
  1070. for (i = 0; i < NUM_URBS; ++i) {
  1071. if (mos7720_port->write_urb_pool[i] &&
  1072. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  1073. urb = mos7720_port->write_urb_pool[i];
  1074. dev_dbg(&port->dev, "URB:%d\n", i);
  1075. break;
  1076. }
  1077. }
  1078. if (urb == NULL) {
  1079. dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
  1080. goto exit;
  1081. }
  1082. if (urb->transfer_buffer == NULL) {
  1083. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  1084. GFP_KERNEL);
  1085. if (urb->transfer_buffer == NULL) {
  1086. dev_err_console(port, "%s no more kernel memory...\n",
  1087. __func__);
  1088. goto exit;
  1089. }
  1090. }
  1091. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  1092. memcpy(urb->transfer_buffer, current_position, transfer_size);
  1093. usb_serial_debug_data(&port->dev, __func__, transfer_size,
  1094. urb->transfer_buffer);
  1095. /* fill urb with data and submit */
  1096. usb_fill_bulk_urb(urb, serial->dev,
  1097. usb_sndbulkpipe(serial->dev,
  1098. port->bulk_out_endpointAddress),
  1099. urb->transfer_buffer, transfer_size,
  1100. mos7720_bulk_out_data_callback, mos7720_port);
  1101. /* send it down the pipe */
  1102. status = usb_submit_urb(urb, GFP_ATOMIC);
  1103. if (status) {
  1104. dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
  1105. "with status = %d\n", __func__, status);
  1106. bytes_sent = status;
  1107. goto exit;
  1108. }
  1109. bytes_sent = transfer_size;
  1110. exit:
  1111. return bytes_sent;
  1112. }
  1113. static void mos7720_throttle(struct tty_struct *tty)
  1114. {
  1115. struct usb_serial_port *port = tty->driver_data;
  1116. struct moschip_port *mos7720_port;
  1117. int status;
  1118. mos7720_port = usb_get_serial_port_data(port);
  1119. if (mos7720_port == NULL)
  1120. return;
  1121. if (!mos7720_port->open) {
  1122. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1123. return;
  1124. }
  1125. /* if we are implementing XON/XOFF, send the stop character */
  1126. if (I_IXOFF(tty)) {
  1127. unsigned char stop_char = STOP_CHAR(tty);
  1128. status = mos7720_write(tty, port, &stop_char, 1);
  1129. if (status <= 0)
  1130. return;
  1131. }
  1132. /* if we are implementing RTS/CTS, toggle that line */
  1133. if (tty->termios.c_cflag & CRTSCTS) {
  1134. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  1135. write_mos_reg(port->serial, port->number - port->serial->minor,
  1136. MCR, mos7720_port->shadowMCR);
  1137. if (status != 0)
  1138. return;
  1139. }
  1140. }
  1141. static void mos7720_unthrottle(struct tty_struct *tty)
  1142. {
  1143. struct usb_serial_port *port = tty->driver_data;
  1144. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1145. int status;
  1146. if (mos7720_port == NULL)
  1147. return;
  1148. if (!mos7720_port->open) {
  1149. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1150. return;
  1151. }
  1152. /* if we are implementing XON/XOFF, send the start character */
  1153. if (I_IXOFF(tty)) {
  1154. unsigned char start_char = START_CHAR(tty);
  1155. status = mos7720_write(tty, port, &start_char, 1);
  1156. if (status <= 0)
  1157. return;
  1158. }
  1159. /* if we are implementing RTS/CTS, toggle that line */
  1160. if (tty->termios.c_cflag & CRTSCTS) {
  1161. mos7720_port->shadowMCR |= UART_MCR_RTS;
  1162. write_mos_reg(port->serial, port->number - port->serial->minor,
  1163. MCR, mos7720_port->shadowMCR);
  1164. if (status != 0)
  1165. return;
  1166. }
  1167. }
  1168. /* FIXME: this function does not work */
  1169. static int set_higher_rates(struct moschip_port *mos7720_port,
  1170. unsigned int baud)
  1171. {
  1172. struct usb_serial_port *port;
  1173. struct usb_serial *serial;
  1174. int port_number;
  1175. enum mos_regs sp_reg;
  1176. if (mos7720_port == NULL)
  1177. return -EINVAL;
  1178. port = mos7720_port->port;
  1179. serial = port->serial;
  1180. /***********************************************
  1181. * Init Sequence for higher rates
  1182. ***********************************************/
  1183. dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
  1184. port_number = port->number - port->serial->minor;
  1185. write_mos_reg(serial, port_number, IER, 0x00);
  1186. write_mos_reg(serial, port_number, FCR, 0x00);
  1187. write_mos_reg(serial, port_number, FCR, 0xcf);
  1188. mos7720_port->shadowMCR = 0x0b;
  1189. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1190. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
  1191. /***********************************************
  1192. * Set for higher rates *
  1193. ***********************************************/
  1194. /* writing baud rate verbatum into uart clock field clearly not right */
  1195. if (port_number == 0)
  1196. sp_reg = SP1_REG;
  1197. else
  1198. sp_reg = SP2_REG;
  1199. write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
  1200. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
  1201. mos7720_port->shadowMCR = 0x2b;
  1202. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1203. /***********************************************
  1204. * Set DLL/DLM
  1205. ***********************************************/
  1206. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1207. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1208. write_mos_reg(serial, port_number, DLL, 0x01);
  1209. write_mos_reg(serial, port_number, DLM, 0x00);
  1210. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1211. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1212. return 0;
  1213. }
  1214. /* baud rate information */
  1215. struct divisor_table_entry {
  1216. __u32 baudrate;
  1217. __u16 divisor;
  1218. };
  1219. /* Define table of divisors for moschip 7720 hardware *
  1220. * These assume a 3.6864MHz crystal, the standard /16, and *
  1221. * MCR.7 = 0. */
  1222. static struct divisor_table_entry divisor_table[] = {
  1223. { 50, 2304},
  1224. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  1225. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  1226. { 150, 768},
  1227. { 300, 384},
  1228. { 600, 192},
  1229. { 1200, 96},
  1230. { 1800, 64},
  1231. { 2400, 48},
  1232. { 4800, 24},
  1233. { 7200, 16},
  1234. { 9600, 12},
  1235. { 19200, 6},
  1236. { 38400, 3},
  1237. { 57600, 2},
  1238. { 115200, 1},
  1239. };
  1240. /*****************************************************************************
  1241. * calc_baud_rate_divisor
  1242. * this function calculates the proper baud rate divisor for the specified
  1243. * baud rate.
  1244. *****************************************************************************/
  1245. static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
  1246. {
  1247. int i;
  1248. __u16 custom;
  1249. __u16 round1;
  1250. __u16 round;
  1251. dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
  1252. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  1253. if (divisor_table[i].baudrate == baudrate) {
  1254. *divisor = divisor_table[i].divisor;
  1255. return 0;
  1256. }
  1257. }
  1258. /* After trying for all the standard baud rates *
  1259. * Try calculating the divisor for this baud rate */
  1260. if (baudrate > 75 && baudrate < 230400) {
  1261. /* get the divisor */
  1262. custom = (__u16)(230400L / baudrate);
  1263. /* Check for round off */
  1264. round1 = (__u16)(2304000L / baudrate);
  1265. round = (__u16)(round1 - (custom * 10));
  1266. if (round > 4)
  1267. custom++;
  1268. *divisor = custom;
  1269. dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
  1270. return 0;
  1271. }
  1272. dev_dbg(&port->dev, "Baud calculation Failed...\n");
  1273. return -EINVAL;
  1274. }
  1275. /*
  1276. * send_cmd_write_baud_rate
  1277. * this function sends the proper command to change the baud rate of the
  1278. * specified port.
  1279. */
  1280. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  1281. int baudrate)
  1282. {
  1283. struct usb_serial_port *port;
  1284. struct usb_serial *serial;
  1285. int divisor;
  1286. int status;
  1287. unsigned char number;
  1288. if (mos7720_port == NULL)
  1289. return -1;
  1290. port = mos7720_port->port;
  1291. serial = port->serial;
  1292. number = port->number - port->serial->minor;
  1293. dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
  1294. /* Calculate the Divisor */
  1295. status = calc_baud_rate_divisor(port, baudrate, &divisor);
  1296. if (status) {
  1297. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  1298. return status;
  1299. }
  1300. /* Enable access to divisor latch */
  1301. mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
  1302. write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
  1303. /* Write the divisor */
  1304. write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
  1305. write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
  1306. /* Disable access to divisor latch */
  1307. mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  1308. write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
  1309. return status;
  1310. }
  1311. /*
  1312. * change_port_settings
  1313. * This routine is called to set the UART on the device to match
  1314. * the specified new settings.
  1315. */
  1316. static void change_port_settings(struct tty_struct *tty,
  1317. struct moschip_port *mos7720_port,
  1318. struct ktermios *old_termios)
  1319. {
  1320. struct usb_serial_port *port;
  1321. struct usb_serial *serial;
  1322. int baud;
  1323. unsigned cflag;
  1324. unsigned iflag;
  1325. __u8 mask = 0xff;
  1326. __u8 lData;
  1327. __u8 lParity;
  1328. __u8 lStop;
  1329. int status;
  1330. int port_number;
  1331. if (mos7720_port == NULL)
  1332. return ;
  1333. port = mos7720_port->port;
  1334. serial = port->serial;
  1335. port_number = port->number - port->serial->minor;
  1336. if (!mos7720_port->open) {
  1337. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1338. return;
  1339. }
  1340. lData = UART_LCR_WLEN8;
  1341. lStop = 0x00; /* 1 stop bit */
  1342. lParity = 0x00; /* No parity */
  1343. cflag = tty->termios.c_cflag;
  1344. iflag = tty->termios.c_iflag;
  1345. /* Change the number of bits */
  1346. switch (cflag & CSIZE) {
  1347. case CS5:
  1348. lData = UART_LCR_WLEN5;
  1349. mask = 0x1f;
  1350. break;
  1351. case CS6:
  1352. lData = UART_LCR_WLEN6;
  1353. mask = 0x3f;
  1354. break;
  1355. case CS7:
  1356. lData = UART_LCR_WLEN7;
  1357. mask = 0x7f;
  1358. break;
  1359. default:
  1360. case CS8:
  1361. lData = UART_LCR_WLEN8;
  1362. break;
  1363. }
  1364. /* Change the Parity bit */
  1365. if (cflag & PARENB) {
  1366. if (cflag & PARODD) {
  1367. lParity = UART_LCR_PARITY;
  1368. dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
  1369. } else {
  1370. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  1371. dev_dbg(&port->dev, "%s - parity = even\n", __func__);
  1372. }
  1373. } else {
  1374. dev_dbg(&port->dev, "%s - parity = none\n", __func__);
  1375. }
  1376. if (cflag & CMSPAR)
  1377. lParity = lParity | 0x20;
  1378. /* Change the Stop bit */
  1379. if (cflag & CSTOPB) {
  1380. lStop = UART_LCR_STOP;
  1381. dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
  1382. } else {
  1383. lStop = 0x00;
  1384. dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
  1385. }
  1386. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1387. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1388. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1389. /* Update the LCR with the correct value */
  1390. mos7720_port->shadowLCR &=
  1391. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1392. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1393. /* Disable Interrupts */
  1394. write_mos_reg(serial, port_number, IER, 0x00);
  1395. write_mos_reg(serial, port_number, FCR, 0x00);
  1396. write_mos_reg(serial, port_number, FCR, 0xcf);
  1397. /* Send the updated LCR value to the mos7720 */
  1398. write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
  1399. mos7720_port->shadowMCR = 0x0b;
  1400. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1401. /* set up the MCR register and send it to the mos7720 */
  1402. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1403. if (cflag & CBAUD)
  1404. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1405. if (cflag & CRTSCTS) {
  1406. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1407. /* To set hardware flow control to the specified *
  1408. * serial port, in SP1/2_CONTROL_REG */
  1409. if (port->number)
  1410. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
  1411. else
  1412. write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
  1413. } else
  1414. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1415. write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
  1416. /* Determine divisor based on baud rate */
  1417. baud = tty_get_baud_rate(tty);
  1418. if (!baud) {
  1419. /* pick a default, any default... */
  1420. dev_dbg(&port->dev, "Picked default baud...\n");
  1421. baud = 9600;
  1422. }
  1423. if (baud >= 230400) {
  1424. set_higher_rates(mos7720_port, baud);
  1425. /* Enable Interrupts */
  1426. write_mos_reg(serial, port_number, IER, 0x0c);
  1427. return;
  1428. }
  1429. dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
  1430. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1431. /* FIXME: needs to write actual resulting baud back not just
  1432. blindly do so */
  1433. if (cflag & CBAUD)
  1434. tty_encode_baud_rate(tty, baud, baud);
  1435. /* Enable Interrupts */
  1436. write_mos_reg(serial, port_number, IER, 0x0c);
  1437. if (port->read_urb->status != -EINPROGRESS) {
  1438. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1439. if (status)
  1440. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1441. }
  1442. }
  1443. /*
  1444. * mos7720_set_termios
  1445. * this function is called by the tty driver when it wants to change the
  1446. * termios structure.
  1447. */
  1448. static void mos7720_set_termios(struct tty_struct *tty,
  1449. struct usb_serial_port *port, struct ktermios *old_termios)
  1450. {
  1451. int status;
  1452. unsigned int cflag;
  1453. struct usb_serial *serial;
  1454. struct moschip_port *mos7720_port;
  1455. serial = port->serial;
  1456. mos7720_port = usb_get_serial_port_data(port);
  1457. if (mos7720_port == NULL)
  1458. return;
  1459. if (!mos7720_port->open) {
  1460. dev_dbg(&port->dev, "%s - port not opened\n", __func__);
  1461. return;
  1462. }
  1463. dev_dbg(&port->dev, "setting termios - ASPIRE\n");
  1464. cflag = tty->termios.c_cflag;
  1465. dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
  1466. tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
  1467. dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
  1468. old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
  1469. /* change the port settings to the new ones specified */
  1470. change_port_settings(tty, mos7720_port, old_termios);
  1471. if (port->read_urb->status != -EINPROGRESS) {
  1472. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1473. if (status)
  1474. dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
  1475. }
  1476. }
  1477. /*
  1478. * get_lsr_info - get line status register info
  1479. *
  1480. * Purpose: Let user call ioctl() to get info when the UART physically
  1481. * is emptied. On bus types like RS485, the transmitter must
  1482. * release the bus after transmitting. This must be done when
  1483. * the transmit shift register is empty, not be done when the
  1484. * transmit holding register is empty. This functionality
  1485. * allows an RS485 driver to be written in user space.
  1486. */
  1487. static int get_lsr_info(struct tty_struct *tty,
  1488. struct moschip_port *mos7720_port, unsigned int __user *value)
  1489. {
  1490. struct usb_serial_port *port = tty->driver_data;
  1491. unsigned int result = 0;
  1492. unsigned char data = 0;
  1493. int port_number = port->number - port->serial->minor;
  1494. int count;
  1495. count = mos7720_chars_in_buffer(tty);
  1496. if (count == 0) {
  1497. read_mos_reg(port->serial, port_number, LSR, &data);
  1498. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1499. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1500. dev_dbg(&port->dev, "%s -- Empty\n", __func__);
  1501. result = TIOCSER_TEMT;
  1502. }
  1503. }
  1504. if (copy_to_user(value, &result, sizeof(int)))
  1505. return -EFAULT;
  1506. return 0;
  1507. }
  1508. static int mos7720_tiocmget(struct tty_struct *tty)
  1509. {
  1510. struct usb_serial_port *port = tty->driver_data;
  1511. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1512. unsigned int result = 0;
  1513. unsigned int mcr ;
  1514. unsigned int msr ;
  1515. mcr = mos7720_port->shadowMCR;
  1516. msr = mos7720_port->shadowMSR;
  1517. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1518. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1519. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1520. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1521. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1522. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1523. return result;
  1524. }
  1525. static int mos7720_tiocmset(struct tty_struct *tty,
  1526. unsigned int set, unsigned int clear)
  1527. {
  1528. struct usb_serial_port *port = tty->driver_data;
  1529. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1530. unsigned int mcr ;
  1531. mcr = mos7720_port->shadowMCR;
  1532. if (set & TIOCM_RTS)
  1533. mcr |= UART_MCR_RTS;
  1534. if (set & TIOCM_DTR)
  1535. mcr |= UART_MCR_DTR;
  1536. if (set & TIOCM_LOOP)
  1537. mcr |= UART_MCR_LOOP;
  1538. if (clear & TIOCM_RTS)
  1539. mcr &= ~UART_MCR_RTS;
  1540. if (clear & TIOCM_DTR)
  1541. mcr &= ~UART_MCR_DTR;
  1542. if (clear & TIOCM_LOOP)
  1543. mcr &= ~UART_MCR_LOOP;
  1544. mos7720_port->shadowMCR = mcr;
  1545. write_mos_reg(port->serial, port->number - port->serial->minor,
  1546. MCR, mos7720_port->shadowMCR);
  1547. return 0;
  1548. }
  1549. static int mos7720_get_icount(struct tty_struct *tty,
  1550. struct serial_icounter_struct *icount)
  1551. {
  1552. struct usb_serial_port *port = tty->driver_data;
  1553. struct moschip_port *mos7720_port;
  1554. struct async_icount cnow;
  1555. mos7720_port = usb_get_serial_port_data(port);
  1556. cnow = mos7720_port->icount;
  1557. icount->cts = cnow.cts;
  1558. icount->dsr = cnow.dsr;
  1559. icount->rng = cnow.rng;
  1560. icount->dcd = cnow.dcd;
  1561. icount->rx = cnow.rx;
  1562. icount->tx = cnow.tx;
  1563. icount->frame = cnow.frame;
  1564. icount->overrun = cnow.overrun;
  1565. icount->parity = cnow.parity;
  1566. icount->brk = cnow.brk;
  1567. icount->buf_overrun = cnow.buf_overrun;
  1568. dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
  1569. icount->rx, icount->tx);
  1570. return 0;
  1571. }
  1572. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1573. unsigned int __user *value)
  1574. {
  1575. unsigned int mcr;
  1576. unsigned int arg;
  1577. struct usb_serial_port *port;
  1578. if (mos7720_port == NULL)
  1579. return -1;
  1580. port = (struct usb_serial_port *)mos7720_port->port;
  1581. mcr = mos7720_port->shadowMCR;
  1582. if (copy_from_user(&arg, value, sizeof(int)))
  1583. return -EFAULT;
  1584. switch (cmd) {
  1585. case TIOCMBIS:
  1586. if (arg & TIOCM_RTS)
  1587. mcr |= UART_MCR_RTS;
  1588. if (arg & TIOCM_DTR)
  1589. mcr |= UART_MCR_RTS;
  1590. if (arg & TIOCM_LOOP)
  1591. mcr |= UART_MCR_LOOP;
  1592. break;
  1593. case TIOCMBIC:
  1594. if (arg & TIOCM_RTS)
  1595. mcr &= ~UART_MCR_RTS;
  1596. if (arg & TIOCM_DTR)
  1597. mcr &= ~UART_MCR_RTS;
  1598. if (arg & TIOCM_LOOP)
  1599. mcr &= ~UART_MCR_LOOP;
  1600. break;
  1601. }
  1602. mos7720_port->shadowMCR = mcr;
  1603. write_mos_reg(port->serial, port->number - port->serial->minor,
  1604. MCR, mos7720_port->shadowMCR);
  1605. return 0;
  1606. }
  1607. static int get_serial_info(struct moschip_port *mos7720_port,
  1608. struct serial_struct __user *retinfo)
  1609. {
  1610. struct serial_struct tmp;
  1611. if (!retinfo)
  1612. return -EFAULT;
  1613. memset(&tmp, 0, sizeof(tmp));
  1614. tmp.type = PORT_16550A;
  1615. tmp.line = mos7720_port->port->serial->minor;
  1616. tmp.port = mos7720_port->port->number;
  1617. tmp.irq = 0;
  1618. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1619. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1620. tmp.baud_base = 9600;
  1621. tmp.close_delay = 5*HZ;
  1622. tmp.closing_wait = 30*HZ;
  1623. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1624. return -EFAULT;
  1625. return 0;
  1626. }
  1627. static int mos7720_ioctl(struct tty_struct *tty,
  1628. unsigned int cmd, unsigned long arg)
  1629. {
  1630. struct usb_serial_port *port = tty->driver_data;
  1631. struct moschip_port *mos7720_port;
  1632. struct async_icount cnow;
  1633. struct async_icount cprev;
  1634. mos7720_port = usb_get_serial_port_data(port);
  1635. if (mos7720_port == NULL)
  1636. return -ENODEV;
  1637. dev_dbg(&port->dev, "%s - cmd = 0x%x", __func__, cmd);
  1638. switch (cmd) {
  1639. case TIOCSERGETLSR:
  1640. dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
  1641. return get_lsr_info(tty, mos7720_port,
  1642. (unsigned int __user *)arg);
  1643. /* FIXME: These should be using the mode methods */
  1644. case TIOCMBIS:
  1645. case TIOCMBIC:
  1646. dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
  1647. return set_modem_info(mos7720_port, cmd,
  1648. (unsigned int __user *)arg);
  1649. case TIOCGSERIAL:
  1650. dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
  1651. return get_serial_info(mos7720_port,
  1652. (struct serial_struct __user *)arg);
  1653. case TIOCMIWAIT:
  1654. dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
  1655. cprev = mos7720_port->icount;
  1656. while (1) {
  1657. if (signal_pending(current))
  1658. return -ERESTARTSYS;
  1659. cnow = mos7720_port->icount;
  1660. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1661. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1662. return -EIO; /* no change => error */
  1663. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1664. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1665. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1666. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1667. return 0;
  1668. }
  1669. cprev = cnow;
  1670. }
  1671. /* NOTREACHED */
  1672. break;
  1673. }
  1674. return -ENOIOCTLCMD;
  1675. }
  1676. static int mos7720_startup(struct usb_serial *serial)
  1677. {
  1678. struct usb_device *dev;
  1679. char data;
  1680. u16 product;
  1681. int ret_val;
  1682. product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1683. dev = serial->dev;
  1684. /*
  1685. * The 7715 uses the first bulk in/out endpoint pair for the parallel
  1686. * port, and the second for the serial port. Because the usbserial core
  1687. * assumes both pairs are serial ports, we must engage in a bit of
  1688. * subterfuge and swap the pointers for ports 0 and 1 in order to make
  1689. * port 0 point to the serial port. However, both moschip devices use a
  1690. * single interrupt-in endpoint for both ports (as mentioned a little
  1691. * further down), and this endpoint was assigned to port 0. So after
  1692. * the swap, we must copy the interrupt endpoint elements from port 1
  1693. * (as newly assigned) to port 0, and null out port 1 pointers.
  1694. */
  1695. if (product == MOSCHIP_DEVICE_ID_7715) {
  1696. struct usb_serial_port *tmp = serial->port[0];
  1697. serial->port[0] = serial->port[1];
  1698. serial->port[1] = tmp;
  1699. serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
  1700. serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
  1701. serial->port[0]->interrupt_in_endpointAddress =
  1702. tmp->interrupt_in_endpointAddress;
  1703. serial->port[1]->interrupt_in_urb = NULL;
  1704. serial->port[1]->interrupt_in_buffer = NULL;
  1705. }
  1706. /* setting configuration feature to one */
  1707. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1708. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1709. /* start the interrupt urb */
  1710. ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
  1711. if (ret_val)
  1712. dev_err(&dev->dev,
  1713. "%s - Error %d submitting control urb\n",
  1714. __func__, ret_val);
  1715. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1716. if (product == MOSCHIP_DEVICE_ID_7715) {
  1717. ret_val = mos7715_parport_init(serial);
  1718. if (ret_val < 0)
  1719. return ret_val;
  1720. }
  1721. #endif
  1722. /* LSR For Port 1 */
  1723. read_mos_reg(serial, 0, LSR, &data);
  1724. dev_dbg(&dev->dev, "LSR:%x\n", data);
  1725. return 0;
  1726. }
  1727. static void mos7720_release(struct usb_serial *serial)
  1728. {
  1729. #ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  1730. /* close the parallel port */
  1731. if (le16_to_cpu(serial->dev->descriptor.idProduct)
  1732. == MOSCHIP_DEVICE_ID_7715) {
  1733. struct urbtracker *urbtrack;
  1734. unsigned long flags;
  1735. struct mos7715_parport *mos_parport =
  1736. usb_get_serial_data(serial);
  1737. /* prevent NULL ptr dereference in port callbacks */
  1738. spin_lock(&release_lock);
  1739. mos_parport->pp->private_data = NULL;
  1740. spin_unlock(&release_lock);
  1741. /* wait for synchronous usb calls to return */
  1742. if (mos_parport->msg_pending)
  1743. wait_for_completion_timeout(&mos_parport->syncmsg_compl,
  1744. MOS_WDR_TIMEOUT);
  1745. parport_remove_port(mos_parport->pp);
  1746. usb_set_serial_data(serial, NULL);
  1747. mos_parport->serial = NULL;
  1748. /* if tasklet currently scheduled, wait for it to complete */
  1749. tasklet_kill(&mos_parport->urb_tasklet);
  1750. /* unlink any urbs sent by the tasklet */
  1751. spin_lock_irqsave(&mos_parport->listlock, flags);
  1752. list_for_each_entry(urbtrack,
  1753. &mos_parport->active_urbs,
  1754. urblist_entry)
  1755. usb_unlink_urb(urbtrack->urb);
  1756. spin_unlock_irqrestore(&mos_parport->listlock, flags);
  1757. kref_put(&mos_parport->ref_count, destroy_mos_parport);
  1758. }
  1759. #endif
  1760. }
  1761. static int mos7720_port_probe(struct usb_serial_port *port)
  1762. {
  1763. struct moschip_port *mos7720_port;
  1764. mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
  1765. if (!mos7720_port)
  1766. return -ENOMEM;
  1767. /* Initialize all port interrupt end point to port 0 int endpoint.
  1768. * Our device has only one interrupt endpoint common to all ports.
  1769. */
  1770. port->interrupt_in_endpointAddress =
  1771. port->serial->port[0]->interrupt_in_endpointAddress;
  1772. mos7720_port->port = port;
  1773. usb_set_serial_port_data(port, mos7720_port);
  1774. return 0;
  1775. }
  1776. static int mos7720_port_remove(struct usb_serial_port *port)
  1777. {
  1778. struct moschip_port *mos7720_port;
  1779. mos7720_port = usb_get_serial_port_data(port);
  1780. kfree(mos7720_port);
  1781. return 0;
  1782. }
  1783. static struct usb_serial_driver moschip7720_2port_driver = {
  1784. .driver = {
  1785. .owner = THIS_MODULE,
  1786. .name = "moschip7720",
  1787. },
  1788. .description = "Moschip 2 port adapter",
  1789. .id_table = id_table,
  1790. .calc_num_ports = mos77xx_calc_num_ports,
  1791. .open = mos7720_open,
  1792. .close = mos7720_close,
  1793. .throttle = mos7720_throttle,
  1794. .unthrottle = mos7720_unthrottle,
  1795. .probe = mos77xx_probe,
  1796. .attach = mos7720_startup,
  1797. .release = mos7720_release,
  1798. .port_probe = mos7720_port_probe,
  1799. .port_remove = mos7720_port_remove,
  1800. .ioctl = mos7720_ioctl,
  1801. .tiocmget = mos7720_tiocmget,
  1802. .tiocmset = mos7720_tiocmset,
  1803. .get_icount = mos7720_get_icount,
  1804. .set_termios = mos7720_set_termios,
  1805. .write = mos7720_write,
  1806. .write_room = mos7720_write_room,
  1807. .chars_in_buffer = mos7720_chars_in_buffer,
  1808. .break_ctl = mos7720_break,
  1809. .read_bulk_callback = mos7720_bulk_in_callback,
  1810. .read_int_callback = NULL /* dynamically assigned in probe() */
  1811. };
  1812. static struct usb_serial_driver * const serial_drivers[] = {
  1813. &moschip7720_2port_driver, NULL
  1814. };
  1815. module_usb_serial_driver(serial_drivers, id_table);
  1816. MODULE_AUTHOR(DRIVER_AUTHOR);
  1817. MODULE_DESCRIPTION(DRIVER_DESC);
  1818. MODULE_LICENSE("GPL");