mos7720.c 55 KB

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