mos7720.c 56 KB

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