mos7720.c 55 KB

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