mos7720.c 55 KB

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