mos7720.c 56 KB

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