pl2303.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /****************************************************************************
  2. * drivers/usbdev/pl2303.c
  3. *
  4. * Copyright (C) 2008-2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * This logic emulates the Prolific PL2303 serial/USB converter
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47. #include <semaphore.h>
  48. #include <string.h>
  49. #include <errno.h>
  50. #include <queue.h>
  51. #include <debug.h>
  52. #include <nuttx/kmalloc.h>
  53. #include <nuttx/arch.h>
  54. #include <nuttx/serial/serial.h>
  55. #include <nuttx/usb/usb.h>
  56. #include <nuttx/usb/usbdev.h>
  57. #include <nuttx/usb/usbdev_trace.h>
  58. /****************************************************************************
  59. * Pre-processor Definitions
  60. ****************************************************************************/
  61. /* Configuration ************************************************************/
  62. /* Number of requests in the write queue */
  63. #ifndef CONFIG_PL2303_NWRREQS
  64. # define CONFIG_PL2303_NWRREQS 4
  65. #endif
  66. /* Number of requests in the read queue */
  67. #ifndef CONFIG_PL2303_NRDREQS
  68. # define CONFIG_PL2303_NRDREQS 4
  69. #endif
  70. /* Logical endpoint numbers / max packet sizes */
  71. #ifndef CONFIG_PL2303_EPINTIN
  72. # warning "EPINTIN not defined in the configuration"
  73. # define CONFIG_PL2303_EPINTIN 1
  74. #endif
  75. #ifndef CONFIG_PL2303_EPBULKOUT
  76. # warning "EPBULKOUT not defined in the configuration"
  77. # define CONFIG_PL2303_EPBULKOUT 2
  78. #endif
  79. #ifndef CONFIG_PL2303_EPBULKIN
  80. # warning "EPBULKIN not defined in the configuration"
  81. # define CONFIG_PL2303_EPBULKIN 3
  82. #endif
  83. /* Packet and request buffer sizes */
  84. #ifndef CONFIG_PL2303_EP0MAXPACKET
  85. # define CONFIG_PL2303_EP0MAXPACKET 64
  86. #endif
  87. /* Ideally, the BULKOUT request size should *not* be the same size as the
  88. * maxpacket size. That is because IN transfers of exactly the maxpacket
  89. * size will be followed by a NULL packet. The BULKOUT request buffer
  90. * size, on the other hand, is always the same as the maxpacket size.
  91. */
  92. #ifndef CONFIG_PL2303_BULKIN_REQLEN
  93. # define CONFIG_PL2303_BULKIN_REQLEN 96
  94. #endif
  95. /* Vendor and product IDs and strings */
  96. #ifndef CONFIG_PL2303_VENDORID
  97. # define CONFIG_PL2303_VENDORID 0x067b
  98. #endif
  99. #ifndef CONFIG_PL2303_PRODUCTID
  100. # define CONFIG_PL2303_PRODUCTID 0x2303
  101. #endif
  102. #ifndef CONFIG_PL2303_VENDORSTR
  103. # warning "No Vendor string specified"
  104. # define CONFIG_PL2303_VENDORSTR "NuttX"
  105. #endif
  106. #ifndef CONFIG_PL2303_PRODUCTSTR
  107. # warning "No Product string specified"
  108. # define CONFIG_PL2303_PRODUCTSTR "USBdev Serial"
  109. #endif
  110. #undef CONFIG_PL2303_SERIALSTR
  111. #define CONFIG_PL2303_SERIALSTR "0"
  112. #undef CONFIG_PL2303_CONFIGSTR
  113. #define CONFIG_PL2303_CONFIGSTR "Bulk"
  114. /* USB Controller */
  115. #ifndef CONFIG_USBDEV_SELFPOWERED
  116. # define SELFPOWERED USB_CONFIG_ATTR_SELFPOWER
  117. #else
  118. # define SELFPOWERED (0)
  119. #endif
  120. #ifndef CONFIG_USBDEV_REMOTEWAKEUP
  121. # define REMOTEWAKEUP USB_CONFIG_ATTR_WAKEUP
  122. #else
  123. # define REMOTEWAKEUP (0)
  124. #endif
  125. #ifndef CONFIG_USBDEV_MAXPOWER
  126. # define CONFIG_USBDEV_MAXPOWER 100
  127. #endif
  128. /* Descriptors ****************************************************************/
  129. /* These settings are not modifiable via the NuttX configuration */
  130. #define PL2303_VERSIONNO (0x0202) /* Device version number */
  131. #define PL2303_CONFIGIDNONE (0) /* Config ID means to return to address mode */
  132. #define PL2303_CONFIGID (1) /* The only supported configuration ID */
  133. #define PL2303_NCONFIGS (1) /* Number of configurations supported */
  134. #define PL2303_INTERFACEID (0)
  135. #define PL2303_ALTINTERFACEID (0)
  136. #define PL2303_NINTERFACES (1) /* Number of interfaces in the configuration */
  137. #define PL2303_NENDPOINTS (3) /* Number of endpoints in the interface */
  138. /* Endpoint configuration */
  139. #define PL2303_EPINTIN_ADDR (USB_DIR_IN|CONFIG_PL2303_EPINTIN)
  140. #define PL2303_EPINTIN_ATTR (USB_EP_ATTR_XFER_INT)
  141. #define PL2303_EPINTIN_MXPACKET (10)
  142. #define PL2303_EPOUTBULK_ADDR (CONFIG_PL2303_EPBULKOUT)
  143. #define PL2303_EPOUTBULK_ATTR (USB_EP_ATTR_XFER_BULK)
  144. #define PL2303_EPINBULK_ADDR (USB_DIR_IN|CONFIG_PL2303_EPBULKIN)
  145. #define PL2303_EPINBULK_ATTR (USB_EP_ATTR_XFER_BULK)
  146. /* String language */
  147. #define PL2303_STR_LANGUAGE (0x0409) /* en-us */
  148. /* Descriptor strings */
  149. #define PL2303_MANUFACTURERSTRID (1)
  150. #define PL2303_PRODUCTSTRID (2)
  151. #define PL2303_SERIALSTRID (3)
  152. #define PL2303_CONFIGSTRID (4)
  153. /* Buffer big enough for any of our descriptors */
  154. #define PL2303_MXDESCLEN (64)
  155. /* Vender specific control requests *******************************************/
  156. #define PL2303_CONTROL_TYPE (0x20)
  157. #define PL2303_SETLINEREQUEST (0x20) /* OUT, Recipient interface */
  158. #define PL2303_GETLINEREQUEST (0x21) /* IN, Recipient interface */
  159. #define PL2303_SETCONTROLREQUEST (0x22) /* OUT, Recipient interface */
  160. #define PL2303_BREAKREQUEST (0x23) /* OUT, Recipient interface */
  161. /* Vendor read/write */
  162. #define PL2303_RWREQUEST_TYPE (0x40)
  163. #define PL2303_RWREQUEST (0x01) /* IN/OUT, Recipient device */
  164. /* Misc Macros ****************************************************************/
  165. /* min/max macros */
  166. #ifndef min
  167. # define min(a,b) ((a)<(b)?(a):(b))
  168. #endif
  169. #ifndef max
  170. # define max(a,b) ((a)>(b)?(a):(b))
  171. #endif
  172. /* Trace values *************************************************************/
  173. #define PL2303_CLASSAPI_SETUP TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SETUP)
  174. #define PL2303_CLASSAPI_SHUTDOWN TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SHUTDOWN)
  175. #define PL2303_CLASSAPI_ATTACH TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_ATTACH)
  176. #define PL2303_CLASSAPI_DETACH TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_DETACH)
  177. #define PL2303_CLASSAPI_IOCTL TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_IOCTL)
  178. #define PL2303_CLASSAPI_RECEIVE TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RECEIVE)
  179. #define PL2303_CLASSAPI_RXINT TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RXINT)
  180. #define PL2303_CLASSAPI_RXAVAILABLE TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_RXAVAILABLE)
  181. #define PL2303_CLASSAPI_SEND TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_SEND)
  182. #define PL2303_CLASSAPI_TXINT TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXINT)
  183. #define PL2303_CLASSAPI_TXREADY TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXREADY)
  184. #define PL2303_CLASSAPI_TXEMPTY TRACE_EVENT(TRACE_CLASSAPI_ID, USBSER_TRACECLASSAPI_TXEMPTY)
  185. /****************************************************************************
  186. * Private Types
  187. ****************************************************************************/
  188. /* Container to support a list of requests */
  189. struct pl2303_req_s
  190. {
  191. FAR struct pl2303_req_s *flink; /* Implements a singly linked list */
  192. FAR struct usbdev_req_s *req; /* The contained request */
  193. };
  194. /* This structure describes the internal state of the driver */
  195. struct pl2303_dev_s
  196. {
  197. FAR struct uart_dev_s serdev; /* Serial device structure */
  198. FAR struct usbdev_s *usbdev; /* usbdev driver pointer */
  199. uint8_t config; /* Configuration number */
  200. uint8_t nwrq; /* Number of queue write requests (in reqlist)*/
  201. uint8_t nrdq; /* Number of queue read requests (in epbulkout) */
  202. bool rxenabled; /* true: UART RX "interrupts" enabled */
  203. uint8_t linest[7]; /* Fake line status */
  204. int16_t rxhead; /* Working head; used when rx int disabled */
  205. FAR struct usbdev_ep_s *epintin; /* Interrupt IN endpoint structure */
  206. FAR struct usbdev_ep_s *epbulkin; /* Bulk IN endpoint structure */
  207. FAR struct usbdev_ep_s *epbulkout; /* Bulk OUT endpoint structure */
  208. FAR struct usbdev_req_s *ctrlreq; /* Control request */
  209. struct sq_queue_s reqlist; /* List of write request containers */
  210. /* Pre-allocated write request containers. The write requests will
  211. * be linked in a free list (reqlist), and used to send requests to
  212. * EPBULKIN; Read requests will be queued in the EBULKOUT.
  213. */
  214. struct pl2303_req_s wrreqs[CONFIG_PL2303_NWRREQS];
  215. struct pl2303_req_s rdreqs[CONFIG_PL2303_NWRREQS];
  216. /* Serial I/O buffers */
  217. char rxbuffer[CONFIG_PL2303_RXBUFSIZE];
  218. char txbuffer[CONFIG_PL2303_TXBUFSIZE];
  219. };
  220. /* The internal version of the class driver */
  221. struct pl2303_driver_s
  222. {
  223. struct usbdevclass_driver_s drvr;
  224. FAR struct pl2303_dev_s *dev;
  225. };
  226. /* This is what is allocated */
  227. struct pl2303_alloc_s
  228. {
  229. struct pl2303_dev_s dev;
  230. struct pl2303_driver_s drvr;
  231. };
  232. /****************************************************************************
  233. * Private Function Prototypes
  234. ****************************************************************************/
  235. /* Transfer helpers *********************************************************/
  236. static uint16_t usbclass_fillrequest(FAR struct pl2303_dev_s *priv,
  237. uint8_t *reqbuf, uint16_t reqlen);
  238. static int usbclass_sndpacket(FAR struct pl2303_dev_s *priv);
  239. static inline int usbclass_recvpacket(FAR struct pl2303_dev_s *priv,
  240. uint8_t *reqbuf, uint16_t reqlen);
  241. /* Request helpers *********************************************************/
  242. static struct usbdev_req_s *usbclass_allocreq(FAR struct usbdev_ep_s *ep,
  243. uint16_t len);
  244. static void usbclass_freereq(FAR struct usbdev_ep_s *ep,
  245. FAR struct usbdev_req_s *req);
  246. /* Configuration ***********************************************************/
  247. static int usbclass_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc);
  248. #ifdef CONFIG_USBDEV_DUALSPEED
  249. static void usbclass_mkepbulkdesc(const struct usb_epdesc_s *indesc,
  250. uint16_t mxpacket, struct usb_epdesc_s *outdesc);
  251. static int16_t usbclass_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type);
  252. #else
  253. static int16_t usbclass_mkcfgdesc(uint8_t *buf);
  254. #endif
  255. static void usbclass_resetconfig(FAR struct pl2303_dev_s *priv);
  256. static int usbclass_setconfig(FAR struct pl2303_dev_s *priv,
  257. uint8_t config);
  258. /* Completion event handlers ***********************************************/
  259. static void usbclass_ep0incomplete(FAR struct usbdev_ep_s *ep,
  260. FAR struct usbdev_req_s *req);
  261. static void usbclass_rdcomplete(FAR struct usbdev_ep_s *ep,
  262. FAR struct usbdev_req_s *req);
  263. static void usbclass_wrcomplete(FAR struct usbdev_ep_s *ep,
  264. FAR struct usbdev_req_s *req);
  265. /* USB class device ********************************************************/
  266. static int usbclass_bind(FAR struct usbdevclass_driver_s *driver,
  267. FAR struct usbdev_s *dev);
  268. static void usbclass_unbind(FAR struct usbdevclass_driver_s *driver,
  269. FAR struct usbdev_s *dev);
  270. static int usbclass_setup(FAR struct usbdevclass_driver_s *driver,
  271. FAR struct usbdev_s *dev,
  272. FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
  273. size_t outlen);
  274. static void usbclass_disconnect(FAR struct usbdevclass_driver_s *driver,
  275. FAR struct usbdev_s *dev);
  276. #ifdef CONFIG_SERIAL_REMOVABLE
  277. static void usbclass_suspend(FAR struct usbdevclass_driver_s *driver,
  278. FAR struct usbdev_s *dev);
  279. static void usbclass_resume(FAR struct usbdevclass_driver_s *driver,
  280. FAR struct usbdev_s *dev);
  281. #endif
  282. /* Serial port *************************************************************/
  283. static int usbser_setup(FAR struct uart_dev_s *dev);
  284. static void usbser_shutdown(FAR struct uart_dev_s *dev);
  285. static int usbser_attach(FAR struct uart_dev_s *dev);
  286. static void usbser_detach(FAR struct uart_dev_s *dev);
  287. static void usbser_rxint(FAR struct uart_dev_s *dev, bool enable);
  288. static void usbser_txint(FAR struct uart_dev_s *dev, bool enable);
  289. static bool usbser_txempty(FAR struct uart_dev_s *dev);
  290. /****************************************************************************
  291. * Private Variables
  292. ****************************************************************************/
  293. /* USB class device ********************************************************/
  294. static const struct usbdevclass_driverops_s g_driverops =
  295. {
  296. usbclass_bind, /* bind */
  297. usbclass_unbind, /* unbind */
  298. usbclass_setup, /* setup */
  299. usbclass_disconnect, /* disconnect */
  300. #ifdef CONFIG_SERIAL_REMOVABLE
  301. usbclass_suspend, /* suspend */
  302. usbclass_resume, /* resume */
  303. #else
  304. NULL, /* suspend */
  305. NULL, /* resume */
  306. #endif
  307. };
  308. /* Serial port *************************************************************/
  309. static const struct uart_ops_s g_uartops =
  310. {
  311. usbser_setup, /* setup */
  312. usbser_shutdown, /* shutdown */
  313. usbser_attach, /* attach */
  314. usbser_detach, /* detach */
  315. NULL, /* ioctl */
  316. NULL, /* receive */
  317. usbser_rxint, /* rxinit */
  318. NULL, /* rxavailable */
  319. NULL, /* send */
  320. usbser_txint, /* txinit */
  321. NULL, /* txready */
  322. usbser_txempty /* txempty */
  323. };
  324. /* USB descriptor templates these will be copied and modified **************/
  325. static const struct usb_devdesc_s g_devdesc =
  326. {
  327. USB_SIZEOF_DEVDESC, /* len */
  328. USB_DESC_TYPE_DEVICE, /* type */
  329. {LSBYTE(0x0200), MSBYTE(0x0200)}, /* usb */
  330. USB_CLASS_PER_INTERFACE, /* classid */
  331. 0, /* subclass */
  332. 0, /* protocol */
  333. CONFIG_PL2303_EP0MAXPACKET, /* maxpacketsize */
  334. { LSBYTE(CONFIG_PL2303_VENDORID), /* vendor */
  335. MSBYTE(CONFIG_PL2303_VENDORID) },
  336. { LSBYTE(CONFIG_PL2303_PRODUCTID), /* product */
  337. MSBYTE(CONFIG_PL2303_PRODUCTID) },
  338. { LSBYTE(PL2303_VERSIONNO), /* device */
  339. MSBYTE(PL2303_VERSIONNO) },
  340. PL2303_MANUFACTURERSTRID, /* imfgr */
  341. PL2303_PRODUCTSTRID, /* iproduct */
  342. PL2303_SERIALSTRID, /* serno */
  343. PL2303_NCONFIGS /* nconfigs */
  344. };
  345. static const struct usb_cfgdesc_s g_cfgdesc =
  346. {
  347. USB_SIZEOF_CFGDESC, /* len */
  348. USB_DESC_TYPE_CONFIG, /* type */
  349. {0, 0}, /* totallen -- to be provided */
  350. PL2303_NINTERFACES, /* ninterfaces */
  351. PL2303_CONFIGID, /* cfgvalue */
  352. PL2303_CONFIGSTRID, /* icfg */
  353. USB_CONFIG_ATTR_ONE|SELFPOWERED|REMOTEWAKEUP, /* attr */
  354. (CONFIG_USBDEV_MAXPOWER + 1) / 2 /* mxpower */
  355. };
  356. static const struct usb_ifdesc_s g_ifdesc =
  357. {
  358. USB_SIZEOF_IFDESC, /* len */
  359. USB_DESC_TYPE_INTERFACE, /* type */
  360. 0, /* ifno */
  361. 0, /* alt */
  362. PL2303_NENDPOINTS, /* neps */
  363. USB_CLASS_VENDOR_SPEC, /* classid */
  364. 0, /* subclass */
  365. 0, /* protocol */
  366. PL2303_CONFIGSTRID /* iif */
  367. };
  368. static const struct usb_epdesc_s g_epintindesc =
  369. {
  370. USB_SIZEOF_EPDESC, /* len */
  371. USB_DESC_TYPE_ENDPOINT, /* type */
  372. PL2303_EPINTIN_ADDR, /* addr */
  373. PL2303_EPINTIN_ATTR, /* attr */
  374. { LSBYTE(PL2303_EPINTIN_MXPACKET), /* maxpacket */
  375. MSBYTE(PL2303_EPINTIN_MXPACKET) },
  376. 1 /* interval */
  377. };
  378. static const struct usb_epdesc_s g_epbulkoutdesc =
  379. {
  380. USB_SIZEOF_EPDESC, /* len */
  381. USB_DESC_TYPE_ENDPOINT, /* type */
  382. PL2303_EPOUTBULK_ADDR, /* addr */
  383. PL2303_EPOUTBULK_ATTR, /* attr */
  384. { LSBYTE(64), MSBYTE(64) }, /* maxpacket -- might change to 512*/
  385. 0 /* interval */
  386. };
  387. static const struct usb_epdesc_s g_epbulkindesc =
  388. {
  389. USB_SIZEOF_EPDESC, /* len */
  390. USB_DESC_TYPE_ENDPOINT, /* type */
  391. PL2303_EPINBULK_ADDR, /* addr */
  392. PL2303_EPINBULK_ATTR, /* attr */
  393. { LSBYTE(64), MSBYTE(64) }, /* maxpacket -- might change to 512*/
  394. 0 /* interval */
  395. };
  396. #ifdef CONFIG_USBDEV_DUALSPEED
  397. static const struct usb_qualdesc_s g_qualdesc =
  398. {
  399. USB_SIZEOF_QUALDESC, /* len */
  400. USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
  401. {LSBYTE(0x0200), MSBYTE(0x0200) }, /* USB */
  402. USB_CLASS_VENDOR_SPEC, /* classid */
  403. 0, /* subclass */
  404. 0, /* protocol */
  405. CONFIG_PL2303_EP0MAXPACKET, /* mxpacketsize */
  406. PL2303_NCONFIGS, /* nconfigs */
  407. 0, /* reserved */
  408. };
  409. #endif
  410. /****************************************************************************
  411. * Private Functions
  412. ****************************************************************************/
  413. /************************************************************************************
  414. * Name: usbclass_fillrequest
  415. *
  416. * Description:
  417. * If there is data to send it is copied to the given buffer. Called either
  418. * to initiate the first write operation, or from the completion interrupt handler
  419. * service consecutive write operations.
  420. *
  421. * NOTE: The USB serial driver does not use the serial drivers uart_xmitchars()
  422. * API. That logic is essentially duplicated here because unlike UART hardware,
  423. * we need to be able to handle writes not byte-by-byte, but packet-by-packet.
  424. * Unfortunately, that decision also exposes some internals of the serial driver
  425. * in the following.
  426. *
  427. ************************************************************************************/
  428. static uint16_t usbclass_fillrequest(FAR struct pl2303_dev_s *priv, uint8_t *reqbuf,
  429. uint16_t reqlen)
  430. {
  431. FAR uart_dev_t *serdev = &priv->serdev;
  432. FAR struct uart_buffer_s *xmit = &serdev->xmit;
  433. irqstate_t flags;
  434. uint16_t nbytes = 0;
  435. /* Disable interrupts */
  436. flags = irqsave();
  437. /* Transfer bytes while we have bytes available and there is room in the request */
  438. while (xmit->head != xmit->tail && nbytes < reqlen)
  439. {
  440. *reqbuf++ = xmit->buffer[xmit->tail];
  441. nbytes++;
  442. /* Increment the tail pointer */
  443. if (++(xmit->tail) >= xmit->size)
  444. {
  445. xmit->tail = 0;
  446. }
  447. }
  448. /* When all of the characters have been sent from the buffer
  449. * disable the "TX interrupt".
  450. */
  451. if (xmit->head == xmit->tail)
  452. {
  453. uart_disabletxint(serdev);
  454. }
  455. /* If any bytes were removed from the buffer, inform any waiters
  456. * there there is space available.
  457. */
  458. if (nbytes)
  459. {
  460. uart_datasent(serdev);
  461. }
  462. irqrestore(flags);
  463. return nbytes;
  464. }
  465. /************************************************************************************
  466. * Name: usbclass_sndpacket
  467. *
  468. * Description:
  469. * This function obtains write requests, transfers the TX data into the request,
  470. * and submits the requests to the USB controller. This continues untils either
  471. * (1) there are no further packets available, or (2) thre is not further data
  472. * to send.
  473. *
  474. ************************************************************************************/
  475. static int usbclass_sndpacket(FAR struct pl2303_dev_s *priv)
  476. {
  477. FAR struct usbdev_ep_s *ep;
  478. FAR struct usbdev_req_s *req;
  479. FAR struct pl2303_req_s *reqcontainer;
  480. uint16_t reqlen;
  481. irqstate_t flags;
  482. int len;
  483. int ret = OK;
  484. #ifdef CONFIG_DEBUG
  485. if (priv == NULL)
  486. {
  487. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  488. return -ENODEV;
  489. }
  490. #endif
  491. flags = irqsave();
  492. /* Use our IN endpoint for the transfer */
  493. ep = priv->epbulkin;
  494. /* Loop until either (1) we run out or write requests, or (2) usbclass_fillrequest()
  495. * is unable to fill the request with data (i.e., until there is no more data
  496. * to be sent).
  497. */
  498. uvdbg("head=%d tail=%d nwrq=%d empty=%d\n",
  499. priv->serdev.xmit.head, priv->serdev.xmit.tail,
  500. priv->nwrq, sq_empty(&priv->reqlist));
  501. /* Get the maximum number of bytes that will fit into one bulk IN request */
  502. reqlen = max(CONFIG_PL2303_BULKIN_REQLEN, ep->maxpacket);
  503. while (!sq_empty(&priv->reqlist))
  504. {
  505. /* Peek at the request in the container at the head of the list */
  506. reqcontainer = (struct pl2303_req_s *)sq_peek(&priv->reqlist);
  507. req = reqcontainer->req;
  508. /* Fill the request with serial TX data */
  509. len = usbclass_fillrequest(priv, req->buf, reqlen);
  510. if (len > 0)
  511. {
  512. /* Remove the empty container from the request list */
  513. (void)sq_remfirst(&priv->reqlist);
  514. priv->nwrq--;
  515. /* Then submit the request to the endpoint */
  516. req->len = len;
  517. req->priv = reqcontainer;
  518. req->flags = USBDEV_REQFLAGS_NULLPKT;
  519. ret = EP_SUBMIT(ep, req);
  520. if (ret != OK)
  521. {
  522. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SUBMITFAIL), (uint16_t)-ret);
  523. break;
  524. }
  525. }
  526. else
  527. {
  528. break;
  529. }
  530. }
  531. irqrestore(flags);
  532. return ret;
  533. }
  534. /************************************************************************************
  535. * Name: usbclass_recvpacket
  536. *
  537. * Description:
  538. * A normal completion event was received by the read completion handler at the
  539. * interrupt level (with interrupts disabled). This function handles the USB packet
  540. * and provides the received data to the uart RX buffer.
  541. *
  542. * Assumptions:
  543. * Called from the USB interrupt handler with interrupts disabled.
  544. *
  545. ************************************************************************************/
  546. static inline int usbclass_recvpacket(FAR struct pl2303_dev_s *priv,
  547. uint8_t *reqbuf, uint16_t reqlen)
  548. {
  549. FAR uart_dev_t *serdev = &priv->serdev;
  550. FAR struct uart_buffer_s *recv = &serdev->recv;
  551. uint16_t currhead;
  552. uint16_t nexthead;
  553. uint16_t nbytes = 0;
  554. /* Get the next head index. During the time that RX interrupts are disabled, the
  555. * the serial driver will be extracting data from the circular buffer and modifying
  556. * recv.tail. During this time, we should avoid modifying recv.head; Instead we will
  557. * use a shadow copy of the index. When interrupts are restored, the real recv.head
  558. * will be updated with this indes.
  559. */
  560. if (priv->rxenabled)
  561. {
  562. currhead = recv->head;
  563. }
  564. else
  565. {
  566. currhead = priv->rxhead;
  567. }
  568. /* Pre-calculate the head index and check for wrap around. We need to do this
  569. * so that we can determine if the circular buffer will overrun BEFORE we
  570. * overrun the buffer!
  571. */
  572. nexthead = currhead + 1;
  573. if (nexthead >= recv->size)
  574. {
  575. nexthead = 0;
  576. }
  577. /* Then copy data into the RX buffer until either: (1) all of the data has been
  578. * copied, or (2) the RX buffer is full. NOTE: If the RX buffer becomes full,
  579. * then we have overrun the serial driver and data will be lost.
  580. */
  581. while (nexthead != recv->tail && nbytes < reqlen)
  582. {
  583. /* Copy one byte to the head of the circular RX buffer */
  584. recv->buffer[currhead] = *reqbuf++;
  585. /* Update counts and indices */
  586. currhead = nexthead;
  587. nbytes++;
  588. /* Increment the head index and check for wrap around */
  589. nexthead = currhead + 1;
  590. if (nexthead >= recv->size)
  591. {
  592. nexthead = 0;
  593. }
  594. }
  595. /* Write back the head pointer using the shadow index if RX "interrupts"
  596. * are disabled.
  597. */
  598. if (priv->rxenabled)
  599. {
  600. recv->head = currhead;
  601. }
  602. else
  603. {
  604. priv->rxhead = currhead;
  605. }
  606. /* If data was added to the incoming serial buffer, then wake up any
  607. * threads is waiting for incoming data. If we are running in an interrupt
  608. * handler, then the serial driver will not run until the interrupt handler
  609. * returns.
  610. */
  611. if (priv->rxenabled && nbytes > 0)
  612. {
  613. uart_datareceived(serdev);
  614. }
  615. /* Return an error if the entire packet could not be transferred */
  616. if (nbytes < reqlen)
  617. {
  618. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RXOVERRUN), 0);
  619. return -ENOSPC;
  620. }
  621. return OK;
  622. }
  623. /****************************************************************************
  624. * Name: usbclass_allocreq
  625. *
  626. * Description:
  627. * Allocate a request instance along with its buffer
  628. *
  629. ****************************************************************************/
  630. static struct usbdev_req_s *usbclass_allocreq(FAR struct usbdev_ep_s *ep,
  631. uint16_t len)
  632. {
  633. FAR struct usbdev_req_s *req;
  634. req = EP_ALLOCREQ(ep);
  635. if (req != NULL)
  636. {
  637. req->len = len;
  638. req->buf = EP_ALLOCBUFFER(ep, len);
  639. if (!req->buf)
  640. {
  641. EP_FREEREQ(ep, req);
  642. req = NULL;
  643. }
  644. }
  645. return req;
  646. }
  647. /****************************************************************************
  648. * Name: usbclass_freereq
  649. *
  650. * Description:
  651. * Free a request instance along with its buffer
  652. *
  653. ****************************************************************************/
  654. static void usbclass_freereq(FAR struct usbdev_ep_s *ep,
  655. FAR struct usbdev_req_s *req)
  656. {
  657. if (ep != NULL && req != NULL)
  658. {
  659. if (req->buf != NULL)
  660. {
  661. EP_FREEBUFFER(ep, req->buf);
  662. }
  663. EP_FREEREQ(ep, req);
  664. }
  665. }
  666. /****************************************************************************
  667. * Name: usbclass_mkstrdesc
  668. *
  669. * Description:
  670. * Construct a string descriptor
  671. *
  672. ****************************************************************************/
  673. static int usbclass_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc)
  674. {
  675. const char *str;
  676. int len;
  677. int ndata;
  678. int i;
  679. switch (id)
  680. {
  681. case 0:
  682. {
  683. /* Descriptor 0 is the language id */
  684. strdesc->len = 4;
  685. strdesc->type = USB_DESC_TYPE_STRING;
  686. strdesc->data[0] = LSBYTE(PL2303_STR_LANGUAGE);
  687. strdesc->data[1] = MSBYTE(PL2303_STR_LANGUAGE);
  688. return 4;
  689. }
  690. case PL2303_MANUFACTURERSTRID:
  691. str = CONFIG_PL2303_VENDORSTR;
  692. break;
  693. case PL2303_PRODUCTSTRID:
  694. str = CONFIG_PL2303_PRODUCTSTR;
  695. break;
  696. case PL2303_SERIALSTRID:
  697. str = CONFIG_PL2303_SERIALSTR;
  698. break;
  699. case PL2303_CONFIGSTRID:
  700. str = CONFIG_PL2303_CONFIGSTR;
  701. break;
  702. default:
  703. return -EINVAL;
  704. }
  705. /* The string is utf16-le. The poor man's utf-8 to utf16-le
  706. * conversion below will only handle 7-bit en-us ascii
  707. */
  708. len = strlen(str);
  709. for (i = 0, ndata = 0; i < len; i++, ndata += 2)
  710. {
  711. strdesc->data[ndata] = str[i];
  712. strdesc->data[ndata+1] = 0;
  713. }
  714. strdesc->len = ndata+2;
  715. strdesc->type = USB_DESC_TYPE_STRING;
  716. return strdesc->len;
  717. }
  718. /****************************************************************************
  719. * Name: usbclass_mkepbulkdesc
  720. *
  721. * Description:
  722. * Construct the endpoint descriptor
  723. *
  724. ****************************************************************************/
  725. #ifdef CONFIG_USBDEV_DUALSPEED
  726. static inline void usbclass_mkepbulkdesc(const FAR struct usb_epdesc_s *indesc,
  727. uint16_t mxpacket,
  728. FAR struct usb_epdesc_s *outdesc)
  729. {
  730. /* Copy the canned descriptor */
  731. memcpy(outdesc, indesc, USB_SIZEOF_EPDESC);
  732. /* Then add the correct max packet size */
  733. outdesc->mxpacketsize[0] = LSBYTE(mxpacket);
  734. outdesc->mxpacketsize[1] = MSBYTE(mxpacket);
  735. }
  736. #endif
  737. /****************************************************************************
  738. * Name: usbclass_mkcfgdesc
  739. *
  740. * Description:
  741. * Construct the configuration descriptor
  742. *
  743. ****************************************************************************/
  744. #ifdef CONFIG_USBDEV_DUALSPEED
  745. static int16_t usbclass_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type)
  746. #else
  747. static int16_t usbclass_mkcfgdesc(uint8_t *buf)
  748. #endif
  749. {
  750. FAR struct usb_cfgdesc_s *cfgdesc = (struct usb_cfgdesc_s*)buf;
  751. #ifdef CONFIG_USBDEV_DUALSPEED
  752. bool hispeed = (speed == USB_SPEED_HIGH);
  753. uint16_t bulkmxpacket;
  754. #endif
  755. uint16_t totallen;
  756. /* This is the total length of the configuration (not necessarily the
  757. * size that we will be sending now.
  758. */
  759. totallen = USB_SIZEOF_CFGDESC + USB_SIZEOF_IFDESC + PL2303_NENDPOINTS * USB_SIZEOF_EPDESC;
  760. /* Configuration descriptor -- Copy the canned descriptor and fill in the
  761. * type (we'll also need to update the size below
  762. */
  763. memcpy(cfgdesc, &g_cfgdesc, USB_SIZEOF_CFGDESC);
  764. buf += USB_SIZEOF_CFGDESC;
  765. /* Copy the canned interface descriptor */
  766. memcpy(buf, &g_ifdesc, USB_SIZEOF_IFDESC);
  767. buf += USB_SIZEOF_IFDESC;
  768. /* Make the three endpoint configurations. First, check for switches
  769. * between high and full speed
  770. */
  771. #ifdef CONFIG_USBDEV_DUALSPEED
  772. if (type == USB_DESC_TYPE_OTHERSPEEDCONFIG)
  773. {
  774. hispeed = !hispeed;
  775. }
  776. #endif
  777. memcpy(buf, &g_epintindesc, USB_SIZEOF_EPDESC);
  778. buf += USB_SIZEOF_EPDESC;
  779. #ifdef CONFIG_USBDEV_DUALSPEED
  780. if (hispeed)
  781. {
  782. bulkmxpacket = 512;
  783. }
  784. else
  785. {
  786. bulkmxpacket = 64;
  787. }
  788. usbclass_mkepbulkdesc(&g_epbulkoutdesc, bulkmxpacket, (struct usb_epdesc_s*)buf);
  789. buf += USB_SIZEOF_EPDESC;
  790. usbclass_mkepbulkdesc(&g_epbulkindesc, bulkmxpacket, (struct usb_epdesc_s*)buf);
  791. #else
  792. memcpy(buf, &g_epbulkoutdesc, USB_SIZEOF_EPDESC);
  793. buf += USB_SIZEOF_EPDESC;
  794. memcpy(buf, &g_epbulkindesc, USB_SIZEOF_EPDESC);
  795. #endif
  796. /* Finally, fill in the total size of the configuration descriptor */
  797. cfgdesc->totallen[0] = LSBYTE(totallen);
  798. cfgdesc->totallen[1] = MSBYTE(totallen);
  799. return totallen;
  800. }
  801. /****************************************************************************
  802. * Name: usbclass_resetconfig
  803. *
  804. * Description:
  805. * Mark the device as not configured and disable all endpoints.
  806. *
  807. ****************************************************************************/
  808. static void usbclass_resetconfig(FAR struct pl2303_dev_s *priv)
  809. {
  810. /* Are we configured? */
  811. if (priv->config != PL2303_CONFIGIDNONE)
  812. {
  813. /* Yes.. but not anymore */
  814. priv->config = PL2303_CONFIGIDNONE;
  815. /* Inform the "upper half" driver that there is no (functional) USB
  816. * connection.
  817. */
  818. #ifdef CONFIG_SERIAL_REMOVABLE
  819. uart_connected(&priv->serdev, false);
  820. #endif
  821. /* Disable endpoints. This should force completion of all pending
  822. * transfers.
  823. */
  824. EP_DISABLE(priv->epintin);
  825. EP_DISABLE(priv->epbulkin);
  826. EP_DISABLE(priv->epbulkout);
  827. }
  828. }
  829. /****************************************************************************
  830. * Name: usbclass_setconfig
  831. *
  832. * Description:
  833. * Set the device configuration by allocating and configuring endpoints and
  834. * by allocating and queue read and write requests.
  835. *
  836. ****************************************************************************/
  837. static int usbclass_setconfig(FAR struct pl2303_dev_s *priv, uint8_t config)
  838. {
  839. FAR struct usbdev_req_s *req;
  840. #ifdef CONFIG_USBDEV_DUALSPEED
  841. struct usb_epdesc_s epdesc;
  842. uint16_t bulkmxpacket;
  843. #endif
  844. int i;
  845. int ret = 0;
  846. #if CONFIG_DEBUG
  847. if (priv == NULL)
  848. {
  849. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  850. return -EIO;
  851. }
  852. #endif
  853. if (config == priv->config)
  854. {
  855. /* Already configured -- Do nothing */
  856. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALREADYCONFIGURED), 0);
  857. return 0;
  858. }
  859. /* Discard the previous configuration data */
  860. usbclass_resetconfig(priv);
  861. /* Was this a request to simply discard the current configuration? */
  862. if (config == PL2303_CONFIGIDNONE)
  863. {
  864. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONFIGNONE), 0);
  865. return 0;
  866. }
  867. /* We only accept one configuration */
  868. if (config != PL2303_CONFIGID)
  869. {
  870. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONFIGIDBAD), 0);
  871. return -EINVAL;
  872. }
  873. /* Configure the IN interrupt endpoint */
  874. ret = EP_CONFIGURE(priv->epintin, &g_epintindesc, false);
  875. if (ret < 0)
  876. {
  877. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPINTINCONFIGFAIL), 0);
  878. goto errout;
  879. }
  880. priv->epintin->priv = priv;
  881. /* Configure the IN bulk endpoint */
  882. #ifdef CONFIG_USBDEV_DUALSPEED
  883. if (priv->usbdev->speed == USB_SPEED_HIGH)
  884. {
  885. bulkmxpacket = 512;
  886. }
  887. else
  888. {
  889. bulkmxpacket = 64;
  890. }
  891. usbclass_mkepbulkdesc(&g_epbulkindesc, bulkmxpacket, &epdesc);
  892. ret = EP_CONFIGURE(priv->epbulkin, &epdesc, false);
  893. #else
  894. ret = EP_CONFIGURE(priv->epbulkin, &g_epbulkindesc, false);
  895. #endif
  896. if (ret < 0)
  897. {
  898. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINCONFIGFAIL), 0);
  899. goto errout;
  900. }
  901. priv->epbulkin->priv = priv;
  902. /* Configure the OUT bulk endpoint */
  903. #ifdef CONFIG_USBDEV_DUALSPEED
  904. usbclass_mkepbulkdesc(&g_epbulkoutdesc, bulkmxpacket, &epdesc);
  905. ret = EP_CONFIGURE(priv->epbulkout, &epdesc, true);
  906. #else
  907. ret = EP_CONFIGURE(priv->epbulkout, &g_epbulkoutdesc, true);
  908. #endif
  909. if (ret < 0)
  910. {
  911. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKOUTCONFIGFAIL), 0);
  912. goto errout;
  913. }
  914. priv->epbulkout->priv = priv;
  915. /* Queue read requests in the bulk OUT endpoint */
  916. DEBUGASSERT(priv->nrdq == 0);
  917. for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
  918. {
  919. req = priv->rdreqs[i].req;
  920. req->callback = usbclass_rdcomplete;
  921. ret = EP_SUBMIT(priv->epbulkout, req);
  922. if (ret != OK)
  923. {
  924. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSUBMIT), (uint16_t)-ret);
  925. goto errout;
  926. }
  927. priv->nrdq++;
  928. }
  929. /* We are successfully configured */
  930. priv->config = config;
  931. /* Inform the "upper half" driver that we are "open for business" */
  932. #ifdef CONFIG_SERIAL_REMOVABLE
  933. uart_connected(&priv->serdev, true);
  934. #endif
  935. return OK;
  936. errout:
  937. usbclass_resetconfig(priv);
  938. return ret;
  939. }
  940. /****************************************************************************
  941. * Name: usbclass_ep0incomplete
  942. *
  943. * Description:
  944. * Handle completion of EP0 control operations
  945. *
  946. ****************************************************************************/
  947. static void usbclass_ep0incomplete(FAR struct usbdev_ep_s *ep,
  948. FAR struct usbdev_req_s *req)
  949. {
  950. if (req->result || req->xfrd != req->len)
  951. {
  952. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_REQRESULT), (uint16_t)-req->result);
  953. }
  954. }
  955. /****************************************************************************
  956. * Name: usbclass_rdcomplete
  957. *
  958. * Description:
  959. * Handle completion of read request on the bulk OUT endpoint. This
  960. * is handled like the receipt of serial data on the "UART"
  961. *
  962. ****************************************************************************/
  963. static void usbclass_rdcomplete(FAR struct usbdev_ep_s *ep,
  964. FAR struct usbdev_req_s *req)
  965. {
  966. FAR struct pl2303_dev_s *priv;
  967. irqstate_t flags;
  968. int ret;
  969. /* Sanity check */
  970. #ifdef CONFIG_DEBUG
  971. if (!ep || !ep->priv || !req)
  972. {
  973. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  974. return;
  975. }
  976. #endif
  977. /* Extract references to private data */
  978. priv = (FAR struct pl2303_dev_s*)ep->priv;
  979. /* Process the received data unless this is some unusual condition */
  980. flags = irqsave();
  981. switch (req->result)
  982. {
  983. case 0: /* Normal completion */
  984. usbtrace(TRACE_CLASSRDCOMPLETE, priv->nrdq);
  985. usbclass_recvpacket(priv, req->buf, req->xfrd);
  986. break;
  987. case -ESHUTDOWN: /* Disconnection */
  988. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSHUTDOWN), 0);
  989. priv->nrdq--;
  990. irqrestore(flags);
  991. return;
  992. default: /* Some other error occurred */
  993. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDUNEXPECTED), (uint16_t)-req->result);
  994. break;
  995. };
  996. /* Requeue the read request */
  997. req->len = ep->maxpacket;
  998. ret = EP_SUBMIT(ep, req);
  999. if (ret != OK)
  1000. {
  1001. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDSUBMIT), (uint16_t)-req->result);
  1002. }
  1003. irqrestore(flags);
  1004. }
  1005. /****************************************************************************
  1006. * Name: usbclass_wrcomplete
  1007. *
  1008. * Description:
  1009. * Handle completion of write request. This function probably executes
  1010. * in the context of an interrupt handler.
  1011. *
  1012. ****************************************************************************/
  1013. static void usbclass_wrcomplete(FAR struct usbdev_ep_s *ep,
  1014. FAR struct usbdev_req_s *req)
  1015. {
  1016. FAR struct pl2303_dev_s *priv;
  1017. FAR struct pl2303_req_s *reqcontainer;
  1018. irqstate_t flags;
  1019. /* Sanity check */
  1020. #ifdef CONFIG_DEBUG
  1021. if (!ep || !ep->priv || !req || !req->priv)
  1022. {
  1023. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1024. return;
  1025. }
  1026. #endif
  1027. /* Extract references to our private data */
  1028. priv = (FAR struct pl2303_dev_s *)ep->priv;
  1029. reqcontainer = (FAR struct pl2303_req_s *)req->priv;
  1030. /* Return the write request to the free list */
  1031. flags = irqsave();
  1032. sq_addlast((sq_entry_t*)reqcontainer, &priv->reqlist);
  1033. priv->nwrq++;
  1034. irqrestore(flags);
  1035. /* Send the next packet unless this was some unusual termination
  1036. * condition
  1037. */
  1038. switch (req->result)
  1039. {
  1040. case OK: /* Normal completion */
  1041. usbtrace(TRACE_CLASSWRCOMPLETE, priv->nwrq);
  1042. usbclass_sndpacket(priv);
  1043. break;
  1044. case -ESHUTDOWN: /* Disconnection */
  1045. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRSHUTDOWN), priv->nwrq);
  1046. break;
  1047. default: /* Some other error occurred */
  1048. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRUNEXPECTED), (uint16_t)-req->result);
  1049. break;
  1050. }
  1051. }
  1052. /****************************************************************************
  1053. * USB Class Driver Methods
  1054. ****************************************************************************/
  1055. /****************************************************************************
  1056. * Name: usbclass_bind
  1057. *
  1058. * Description:
  1059. * Invoked when the driver is bound to a USB device driver
  1060. *
  1061. ****************************************************************************/
  1062. static int usbclass_bind(FAR struct usbdevclass_driver_s *driver,
  1063. FAR struct usbdev_s *dev)
  1064. {
  1065. FAR struct pl2303_dev_s *priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1066. FAR struct pl2303_req_s *reqcontainer;
  1067. irqstate_t flags;
  1068. uint16_t reqlen;
  1069. int ret;
  1070. int i;
  1071. usbtrace(TRACE_CLASSBIND, 0);
  1072. /* Bind the structures */
  1073. priv->usbdev = dev;
  1074. /* Save the reference to our private data structure in EP0 so that it
  1075. * can be recovered in ep0 completion events (Unless we are part of
  1076. * a composite device and, in that case, the composite device owns
  1077. * EP0).
  1078. */
  1079. dev->ep0->priv = priv;
  1080. /* Preallocate control request */
  1081. priv->ctrlreq = usbclass_allocreq(dev->ep0, PL2303_MXDESCLEN);
  1082. if (priv->ctrlreq == NULL)
  1083. {
  1084. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCCTRLREQ), 0);
  1085. ret = -ENOMEM;
  1086. goto errout;
  1087. }
  1088. priv->ctrlreq->callback = usbclass_ep0incomplete;
  1089. /* Pre-allocate all endpoints... the endpoints will not be functional
  1090. * until the SET CONFIGURATION request is processed in usbclass_setconfig.
  1091. * This is done here because there may be calls to kmalloc and the SET
  1092. * CONFIGURATION processing probably occurrs within interrupt handling
  1093. * logic where kmalloc calls will fail.
  1094. */
  1095. /* Pre-allocate the IN interrupt endpoint */
  1096. priv->epintin = DEV_ALLOCEP(dev, PL2303_EPINTIN_ADDR, true, USB_EP_ATTR_XFER_INT);
  1097. if (!priv->epintin)
  1098. {
  1099. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPINTINALLOCFAIL), 0);
  1100. ret = -ENODEV;
  1101. goto errout;
  1102. }
  1103. priv->epintin->priv = priv;
  1104. /* Pre-allocate the IN bulk endpoint */
  1105. priv->epbulkin = DEV_ALLOCEP(dev, PL2303_EPINBULK_ADDR, true, USB_EP_ATTR_XFER_BULK);
  1106. if (!priv->epbulkin)
  1107. {
  1108. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKINALLOCFAIL), 0);
  1109. ret = -ENODEV;
  1110. goto errout;
  1111. }
  1112. priv->epbulkin->priv = priv;
  1113. /* Pre-allocate the OUT bulk endpoint */
  1114. priv->epbulkout = DEV_ALLOCEP(dev, PL2303_EPOUTBULK_ADDR, false, USB_EP_ATTR_XFER_BULK);
  1115. if (!priv->epbulkout)
  1116. {
  1117. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPBULKOUTALLOCFAIL), 0);
  1118. ret = -ENODEV;
  1119. goto errout;
  1120. }
  1121. priv->epbulkout->priv = priv;
  1122. /* Pre-allocate read requests */
  1123. reqlen = priv->epbulkout->maxpacket;
  1124. for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
  1125. {
  1126. reqcontainer = &priv->rdreqs[i];
  1127. reqcontainer->req = usbclass_allocreq(priv->epbulkout, reqlen);
  1128. if (reqcontainer->req == NULL)
  1129. {
  1130. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_RDALLOCREQ), -ENOMEM);
  1131. ret = -ENOMEM;
  1132. goto errout;
  1133. }
  1134. reqcontainer->req->priv = reqcontainer;
  1135. reqcontainer->req->callback = usbclass_rdcomplete;
  1136. }
  1137. /* Pre-allocate write request containers and put in a free list */
  1138. reqlen = max(CONFIG_PL2303_BULKIN_REQLEN, priv->epbulkin->maxpacket);
  1139. for (i = 0; i < CONFIG_PL2303_NWRREQS; i++)
  1140. {
  1141. reqcontainer = &priv->wrreqs[i];
  1142. reqcontainer->req = usbclass_allocreq(priv->epbulkin, reqlen);
  1143. if (reqcontainer->req == NULL)
  1144. {
  1145. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_WRALLOCREQ), -ENOMEM);
  1146. ret = -ENOMEM;
  1147. goto errout;
  1148. }
  1149. reqcontainer->req->priv = reqcontainer;
  1150. reqcontainer->req->callback = usbclass_wrcomplete;
  1151. flags = irqsave();
  1152. sq_addlast((sq_entry_t*)reqcontainer, &priv->reqlist);
  1153. priv->nwrq++; /* Count of write requests available */
  1154. irqrestore(flags);
  1155. }
  1156. /* Report if we are selfpowered */
  1157. #ifdef CONFIG_USBDEV_SELFPOWERED
  1158. DEV_SETSELFPOWERED(dev);
  1159. #endif
  1160. /* And pull-up the data line for the soft connect function */
  1161. DEV_CONNECT(dev);
  1162. return OK;
  1163. errout:
  1164. usbclass_unbind(driver, dev);
  1165. return ret;
  1166. }
  1167. /****************************************************************************
  1168. * Name: usbclass_unbind
  1169. *
  1170. * Description:
  1171. * Invoked when the driver is unbound from a USB device driver
  1172. *
  1173. ****************************************************************************/
  1174. static void usbclass_unbind(FAR struct usbdevclass_driver_s *driver,
  1175. FAR struct usbdev_s *dev)
  1176. {
  1177. FAR struct pl2303_dev_s *priv;
  1178. FAR struct pl2303_req_s *reqcontainer;
  1179. irqstate_t flags;
  1180. int i;
  1181. usbtrace(TRACE_CLASSUNBIND, 0);
  1182. #ifdef CONFIG_DEBUG
  1183. if (!driver || !dev || !dev->ep0)
  1184. {
  1185. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1186. return;
  1187. }
  1188. #endif
  1189. /* Extract reference to private data */
  1190. priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1191. #ifdef CONFIG_DEBUG
  1192. if (!priv)
  1193. {
  1194. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
  1195. return;
  1196. }
  1197. #endif
  1198. /* Make sure that we are not already unbound */
  1199. if (priv != NULL)
  1200. {
  1201. /* Make sure that the endpoints have been unconfigured. If
  1202. * we were terminated gracefully, then the configuration should
  1203. * already have been reset. If not, then calling usbclass_resetconfig
  1204. * should cause the endpoints to immediately terminate all
  1205. * transfers and return the requests to us (with result == -ESHUTDOWN)
  1206. */
  1207. usbclass_resetconfig(priv);
  1208. up_mdelay(50);
  1209. /* Free the interrupt IN endpoint */
  1210. if (priv->epintin)
  1211. {
  1212. DEV_FREEEP(dev, priv->epintin);
  1213. priv->epintin = NULL;
  1214. }
  1215. /* Free the bulk IN endpoint */
  1216. if (priv->epbulkin)
  1217. {
  1218. DEV_FREEEP(dev, priv->epbulkin);
  1219. priv->epbulkin = NULL;
  1220. }
  1221. /* Free the pre-allocated control request */
  1222. if (priv->ctrlreq != NULL)
  1223. {
  1224. usbclass_freereq(dev->ep0, priv->ctrlreq);
  1225. priv->ctrlreq = NULL;
  1226. }
  1227. /* Free pre-allocated read requests (which should all have
  1228. * been returned to the free list at this time -- we don't check)
  1229. */
  1230. DEBUGASSERT(priv->nrdq == 0);
  1231. for (i = 0; i < CONFIG_PL2303_NRDREQS; i++)
  1232. {
  1233. reqcontainer = &priv->rdreqs[i];
  1234. if (reqcontainer->req)
  1235. {
  1236. usbclass_freereq(priv->epbulkout, reqcontainer->req);
  1237. reqcontainer->req = NULL;
  1238. }
  1239. }
  1240. /* Free the bulk OUT endpoint */
  1241. if (priv->epbulkout)
  1242. {
  1243. DEV_FREEEP(dev, priv->epbulkout);
  1244. priv->epbulkout = NULL;
  1245. }
  1246. /* Free write requests that are not in use (which should be all
  1247. * of them
  1248. */
  1249. flags = irqsave();
  1250. DEBUGASSERT(priv->nwrq == CONFIG_PL2303_NWRREQS);
  1251. while (!sq_empty(&priv->reqlist))
  1252. {
  1253. reqcontainer = (struct pl2303_req_s *)sq_remfirst(&priv->reqlist);
  1254. if (reqcontainer->req != NULL)
  1255. {
  1256. usbclass_freereq(priv->epbulkin, reqcontainer->req);
  1257. priv->nwrq--; /* Number of write requests queued */
  1258. }
  1259. }
  1260. DEBUGASSERT(priv->nwrq == 0);
  1261. irqrestore(flags);
  1262. }
  1263. /* Clear out all data in the circular buffer */
  1264. priv->serdev.xmit.head = 0;
  1265. priv->serdev.xmit.tail = 0;
  1266. }
  1267. /****************************************************************************
  1268. * Name: usbclass_setup
  1269. *
  1270. * Description:
  1271. * Invoked for ep0 control requests. This function probably executes
  1272. * in the context of an interrupt handler.
  1273. *
  1274. ****************************************************************************/
  1275. static int usbclass_setup(FAR struct usbdevclass_driver_s *driver,
  1276. FAR struct usbdev_s *dev,
  1277. FAR const struct usb_ctrlreq_s *ctrl,
  1278. FAR uint8_t *dataout, size_t outlen)
  1279. {
  1280. FAR struct pl2303_dev_s *priv;
  1281. FAR struct usbdev_req_s *ctrlreq;
  1282. uint16_t value;
  1283. uint16_t index;
  1284. uint16_t len;
  1285. int ret = -EOPNOTSUPP;
  1286. #ifdef CONFIG_DEBUG
  1287. if (!driver || !dev || !dev->ep0 || !ctrl)
  1288. {
  1289. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1290. return -EIO;
  1291. }
  1292. #endif
  1293. /* Extract reference to private data */
  1294. usbtrace(TRACE_CLASSSETUP, ctrl->req);
  1295. priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1296. #ifdef CONFIG_DEBUG
  1297. if (!priv || !priv->ctrlreq)
  1298. {
  1299. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
  1300. return -ENODEV;
  1301. }
  1302. #endif
  1303. ctrlreq = priv->ctrlreq;
  1304. /* Extract the little-endian 16-bit values to host order */
  1305. value = GETUINT16(ctrl->value);
  1306. index = GETUINT16(ctrl->index);
  1307. len = GETUINT16(ctrl->len);
  1308. uvdbg("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
  1309. ctrl->type, ctrl->req, value, index, len);
  1310. switch (ctrl->type & USB_REQ_TYPE_MASK)
  1311. {
  1312. /***********************************************************************
  1313. * Standard Requests
  1314. ***********************************************************************/
  1315. case USB_REQ_TYPE_STANDARD:
  1316. {
  1317. switch (ctrl->req)
  1318. {
  1319. case USB_REQ_GETDESCRIPTOR:
  1320. {
  1321. /* The value field specifies the descriptor type in the MS byte and the
  1322. * descriptor index in the LS byte (order is little endian)
  1323. */
  1324. switch (ctrl->value[1])
  1325. {
  1326. case USB_DESC_TYPE_DEVICE:
  1327. {
  1328. ret = USB_SIZEOF_DEVDESC;
  1329. memcpy(ctrlreq->buf, &g_devdesc, ret);
  1330. }
  1331. break;
  1332. #ifdef CONFIG_USBDEV_DUALSPEED
  1333. case USB_DESC_TYPE_DEVICEQUALIFIER:
  1334. {
  1335. ret = USB_SIZEOF_QUALDESC;
  1336. memcpy(ctrlreq->buf, &g_qualdesc, ret);
  1337. }
  1338. break;
  1339. case USB_DESC_TYPE_OTHERSPEEDCONFIG:
  1340. #endif /* CONFIG_USBDEV_DUALSPEED */
  1341. case USB_DESC_TYPE_CONFIG:
  1342. {
  1343. #ifdef CONFIG_USBDEV_DUALSPEED
  1344. ret = usbclass_mkcfgdesc(ctrlreq->buf, dev->speed, ctrl->req);
  1345. #else
  1346. ret = usbclass_mkcfgdesc(ctrlreq->buf);
  1347. #endif
  1348. }
  1349. break;
  1350. case USB_DESC_TYPE_STRING:
  1351. {
  1352. /* index == language code. */
  1353. ret = usbclass_mkstrdesc(ctrl->value[0], (struct usb_strdesc_s *)ctrlreq->buf);
  1354. }
  1355. break;
  1356. default:
  1357. {
  1358. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_GETUNKNOWNDESC), value);
  1359. }
  1360. break;
  1361. }
  1362. }
  1363. break;
  1364. case USB_REQ_SETCONFIGURATION:
  1365. {
  1366. if (ctrl->type == 0)
  1367. {
  1368. ret = usbclass_setconfig(priv, value);
  1369. }
  1370. }
  1371. break;
  1372. case USB_REQ_GETCONFIGURATION:
  1373. {
  1374. if (ctrl->type == USB_DIR_IN)
  1375. {
  1376. *(uint8_t*)ctrlreq->buf = priv->config;
  1377. ret = 1;
  1378. }
  1379. }
  1380. break;
  1381. case USB_REQ_SETINTERFACE:
  1382. {
  1383. if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE)
  1384. {
  1385. if (priv->config == PL2303_CONFIGID &&
  1386. index == PL2303_INTERFACEID &&
  1387. value == PL2303_ALTINTERFACEID)
  1388. {
  1389. usbclass_resetconfig(priv);
  1390. usbclass_setconfig(priv, priv->config);
  1391. ret = 0;
  1392. }
  1393. }
  1394. }
  1395. break;
  1396. case USB_REQ_GETINTERFACE:
  1397. {
  1398. if (ctrl->type == (USB_DIR_IN|USB_REQ_RECIPIENT_INTERFACE) &&
  1399. priv->config == PL2303_CONFIGIDNONE)
  1400. {
  1401. if (index != PL2303_INTERFACEID)
  1402. {
  1403. ret = -EDOM;
  1404. }
  1405. else
  1406. {
  1407. *(uint8_t*) ctrlreq->buf = PL2303_ALTINTERFACEID;
  1408. ret = 1;
  1409. }
  1410. }
  1411. }
  1412. break;
  1413. default:
  1414. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDSTDREQ), ctrl->req);
  1415. break;
  1416. }
  1417. }
  1418. break;
  1419. /***********************************************************************
  1420. * PL2303 Vendor-Specific Requests
  1421. ***********************************************************************/
  1422. case PL2303_CONTROL_TYPE:
  1423. {
  1424. if ((ctrl->type & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_INTERFACE)
  1425. {
  1426. switch (ctrl->req)
  1427. {
  1428. case PL2303_SETLINEREQUEST:
  1429. {
  1430. memcpy(priv->linest, ctrlreq->buf, min(len, 7));
  1431. ret = 0;
  1432. }
  1433. break;
  1434. case PL2303_GETLINEREQUEST:
  1435. {
  1436. memcpy(ctrlreq->buf, priv->linest, 7);
  1437. ret = 7;
  1438. }
  1439. break;
  1440. case PL2303_SETCONTROLREQUEST:
  1441. case PL2303_BREAKREQUEST:
  1442. {
  1443. ret = 0;
  1444. }
  1445. break;
  1446. default:
  1447. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDCLASSREQ), ctrl->type);
  1448. break;
  1449. }
  1450. }
  1451. }
  1452. break;
  1453. case PL2303_RWREQUEST_TYPE:
  1454. {
  1455. if ((ctrl->type & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_DEVICE)
  1456. {
  1457. if (ctrl->req == PL2303_RWREQUEST)
  1458. {
  1459. if ((ctrl->type & USB_DIR_IN) != 0)
  1460. {
  1461. *(uint32_t*)ctrlreq->buf = 0xdeadbeef;
  1462. ret = 4;
  1463. }
  1464. else
  1465. {
  1466. ret = 0;
  1467. }
  1468. }
  1469. else
  1470. {
  1471. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDCLASSREQ), ctrl->type);
  1472. }
  1473. }
  1474. }
  1475. break;
  1476. default:
  1477. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UNSUPPORTEDTYPE), ctrl->type);
  1478. break;
  1479. }
  1480. /* Respond to the setup command if data was returned. On an error return
  1481. * value (ret < 0), the USB driver will stall.
  1482. */
  1483. if (ret >= 0)
  1484. {
  1485. ctrlreq->len = min(len, ret);
  1486. ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
  1487. ret = EP_SUBMIT(dev->ep0, ctrlreq);
  1488. if (ret < 0)
  1489. {
  1490. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EPRESPQ), (uint16_t)-ret);
  1491. ctrlreq->result = OK;
  1492. usbclass_ep0incomplete(dev->ep0, ctrlreq);
  1493. }
  1494. }
  1495. return ret;
  1496. }
  1497. /****************************************************************************
  1498. * Name: usbclass_disconnect
  1499. *
  1500. * Description:
  1501. * Invoked after all transfers have been stopped, when the host is
  1502. * disconnected. This function is probably called from the context of an
  1503. * interrupt handler.
  1504. *
  1505. ****************************************************************************/
  1506. static void usbclass_disconnect(FAR struct usbdevclass_driver_s *driver,
  1507. FAR struct usbdev_s *dev)
  1508. {
  1509. FAR struct pl2303_dev_s *priv;
  1510. irqstate_t flags;
  1511. usbtrace(TRACE_CLASSDISCONNECT, 0);
  1512. #ifdef CONFIG_DEBUG
  1513. if (!driver || !dev || !dev->ep0)
  1514. {
  1515. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1516. return;
  1517. }
  1518. #endif
  1519. /* Extract reference to private data */
  1520. priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1521. #ifdef CONFIG_DEBUG
  1522. if (!priv)
  1523. {
  1524. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_EP0NOTBOUND), 0);
  1525. return;
  1526. }
  1527. #endif
  1528. /* Inform the "upper half serial driver that we have lost the USB serial
  1529. * connection.
  1530. */
  1531. flags = irqsave();
  1532. #ifdef CONFIG_SERIAL_REMOVABLE
  1533. uart_connected(&priv->serdev, false);
  1534. #endif
  1535. /* Reset the configuration */
  1536. usbclass_resetconfig(priv);
  1537. /* Clear out all outgoing data in the circular buffer */
  1538. priv->serdev.xmit.head = 0;
  1539. priv->serdev.xmit.tail = 0;
  1540. irqrestore(flags);
  1541. /* Perform the soft connect function so that we will we can be
  1542. * re-enumerated.
  1543. */
  1544. DEV_CONNECT(dev);
  1545. }
  1546. /****************************************************************************
  1547. * Name: usbclass_suspend
  1548. *
  1549. * Description:
  1550. * Handle the USB suspend event.
  1551. *
  1552. ****************************************************************************/
  1553. #ifdef CONFIG_SERIAL_REMOVABLE
  1554. static void usbclass_suspend(FAR struct usbdevclass_driver_s *driver,
  1555. FAR struct usbdev_s *dev)
  1556. {
  1557. FAR struct pl2303_dev_s *priv;
  1558. usbtrace(TRACE_CLASSSUSPEND, 0);
  1559. #ifdef CONFIG_DEBUG
  1560. if (!driver || !dev)
  1561. {
  1562. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1563. return;
  1564. }
  1565. #endif
  1566. /* Extract reference to private data */
  1567. priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1568. /* And let the "upper half" driver now that we are suspended */
  1569. uart_connected(&priv->serdev, false);
  1570. }
  1571. #endif
  1572. /****************************************************************************
  1573. * Name: usbclass_resume
  1574. *
  1575. * Description:
  1576. * Handle the USB resume event.
  1577. *
  1578. ****************************************************************************/
  1579. #ifdef CONFIG_SERIAL_REMOVABLE
  1580. static void usbclass_resume(FAR struct usbdevclass_driver_s *driver,
  1581. FAR struct usbdev_s *dev)
  1582. {
  1583. FAR struct pl2303_dev_s *priv;
  1584. usbtrace(TRACE_CLASSRESUME, 0);
  1585. #ifdef CONFIG_DEBUG
  1586. if (!driver || !dev)
  1587. {
  1588. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1589. return;
  1590. }
  1591. #endif
  1592. /* Extract reference to private data */
  1593. priv = ((FAR struct pl2303_driver_s*)driver)->dev;
  1594. /* Are we still configured? */
  1595. if (priv->config != PL2303_CONFIGIDNONE)
  1596. {
  1597. /* Yes.. let the "upper half" know that have resumed */
  1598. uart_connected(&priv->serdev, true);
  1599. }
  1600. }
  1601. #endif
  1602. /****************************************************************************
  1603. * Serial Device Methods
  1604. ****************************************************************************/
  1605. /****************************************************************************
  1606. * Name: usbser_setup
  1607. *
  1608. * Description:
  1609. * This method is called the first time that the serial port is opened.
  1610. *
  1611. ****************************************************************************/
  1612. static int usbser_setup(FAR struct uart_dev_s *dev)
  1613. {
  1614. FAR struct pl2303_dev_s *priv;
  1615. usbtrace(PL2303_CLASSAPI_SETUP, 0);
  1616. /* Sanity check */
  1617. #if CONFIG_DEBUG
  1618. if (!dev || !dev->priv)
  1619. {
  1620. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1621. return -EIO;
  1622. }
  1623. #endif
  1624. /* Extract reference to private data */
  1625. priv = (FAR struct pl2303_dev_s*)dev->priv;
  1626. /* Check if we have been configured */
  1627. if (priv->config == PL2303_CONFIGIDNONE)
  1628. {
  1629. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_SETUPNOTCONNECTED), 0);
  1630. return -ENOTCONN;
  1631. }
  1632. return OK;
  1633. }
  1634. /****************************************************************************
  1635. * Name: usbser_shutdown
  1636. *
  1637. * Description:
  1638. * This method is called when the serial port is closed. This operation
  1639. * is very simple for the USB serial backend because the serial driver
  1640. * has already assured that the TX data has full drained -- it calls
  1641. * usbser_txempty() until that function returns true before calling this
  1642. * function.
  1643. *
  1644. ****************************************************************************/
  1645. static void usbser_shutdown(FAR struct uart_dev_s *dev)
  1646. {
  1647. usbtrace(PL2303_CLASSAPI_SHUTDOWN, 0);
  1648. /* Sanity check */
  1649. #if CONFIG_DEBUG
  1650. if (!dev || !dev->priv)
  1651. {
  1652. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1653. }
  1654. #endif
  1655. }
  1656. /****************************************************************************
  1657. * Name: usbser_attach
  1658. *
  1659. * Description:
  1660. * Does not apply to the USB serial class device
  1661. *
  1662. ****************************************************************************/
  1663. static int usbser_attach(FAR struct uart_dev_s *dev)
  1664. {
  1665. usbtrace(PL2303_CLASSAPI_ATTACH, 0);
  1666. return OK;
  1667. }
  1668. /****************************************************************************
  1669. * Name: usbser_detach
  1670. *
  1671. * Description:
  1672. * Does not apply to the USB serial class device
  1673. *
  1674. ****************************************************************************/
  1675. static void usbser_detach(FAR struct uart_dev_s *dev)
  1676. {
  1677. usbtrace(PL2303_CLASSAPI_DETACH, 0);
  1678. }
  1679. /****************************************************************************
  1680. * Name: usbser_rxint
  1681. *
  1682. * Description:
  1683. * Called by the serial driver to enable or disable RX interrupts. We, of
  1684. * course, have no RX interrupts but must behave consistently. This method
  1685. * is called under the conditions:
  1686. *
  1687. * 1. With enable==true when the port is opened (just after usbser_setup
  1688. * and usbser_attach are called called)
  1689. * 2. With enable==false while transferring data from the RX buffer
  1690. * 2. With enable==true while waiting for more incoming data
  1691. * 3. With enable==false when the port is closed (just before usbser_detach
  1692. * and usbser_shutdown are called).
  1693. *
  1694. ****************************************************************************/
  1695. static void usbser_rxint(FAR struct uart_dev_s *dev, bool enable)
  1696. {
  1697. FAR struct pl2303_dev_s *priv;
  1698. FAR uart_dev_t *serdev;
  1699. irqstate_t flags;
  1700. usbtrace(PL2303_CLASSAPI_RXINT, (uint16_t)enable);
  1701. /* Sanity check */
  1702. #if CONFIG_DEBUG
  1703. if (!dev || !dev->priv)
  1704. {
  1705. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1706. return;
  1707. }
  1708. #endif
  1709. /* Extract reference to private data */
  1710. priv = (FAR struct pl2303_dev_s*)dev->priv;
  1711. serdev = &priv->serdev;
  1712. /* We need exclusive access to the RX buffer and private structure
  1713. * in the following.
  1714. */
  1715. flags = irqsave();
  1716. if (enable)
  1717. {
  1718. /* RX "interrupts" are enabled. Is this a transition from disabled
  1719. * to enabled state?
  1720. */
  1721. if (!priv->rxenabled)
  1722. {
  1723. /* Yes. During the time that RX interrupts are disabled, the
  1724. * the serial driver will be extracting data from the circular
  1725. * buffer and modifying recv.tail. During this time, we
  1726. * should avoid modifying recv.head; When interrupts are restored,
  1727. * we can update the head pointer for all of the data that we
  1728. * put into cicular buffer while "interrupts" were disabled.
  1729. */
  1730. if (priv->rxhead != serdev->recv.head)
  1731. {
  1732. serdev->recv.head = priv->rxhead;
  1733. /* Yes... signal the availability of new data */
  1734. uart_datareceived(serdev);
  1735. }
  1736. /* RX "interrupts are no longer disabled */
  1737. priv->rxenabled = true;
  1738. }
  1739. }
  1740. /* RX "interrupts" are disabled. Is this a transition from enabled
  1741. * to disabled state?
  1742. */
  1743. else if (priv->rxenabled)
  1744. {
  1745. /* Yes. During the time that RX interrupts are disabled, the
  1746. * the serial driver will be extracting data from the circular
  1747. * buffer and modifying recv.tail. During this time, we
  1748. * should avoid modifying recv.head; When interrupts are disabled,
  1749. * we use a shadow index and continue adding data to the circular
  1750. * buffer.
  1751. */
  1752. priv->rxhead = serdev->recv.head;
  1753. priv->rxenabled = false;
  1754. }
  1755. irqrestore(flags);
  1756. }
  1757. /****************************************************************************
  1758. * Name: usbser_txint
  1759. *
  1760. * Description:
  1761. * Called by the serial driver to enable or disable TX interrupts. We, of
  1762. * course, have no TX interrupts but must behave consistently. Initially,
  1763. * TX interrupts are disabled. This method is called under the conditions:
  1764. *
  1765. * 1. With enable==false while transferring data into the TX buffer
  1766. * 2. With enable==true when data may be taken from the buffer.
  1767. * 3. With enable==false when the TX buffer is empty
  1768. *
  1769. ****************************************************************************/
  1770. static void usbser_txint(FAR struct uart_dev_s *dev, bool enable)
  1771. {
  1772. FAR struct pl2303_dev_s *priv;
  1773. usbtrace(PL2303_CLASSAPI_TXINT, (uint16_t)enable);
  1774. /* Sanity checks */
  1775. #if CONFIG_DEBUG
  1776. if (!dev || !dev->priv)
  1777. {
  1778. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1779. return;
  1780. }
  1781. #endif
  1782. /* Extract references to private data */
  1783. priv = (FAR struct pl2303_dev_s*)dev->priv;
  1784. /* If the new state is enabled and if there is data in the XMIT buffer,
  1785. * send the next packet now.
  1786. */
  1787. uvdbg("enable=%d head=%d tail=%d\n",
  1788. enable, priv->serdev.xmit.head, priv->serdev.xmit.tail);
  1789. if (enable && priv->serdev.xmit.head != priv->serdev.xmit.tail)
  1790. {
  1791. usbclass_sndpacket(priv);
  1792. }
  1793. }
  1794. /****************************************************************************
  1795. * Name: usbser_txempty
  1796. *
  1797. * Description:
  1798. * Return true when all data has been sent. This is called from the
  1799. * serial driver when the driver is closed. It will call this API
  1800. * periodically until it reports true. NOTE that the serial driver takes all
  1801. * responsibility for flushing TX data through the hardware so we can be
  1802. * a bit sloppy about that.
  1803. *
  1804. ****************************************************************************/
  1805. static bool usbser_txempty(FAR struct uart_dev_s *dev)
  1806. {
  1807. FAR struct pl2303_dev_s *priv = (FAR struct pl2303_dev_s*)dev->priv;
  1808. usbtrace(PL2303_CLASSAPI_TXEMPTY, 0);
  1809. #if CONFIG_DEBUG
  1810. if (!priv)
  1811. {
  1812. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_INVALIDARG), 0);
  1813. return true;
  1814. }
  1815. #endif
  1816. /* When all of the allocated write requests have been returned to the
  1817. * reqlist, then there is no longer any TX data in flight.
  1818. */
  1819. return priv->nwrq >= CONFIG_PL2303_NWRREQS;
  1820. }
  1821. /****************************************************************************
  1822. * Public Functions
  1823. ****************************************************************************/
  1824. /****************************************************************************
  1825. * Name: usbdev_serialinitialize
  1826. *
  1827. * Description:
  1828. * Register USB serial port (and USB serial console if so configured).
  1829. *
  1830. ****************************************************************************/
  1831. int usbdev_serialinitialize(int minor)
  1832. {
  1833. FAR struct pl2303_alloc_s *alloc;
  1834. FAR struct pl2303_dev_s *priv;
  1835. FAR struct pl2303_driver_s *drvr;
  1836. char devname[16];
  1837. int ret;
  1838. /* Allocate the structures needed */
  1839. alloc = (FAR struct pl2303_alloc_s*)kmalloc(sizeof(struct pl2303_alloc_s));
  1840. if (!alloc)
  1841. {
  1842. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCDEVSTRUCT), 0);
  1843. return -ENOMEM;
  1844. }
  1845. /* Convenience pointers into the allocated blob */
  1846. priv = &alloc->dev;
  1847. drvr = &alloc->drvr;
  1848. /* Initialize the USB serial driver structure */
  1849. memset(priv, 0, sizeof(struct pl2303_dev_s));
  1850. sq_init(&priv->reqlist);
  1851. /* Fake line status */
  1852. priv->linest[0] = (115200) & 0xff; /* Baud=115200 */
  1853. priv->linest[1] = (115200 >> 8) & 0xff;
  1854. priv->linest[2] = (115200 >> 16) & 0xff;
  1855. priv->linest[3] = (115200 >> 24) & 0xff;
  1856. priv->linest[4] = 0; /* One stop bit */
  1857. priv->linest[5] = 0; /* No parity */
  1858. priv->linest[6] = 8; /*8 data bits */
  1859. /* Initialize the serial driver sub-structure */
  1860. #ifdef CONFIG_SERIAL_REMOVABLE
  1861. priv->serdev.disconnected = true;
  1862. #endif
  1863. priv->serdev.recv.size = CONFIG_PL2303_RXBUFSIZE;
  1864. priv->serdev.recv.buffer = priv->rxbuffer;
  1865. priv->serdev.xmit.size = CONFIG_PL2303_TXBUFSIZE;
  1866. priv->serdev.xmit.buffer = priv->txbuffer;
  1867. priv->serdev.ops = &g_uartops;
  1868. priv->serdev.priv = priv;
  1869. /* Initialize the USB class driver structure */
  1870. #ifdef CONFIG_USBDEV_DUALSPEED
  1871. drvr->drvr.speed = USB_SPEED_HIGH;
  1872. #else
  1873. drvr->drvr.speed = USB_SPEED_FULL;
  1874. #endif
  1875. drvr->drvr.ops = &g_driverops;
  1876. drvr->dev = priv;
  1877. /* Register the USB serial class driver */
  1878. ret = usbdev_register(&drvr->drvr);
  1879. if (ret)
  1880. {
  1881. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_DEVREGISTER), (uint16_t)-ret);
  1882. goto errout_with_alloc;
  1883. }
  1884. /* Register the USB serial console */
  1885. #ifdef CONFIG_PL2303_CONSOLE
  1886. priv->serdev.isconsole = true;
  1887. ret = uart_register("/dev/console", &priv->serdev);
  1888. if (ret < 0)
  1889. {
  1890. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_CONSOLEREGISTER), (uint16_t)-ret);
  1891. goto errout_with_class;
  1892. }
  1893. #endif
  1894. /* Register the single port supported by this implementation */
  1895. sprintf(devname, "/dev/ttyUSB%d", minor);
  1896. ret = uart_register(devname, &priv->serdev);
  1897. if (ret)
  1898. {
  1899. usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_UARTREGISTER), (uint16_t)-ret);
  1900. goto errout_with_class;
  1901. }
  1902. return OK;
  1903. errout_with_class:
  1904. usbdev_unregister(&drvr->drvr);
  1905. errout_with_alloc:
  1906. kfree(alloc);
  1907. return ret;
  1908. }