mos7720.c 54 KB

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