io_edgeport.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286
  1. /*
  2. * Edgeport USB Serial Converter driver
  3. *
  4. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  5. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  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; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Supports the following devices:
  13. * Edgeport/4
  14. * Edgeport/4t
  15. * Edgeport/2
  16. * Edgeport/4i
  17. * Edgeport/2i
  18. * Edgeport/421
  19. * Edgeport/21
  20. * Rapidport/4
  21. * Edgeport/8
  22. * Edgeport/2D8
  23. * Edgeport/4D8
  24. * Edgeport/8i
  25. *
  26. * For questions or problems with this driver, contact Inside Out
  27. * Networks technical support, or Peter Berger <pberger@brimson.com>,
  28. * or Al Borchers <alborchers@steinerpoint.com>.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_driver.h>
  38. #include <linux/tty_flip.h>
  39. #include <linux/module.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/serial.h>
  42. #include <linux/ioctl.h>
  43. #include <linux/wait.h>
  44. #include <linux/firmware.h>
  45. #include <linux/ihex.h>
  46. #include <linux/uaccess.h>
  47. #include <linux/usb.h>
  48. #include <linux/usb/serial.h>
  49. #include "io_edgeport.h"
  50. #include "io_ionsp.h" /* info for the iosp messages */
  51. #include "io_16654.h" /* 16654 UART defines */
  52. /*
  53. * Version Information
  54. */
  55. #define DRIVER_VERSION "v2.7"
  56. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com> and David Iacovelli"
  57. #define DRIVER_DESC "Edgeport USB Serial Driver"
  58. #define MAX_NAME_LEN 64
  59. #define CHASE_TIMEOUT (5*HZ) /* 5 seconds */
  60. #define OPEN_TIMEOUT (5*HZ) /* 5 seconds */
  61. #define COMMAND_TIMEOUT (5*HZ) /* 5 seconds */
  62. /* receive port state */
  63. enum RXSTATE {
  64. EXPECT_HDR1 = 0, /* Expect header byte 1 */
  65. EXPECT_HDR2 = 1, /* Expect header byte 2 */
  66. EXPECT_DATA = 2, /* Expect 'RxBytesRemaining' data */
  67. EXPECT_HDR3 = 3, /* Expect header byte 3 (for status hdrs only) */
  68. };
  69. /* Transmit Fifo
  70. * This Transmit queue is an extension of the edgeport Rx buffer.
  71. * The maximum amount of data buffered in both the edgeport
  72. * Rx buffer (maxTxCredits) and this buffer will never exceed maxTxCredits.
  73. */
  74. struct TxFifo {
  75. unsigned int head; /* index to head pointer (write) */
  76. unsigned int tail; /* index to tail pointer (read) */
  77. unsigned int count; /* Bytes in queue */
  78. unsigned int size; /* Max size of queue (equal to Max number of TxCredits) */
  79. unsigned char *fifo; /* allocated Buffer */
  80. };
  81. /* This structure holds all of the local port information */
  82. struct edgeport_port {
  83. __u16 txCredits; /* our current credits for this port */
  84. __u16 maxTxCredits; /* the max size of the port */
  85. struct TxFifo txfifo; /* transmit fifo -- size will be maxTxCredits */
  86. struct urb *write_urb; /* write URB for this port */
  87. bool write_in_progress; /* 'true' while a write URB is outstanding */
  88. spinlock_t ep_lock;
  89. __u8 shadowLCR; /* last LCR value received */
  90. __u8 shadowMCR; /* last MCR value received */
  91. __u8 shadowMSR; /* last MSR value received */
  92. __u8 shadowLSR; /* last LSR value received */
  93. __u8 shadowXonChar; /* last value set as XON char in Edgeport */
  94. __u8 shadowXoffChar; /* last value set as XOFF char in Edgeport */
  95. __u8 validDataMask;
  96. __u32 baudRate;
  97. bool open;
  98. bool openPending;
  99. bool commandPending;
  100. bool closePending;
  101. bool chaseResponsePending;
  102. wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
  103. wait_queue_head_t wait_open; /* for handling sleeping while waiting for open to finish */
  104. wait_queue_head_t wait_command; /* for handling sleeping while waiting for command to finish */
  105. wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
  106. struct async_icount icount;
  107. struct usb_serial_port *port; /* loop back to the owner of this object */
  108. };
  109. /* This structure holds all of the individual device information */
  110. struct edgeport_serial {
  111. char name[MAX_NAME_LEN+2]; /* string name of this device */
  112. struct edge_manuf_descriptor manuf_descriptor; /* the manufacturer descriptor */
  113. struct edge_boot_descriptor boot_descriptor; /* the boot firmware descriptor */
  114. struct edgeport_product_info product_info; /* Product Info */
  115. struct edge_compatibility_descriptor epic_descriptor; /* Edgeport compatible descriptor */
  116. int is_epic; /* flag if EPiC device or not */
  117. __u8 interrupt_in_endpoint; /* the interrupt endpoint handle */
  118. unsigned char *interrupt_in_buffer; /* the buffer we use for the interrupt endpoint */
  119. struct urb *interrupt_read_urb; /* our interrupt urb */
  120. __u8 bulk_in_endpoint; /* the bulk in endpoint handle */
  121. unsigned char *bulk_in_buffer; /* the buffer we use for the bulk in endpoint */
  122. struct urb *read_urb; /* our bulk read urb */
  123. bool read_in_progress;
  124. spinlock_t es_lock;
  125. __u8 bulk_out_endpoint; /* the bulk out endpoint handle */
  126. __s16 rxBytesAvail; /* the number of bytes that we need to read from this device */
  127. enum RXSTATE rxState; /* the current state of the bulk receive processor */
  128. __u8 rxHeader1; /* receive header byte 1 */
  129. __u8 rxHeader2; /* receive header byte 2 */
  130. __u8 rxHeader3; /* receive header byte 3 */
  131. __u8 rxPort; /* the port that we are currently receiving data for */
  132. __u8 rxStatusCode; /* the receive status code */
  133. __u8 rxStatusParam; /* the receive status paramater */
  134. __s16 rxBytesRemaining; /* the number of port bytes left to read */
  135. struct usb_serial *serial; /* loop back to the owner of this object */
  136. };
  137. /* baud rate information */
  138. struct divisor_table_entry {
  139. __u32 BaudRate;
  140. __u16 Divisor;
  141. };
  142. /*
  143. * Define table of divisors for Rev A EdgePort/4 hardware
  144. * These assume a 3.6864MHz crystal, the standard /16, and
  145. * MCR.7 = 0.
  146. */
  147. static const struct divisor_table_entry divisor_table[] = {
  148. { 50, 4608},
  149. { 75, 3072},
  150. { 110, 2095}, /* 2094.545455 => 230450 => .0217 % over */
  151. { 134, 1713}, /* 1713.011152 => 230398.5 => .00065% under */
  152. { 150, 1536},
  153. { 300, 768},
  154. { 600, 384},
  155. { 1200, 192},
  156. { 1800, 128},
  157. { 2400, 96},
  158. { 4800, 48},
  159. { 7200, 32},
  160. { 9600, 24},
  161. { 14400, 16},
  162. { 19200, 12},
  163. { 38400, 6},
  164. { 57600, 4},
  165. { 115200, 2},
  166. { 230400, 1},
  167. };
  168. /* local variables */
  169. static int debug;
  170. static int low_latency = 1; /* tty low latency flag, on by default */
  171. static atomic_t CmdUrbs; /* Number of outstanding Command Write Urbs */
  172. /* local function prototypes */
  173. /* function prototypes for all URB callbacks */
  174. static void edge_interrupt_callback(struct urb *urb);
  175. static void edge_bulk_in_callback(struct urb *urb);
  176. static void edge_bulk_out_data_callback(struct urb *urb);
  177. static void edge_bulk_out_cmd_callback(struct urb *urb);
  178. /* function prototypes for the usbserial callbacks */
  179. static int edge_open(struct tty_struct *tty, struct usb_serial_port *port,
  180. struct file *filp);
  181. static void edge_close(struct tty_struct *tty, struct usb_serial_port *port,
  182. struct file *filp);
  183. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  184. const unsigned char *buf, int count);
  185. static int edge_write_room(struct tty_struct *tty);
  186. static int edge_chars_in_buffer(struct tty_struct *tty);
  187. static void edge_throttle(struct tty_struct *tty);
  188. static void edge_unthrottle(struct tty_struct *tty);
  189. static void edge_set_termios(struct tty_struct *tty,
  190. struct usb_serial_port *port,
  191. struct ktermios *old_termios);
  192. static int edge_ioctl(struct tty_struct *tty, struct file *file,
  193. unsigned int cmd, unsigned long arg);
  194. static void edge_break(struct tty_struct *tty, int break_state);
  195. static int edge_tiocmget(struct tty_struct *tty, struct file *file);
  196. static int edge_tiocmset(struct tty_struct *tty, struct file *file,
  197. unsigned int set, unsigned int clear);
  198. static int edge_startup(struct usb_serial *serial);
  199. static void edge_shutdown(struct usb_serial *serial);
  200. #include "io_tables.h" /* all of the devices that this driver supports */
  201. /* function prototypes for all of our local functions */
  202. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  203. unsigned char *buffer, __u16 bufferLength);
  204. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  205. __u8 byte2, __u8 byte3);
  206. static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
  207. unsigned char *data, int length);
  208. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr);
  209. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  210. __u8 lsr, __u8 data);
  211. static int send_iosp_ext_cmd(struct edgeport_port *edge_port, __u8 command,
  212. __u8 param);
  213. static int calc_baud_rate_divisor(int baud_rate, int *divisor);
  214. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  215. int baudRate);
  216. static void change_port_settings(struct tty_struct *tty,
  217. struct edgeport_port *edge_port,
  218. struct ktermios *old_termios);
  219. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  220. __u8 regNum, __u8 regValue);
  221. static int write_cmd_usb(struct edgeport_port *edge_port,
  222. unsigned char *buffer, int writeLength);
  223. static void send_more_port_data(struct edgeport_serial *edge_serial,
  224. struct edgeport_port *edge_port);
  225. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  226. __u16 length, const __u8 *data);
  227. static int rom_read(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  228. __u16 length, __u8 *data);
  229. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  230. __u16 length, const __u8 *data);
  231. static void get_manufacturing_desc(struct edgeport_serial *edge_serial);
  232. static void get_boot_desc(struct edgeport_serial *edge_serial);
  233. static void load_application_firmware(struct edgeport_serial *edge_serial);
  234. static void unicode_to_ascii(char *string, int buflen,
  235. __le16 *unicode, int unicode_size);
  236. /* ************************************************************************ */
  237. /* ************************************************************************ */
  238. /* ************************************************************************ */
  239. /* ************************************************************************ */
  240. /************************************************************************
  241. * *
  242. * update_edgeport_E2PROM() Compare current versions of *
  243. * Boot ROM and Manufacture *
  244. * Descriptors with versions *
  245. * embedded in this driver *
  246. * *
  247. ************************************************************************/
  248. static void update_edgeport_E2PROM(struct edgeport_serial *edge_serial)
  249. {
  250. __u32 BootCurVer;
  251. __u32 BootNewVer;
  252. __u8 BootMajorVersion;
  253. __u8 BootMinorVersion;
  254. __u16 BootBuildNumber;
  255. __u32 Bootaddr;
  256. const struct ihex_binrec *rec;
  257. const struct firmware *fw;
  258. const char *fw_name;
  259. int response;
  260. switch (edge_serial->product_info.iDownloadFile) {
  261. case EDGE_DOWNLOAD_FILE_I930:
  262. fw_name = "edgeport/boot.fw";
  263. break;
  264. case EDGE_DOWNLOAD_FILE_80251:
  265. fw_name = "edgeport/boot2.fw";
  266. break;
  267. default:
  268. return;
  269. }
  270. response = request_ihex_firmware(&fw, fw_name,
  271. &edge_serial->serial->dev->dev);
  272. if (response) {
  273. printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
  274. fw_name, response);
  275. return;
  276. }
  277. rec = (const struct ihex_binrec *)fw->data;
  278. BootMajorVersion = rec->data[0];
  279. BootMinorVersion = rec->data[1];
  280. BootBuildNumber = (rec->data[2] << 8) | rec->data[3];
  281. /* Check Boot Image Version */
  282. BootCurVer = (edge_serial->boot_descriptor.MajorVersion << 24) +
  283. (edge_serial->boot_descriptor.MinorVersion << 16) +
  284. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber);
  285. BootNewVer = (BootMajorVersion << 24) +
  286. (BootMinorVersion << 16) +
  287. BootBuildNumber;
  288. dbg("Current Boot Image version %d.%d.%d",
  289. edge_serial->boot_descriptor.MajorVersion,
  290. edge_serial->boot_descriptor.MinorVersion,
  291. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  292. if (BootNewVer > BootCurVer) {
  293. dbg("**Update Boot Image from %d.%d.%d to %d.%d.%d",
  294. edge_serial->boot_descriptor.MajorVersion,
  295. edge_serial->boot_descriptor.MinorVersion,
  296. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber),
  297. BootMajorVersion, BootMinorVersion, BootBuildNumber);
  298. dbg("Downloading new Boot Image");
  299. for (rec = ihex_next_binrec(rec); rec;
  300. rec = ihex_next_binrec(rec)) {
  301. Bootaddr = be32_to_cpu(rec->addr);
  302. response = rom_write(edge_serial->serial,
  303. Bootaddr >> 16,
  304. Bootaddr & 0xFFFF,
  305. be16_to_cpu(rec->len),
  306. &rec->data[0]);
  307. if (response < 0) {
  308. dev_err(&edge_serial->serial->dev->dev,
  309. "rom_write failed (%x, %x, %d)\n",
  310. Bootaddr >> 16, Bootaddr & 0xFFFF,
  311. be16_to_cpu(rec->len));
  312. break;
  313. }
  314. }
  315. } else {
  316. dbg("Boot Image -- already up to date");
  317. }
  318. release_firmware(fw);
  319. }
  320. /************************************************************************
  321. * *
  322. * Get string descriptor from device *
  323. * *
  324. ************************************************************************/
  325. static int get_string(struct usb_device *dev, int Id, char *string, int buflen)
  326. {
  327. struct usb_string_descriptor StringDesc;
  328. struct usb_string_descriptor *pStringDesc;
  329. dbg("%s - USB String ID = %d", __func__, Id);
  330. if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
  331. &StringDesc, sizeof(StringDesc)))
  332. return 0;
  333. pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
  334. if (!pStringDesc)
  335. return 0;
  336. if (!usb_get_descriptor(dev, USB_DT_STRING, Id,
  337. pStringDesc, StringDesc.bLength)) {
  338. kfree(pStringDesc);
  339. return 0;
  340. }
  341. unicode_to_ascii(string, buflen,
  342. pStringDesc->wData, pStringDesc->bLength/2);
  343. kfree(pStringDesc);
  344. dbg("%s - USB String %s", __func__, string);
  345. return strlen(string);
  346. }
  347. #if 0
  348. /************************************************************************
  349. *
  350. * Get string descriptor from device
  351. *
  352. ************************************************************************/
  353. static int get_string_desc(struct usb_device *dev, int Id,
  354. struct usb_string_descriptor **pRetDesc)
  355. {
  356. struct usb_string_descriptor StringDesc;
  357. struct usb_string_descriptor *pStringDesc;
  358. dbg("%s - USB String ID = %d", __func__, Id);
  359. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, &StringDesc,
  360. sizeof(StringDesc)))
  361. return 0;
  362. pStringDesc = kmalloc(StringDesc.bLength, GFP_KERNEL);
  363. if (!pStringDesc)
  364. return -1;
  365. if (!usb_get_descriptor(dev, USB_DT_STRING, Id, pStringDesc,
  366. StringDesc.bLength)) {
  367. kfree(pStringDesc);
  368. return -1;
  369. }
  370. *pRetDesc = pStringDesc;
  371. return 0;
  372. }
  373. #endif
  374. static void dump_product_info(struct edgeport_product_info *product_info)
  375. {
  376. /* Dump Product Info structure */
  377. dbg("**Product Information:");
  378. dbg(" ProductId %x", product_info->ProductId);
  379. dbg(" NumPorts %d", product_info->NumPorts);
  380. dbg(" ProdInfoVer %d", product_info->ProdInfoVer);
  381. dbg(" IsServer %d", product_info->IsServer);
  382. dbg(" IsRS232 %d", product_info->IsRS232);
  383. dbg(" IsRS422 %d", product_info->IsRS422);
  384. dbg(" IsRS485 %d", product_info->IsRS485);
  385. dbg(" RomSize %d", product_info->RomSize);
  386. dbg(" RamSize %d", product_info->RamSize);
  387. dbg(" CpuRev %x", product_info->CpuRev);
  388. dbg(" BoardRev %x", product_info->BoardRev);
  389. dbg(" BootMajorVersion %d.%d.%d", product_info->BootMajorVersion,
  390. product_info->BootMinorVersion,
  391. le16_to_cpu(product_info->BootBuildNumber));
  392. dbg(" FirmwareMajorVersion %d.%d.%d",
  393. product_info->FirmwareMajorVersion,
  394. product_info->FirmwareMinorVersion,
  395. le16_to_cpu(product_info->FirmwareBuildNumber));
  396. dbg(" ManufactureDescDate %d/%d/%d",
  397. product_info->ManufactureDescDate[0],
  398. product_info->ManufactureDescDate[1],
  399. product_info->ManufactureDescDate[2]+1900);
  400. dbg(" iDownloadFile 0x%x", product_info->iDownloadFile);
  401. dbg(" EpicVer %d", product_info->EpicVer);
  402. }
  403. static void get_product_info(struct edgeport_serial *edge_serial)
  404. {
  405. struct edgeport_product_info *product_info = &edge_serial->product_info;
  406. memset(product_info, 0, sizeof(struct edgeport_product_info));
  407. product_info->ProductId = (__u16)(le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct) & ~ION_DEVICE_ID_80251_NETCHIP);
  408. product_info->NumPorts = edge_serial->manuf_descriptor.NumPorts;
  409. product_info->ProdInfoVer = 0;
  410. product_info->RomSize = edge_serial->manuf_descriptor.RomSize;
  411. product_info->RamSize = edge_serial->manuf_descriptor.RamSize;
  412. product_info->CpuRev = edge_serial->manuf_descriptor.CpuRev;
  413. product_info->BoardRev = edge_serial->manuf_descriptor.BoardRev;
  414. product_info->BootMajorVersion =
  415. edge_serial->boot_descriptor.MajorVersion;
  416. product_info->BootMinorVersion =
  417. edge_serial->boot_descriptor.MinorVersion;
  418. product_info->BootBuildNumber =
  419. edge_serial->boot_descriptor.BuildNumber;
  420. memcpy(product_info->ManufactureDescDate,
  421. edge_serial->manuf_descriptor.DescDate,
  422. sizeof(edge_serial->manuf_descriptor.DescDate));
  423. /* check if this is 2nd generation hardware */
  424. if (le16_to_cpu(edge_serial->serial->dev->descriptor.idProduct)
  425. & ION_DEVICE_ID_80251_NETCHIP)
  426. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_80251;
  427. else
  428. product_info->iDownloadFile = EDGE_DOWNLOAD_FILE_I930;
  429. /* Determine Product type and set appropriate flags */
  430. switch (DEVICE_ID_FROM_USB_PRODUCT_ID(product_info->ProductId)) {
  431. case ION_DEVICE_ID_EDGEPORT_COMPATIBLE:
  432. case ION_DEVICE_ID_EDGEPORT_4T:
  433. case ION_DEVICE_ID_EDGEPORT_4:
  434. case ION_DEVICE_ID_EDGEPORT_2:
  435. case ION_DEVICE_ID_EDGEPORT_8_DUAL_CPU:
  436. case ION_DEVICE_ID_EDGEPORT_8:
  437. case ION_DEVICE_ID_EDGEPORT_421:
  438. case ION_DEVICE_ID_EDGEPORT_21:
  439. case ION_DEVICE_ID_EDGEPORT_2_DIN:
  440. case ION_DEVICE_ID_EDGEPORT_4_DIN:
  441. case ION_DEVICE_ID_EDGEPORT_16_DUAL_CPU:
  442. product_info->IsRS232 = 1;
  443. break;
  444. case ION_DEVICE_ID_EDGEPORT_2I: /* Edgeport/2 RS422/RS485 */
  445. product_info->IsRS422 = 1;
  446. product_info->IsRS485 = 1;
  447. break;
  448. case ION_DEVICE_ID_EDGEPORT_8I: /* Edgeport/4 RS422 */
  449. case ION_DEVICE_ID_EDGEPORT_4I: /* Edgeport/4 RS422 */
  450. product_info->IsRS422 = 1;
  451. break;
  452. }
  453. dump_product_info(product_info);
  454. }
  455. static int get_epic_descriptor(struct edgeport_serial *ep)
  456. {
  457. int result;
  458. struct usb_serial *serial = ep->serial;
  459. struct edgeport_product_info *product_info = &ep->product_info;
  460. struct edge_compatibility_descriptor *epic = &ep->epic_descriptor;
  461. struct edge_compatibility_bits *bits;
  462. ep->is_epic = 0;
  463. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  464. USB_REQUEST_ION_GET_EPIC_DESC,
  465. 0xC0, 0x00, 0x00,
  466. &ep->epic_descriptor,
  467. sizeof(struct edge_compatibility_descriptor),
  468. 300);
  469. dbg("%s result = %d", __func__, result);
  470. if (result > 0) {
  471. ep->is_epic = 1;
  472. memset(product_info, 0, sizeof(struct edgeport_product_info));
  473. product_info->NumPorts = epic->NumPorts;
  474. product_info->ProdInfoVer = 0;
  475. product_info->FirmwareMajorVersion = epic->MajorVersion;
  476. product_info->FirmwareMinorVersion = epic->MinorVersion;
  477. product_info->FirmwareBuildNumber = epic->BuildNumber;
  478. product_info->iDownloadFile = epic->iDownloadFile;
  479. product_info->EpicVer = epic->EpicVer;
  480. product_info->Epic = epic->Supports;
  481. product_info->ProductId = ION_DEVICE_ID_EDGEPORT_COMPATIBLE;
  482. dump_product_info(product_info);
  483. bits = &ep->epic_descriptor.Supports;
  484. dbg("**EPIC descriptor:");
  485. dbg(" VendEnableSuspend: %s", bits->VendEnableSuspend ? "TRUE": "FALSE");
  486. dbg(" IOSPOpen : %s", bits->IOSPOpen ? "TRUE": "FALSE");
  487. dbg(" IOSPClose : %s", bits->IOSPClose ? "TRUE": "FALSE");
  488. dbg(" IOSPChase : %s", bits->IOSPChase ? "TRUE": "FALSE");
  489. dbg(" IOSPSetRxFlow : %s", bits->IOSPSetRxFlow ? "TRUE": "FALSE");
  490. dbg(" IOSPSetTxFlow : %s", bits->IOSPSetTxFlow ? "TRUE": "FALSE");
  491. dbg(" IOSPSetXChar : %s", bits->IOSPSetXChar ? "TRUE": "FALSE");
  492. dbg(" IOSPRxCheck : %s", bits->IOSPRxCheck ? "TRUE": "FALSE");
  493. dbg(" IOSPSetClrBreak : %s", bits->IOSPSetClrBreak ? "TRUE": "FALSE");
  494. dbg(" IOSPWriteMCR : %s", bits->IOSPWriteMCR ? "TRUE": "FALSE");
  495. dbg(" IOSPWriteLCR : %s", bits->IOSPWriteLCR ? "TRUE": "FALSE");
  496. dbg(" IOSPSetBaudRate : %s", bits->IOSPSetBaudRate ? "TRUE": "FALSE");
  497. dbg(" TrueEdgeport : %s", bits->TrueEdgeport ? "TRUE": "FALSE");
  498. }
  499. return result;
  500. }
  501. /************************************************************************/
  502. /************************************************************************/
  503. /* U S B C A L L B A C K F U N C T I O N S */
  504. /* U S B C A L L B A C K F U N C T I O N S */
  505. /************************************************************************/
  506. /************************************************************************/
  507. /*****************************************************************************
  508. * edge_interrupt_callback
  509. * this is the callback function for when we have received data on the
  510. * interrupt endpoint.
  511. *****************************************************************************/
  512. static void edge_interrupt_callback(struct urb *urb)
  513. {
  514. struct edgeport_serial *edge_serial = urb->context;
  515. struct edgeport_port *edge_port;
  516. struct usb_serial_port *port;
  517. unsigned char *data = urb->transfer_buffer;
  518. int length = urb->actual_length;
  519. int bytes_avail;
  520. int position;
  521. int txCredits;
  522. int portNumber;
  523. int result;
  524. int status = urb->status;
  525. dbg("%s", __func__);
  526. switch (status) {
  527. case 0:
  528. /* success */
  529. break;
  530. case -ECONNRESET:
  531. case -ENOENT:
  532. case -ESHUTDOWN:
  533. /* this urb is terminated, clean up */
  534. dbg("%s - urb shutting down with status: %d",
  535. __func__, status);
  536. return;
  537. default:
  538. dbg("%s - nonzero urb status received: %d", __func__, status);
  539. goto exit;
  540. }
  541. /* process this interrupt-read even if there are no ports open */
  542. if (length) {
  543. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
  544. __func__, length, data);
  545. if (length > 1) {
  546. bytes_avail = data[0] | (data[1] << 8);
  547. if (bytes_avail) {
  548. spin_lock(&edge_serial->es_lock);
  549. edge_serial->rxBytesAvail += bytes_avail;
  550. dbg("%s - bytes_avail=%d, rxBytesAvail=%d, read_in_progress=%d", __func__, bytes_avail, edge_serial->rxBytesAvail, edge_serial->read_in_progress);
  551. if (edge_serial->rxBytesAvail > 0 &&
  552. !edge_serial->read_in_progress) {
  553. dbg("%s - posting a read", __func__);
  554. edge_serial->read_in_progress = true;
  555. /* we have pending bytes on the
  556. bulk in pipe, send a request */
  557. edge_serial->read_urb->dev = edge_serial->serial->dev;
  558. result = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  559. if (result) {
  560. dev_err(&edge_serial->serial->dev->dev, "%s - usb_submit_urb(read bulk) failed with result = %d\n", __func__, result);
  561. edge_serial->read_in_progress = false;
  562. }
  563. }
  564. spin_unlock(&edge_serial->es_lock);
  565. }
  566. }
  567. /* grab the txcredits for the ports if available */
  568. position = 2;
  569. portNumber = 0;
  570. while ((position < length) &&
  571. (portNumber < edge_serial->serial->num_ports)) {
  572. txCredits = data[position] | (data[position+1] << 8);
  573. if (txCredits) {
  574. port = edge_serial->serial->port[portNumber];
  575. edge_port = usb_get_serial_port_data(port);
  576. if (edge_port->open) {
  577. spin_lock(&edge_port->ep_lock);
  578. edge_port->txCredits += txCredits;
  579. spin_unlock(&edge_port->ep_lock);
  580. dbg("%s - txcredits for port%d = %d",
  581. __func__, portNumber,
  582. edge_port->txCredits);
  583. /* tell the tty driver that something
  584. has changed */
  585. if (edge_port->port->port.tty)
  586. tty_wakeup(edge_port->port->port.tty);
  587. /* Since we have more credit, check
  588. if more data can be sent */
  589. send_more_port_data(edge_serial,
  590. edge_port);
  591. }
  592. }
  593. position += 2;
  594. ++portNumber;
  595. }
  596. }
  597. exit:
  598. result = usb_submit_urb(urb, GFP_ATOMIC);
  599. if (result)
  600. dev_err(&urb->dev->dev,
  601. "%s - Error %d submitting control urb\n",
  602. __func__, result);
  603. }
  604. /*****************************************************************************
  605. * edge_bulk_in_callback
  606. * this is the callback function for when we have received data on the
  607. * bulk in endpoint.
  608. *****************************************************************************/
  609. static void edge_bulk_in_callback(struct urb *urb)
  610. {
  611. struct edgeport_serial *edge_serial = urb->context;
  612. unsigned char *data = urb->transfer_buffer;
  613. int retval;
  614. __u16 raw_data_length;
  615. int status = urb->status;
  616. dbg("%s", __func__);
  617. if (status) {
  618. dbg("%s - nonzero read bulk status received: %d",
  619. __func__, status);
  620. edge_serial->read_in_progress = false;
  621. return;
  622. }
  623. if (urb->actual_length == 0) {
  624. dbg("%s - read bulk callback with no data", __func__);
  625. edge_serial->read_in_progress = false;
  626. return;
  627. }
  628. raw_data_length = urb->actual_length;
  629. usb_serial_debug_data(debug, &edge_serial->serial->dev->dev,
  630. __func__, raw_data_length, data);
  631. spin_lock(&edge_serial->es_lock);
  632. /* decrement our rxBytes available by the number that we just got */
  633. edge_serial->rxBytesAvail -= raw_data_length;
  634. dbg("%s - Received = %d, rxBytesAvail %d", __func__,
  635. raw_data_length, edge_serial->rxBytesAvail);
  636. process_rcvd_data(edge_serial, data, urb->actual_length);
  637. /* check to see if there's any more data for us to read */
  638. if (edge_serial->rxBytesAvail > 0) {
  639. dbg("%s - posting a read", __func__);
  640. edge_serial->read_urb->dev = edge_serial->serial->dev;
  641. retval = usb_submit_urb(edge_serial->read_urb, GFP_ATOMIC);
  642. if (retval) {
  643. dev_err(&urb->dev->dev,
  644. "%s - usb_submit_urb(read bulk) failed, "
  645. "retval = %d\n", __func__, retval);
  646. edge_serial->read_in_progress = false;
  647. }
  648. } else {
  649. edge_serial->read_in_progress = false;
  650. }
  651. spin_unlock(&edge_serial->es_lock);
  652. }
  653. /*****************************************************************************
  654. * edge_bulk_out_data_callback
  655. * this is the callback function for when we have finished sending
  656. * serial data on the bulk out endpoint.
  657. *****************************************************************************/
  658. static void edge_bulk_out_data_callback(struct urb *urb)
  659. {
  660. struct edgeport_port *edge_port = urb->context;
  661. struct tty_struct *tty;
  662. int status = urb->status;
  663. dbg("%s", __func__);
  664. if (status) {
  665. dbg("%s - nonzero write bulk status received: %d",
  666. __func__, status);
  667. }
  668. tty = edge_port->port->port.tty;
  669. if (tty && edge_port->open) {
  670. /* let the tty driver wakeup if it has a special
  671. write_wakeup function */
  672. tty_wakeup(tty);
  673. }
  674. /* Release the Write URB */
  675. edge_port->write_in_progress = false;
  676. /* Check if more data needs to be sent */
  677. send_more_port_data((struct edgeport_serial *)
  678. (usb_get_serial_data(edge_port->port->serial)), edge_port);
  679. }
  680. /*****************************************************************************
  681. * BulkOutCmdCallback
  682. * this is the callback function for when we have finished sending a
  683. * command on the bulk out endpoint.
  684. *****************************************************************************/
  685. static void edge_bulk_out_cmd_callback(struct urb *urb)
  686. {
  687. struct edgeport_port *edge_port = urb->context;
  688. struct tty_struct *tty;
  689. int status = urb->status;
  690. dbg("%s", __func__);
  691. atomic_dec(&CmdUrbs);
  692. dbg("%s - FREE URB %p (outstanding %d)", __func__,
  693. urb, atomic_read(&CmdUrbs));
  694. /* clean up the transfer buffer */
  695. kfree(urb->transfer_buffer);
  696. /* Free the command urb */
  697. usb_free_urb(urb);
  698. if (status) {
  699. dbg("%s - nonzero write bulk status received: %d",
  700. __func__, status);
  701. return;
  702. }
  703. /* Get pointer to tty */
  704. tty = edge_port->port->port.tty;
  705. /* tell the tty driver that something has changed */
  706. if (tty && edge_port->open)
  707. tty_wakeup(tty);
  708. /* we have completed the command */
  709. edge_port->commandPending = false;
  710. wake_up(&edge_port->wait_command);
  711. }
  712. /*****************************************************************************
  713. * Driver tty interface functions
  714. *****************************************************************************/
  715. /*****************************************************************************
  716. * SerialOpen
  717. * this function is called by the tty driver when a port is opened
  718. * If successful, we return 0
  719. * Otherwise we return a negative error number.
  720. *****************************************************************************/
  721. static int edge_open(struct tty_struct *tty,
  722. struct usb_serial_port *port, struct file *filp)
  723. {
  724. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  725. struct usb_serial *serial;
  726. struct edgeport_serial *edge_serial;
  727. int response;
  728. dbg("%s - port %d", __func__, port->number);
  729. if (edge_port == NULL)
  730. return -ENODEV;
  731. if (tty)
  732. tty->low_latency = low_latency;
  733. /* see if we've set up our endpoint info yet (can't set it up
  734. in edge_startup as the structures were not set up at that time.) */
  735. serial = port->serial;
  736. edge_serial = usb_get_serial_data(serial);
  737. if (edge_serial == NULL)
  738. return -ENODEV;
  739. if (edge_serial->interrupt_in_buffer == NULL) {
  740. struct usb_serial_port *port0 = serial->port[0];
  741. /* not set up yet, so do it now */
  742. edge_serial->interrupt_in_buffer =
  743. port0->interrupt_in_buffer;
  744. edge_serial->interrupt_in_endpoint =
  745. port0->interrupt_in_endpointAddress;
  746. edge_serial->interrupt_read_urb = port0->interrupt_in_urb;
  747. edge_serial->bulk_in_buffer = port0->bulk_in_buffer;
  748. edge_serial->bulk_in_endpoint =
  749. port0->bulk_in_endpointAddress;
  750. edge_serial->read_urb = port0->read_urb;
  751. edge_serial->bulk_out_endpoint =
  752. port0->bulk_out_endpointAddress;
  753. /* set up our interrupt urb */
  754. usb_fill_int_urb(edge_serial->interrupt_read_urb,
  755. serial->dev,
  756. usb_rcvintpipe(serial->dev,
  757. port0->interrupt_in_endpointAddress),
  758. port0->interrupt_in_buffer,
  759. edge_serial->interrupt_read_urb->transfer_buffer_length,
  760. edge_interrupt_callback, edge_serial,
  761. edge_serial->interrupt_read_urb->interval);
  762. /* set up our bulk in urb */
  763. usb_fill_bulk_urb(edge_serial->read_urb, serial->dev,
  764. usb_rcvbulkpipe(serial->dev,
  765. port0->bulk_in_endpointAddress),
  766. port0->bulk_in_buffer,
  767. edge_serial->read_urb->transfer_buffer_length,
  768. edge_bulk_in_callback, edge_serial);
  769. edge_serial->read_in_progress = false;
  770. /* start interrupt read for this edgeport
  771. * this interrupt will continue as long
  772. * as the edgeport is connected */
  773. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  774. GFP_KERNEL);
  775. if (response) {
  776. dev_err(&port->dev,
  777. "%s - Error %d submitting control urb\n",
  778. __func__, response);
  779. }
  780. }
  781. /* initialize our wait queues */
  782. init_waitqueue_head(&edge_port->wait_open);
  783. init_waitqueue_head(&edge_port->wait_chase);
  784. init_waitqueue_head(&edge_port->delta_msr_wait);
  785. init_waitqueue_head(&edge_port->wait_command);
  786. /* initialize our icount structure */
  787. memset(&(edge_port->icount), 0x00, sizeof(edge_port->icount));
  788. /* initialize our port settings */
  789. edge_port->txCredits = 0; /* Can't send any data yet */
  790. /* Must always set this bit to enable ints! */
  791. edge_port->shadowMCR = MCR_MASTER_IE;
  792. edge_port->chaseResponsePending = false;
  793. /* send a open port command */
  794. edge_port->openPending = true;
  795. edge_port->open = false;
  796. response = send_iosp_ext_cmd(edge_port, IOSP_CMD_OPEN_PORT, 0);
  797. if (response < 0) {
  798. dev_err(&port->dev, "%s - error sending open port command\n",
  799. __func__);
  800. edge_port->openPending = false;
  801. return -ENODEV;
  802. }
  803. /* now wait for the port to be completely opened */
  804. wait_event_timeout(edge_port->wait_open, !edge_port->openPending,
  805. OPEN_TIMEOUT);
  806. if (!edge_port->open) {
  807. /* open timed out */
  808. dbg("%s - open timedout", __func__);
  809. edge_port->openPending = false;
  810. return -ENODEV;
  811. }
  812. /* create the txfifo */
  813. edge_port->txfifo.head = 0;
  814. edge_port->txfifo.tail = 0;
  815. edge_port->txfifo.count = 0;
  816. edge_port->txfifo.size = edge_port->maxTxCredits;
  817. edge_port->txfifo.fifo = kmalloc(edge_port->maxTxCredits, GFP_KERNEL);
  818. if (!edge_port->txfifo.fifo) {
  819. dbg("%s - no memory", __func__);
  820. edge_close(tty, port, filp);
  821. return -ENOMEM;
  822. }
  823. /* Allocate a URB for the write */
  824. edge_port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  825. edge_port->write_in_progress = false;
  826. if (!edge_port->write_urb) {
  827. dbg("%s - no memory", __func__);
  828. edge_close(tty, port, filp);
  829. return -ENOMEM;
  830. }
  831. dbg("%s(%d) - Initialize TX fifo to %d bytes",
  832. __func__, port->number, edge_port->maxTxCredits);
  833. dbg("%s exited", __func__);
  834. return 0;
  835. }
  836. /************************************************************************
  837. *
  838. * block_until_chase_response
  839. *
  840. * This function will block the close until one of the following:
  841. * 1. Response to our Chase comes from Edgeport
  842. * 2. A timeout of 10 seconds without activity has expired
  843. * (1K of Edgeport data @ 2400 baud ==> 4 sec to empty)
  844. *
  845. ************************************************************************/
  846. static void block_until_chase_response(struct edgeport_port *edge_port)
  847. {
  848. DEFINE_WAIT(wait);
  849. __u16 lastCredits;
  850. int timeout = 1*HZ;
  851. int loop = 10;
  852. while (1) {
  853. /* Save Last credits */
  854. lastCredits = edge_port->txCredits;
  855. /* Did we get our Chase response */
  856. if (!edge_port->chaseResponsePending) {
  857. dbg("%s - Got Chase Response", __func__);
  858. /* did we get all of our credit back? */
  859. if (edge_port->txCredits == edge_port->maxTxCredits) {
  860. dbg("%s - Got all credits", __func__);
  861. return;
  862. }
  863. }
  864. /* Block the thread for a while */
  865. prepare_to_wait(&edge_port->wait_chase, &wait,
  866. TASK_UNINTERRUPTIBLE);
  867. schedule_timeout(timeout);
  868. finish_wait(&edge_port->wait_chase, &wait);
  869. if (lastCredits == edge_port->txCredits) {
  870. /* No activity.. count down. */
  871. loop--;
  872. if (loop == 0) {
  873. edge_port->chaseResponsePending = false;
  874. dbg("%s - Chase TIMEOUT", __func__);
  875. return;
  876. }
  877. } else {
  878. /* Reset timeout value back to 10 seconds */
  879. dbg("%s - Last %d, Current %d", __func__,
  880. lastCredits, edge_port->txCredits);
  881. loop = 10;
  882. }
  883. }
  884. }
  885. /************************************************************************
  886. *
  887. * block_until_tx_empty
  888. *
  889. * This function will block the close until one of the following:
  890. * 1. TX count are 0
  891. * 2. The edgeport has stopped
  892. * 3. A timeout of 3 seconds without activity has expired
  893. *
  894. ************************************************************************/
  895. static void block_until_tx_empty(struct edgeport_port *edge_port)
  896. {
  897. DEFINE_WAIT(wait);
  898. struct TxFifo *fifo = &edge_port->txfifo;
  899. __u32 lastCount;
  900. int timeout = HZ/10;
  901. int loop = 30;
  902. while (1) {
  903. /* Save Last count */
  904. lastCount = fifo->count;
  905. /* Is the Edgeport Buffer empty? */
  906. if (lastCount == 0) {
  907. dbg("%s - TX Buffer Empty", __func__);
  908. return;
  909. }
  910. /* Block the thread for a while */
  911. prepare_to_wait(&edge_port->wait_chase, &wait,
  912. TASK_UNINTERRUPTIBLE);
  913. schedule_timeout(timeout);
  914. finish_wait(&edge_port->wait_chase, &wait);
  915. dbg("%s wait", __func__);
  916. if (lastCount == fifo->count) {
  917. /* No activity.. count down. */
  918. loop--;
  919. if (loop == 0) {
  920. dbg("%s - TIMEOUT", __func__);
  921. return;
  922. }
  923. } else {
  924. /* Reset timeout value back to seconds */
  925. loop = 30;
  926. }
  927. }
  928. }
  929. /*****************************************************************************
  930. * edge_close
  931. * this function is called by the tty driver when a port is closed
  932. *****************************************************************************/
  933. static void edge_close(struct tty_struct *tty,
  934. struct usb_serial_port *port, struct file *filp)
  935. {
  936. struct edgeport_serial *edge_serial;
  937. struct edgeport_port *edge_port;
  938. int status;
  939. dbg("%s - port %d", __func__, port->number);
  940. edge_serial = usb_get_serial_data(port->serial);
  941. edge_port = usb_get_serial_port_data(port);
  942. if (edge_serial == NULL || edge_port == NULL)
  943. return;
  944. /* block until tx is empty */
  945. block_until_tx_empty(edge_port);
  946. edge_port->closePending = true;
  947. if ((!edge_serial->is_epic) ||
  948. ((edge_serial->is_epic) &&
  949. (edge_serial->epic_descriptor.Supports.IOSPChase))) {
  950. /* flush and chase */
  951. edge_port->chaseResponsePending = true;
  952. dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
  953. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  954. if (status == 0)
  955. /* block until chase finished */
  956. block_until_chase_response(edge_port);
  957. else
  958. edge_port->chaseResponsePending = false;
  959. }
  960. if ((!edge_serial->is_epic) ||
  961. ((edge_serial->is_epic) &&
  962. (edge_serial->epic_descriptor.Supports.IOSPClose))) {
  963. /* close the port */
  964. dbg("%s - Sending IOSP_CMD_CLOSE_PORT", __func__);
  965. send_iosp_ext_cmd(edge_port, IOSP_CMD_CLOSE_PORT, 0);
  966. }
  967. /* port->close = true; */
  968. edge_port->closePending = false;
  969. edge_port->open = false;
  970. edge_port->openPending = false;
  971. usb_kill_urb(edge_port->write_urb);
  972. if (edge_port->write_urb) {
  973. /* if this urb had a transfer buffer already
  974. (old transfer) free it */
  975. kfree(edge_port->write_urb->transfer_buffer);
  976. usb_free_urb(edge_port->write_urb);
  977. edge_port->write_urb = NULL;
  978. }
  979. kfree(edge_port->txfifo.fifo);
  980. edge_port->txfifo.fifo = NULL;
  981. dbg("%s exited", __func__);
  982. }
  983. /*****************************************************************************
  984. * SerialWrite
  985. * this function is called by the tty driver when data should be written
  986. * to the port.
  987. * If successful, we return the number of bytes written, otherwise we
  988. * return a negative error number.
  989. *****************************************************************************/
  990. static int edge_write(struct tty_struct *tty, struct usb_serial_port *port,
  991. const unsigned char *data, int count)
  992. {
  993. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  994. struct TxFifo *fifo;
  995. int copySize;
  996. int bytesleft;
  997. int firsthalf;
  998. int secondhalf;
  999. unsigned long flags;
  1000. dbg("%s - port %d", __func__, port->number);
  1001. if (edge_port == NULL)
  1002. return -ENODEV;
  1003. /* get a pointer to the Tx fifo */
  1004. fifo = &edge_port->txfifo;
  1005. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1006. /* calculate number of bytes to put in fifo */
  1007. copySize = min((unsigned int)count,
  1008. (edge_port->txCredits - fifo->count));
  1009. dbg("%s(%d) of %d byte(s) Fifo room %d -- will copy %d bytes",
  1010. __func__, port->number, count,
  1011. edge_port->txCredits - fifo->count, copySize);
  1012. /* catch writes of 0 bytes which the tty driver likes to give us,
  1013. and when txCredits is empty */
  1014. if (copySize == 0) {
  1015. dbg("%s - copySize = Zero", __func__);
  1016. goto finish_write;
  1017. }
  1018. /* queue the data
  1019. * since we can never overflow the buffer we do not have to check for a
  1020. * full condition
  1021. *
  1022. * the copy is done is two parts -- first fill to the end of the buffer
  1023. * then copy the reset from the start of the buffer
  1024. */
  1025. bytesleft = fifo->size - fifo->head;
  1026. firsthalf = min(bytesleft, copySize);
  1027. dbg("%s - copy %d bytes of %d into fifo ", __func__,
  1028. firsthalf, bytesleft);
  1029. /* now copy our data */
  1030. memcpy(&fifo->fifo[fifo->head], data, firsthalf);
  1031. usb_serial_debug_data(debug, &port->dev, __func__,
  1032. firsthalf, &fifo->fifo[fifo->head]);
  1033. /* update the index and size */
  1034. fifo->head += firsthalf;
  1035. fifo->count += firsthalf;
  1036. /* wrap the index */
  1037. if (fifo->head == fifo->size)
  1038. fifo->head = 0;
  1039. secondhalf = copySize-firsthalf;
  1040. if (secondhalf) {
  1041. dbg("%s - copy rest of data %d", __func__, secondhalf);
  1042. memcpy(&fifo->fifo[fifo->head], &data[firsthalf], secondhalf);
  1043. usb_serial_debug_data(debug, &port->dev, __func__,
  1044. secondhalf, &fifo->fifo[fifo->head]);
  1045. /* update the index and size */
  1046. fifo->count += secondhalf;
  1047. fifo->head += secondhalf;
  1048. /* No need to check for wrap since we can not get to end of
  1049. * the fifo in this part
  1050. */
  1051. }
  1052. finish_write:
  1053. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1054. send_more_port_data((struct edgeport_serial *)
  1055. usb_get_serial_data(port->serial), edge_port);
  1056. dbg("%s wrote %d byte(s) TxCredits %d, Fifo %d", __func__,
  1057. copySize, edge_port->txCredits, fifo->count);
  1058. return copySize;
  1059. }
  1060. /************************************************************************
  1061. *
  1062. * send_more_port_data()
  1063. *
  1064. * This routine attempts to write additional UART transmit data
  1065. * to a port over the USB bulk pipe. It is called (1) when new
  1066. * data has been written to a port's TxBuffer from higher layers
  1067. * (2) when the peripheral sends us additional TxCredits indicating
  1068. * that it can accept more Tx data for a given port; and (3) when
  1069. * a bulk write completes successfully and we want to see if we
  1070. * can transmit more.
  1071. *
  1072. ************************************************************************/
  1073. static void send_more_port_data(struct edgeport_serial *edge_serial,
  1074. struct edgeport_port *edge_port)
  1075. {
  1076. struct TxFifo *fifo = &edge_port->txfifo;
  1077. struct urb *urb;
  1078. unsigned char *buffer;
  1079. int status;
  1080. int count;
  1081. int bytesleft;
  1082. int firsthalf;
  1083. int secondhalf;
  1084. unsigned long flags;
  1085. dbg("%s(%d)", __func__, edge_port->port->number);
  1086. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1087. if (edge_port->write_in_progress ||
  1088. !edge_port->open ||
  1089. (fifo->count == 0)) {
  1090. dbg("%s(%d) EXIT - fifo %d, PendingWrite = %d",
  1091. __func__, edge_port->port->number,
  1092. fifo->count, edge_port->write_in_progress);
  1093. goto exit_send;
  1094. }
  1095. /* since the amount of data in the fifo will always fit into the
  1096. * edgeport buffer we do not need to check the write length
  1097. *
  1098. * Do we have enough credits for this port to make it worthwhile
  1099. * to bother queueing a write. If it's too small, say a few bytes,
  1100. * it's better to wait for more credits so we can do a larger write.
  1101. */
  1102. if (edge_port->txCredits < EDGE_FW_GET_TX_CREDITS_SEND_THRESHOLD(edge_port->maxTxCredits, EDGE_FW_BULK_MAX_PACKET_SIZE)) {
  1103. dbg("%s(%d) Not enough credit - fifo %d TxCredit %d",
  1104. __func__, edge_port->port->number, fifo->count,
  1105. edge_port->txCredits);
  1106. goto exit_send;
  1107. }
  1108. /* lock this write */
  1109. edge_port->write_in_progress = true;
  1110. /* get a pointer to the write_urb */
  1111. urb = edge_port->write_urb;
  1112. /* make sure transfer buffer is freed */
  1113. kfree(urb->transfer_buffer);
  1114. urb->transfer_buffer = NULL;
  1115. /* build the data header for the buffer and port that we are about
  1116. to send out */
  1117. count = fifo->count;
  1118. buffer = kmalloc(count+2, GFP_ATOMIC);
  1119. if (buffer == NULL) {
  1120. dev_err(&edge_port->port->dev,
  1121. "%s - no more kernel memory...\n", __func__);
  1122. edge_port->write_in_progress = false;
  1123. goto exit_send;
  1124. }
  1125. buffer[0] = IOSP_BUILD_DATA_HDR1(edge_port->port->number
  1126. - edge_port->port->serial->minor, count);
  1127. buffer[1] = IOSP_BUILD_DATA_HDR2(edge_port->port->number
  1128. - edge_port->port->serial->minor, count);
  1129. /* now copy our data */
  1130. bytesleft = fifo->size - fifo->tail;
  1131. firsthalf = min(bytesleft, count);
  1132. memcpy(&buffer[2], &fifo->fifo[fifo->tail], firsthalf);
  1133. fifo->tail += firsthalf;
  1134. fifo->count -= firsthalf;
  1135. if (fifo->tail == fifo->size)
  1136. fifo->tail = 0;
  1137. secondhalf = count-firsthalf;
  1138. if (secondhalf) {
  1139. memcpy(&buffer[2+firsthalf], &fifo->fifo[fifo->tail],
  1140. secondhalf);
  1141. fifo->tail += secondhalf;
  1142. fifo->count -= secondhalf;
  1143. }
  1144. if (count)
  1145. usb_serial_debug_data(debug, &edge_port->port->dev,
  1146. __func__, count, &buffer[2]);
  1147. /* fill up the urb with all of our data and submit it */
  1148. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  1149. usb_sndbulkpipe(edge_serial->serial->dev,
  1150. edge_serial->bulk_out_endpoint),
  1151. buffer, count+2,
  1152. edge_bulk_out_data_callback, edge_port);
  1153. /* decrement the number of credits we have by the number we just sent */
  1154. edge_port->txCredits -= count;
  1155. edge_port->icount.tx += count;
  1156. urb->dev = edge_serial->serial->dev;
  1157. status = usb_submit_urb(urb, GFP_ATOMIC);
  1158. if (status) {
  1159. /* something went wrong */
  1160. dev_err(&edge_port->port->dev,
  1161. "%s - usb_submit_urb(write bulk) failed, status = %d, data lost\n",
  1162. __func__, status);
  1163. edge_port->write_in_progress = false;
  1164. /* revert the credits as something bad happened. */
  1165. edge_port->txCredits += count;
  1166. edge_port->icount.tx -= count;
  1167. }
  1168. dbg("%s wrote %d byte(s) TxCredit %d, Fifo %d",
  1169. __func__, count, edge_port->txCredits, fifo->count);
  1170. exit_send:
  1171. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1172. }
  1173. /*****************************************************************************
  1174. * edge_write_room
  1175. * this function is called by the tty driver when it wants to know how
  1176. * many bytes of data we can accept for a specific port. If successful,
  1177. * we return the amount of room that we have for this port (the txCredits)
  1178. * otherwise we return a negative error number.
  1179. *****************************************************************************/
  1180. static int edge_write_room(struct tty_struct *tty)
  1181. {
  1182. struct usb_serial_port *port = tty->driver_data;
  1183. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1184. int room;
  1185. unsigned long flags;
  1186. dbg("%s", __func__);
  1187. if (edge_port == NULL)
  1188. return 0;
  1189. if (edge_port->closePending)
  1190. return 0;
  1191. dbg("%s - port %d", __func__, port->number);
  1192. if (!edge_port->open) {
  1193. dbg("%s - port not opened", __func__);
  1194. return 0;
  1195. }
  1196. /* total of both buffers is still txCredit */
  1197. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1198. room = edge_port->txCredits - edge_port->txfifo.count;
  1199. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1200. dbg("%s - returns %d", __func__, room);
  1201. return room;
  1202. }
  1203. /*****************************************************************************
  1204. * edge_chars_in_buffer
  1205. * this function is called by the tty driver when it wants to know how
  1206. * many bytes of data we currently have outstanding in the port (data that
  1207. * has been written, but hasn't made it out the port yet)
  1208. * If successful, we return the number of bytes left to be written in the
  1209. * system,
  1210. * Otherwise we return a negative error number.
  1211. *****************************************************************************/
  1212. static int edge_chars_in_buffer(struct tty_struct *tty)
  1213. {
  1214. struct usb_serial_port *port = tty->driver_data;
  1215. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1216. int num_chars;
  1217. unsigned long flags;
  1218. dbg("%s", __func__);
  1219. if (edge_port == NULL)
  1220. return 0;
  1221. if (edge_port->closePending)
  1222. return 0;
  1223. if (!edge_port->open) {
  1224. dbg("%s - port not opened", __func__);
  1225. return 0;
  1226. }
  1227. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1228. num_chars = edge_port->maxTxCredits - edge_port->txCredits +
  1229. edge_port->txfifo.count;
  1230. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1231. if (num_chars) {
  1232. dbg("%s(port %d) - returns %d", __func__,
  1233. port->number, num_chars);
  1234. }
  1235. return num_chars;
  1236. }
  1237. /*****************************************************************************
  1238. * SerialThrottle
  1239. * this function is called by the tty driver when it wants to stop the data
  1240. * being read from the port.
  1241. *****************************************************************************/
  1242. static void edge_throttle(struct tty_struct *tty)
  1243. {
  1244. struct usb_serial_port *port = tty->driver_data;
  1245. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1246. int status;
  1247. dbg("%s - port %d", __func__, port->number);
  1248. if (edge_port == NULL)
  1249. return;
  1250. if (!edge_port->open) {
  1251. dbg("%s - port not opened", __func__);
  1252. return;
  1253. }
  1254. /* if we are implementing XON/XOFF, send the stop character */
  1255. if (I_IXOFF(tty)) {
  1256. unsigned char stop_char = STOP_CHAR(tty);
  1257. status = edge_write(tty, port, &stop_char, 1);
  1258. if (status <= 0)
  1259. return;
  1260. }
  1261. /* if we are implementing RTS/CTS, toggle that line */
  1262. if (tty->termios->c_cflag & CRTSCTS) {
  1263. edge_port->shadowMCR &= ~MCR_RTS;
  1264. status = send_cmd_write_uart_register(edge_port, MCR,
  1265. edge_port->shadowMCR);
  1266. if (status != 0)
  1267. return;
  1268. }
  1269. return;
  1270. }
  1271. /*****************************************************************************
  1272. * edge_unthrottle
  1273. * this function is called by the tty driver when it wants to resume the
  1274. * data being read from the port (called after SerialThrottle is called)
  1275. *****************************************************************************/
  1276. static void edge_unthrottle(struct tty_struct *tty)
  1277. {
  1278. struct usb_serial_port *port = tty->driver_data;
  1279. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1280. int status;
  1281. dbg("%s - port %d", __func__, port->number);
  1282. if (edge_port == NULL)
  1283. return;
  1284. if (!edge_port->open) {
  1285. dbg("%s - port not opened", __func__);
  1286. return;
  1287. }
  1288. /* if we are implementing XON/XOFF, send the start character */
  1289. if (I_IXOFF(tty)) {
  1290. unsigned char start_char = START_CHAR(tty);
  1291. status = edge_write(tty, port, &start_char, 1);
  1292. if (status <= 0)
  1293. return;
  1294. }
  1295. /* if we are implementing RTS/CTS, toggle that line */
  1296. if (tty->termios->c_cflag & CRTSCTS) {
  1297. edge_port->shadowMCR |= MCR_RTS;
  1298. send_cmd_write_uart_register(edge_port, MCR,
  1299. edge_port->shadowMCR);
  1300. }
  1301. }
  1302. /*****************************************************************************
  1303. * SerialSetTermios
  1304. * this function is called by the tty driver when it wants to change
  1305. * the termios structure
  1306. *****************************************************************************/
  1307. static void edge_set_termios(struct tty_struct *tty,
  1308. struct usb_serial_port *port, struct ktermios *old_termios)
  1309. {
  1310. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1311. unsigned int cflag;
  1312. cflag = tty->termios->c_cflag;
  1313. dbg("%s - clfag %08x iflag %08x", __func__,
  1314. tty->termios->c_cflag, tty->termios->c_iflag);
  1315. dbg("%s - old clfag %08x old iflag %08x", __func__,
  1316. old_termios->c_cflag, old_termios->c_iflag);
  1317. dbg("%s - port %d", __func__, port->number);
  1318. if (edge_port == NULL)
  1319. return;
  1320. if (!edge_port->open) {
  1321. dbg("%s - port not opened", __func__);
  1322. return;
  1323. }
  1324. /* change the port settings to the new ones specified */
  1325. change_port_settings(tty, edge_port, old_termios);
  1326. }
  1327. /*****************************************************************************
  1328. * get_lsr_info - get line status register info
  1329. *
  1330. * Purpose: Let user call ioctl() to get info when the UART physically
  1331. * is emptied. On bus types like RS485, the transmitter must
  1332. * release the bus after transmitting. This must be done when
  1333. * the transmit shift register is empty, not be done when the
  1334. * transmit holding register is empty. This functionality
  1335. * allows an RS485 driver to be written in user space.
  1336. *****************************************************************************/
  1337. static int get_lsr_info(struct edgeport_port *edge_port,
  1338. unsigned int __user *value)
  1339. {
  1340. unsigned int result = 0;
  1341. unsigned long flags;
  1342. spin_lock_irqsave(&edge_port->ep_lock, flags);
  1343. if (edge_port->maxTxCredits == edge_port->txCredits &&
  1344. edge_port->txfifo.count == 0) {
  1345. dbg("%s -- Empty", __func__);
  1346. result = TIOCSER_TEMT;
  1347. }
  1348. spin_unlock_irqrestore(&edge_port->ep_lock, flags);
  1349. if (copy_to_user(value, &result, sizeof(int)))
  1350. return -EFAULT;
  1351. return 0;
  1352. }
  1353. static int edge_tiocmset(struct tty_struct *tty, struct file *file,
  1354. unsigned int set, unsigned int clear)
  1355. {
  1356. struct usb_serial_port *port = tty->driver_data;
  1357. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1358. unsigned int mcr;
  1359. dbg("%s - port %d", __func__, port->number);
  1360. mcr = edge_port->shadowMCR;
  1361. if (set & TIOCM_RTS)
  1362. mcr |= MCR_RTS;
  1363. if (set & TIOCM_DTR)
  1364. mcr |= MCR_DTR;
  1365. if (set & TIOCM_LOOP)
  1366. mcr |= MCR_LOOPBACK;
  1367. if (clear & TIOCM_RTS)
  1368. mcr &= ~MCR_RTS;
  1369. if (clear & TIOCM_DTR)
  1370. mcr &= ~MCR_DTR;
  1371. if (clear & TIOCM_LOOP)
  1372. mcr &= ~MCR_LOOPBACK;
  1373. edge_port->shadowMCR = mcr;
  1374. send_cmd_write_uart_register(edge_port, MCR, edge_port->shadowMCR);
  1375. return 0;
  1376. }
  1377. static int edge_tiocmget(struct tty_struct *tty, struct file *file)
  1378. {
  1379. struct usb_serial_port *port = tty->driver_data;
  1380. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1381. unsigned int result = 0;
  1382. unsigned int msr;
  1383. unsigned int mcr;
  1384. dbg("%s - port %d", __func__, port->number);
  1385. msr = edge_port->shadowMSR;
  1386. mcr = edge_port->shadowMCR;
  1387. result = ((mcr & MCR_DTR) ? TIOCM_DTR: 0) /* 0x002 */
  1388. | ((mcr & MCR_RTS) ? TIOCM_RTS: 0) /* 0x004 */
  1389. | ((msr & EDGEPORT_MSR_CTS) ? TIOCM_CTS: 0) /* 0x020 */
  1390. | ((msr & EDGEPORT_MSR_CD) ? TIOCM_CAR: 0) /* 0x040 */
  1391. | ((msr & EDGEPORT_MSR_RI) ? TIOCM_RI: 0) /* 0x080 */
  1392. | ((msr & EDGEPORT_MSR_DSR) ? TIOCM_DSR: 0); /* 0x100 */
  1393. dbg("%s -- %x", __func__, result);
  1394. return result;
  1395. }
  1396. static int get_serial_info(struct edgeport_port *edge_port,
  1397. struct serial_struct __user *retinfo)
  1398. {
  1399. struct serial_struct tmp;
  1400. if (!retinfo)
  1401. return -EFAULT;
  1402. memset(&tmp, 0, sizeof(tmp));
  1403. tmp.type = PORT_16550A;
  1404. tmp.line = edge_port->port->serial->minor;
  1405. tmp.port = edge_port->port->number;
  1406. tmp.irq = 0;
  1407. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1408. tmp.xmit_fifo_size = edge_port->maxTxCredits;
  1409. tmp.baud_base = 9600;
  1410. tmp.close_delay = 5*HZ;
  1411. tmp.closing_wait = 30*HZ;
  1412. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1413. return -EFAULT;
  1414. return 0;
  1415. }
  1416. /*****************************************************************************
  1417. * SerialIoctl
  1418. * this function handles any ioctl calls to the driver
  1419. *****************************************************************************/
  1420. static int edge_ioctl(struct tty_struct *tty, struct file *file,
  1421. unsigned int cmd, unsigned long arg)
  1422. {
  1423. struct usb_serial_port *port = tty->driver_data;
  1424. DEFINE_WAIT(wait);
  1425. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1426. struct async_icount cnow;
  1427. struct async_icount cprev;
  1428. struct serial_icounter_struct icount;
  1429. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1430. switch (cmd) {
  1431. case TIOCSERGETLSR:
  1432. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1433. return get_lsr_info(edge_port, (unsigned int __user *) arg);
  1434. case TIOCGSERIAL:
  1435. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1436. return get_serial_info(edge_port, (struct serial_struct __user *) arg);
  1437. case TIOCMIWAIT:
  1438. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1439. cprev = edge_port->icount;
  1440. while (1) {
  1441. prepare_to_wait(&edge_port->delta_msr_wait,
  1442. &wait, TASK_INTERRUPTIBLE);
  1443. schedule();
  1444. finish_wait(&edge_port->delta_msr_wait, &wait);
  1445. /* see if a signal did it */
  1446. if (signal_pending(current))
  1447. return -ERESTARTSYS;
  1448. cnow = edge_port->icount;
  1449. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1450. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1451. return -EIO; /* no change => error */
  1452. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1453. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1454. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1455. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1456. return 0;
  1457. }
  1458. cprev = cnow;
  1459. }
  1460. /* NOTREACHED */
  1461. break;
  1462. case TIOCGICOUNT:
  1463. cnow = edge_port->icount;
  1464. memset(&icount, 0, sizeof(icount));
  1465. icount.cts = cnow.cts;
  1466. icount.dsr = cnow.dsr;
  1467. icount.rng = cnow.rng;
  1468. icount.dcd = cnow.dcd;
  1469. icount.rx = cnow.rx;
  1470. icount.tx = cnow.tx;
  1471. icount.frame = cnow.frame;
  1472. icount.overrun = cnow.overrun;
  1473. icount.parity = cnow.parity;
  1474. icount.brk = cnow.brk;
  1475. icount.buf_overrun = cnow.buf_overrun;
  1476. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
  1477. __func__, port->number, icount.rx, icount.tx);
  1478. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1479. return -EFAULT;
  1480. return 0;
  1481. }
  1482. return -ENOIOCTLCMD;
  1483. }
  1484. /*****************************************************************************
  1485. * SerialBreak
  1486. * this function sends a break to the port
  1487. *****************************************************************************/
  1488. static void edge_break(struct tty_struct *tty, int break_state)
  1489. {
  1490. struct usb_serial_port *port = tty->driver_data;
  1491. struct edgeport_port *edge_port = usb_get_serial_port_data(port);
  1492. struct edgeport_serial *edge_serial = usb_get_serial_data(port->serial);
  1493. int status;
  1494. if ((!edge_serial->is_epic) ||
  1495. ((edge_serial->is_epic) &&
  1496. (edge_serial->epic_descriptor.Supports.IOSPChase))) {
  1497. /* flush and chase */
  1498. edge_port->chaseResponsePending = true;
  1499. dbg("%s - Sending IOSP_CMD_CHASE_PORT", __func__);
  1500. status = send_iosp_ext_cmd(edge_port, IOSP_CMD_CHASE_PORT, 0);
  1501. if (status == 0) {
  1502. /* block until chase finished */
  1503. block_until_chase_response(edge_port);
  1504. } else {
  1505. edge_port->chaseResponsePending = false;
  1506. }
  1507. }
  1508. if ((!edge_serial->is_epic) ||
  1509. ((edge_serial->is_epic) &&
  1510. (edge_serial->epic_descriptor.Supports.IOSPSetClrBreak))) {
  1511. if (break_state == -1) {
  1512. dbg("%s - Sending IOSP_CMD_SET_BREAK", __func__);
  1513. status = send_iosp_ext_cmd(edge_port,
  1514. IOSP_CMD_SET_BREAK, 0);
  1515. } else {
  1516. dbg("%s - Sending IOSP_CMD_CLEAR_BREAK", __func__);
  1517. status = send_iosp_ext_cmd(edge_port,
  1518. IOSP_CMD_CLEAR_BREAK, 0);
  1519. }
  1520. if (status)
  1521. dbg("%s - error sending break set/clear command.",
  1522. __func__);
  1523. }
  1524. return;
  1525. }
  1526. /*****************************************************************************
  1527. * process_rcvd_data
  1528. * this function handles the data received on the bulk in pipe.
  1529. *****************************************************************************/
  1530. static void process_rcvd_data(struct edgeport_serial *edge_serial,
  1531. unsigned char *buffer, __u16 bufferLength)
  1532. {
  1533. struct usb_serial_port *port;
  1534. struct edgeport_port *edge_port;
  1535. struct tty_struct *tty;
  1536. __u16 lastBufferLength;
  1537. __u16 rxLen;
  1538. dbg("%s", __func__);
  1539. lastBufferLength = bufferLength + 1;
  1540. while (bufferLength > 0) {
  1541. /* failsafe incase we get a message that we don't understand */
  1542. if (lastBufferLength == bufferLength) {
  1543. dbg("%s - stuck in loop, exiting it.", __func__);
  1544. break;
  1545. }
  1546. lastBufferLength = bufferLength;
  1547. switch (edge_serial->rxState) {
  1548. case EXPECT_HDR1:
  1549. edge_serial->rxHeader1 = *buffer;
  1550. ++buffer;
  1551. --bufferLength;
  1552. if (bufferLength == 0) {
  1553. edge_serial->rxState = EXPECT_HDR2;
  1554. break;
  1555. }
  1556. /* otherwise, drop on through */
  1557. case EXPECT_HDR2:
  1558. edge_serial->rxHeader2 = *buffer;
  1559. ++buffer;
  1560. --bufferLength;
  1561. dbg("%s - Hdr1=%02X Hdr2=%02X", __func__,
  1562. edge_serial->rxHeader1, edge_serial->rxHeader2);
  1563. /* Process depending on whether this header is
  1564. * data or status */
  1565. if (IS_CMD_STAT_HDR(edge_serial->rxHeader1)) {
  1566. /* Decode this status header and go to
  1567. * EXPECT_HDR1 (if we can process the status
  1568. * with only 2 bytes), or go to EXPECT_HDR3 to
  1569. * get the third byte. */
  1570. edge_serial->rxPort =
  1571. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1572. edge_serial->rxStatusCode =
  1573. IOSP_GET_STATUS_CODE(
  1574. edge_serial->rxHeader1);
  1575. if (!IOSP_STATUS_IS_2BYTE(
  1576. edge_serial->rxStatusCode)) {
  1577. /* This status needs additional bytes.
  1578. * Save what we have and then wait for
  1579. * more data.
  1580. */
  1581. edge_serial->rxStatusParam
  1582. = edge_serial->rxHeader2;
  1583. edge_serial->rxState = EXPECT_HDR3;
  1584. break;
  1585. }
  1586. /* We have all the header bytes, process the
  1587. status now */
  1588. process_rcvd_status(edge_serial,
  1589. edge_serial->rxHeader2, 0);
  1590. edge_serial->rxState = EXPECT_HDR1;
  1591. break;
  1592. } else {
  1593. edge_serial->rxPort =
  1594. IOSP_GET_HDR_PORT(edge_serial->rxHeader1);
  1595. edge_serial->rxBytesRemaining =
  1596. IOSP_GET_HDR_DATA_LEN(
  1597. edge_serial->rxHeader1,
  1598. edge_serial->rxHeader2);
  1599. dbg("%s - Data for Port %u Len %u",
  1600. __func__,
  1601. edge_serial->rxPort,
  1602. edge_serial->rxBytesRemaining);
  1603. /* ASSERT(DevExt->RxPort < DevExt->NumPorts);
  1604. * ASSERT(DevExt->RxBytesRemaining <
  1605. * IOSP_MAX_DATA_LENGTH);
  1606. */
  1607. if (bufferLength == 0) {
  1608. edge_serial->rxState = EXPECT_DATA;
  1609. break;
  1610. }
  1611. /* Else, drop through */
  1612. }
  1613. case EXPECT_DATA: /* Expect data */
  1614. if (bufferLength < edge_serial->rxBytesRemaining) {
  1615. rxLen = bufferLength;
  1616. /* Expect data to start next buffer */
  1617. edge_serial->rxState = EXPECT_DATA;
  1618. } else {
  1619. /* BufLen >= RxBytesRemaining */
  1620. rxLen = edge_serial->rxBytesRemaining;
  1621. /* Start another header next time */
  1622. edge_serial->rxState = EXPECT_HDR1;
  1623. }
  1624. bufferLength -= rxLen;
  1625. edge_serial->rxBytesRemaining -= rxLen;
  1626. /* spit this data back into the tty driver if this
  1627. port is open */
  1628. if (rxLen) {
  1629. port = edge_serial->serial->port[
  1630. edge_serial->rxPort];
  1631. edge_port = usb_get_serial_port_data(port);
  1632. if (edge_port->open) {
  1633. tty = edge_port->port->port.tty;
  1634. if (tty) {
  1635. dbg("%s - Sending %d bytes to TTY for port %d",
  1636. __func__, rxLen, edge_serial->rxPort);
  1637. edge_tty_recv(&edge_serial->serial->dev->dev, tty, buffer, rxLen);
  1638. }
  1639. edge_port->icount.rx += rxLen;
  1640. }
  1641. buffer += rxLen;
  1642. }
  1643. break;
  1644. case EXPECT_HDR3: /* Expect 3rd byte of status header */
  1645. edge_serial->rxHeader3 = *buffer;
  1646. ++buffer;
  1647. --bufferLength;
  1648. /* We have all the header bytes, process the
  1649. status now */
  1650. process_rcvd_status(edge_serial,
  1651. edge_serial->rxStatusParam,
  1652. edge_serial->rxHeader3);
  1653. edge_serial->rxState = EXPECT_HDR1;
  1654. break;
  1655. }
  1656. }
  1657. }
  1658. /*****************************************************************************
  1659. * process_rcvd_status
  1660. * this function handles the any status messages received on the
  1661. * bulk in pipe.
  1662. *****************************************************************************/
  1663. static void process_rcvd_status(struct edgeport_serial *edge_serial,
  1664. __u8 byte2, __u8 byte3)
  1665. {
  1666. struct usb_serial_port *port;
  1667. struct edgeport_port *edge_port;
  1668. __u8 code = edge_serial->rxStatusCode;
  1669. /* switch the port pointer to the one being currently talked about */
  1670. port = edge_serial->serial->port[edge_serial->rxPort];
  1671. edge_port = usb_get_serial_port_data(port);
  1672. if (edge_port == NULL) {
  1673. dev_err(&edge_serial->serial->dev->dev,
  1674. "%s - edge_port == NULL for port %d\n",
  1675. __func__, edge_serial->rxPort);
  1676. return;
  1677. }
  1678. dbg("%s - port %d", __func__, edge_serial->rxPort);
  1679. if (code == IOSP_EXT_STATUS) {
  1680. switch (byte2) {
  1681. case IOSP_EXT_STATUS_CHASE_RSP:
  1682. /* we want to do EXT status regardless of port
  1683. * open/closed */
  1684. dbg("%s - Port %u EXT CHASE_RSP Data = %02x",
  1685. __func__, edge_serial->rxPort, byte3);
  1686. /* Currently, the only EXT_STATUS is Chase, so process
  1687. * here instead of one more call to one more subroutine
  1688. * If/when more EXT_STATUS, there'll be more work to do
  1689. * Also, we currently clear flag and close the port
  1690. * regardless of content of above's Byte3.
  1691. * We could choose to do something else when Byte3 says
  1692. * Timeout on Chase from Edgeport, like wait longer in
  1693. * block_until_chase_response, but for now we don't.
  1694. */
  1695. edge_port->chaseResponsePending = false;
  1696. wake_up(&edge_port->wait_chase);
  1697. return;
  1698. case IOSP_EXT_STATUS_RX_CHECK_RSP:
  1699. dbg("%s ========== Port %u CHECK_RSP Sequence = %02x =============\n", __func__, edge_serial->rxPort, byte3);
  1700. /* Port->RxCheckRsp = true; */
  1701. return;
  1702. }
  1703. }
  1704. if (code == IOSP_STATUS_OPEN_RSP) {
  1705. edge_port->txCredits = GET_TX_BUFFER_SIZE(byte3);
  1706. edge_port->maxTxCredits = edge_port->txCredits;
  1707. dbg("%s - Port %u Open Response Inital MSR = %02x TxBufferSize = %d", __func__, edge_serial->rxPort, byte2, edge_port->txCredits);
  1708. handle_new_msr(edge_port, byte2);
  1709. /* send the current line settings to the port so we are
  1710. in sync with any further termios calls */
  1711. /* FIXME: locking on tty */
  1712. if (edge_port->port->port.tty)
  1713. change_port_settings(edge_port->port->port.tty,
  1714. edge_port, edge_port->port->port.tty->termios);
  1715. /* we have completed the open */
  1716. edge_port->openPending = false;
  1717. edge_port->open = true;
  1718. wake_up(&edge_port->wait_open);
  1719. return;
  1720. }
  1721. /* If port is closed, silently discard all rcvd status. We can
  1722. * have cases where buffered status is received AFTER the close
  1723. * port command is sent to the Edgeport.
  1724. */
  1725. if (!edge_port->open || edge_port->closePending)
  1726. return;
  1727. switch (code) {
  1728. /* Not currently sent by Edgeport */
  1729. case IOSP_STATUS_LSR:
  1730. dbg("%s - Port %u LSR Status = %02x",
  1731. __func__, edge_serial->rxPort, byte2);
  1732. handle_new_lsr(edge_port, false, byte2, 0);
  1733. break;
  1734. case IOSP_STATUS_LSR_DATA:
  1735. dbg("%s - Port %u LSR Status = %02x, Data = %02x",
  1736. __func__, edge_serial->rxPort, byte2, byte3);
  1737. /* byte2 is LSR Register */
  1738. /* byte3 is broken data byte */
  1739. handle_new_lsr(edge_port, true, byte2, byte3);
  1740. break;
  1741. /*
  1742. * case IOSP_EXT_4_STATUS:
  1743. * dbg("%s - Port %u LSR Status = %02x Data = %02x",
  1744. * __func__, edge_serial->rxPort, byte2, byte3);
  1745. * break;
  1746. */
  1747. case IOSP_STATUS_MSR:
  1748. dbg("%s - Port %u MSR Status = %02x",
  1749. __func__, edge_serial->rxPort, byte2);
  1750. /*
  1751. * Process this new modem status and generate appropriate
  1752. * events, etc, based on the new status. This routine
  1753. * also saves the MSR in Port->ShadowMsr.
  1754. */
  1755. handle_new_msr(edge_port, byte2);
  1756. break;
  1757. default:
  1758. dbg("%s - Unrecognized IOSP status code %u\n", __func__, code);
  1759. break;
  1760. }
  1761. return;
  1762. }
  1763. /*****************************************************************************
  1764. * edge_tty_recv
  1765. * this function passes data on to the tty flip buffer
  1766. *****************************************************************************/
  1767. static void edge_tty_recv(struct device *dev, struct tty_struct *tty,
  1768. unsigned char *data, int length)
  1769. {
  1770. int cnt;
  1771. do {
  1772. cnt = tty_buffer_request_room(tty, length);
  1773. if (cnt < length) {
  1774. dev_err(dev, "%s - dropping data, %d bytes lost\n",
  1775. __func__, length - cnt);
  1776. if (cnt == 0)
  1777. break;
  1778. }
  1779. tty_insert_flip_string(tty, data, cnt);
  1780. data += cnt;
  1781. length -= cnt;
  1782. } while (length > 0);
  1783. tty_flip_buffer_push(tty);
  1784. }
  1785. /*****************************************************************************
  1786. * handle_new_msr
  1787. * this function handles any change to the msr register for a port.
  1788. *****************************************************************************/
  1789. static void handle_new_msr(struct edgeport_port *edge_port, __u8 newMsr)
  1790. {
  1791. struct async_icount *icount;
  1792. dbg("%s %02x", __func__, newMsr);
  1793. if (newMsr & (EDGEPORT_MSR_DELTA_CTS | EDGEPORT_MSR_DELTA_DSR |
  1794. EDGEPORT_MSR_DELTA_RI | EDGEPORT_MSR_DELTA_CD)) {
  1795. icount = &edge_port->icount;
  1796. /* update input line counters */
  1797. if (newMsr & EDGEPORT_MSR_DELTA_CTS)
  1798. icount->cts++;
  1799. if (newMsr & EDGEPORT_MSR_DELTA_DSR)
  1800. icount->dsr++;
  1801. if (newMsr & EDGEPORT_MSR_DELTA_CD)
  1802. icount->dcd++;
  1803. if (newMsr & EDGEPORT_MSR_DELTA_RI)
  1804. icount->rng++;
  1805. wake_up_interruptible(&edge_port->delta_msr_wait);
  1806. }
  1807. /* Save the new modem status */
  1808. edge_port->shadowMSR = newMsr & 0xf0;
  1809. return;
  1810. }
  1811. /*****************************************************************************
  1812. * handle_new_lsr
  1813. * this function handles any change to the lsr register for a port.
  1814. *****************************************************************************/
  1815. static void handle_new_lsr(struct edgeport_port *edge_port, __u8 lsrData,
  1816. __u8 lsr, __u8 data)
  1817. {
  1818. __u8 newLsr = (__u8) (lsr & (__u8)
  1819. (LSR_OVER_ERR | LSR_PAR_ERR | LSR_FRM_ERR | LSR_BREAK));
  1820. struct async_icount *icount;
  1821. dbg("%s - %02x", __func__, newLsr);
  1822. edge_port->shadowLSR = lsr;
  1823. if (newLsr & LSR_BREAK) {
  1824. /*
  1825. * Parity and Framing errors only count if they
  1826. * occur exclusive of a break being
  1827. * received.
  1828. */
  1829. newLsr &= (__u8)(LSR_OVER_ERR | LSR_BREAK);
  1830. }
  1831. /* Place LSR data byte into Rx buffer */
  1832. if (lsrData && edge_port->port->port.tty)
  1833. edge_tty_recv(&edge_port->port->dev,
  1834. edge_port->port->port.tty, &data, 1);
  1835. /* update input line counters */
  1836. icount = &edge_port->icount;
  1837. if (newLsr & LSR_BREAK)
  1838. icount->brk++;
  1839. if (newLsr & LSR_OVER_ERR)
  1840. icount->overrun++;
  1841. if (newLsr & LSR_PAR_ERR)
  1842. icount->parity++;
  1843. if (newLsr & LSR_FRM_ERR)
  1844. icount->frame++;
  1845. return;
  1846. }
  1847. /****************************************************************************
  1848. * sram_write
  1849. * writes a number of bytes to the Edgeport device's sram starting at the
  1850. * given address.
  1851. * If successful returns the number of bytes written, otherwise it returns
  1852. * a negative error number of the problem.
  1853. ****************************************************************************/
  1854. static int sram_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1855. __u16 length, const __u8 *data)
  1856. {
  1857. int result;
  1858. __u16 current_length;
  1859. unsigned char *transfer_buffer;
  1860. dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
  1861. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1862. if (!transfer_buffer) {
  1863. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
  1864. __func__, 64);
  1865. return -ENOMEM;
  1866. }
  1867. /* need to split these writes up into 64 byte chunks */
  1868. result = 0;
  1869. while (length > 0) {
  1870. if (length > 64)
  1871. current_length = 64;
  1872. else
  1873. current_length = length;
  1874. /* dbg("%s - writing %x, %x, %d", __func__,
  1875. extAddr, addr, current_length); */
  1876. memcpy(transfer_buffer, data, current_length);
  1877. result = usb_control_msg(serial->dev,
  1878. usb_sndctrlpipe(serial->dev, 0),
  1879. USB_REQUEST_ION_WRITE_RAM,
  1880. 0x40, addr, extAddr, transfer_buffer,
  1881. current_length, 300);
  1882. if (result < 0)
  1883. break;
  1884. length -= current_length;
  1885. addr += current_length;
  1886. data += current_length;
  1887. }
  1888. kfree(transfer_buffer);
  1889. return result;
  1890. }
  1891. /****************************************************************************
  1892. * rom_write
  1893. * writes a number of bytes to the Edgeport device's ROM starting at the
  1894. * given address.
  1895. * If successful returns the number of bytes written, otherwise it returns
  1896. * a negative error number of the problem.
  1897. ****************************************************************************/
  1898. static int rom_write(struct usb_serial *serial, __u16 extAddr, __u16 addr,
  1899. __u16 length, const __u8 *data)
  1900. {
  1901. int result;
  1902. __u16 current_length;
  1903. unsigned char *transfer_buffer;
  1904. /* dbg("%s - %x, %x, %d", __func__, extAddr, addr, length); */
  1905. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1906. if (!transfer_buffer) {
  1907. dev_err(&serial->dev->dev, "%s - kmalloc(%d) failed.\n",
  1908. __func__, 64);
  1909. return -ENOMEM;
  1910. }
  1911. /* need to split these writes up into 64 byte chunks */
  1912. result = 0;
  1913. while (length > 0) {
  1914. if (length > 64)
  1915. current_length = 64;
  1916. else
  1917. current_length = length;
  1918. /* dbg("%s - writing %x, %x, %d", __func__,
  1919. extAddr, addr, current_length); */
  1920. memcpy(transfer_buffer, data, current_length);
  1921. result = usb_control_msg(serial->dev,
  1922. usb_sndctrlpipe(serial->dev, 0),
  1923. USB_REQUEST_ION_WRITE_ROM, 0x40,
  1924. addr, extAddr,
  1925. transfer_buffer, current_length, 300);
  1926. if (result < 0)
  1927. break;
  1928. length -= current_length;
  1929. addr += current_length;
  1930. data += current_length;
  1931. }
  1932. kfree(transfer_buffer);
  1933. return result;
  1934. }
  1935. /****************************************************************************
  1936. * rom_read
  1937. * reads a number of bytes from the Edgeport device starting at the given
  1938. * address.
  1939. * If successful returns the number of bytes read, otherwise it returns
  1940. * a negative error number of the problem.
  1941. ****************************************************************************/
  1942. static int rom_read(struct usb_serial *serial, __u16 extAddr,
  1943. __u16 addr, __u16 length, __u8 *data)
  1944. {
  1945. int result;
  1946. __u16 current_length;
  1947. unsigned char *transfer_buffer;
  1948. dbg("%s - %x, %x, %d", __func__, extAddr, addr, length);
  1949. transfer_buffer = kmalloc(64, GFP_KERNEL);
  1950. if (!transfer_buffer) {
  1951. dev_err(&serial->dev->dev,
  1952. "%s - kmalloc(%d) failed.\n", __func__, 64);
  1953. return -ENOMEM;
  1954. }
  1955. /* need to split these reads up into 64 byte chunks */
  1956. result = 0;
  1957. while (length > 0) {
  1958. if (length > 64)
  1959. current_length = 64;
  1960. else
  1961. current_length = length;
  1962. /* dbg("%s - %x, %x, %d", __func__,
  1963. extAddr, addr, current_length); */
  1964. result = usb_control_msg(serial->dev,
  1965. usb_rcvctrlpipe(serial->dev, 0),
  1966. USB_REQUEST_ION_READ_ROM,
  1967. 0xC0, addr, extAddr, transfer_buffer,
  1968. current_length, 300);
  1969. if (result < 0)
  1970. break;
  1971. memcpy(data, transfer_buffer, current_length);
  1972. length -= current_length;
  1973. addr += current_length;
  1974. data += current_length;
  1975. }
  1976. kfree(transfer_buffer);
  1977. return result;
  1978. }
  1979. /****************************************************************************
  1980. * send_iosp_ext_cmd
  1981. * Is used to send a IOSP message to the Edgeport device
  1982. ****************************************************************************/
  1983. static int send_iosp_ext_cmd(struct edgeport_port *edge_port,
  1984. __u8 command, __u8 param)
  1985. {
  1986. unsigned char *buffer;
  1987. unsigned char *currentCommand;
  1988. int length = 0;
  1989. int status = 0;
  1990. dbg("%s - %d, %d", __func__, command, param);
  1991. buffer = kmalloc(10, GFP_ATOMIC);
  1992. if (!buffer) {
  1993. dev_err(&edge_port->port->dev,
  1994. "%s - kmalloc(%d) failed.\n", __func__, 10);
  1995. return -ENOMEM;
  1996. }
  1997. currentCommand = buffer;
  1998. MAKE_CMD_EXT_CMD(&currentCommand, &length,
  1999. edge_port->port->number - edge_port->port->serial->minor,
  2000. command, param);
  2001. status = write_cmd_usb(edge_port, buffer, length);
  2002. if (status) {
  2003. /* something bad happened, let's free up the memory */
  2004. kfree(buffer);
  2005. }
  2006. return status;
  2007. }
  2008. /*****************************************************************************
  2009. * write_cmd_usb
  2010. * this function writes the given buffer out to the bulk write endpoint.
  2011. *****************************************************************************/
  2012. static int write_cmd_usb(struct edgeport_port *edge_port,
  2013. unsigned char *buffer, int length)
  2014. {
  2015. struct edgeport_serial *edge_serial =
  2016. usb_get_serial_data(edge_port->port->serial);
  2017. int status = 0;
  2018. struct urb *urb;
  2019. int timeout;
  2020. usb_serial_debug_data(debug, &edge_port->port->dev,
  2021. __func__, length, buffer);
  2022. /* Allocate our next urb */
  2023. urb = usb_alloc_urb(0, GFP_ATOMIC);
  2024. if (!urb)
  2025. return -ENOMEM;
  2026. atomic_inc(&CmdUrbs);
  2027. dbg("%s - ALLOCATE URB %p (outstanding %d)",
  2028. __func__, urb, atomic_read(&CmdUrbs));
  2029. usb_fill_bulk_urb(urb, edge_serial->serial->dev,
  2030. usb_sndbulkpipe(edge_serial->serial->dev,
  2031. edge_serial->bulk_out_endpoint),
  2032. buffer, length, edge_bulk_out_cmd_callback, edge_port);
  2033. edge_port->commandPending = true;
  2034. status = usb_submit_urb(urb, GFP_ATOMIC);
  2035. if (status) {
  2036. /* something went wrong */
  2037. dev_err(&edge_port->port->dev,
  2038. "%s - usb_submit_urb(write command) failed, status = %d\n",
  2039. __func__, status);
  2040. usb_kill_urb(urb);
  2041. usb_free_urb(urb);
  2042. atomic_dec(&CmdUrbs);
  2043. return status;
  2044. }
  2045. /* wait for command to finish */
  2046. timeout = COMMAND_TIMEOUT;
  2047. #if 0
  2048. wait_event(&edge_port->wait_command, !edge_port->commandPending);
  2049. if (edge_port->commandPending) {
  2050. /* command timed out */
  2051. dbg("%s - command timed out", __func__);
  2052. status = -EINVAL;
  2053. }
  2054. #endif
  2055. return status;
  2056. }
  2057. /*****************************************************************************
  2058. * send_cmd_write_baud_rate
  2059. * this function sends the proper command to change the baud rate of the
  2060. * specified port.
  2061. *****************************************************************************/
  2062. static int send_cmd_write_baud_rate(struct edgeport_port *edge_port,
  2063. int baudRate)
  2064. {
  2065. struct edgeport_serial *edge_serial =
  2066. usb_get_serial_data(edge_port->port->serial);
  2067. unsigned char *cmdBuffer;
  2068. unsigned char *currCmd;
  2069. int cmdLen = 0;
  2070. int divisor;
  2071. int status;
  2072. unsigned char number =
  2073. edge_port->port->number - edge_port->port->serial->minor;
  2074. if (edge_serial->is_epic &&
  2075. !edge_serial->epic_descriptor.Supports.IOSPSetBaudRate) {
  2076. dbg("SendCmdWriteBaudRate - NOT Setting baud rate for port = %d, baud = %d",
  2077. edge_port->port->number, baudRate);
  2078. return 0;
  2079. }
  2080. dbg("%s - port = %d, baud = %d", __func__,
  2081. edge_port->port->number, baudRate);
  2082. status = calc_baud_rate_divisor(baudRate, &divisor);
  2083. if (status) {
  2084. dev_err(&edge_port->port->dev, "%s - bad baud rate\n",
  2085. __func__);
  2086. return status;
  2087. }
  2088. /* Alloc memory for the string of commands. */
  2089. cmdBuffer = kmalloc(0x100, GFP_ATOMIC);
  2090. if (!cmdBuffer) {
  2091. dev_err(&edge_port->port->dev,
  2092. "%s - kmalloc(%d) failed.\n", __func__, 0x100);
  2093. return -ENOMEM;
  2094. }
  2095. currCmd = cmdBuffer;
  2096. /* Enable access to divisor latch */
  2097. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR, LCR_DL_ENABLE);
  2098. /* Write the divisor itself */
  2099. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLL, LOW8(divisor));
  2100. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, DLM, HIGH8(divisor));
  2101. /* Restore original value to disable access to divisor latch */
  2102. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen, number, LCR,
  2103. edge_port->shadowLCR);
  2104. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2105. if (status) {
  2106. /* something bad happened, let's free up the memory */
  2107. kfree(cmdBuffer);
  2108. }
  2109. return status;
  2110. }
  2111. /*****************************************************************************
  2112. * calc_baud_rate_divisor
  2113. * this function calculates the proper baud rate divisor for the specified
  2114. * baud rate.
  2115. *****************************************************************************/
  2116. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  2117. {
  2118. int i;
  2119. __u16 custom;
  2120. dbg("%s - %d", __func__, baudrate);
  2121. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  2122. if (divisor_table[i].BaudRate == baudrate) {
  2123. *divisor = divisor_table[i].Divisor;
  2124. return 0;
  2125. }
  2126. }
  2127. /* We have tried all of the standard baud rates
  2128. * lets try to calculate the divisor for this baud rate
  2129. * Make sure the baud rate is reasonable */
  2130. if (baudrate > 50 && baudrate < 230400) {
  2131. /* get divisor */
  2132. custom = (__u16)((230400L + baudrate/2) / baudrate);
  2133. *divisor = custom;
  2134. dbg("%s - Baud %d = %d\n", __func__, baudrate, custom);
  2135. return 0;
  2136. }
  2137. return -1;
  2138. }
  2139. /*****************************************************************************
  2140. * send_cmd_write_uart_register
  2141. * this function builds up a uart register message and sends to to the device.
  2142. *****************************************************************************/
  2143. static int send_cmd_write_uart_register(struct edgeport_port *edge_port,
  2144. __u8 regNum, __u8 regValue)
  2145. {
  2146. struct edgeport_serial *edge_serial =
  2147. usb_get_serial_data(edge_port->port->serial);
  2148. unsigned char *cmdBuffer;
  2149. unsigned char *currCmd;
  2150. unsigned long cmdLen = 0;
  2151. int status;
  2152. dbg("%s - write to %s register 0x%02x",
  2153. (regNum == MCR) ? "MCR" : "LCR", __func__, regValue);
  2154. if (edge_serial->is_epic &&
  2155. !edge_serial->epic_descriptor.Supports.IOSPWriteMCR &&
  2156. regNum == MCR) {
  2157. dbg("SendCmdWriteUartReg - Not writing to MCR Register");
  2158. return 0;
  2159. }
  2160. if (edge_serial->is_epic &&
  2161. !edge_serial->epic_descriptor.Supports.IOSPWriteLCR &&
  2162. regNum == LCR) {
  2163. dbg("SendCmdWriteUartReg - Not writing to LCR Register");
  2164. return 0;
  2165. }
  2166. /* Alloc memory for the string of commands. */
  2167. cmdBuffer = kmalloc(0x10, GFP_ATOMIC);
  2168. if (cmdBuffer == NULL)
  2169. return -ENOMEM;
  2170. currCmd = cmdBuffer;
  2171. /* Build a cmd in the buffer to write the given register */
  2172. MAKE_CMD_WRITE_REG(&currCmd, &cmdLen,
  2173. edge_port->port->number - edge_port->port->serial->minor,
  2174. regNum, regValue);
  2175. status = write_cmd_usb(edge_port, cmdBuffer, cmdLen);
  2176. if (status) {
  2177. /* something bad happened, let's free up the memory */
  2178. kfree(cmdBuffer);
  2179. }
  2180. return status;
  2181. }
  2182. /*****************************************************************************
  2183. * change_port_settings
  2184. * This routine is called to set the UART on the device to match the
  2185. * specified new settings.
  2186. *****************************************************************************/
  2187. static void change_port_settings(struct tty_struct *tty,
  2188. struct edgeport_port *edge_port, struct ktermios *old_termios)
  2189. {
  2190. struct edgeport_serial *edge_serial =
  2191. usb_get_serial_data(edge_port->port->serial);
  2192. int baud;
  2193. unsigned cflag;
  2194. __u8 mask = 0xff;
  2195. __u8 lData;
  2196. __u8 lParity;
  2197. __u8 lStop;
  2198. __u8 rxFlow;
  2199. __u8 txFlow;
  2200. int status;
  2201. dbg("%s - port %d", __func__, edge_port->port->number);
  2202. if (!edge_port->open &&
  2203. !edge_port->openPending) {
  2204. dbg("%s - port not opened", __func__);
  2205. return;
  2206. }
  2207. cflag = tty->termios->c_cflag;
  2208. switch (cflag & CSIZE) {
  2209. case CS5:
  2210. lData = LCR_BITS_5; mask = 0x1f;
  2211. dbg("%s - data bits = 5", __func__);
  2212. break;
  2213. case CS6:
  2214. lData = LCR_BITS_6; mask = 0x3f;
  2215. dbg("%s - data bits = 6", __func__);
  2216. break;
  2217. case CS7:
  2218. lData = LCR_BITS_7; mask = 0x7f;
  2219. dbg("%s - data bits = 7", __func__);
  2220. break;
  2221. default:
  2222. case CS8:
  2223. lData = LCR_BITS_8;
  2224. dbg("%s - data bits = 8", __func__);
  2225. break;
  2226. }
  2227. lParity = LCR_PAR_NONE;
  2228. if (cflag & PARENB) {
  2229. if (cflag & CMSPAR) {
  2230. if (cflag & PARODD) {
  2231. lParity = LCR_PAR_MARK;
  2232. dbg("%s - parity = mark", __func__);
  2233. } else {
  2234. lParity = LCR_PAR_SPACE;
  2235. dbg("%s - parity = space", __func__);
  2236. }
  2237. } else if (cflag & PARODD) {
  2238. lParity = LCR_PAR_ODD;
  2239. dbg("%s - parity = odd", __func__);
  2240. } else {
  2241. lParity = LCR_PAR_EVEN;
  2242. dbg("%s - parity = even", __func__);
  2243. }
  2244. } else {
  2245. dbg("%s - parity = none", __func__);
  2246. }
  2247. if (cflag & CSTOPB) {
  2248. lStop = LCR_STOP_2;
  2249. dbg("%s - stop bits = 2", __func__);
  2250. } else {
  2251. lStop = LCR_STOP_1;
  2252. dbg("%s - stop bits = 1", __func__);
  2253. }
  2254. /* figure out the flow control settings */
  2255. rxFlow = txFlow = 0x00;
  2256. if (cflag & CRTSCTS) {
  2257. rxFlow |= IOSP_RX_FLOW_RTS;
  2258. txFlow |= IOSP_TX_FLOW_CTS;
  2259. dbg("%s - RTS/CTS is enabled", __func__);
  2260. } else {
  2261. dbg("%s - RTS/CTS is disabled", __func__);
  2262. }
  2263. /* if we are implementing XON/XOFF, set the start and stop character
  2264. in the device */
  2265. if (I_IXOFF(tty) || I_IXON(tty)) {
  2266. unsigned char stop_char = STOP_CHAR(tty);
  2267. unsigned char start_char = START_CHAR(tty);
  2268. if ((!edge_serial->is_epic) ||
  2269. ((edge_serial->is_epic) &&
  2270. (edge_serial->epic_descriptor.Supports.IOSPSetXChar))) {
  2271. send_iosp_ext_cmd(edge_port,
  2272. IOSP_CMD_SET_XON_CHAR, start_char);
  2273. send_iosp_ext_cmd(edge_port,
  2274. IOSP_CMD_SET_XOFF_CHAR, stop_char);
  2275. }
  2276. /* if we are implementing INBOUND XON/XOFF */
  2277. if (I_IXOFF(tty)) {
  2278. rxFlow |= IOSP_RX_FLOW_XON_XOFF;
  2279. dbg("%s - INBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2280. __func__, start_char, stop_char);
  2281. } else {
  2282. dbg("%s - INBOUND XON/XOFF is disabled", __func__);
  2283. }
  2284. /* if we are implementing OUTBOUND XON/XOFF */
  2285. if (I_IXON(tty)) {
  2286. txFlow |= IOSP_TX_FLOW_XON_XOFF;
  2287. dbg("%s - OUTBOUND XON/XOFF is enabled, XON = %2x, XOFF = %2x",
  2288. __func__, start_char, stop_char);
  2289. } else {
  2290. dbg("%s - OUTBOUND XON/XOFF is disabled", __func__);
  2291. }
  2292. }
  2293. /* Set flow control to the configured value */
  2294. if ((!edge_serial->is_epic) ||
  2295. ((edge_serial->is_epic) &&
  2296. (edge_serial->epic_descriptor.Supports.IOSPSetRxFlow)))
  2297. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_RX_FLOW, rxFlow);
  2298. if ((!edge_serial->is_epic) ||
  2299. ((edge_serial->is_epic) &&
  2300. (edge_serial->epic_descriptor.Supports.IOSPSetTxFlow)))
  2301. send_iosp_ext_cmd(edge_port, IOSP_CMD_SET_TX_FLOW, txFlow);
  2302. edge_port->shadowLCR &= ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  2303. edge_port->shadowLCR |= (lData | lParity | lStop);
  2304. edge_port->validDataMask = mask;
  2305. /* Send the updated LCR value to the EdgePort */
  2306. status = send_cmd_write_uart_register(edge_port, LCR,
  2307. edge_port->shadowLCR);
  2308. if (status != 0)
  2309. return;
  2310. /* set up the MCR register and send it to the EdgePort */
  2311. edge_port->shadowMCR = MCR_MASTER_IE;
  2312. if (cflag & CBAUD)
  2313. edge_port->shadowMCR |= (MCR_DTR | MCR_RTS);
  2314. status = send_cmd_write_uart_register(edge_port, MCR,
  2315. edge_port->shadowMCR);
  2316. if (status != 0)
  2317. return;
  2318. /* Determine divisor based on baud rate */
  2319. baud = tty_get_baud_rate(tty);
  2320. if (!baud) {
  2321. /* pick a default, any default... */
  2322. baud = 9600;
  2323. }
  2324. dbg("%s - baud rate = %d", __func__, baud);
  2325. status = send_cmd_write_baud_rate(edge_port, baud);
  2326. if (status == -1) {
  2327. /* Speed change was not possible - put back the old speed */
  2328. baud = tty_termios_baud_rate(old_termios);
  2329. tty_encode_baud_rate(tty, baud, baud);
  2330. }
  2331. return;
  2332. }
  2333. /****************************************************************************
  2334. * unicode_to_ascii
  2335. * Turns a string from Unicode into ASCII.
  2336. * Doesn't do a good job with any characters that are outside the normal
  2337. * ASCII range, but it's only for debugging...
  2338. * NOTE: expects the unicode in LE format
  2339. ****************************************************************************/
  2340. static void unicode_to_ascii(char *string, int buflen,
  2341. __le16 *unicode, int unicode_size)
  2342. {
  2343. int i;
  2344. if (buflen <= 0) /* never happens, but... */
  2345. return;
  2346. --buflen; /* space for nul */
  2347. for (i = 0; i < unicode_size; i++) {
  2348. if (i >= buflen)
  2349. break;
  2350. string[i] = (char)(le16_to_cpu(unicode[i]));
  2351. }
  2352. string[i] = 0x00;
  2353. }
  2354. /****************************************************************************
  2355. * get_manufacturing_desc
  2356. * reads in the manufacturing descriptor and stores it into the serial
  2357. * structure.
  2358. ****************************************************************************/
  2359. static void get_manufacturing_desc(struct edgeport_serial *edge_serial)
  2360. {
  2361. int response;
  2362. dbg("getting manufacturer descriptor");
  2363. response = rom_read(edge_serial->serial,
  2364. (EDGE_MANUF_DESC_ADDR & 0xffff0000) >> 16,
  2365. (__u16)(EDGE_MANUF_DESC_ADDR & 0x0000ffff),
  2366. EDGE_MANUF_DESC_LEN,
  2367. (__u8 *)(&edge_serial->manuf_descriptor));
  2368. if (response < 1)
  2369. dev_err(&edge_serial->serial->dev->dev,
  2370. "error in getting manufacturer descriptor\n");
  2371. else {
  2372. char string[30];
  2373. dbg("**Manufacturer Descriptor");
  2374. dbg(" RomSize: %dK",
  2375. edge_serial->manuf_descriptor.RomSize);
  2376. dbg(" RamSize: %dK",
  2377. edge_serial->manuf_descriptor.RamSize);
  2378. dbg(" CpuRev: %d",
  2379. edge_serial->manuf_descriptor.CpuRev);
  2380. dbg(" BoardRev: %d",
  2381. edge_serial->manuf_descriptor.BoardRev);
  2382. dbg(" NumPorts: %d",
  2383. edge_serial->manuf_descriptor.NumPorts);
  2384. dbg(" DescDate: %d/%d/%d",
  2385. edge_serial->manuf_descriptor.DescDate[0],
  2386. edge_serial->manuf_descriptor.DescDate[1],
  2387. edge_serial->manuf_descriptor.DescDate[2]+1900);
  2388. unicode_to_ascii(string, sizeof(string),
  2389. edge_serial->manuf_descriptor.SerialNumber,
  2390. edge_serial->manuf_descriptor.SerNumLength/2);
  2391. dbg(" SerialNumber: %s", string);
  2392. unicode_to_ascii(string, sizeof(string),
  2393. edge_serial->manuf_descriptor.AssemblyNumber,
  2394. edge_serial->manuf_descriptor.AssemblyNumLength/2);
  2395. dbg(" AssemblyNumber: %s", string);
  2396. unicode_to_ascii(string, sizeof(string),
  2397. edge_serial->manuf_descriptor.OemAssyNumber,
  2398. edge_serial->manuf_descriptor.OemAssyNumLength/2);
  2399. dbg(" OemAssyNumber: %s", string);
  2400. dbg(" UartType: %d",
  2401. edge_serial->manuf_descriptor.UartType);
  2402. dbg(" IonPid: %d",
  2403. edge_serial->manuf_descriptor.IonPid);
  2404. dbg(" IonConfig: %d",
  2405. edge_serial->manuf_descriptor.IonConfig);
  2406. }
  2407. }
  2408. /****************************************************************************
  2409. * get_boot_desc
  2410. * reads in the bootloader descriptor and stores it into the serial
  2411. * structure.
  2412. ****************************************************************************/
  2413. static void get_boot_desc(struct edgeport_serial *edge_serial)
  2414. {
  2415. int response;
  2416. dbg("getting boot descriptor");
  2417. response = rom_read(edge_serial->serial,
  2418. (EDGE_BOOT_DESC_ADDR & 0xffff0000) >> 16,
  2419. (__u16)(EDGE_BOOT_DESC_ADDR & 0x0000ffff),
  2420. EDGE_BOOT_DESC_LEN,
  2421. (__u8 *)(&edge_serial->boot_descriptor));
  2422. if (response < 1)
  2423. dev_err(&edge_serial->serial->dev->dev,
  2424. "error in getting boot descriptor\n");
  2425. else {
  2426. dbg("**Boot Descriptor:");
  2427. dbg(" BootCodeLength: %d",
  2428. le16_to_cpu(edge_serial->boot_descriptor.BootCodeLength));
  2429. dbg(" MajorVersion: %d",
  2430. edge_serial->boot_descriptor.MajorVersion);
  2431. dbg(" MinorVersion: %d",
  2432. edge_serial->boot_descriptor.MinorVersion);
  2433. dbg(" BuildNumber: %d",
  2434. le16_to_cpu(edge_serial->boot_descriptor.BuildNumber));
  2435. dbg(" Capabilities: 0x%x",
  2436. le16_to_cpu(edge_serial->boot_descriptor.Capabilities));
  2437. dbg(" UConfig0: %d",
  2438. edge_serial->boot_descriptor.UConfig0);
  2439. dbg(" UConfig1: %d",
  2440. edge_serial->boot_descriptor.UConfig1);
  2441. }
  2442. }
  2443. /****************************************************************************
  2444. * load_application_firmware
  2445. * This is called to load the application firmware to the device
  2446. ****************************************************************************/
  2447. static void load_application_firmware(struct edgeport_serial *edge_serial)
  2448. {
  2449. const struct ihex_binrec *rec;
  2450. const struct firmware *fw;
  2451. const char *fw_name;
  2452. const char *fw_info;
  2453. int response;
  2454. __u32 Operaddr;
  2455. __u16 build;
  2456. switch (edge_serial->product_info.iDownloadFile) {
  2457. case EDGE_DOWNLOAD_FILE_I930:
  2458. fw_info = "downloading firmware version (930)";
  2459. fw_name = "edgeport/down.fw";
  2460. break;
  2461. case EDGE_DOWNLOAD_FILE_80251:
  2462. fw_info = "downloading firmware version (80251)";
  2463. fw_name = "edgeport/down2.fw";
  2464. break;
  2465. case EDGE_DOWNLOAD_FILE_NONE:
  2466. dbg ("No download file specified, skipping download\n");
  2467. return;
  2468. default:
  2469. return;
  2470. }
  2471. response = request_ihex_firmware(&fw, fw_name,
  2472. &edge_serial->serial->dev->dev);
  2473. if (response) {
  2474. printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
  2475. fw_name, response);
  2476. return;
  2477. }
  2478. rec = (const struct ihex_binrec *)fw->data;
  2479. build = (rec->data[2] << 8) | rec->data[3];
  2480. dbg("%s %d.%d.%d", fw_info, rec->data[0], rec->data[1], build);
  2481. edge_serial->product_info.FirmwareMajorVersion = fw->data[0];
  2482. edge_serial->product_info.FirmwareMinorVersion = fw->data[1];
  2483. edge_serial->product_info.FirmwareBuildNumber = cpu_to_le16(build);
  2484. for (rec = ihex_next_binrec(rec); rec;
  2485. rec = ihex_next_binrec(rec)) {
  2486. Operaddr = be32_to_cpu(rec->addr);
  2487. response = sram_write(edge_serial->serial,
  2488. Operaddr >> 16,
  2489. Operaddr & 0xFFFF,
  2490. be16_to_cpu(rec->len),
  2491. &rec->data[0]);
  2492. if (response < 0) {
  2493. dev_err(&edge_serial->serial->dev->dev,
  2494. "sram_write failed (%x, %x, %d)\n",
  2495. Operaddr >> 16, Operaddr & 0xFFFF,
  2496. be16_to_cpu(rec->len));
  2497. break;
  2498. }
  2499. }
  2500. dbg("sending exec_dl_code");
  2501. response = usb_control_msg (edge_serial->serial->dev,
  2502. usb_sndctrlpipe(edge_serial->serial->dev, 0),
  2503. USB_REQUEST_ION_EXEC_DL_CODE,
  2504. 0x40, 0x4000, 0x0001, NULL, 0, 3000);
  2505. release_firmware(fw);
  2506. return;
  2507. }
  2508. /****************************************************************************
  2509. * edge_startup
  2510. ****************************************************************************/
  2511. static int edge_startup(struct usb_serial *serial)
  2512. {
  2513. struct edgeport_serial *edge_serial;
  2514. struct edgeport_port *edge_port;
  2515. struct usb_device *dev;
  2516. int i, j;
  2517. int response;
  2518. bool interrupt_in_found;
  2519. bool bulk_in_found;
  2520. bool bulk_out_found;
  2521. static __u32 descriptor[3] = { EDGE_COMPATIBILITY_MASK0,
  2522. EDGE_COMPATIBILITY_MASK1,
  2523. EDGE_COMPATIBILITY_MASK2 };
  2524. dev = serial->dev;
  2525. /* create our private serial structure */
  2526. edge_serial = kzalloc(sizeof(struct edgeport_serial), GFP_KERNEL);
  2527. if (edge_serial == NULL) {
  2528. dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
  2529. return -ENOMEM;
  2530. }
  2531. spin_lock_init(&edge_serial->es_lock);
  2532. edge_serial->serial = serial;
  2533. usb_set_serial_data(serial, edge_serial);
  2534. /* get the name for the device from the device */
  2535. i = get_string(dev, dev->descriptor.iManufacturer,
  2536. &edge_serial->name[0], MAX_NAME_LEN+1);
  2537. edge_serial->name[i++] = ' ';
  2538. get_string(dev, dev->descriptor.iProduct,
  2539. &edge_serial->name[i], MAX_NAME_LEN+2 - i);
  2540. dev_info(&serial->dev->dev, "%s detected\n", edge_serial->name);
  2541. /* Read the epic descriptor */
  2542. if (get_epic_descriptor(edge_serial) <= 0) {
  2543. /* memcpy descriptor to Supports structures */
  2544. memcpy(&edge_serial->epic_descriptor.Supports, descriptor,
  2545. sizeof(struct edge_compatibility_bits));
  2546. /* get the manufacturing descriptor for this device */
  2547. get_manufacturing_desc(edge_serial);
  2548. /* get the boot descriptor */
  2549. get_boot_desc(edge_serial);
  2550. get_product_info(edge_serial);
  2551. }
  2552. /* set the number of ports from the manufacturing description */
  2553. /* serial->num_ports = serial->product_info.NumPorts; */
  2554. if ((!edge_serial->is_epic) &&
  2555. (edge_serial->product_info.NumPorts != serial->num_ports)) {
  2556. dev_warn(&serial->dev->dev, "Device Reported %d serial ports "
  2557. "vs. core thinking we have %d ports, email "
  2558. "greg@kroah.com this information.\n",
  2559. edge_serial->product_info.NumPorts,
  2560. serial->num_ports);
  2561. }
  2562. dbg("%s - time 1 %ld", __func__, jiffies);
  2563. /* If not an EPiC device */
  2564. if (!edge_serial->is_epic) {
  2565. /* now load the application firmware into this device */
  2566. load_application_firmware(edge_serial);
  2567. dbg("%s - time 2 %ld", __func__, jiffies);
  2568. /* Check current Edgeport EEPROM and update if necessary */
  2569. update_edgeport_E2PROM(edge_serial);
  2570. dbg("%s - time 3 %ld", __func__, jiffies);
  2571. /* set the configuration to use #1 */
  2572. /* dbg("set_configuration 1"); */
  2573. /* usb_set_configuration (dev, 1); */
  2574. }
  2575. dbg(" FirmwareMajorVersion %d.%d.%d",
  2576. edge_serial->product_info.FirmwareMajorVersion,
  2577. edge_serial->product_info.FirmwareMinorVersion,
  2578. le16_to_cpu(edge_serial->product_info.FirmwareBuildNumber));
  2579. /* we set up the pointers to the endpoints in the edge_open function,
  2580. * as the structures aren't created yet. */
  2581. /* set up our port private structures */
  2582. for (i = 0; i < serial->num_ports; ++i) {
  2583. edge_port = kmalloc(sizeof(struct edgeport_port), GFP_KERNEL);
  2584. if (edge_port == NULL) {
  2585. dev_err(&serial->dev->dev, "%s - Out of memory\n",
  2586. __func__);
  2587. for (j = 0; j < i; ++j) {
  2588. kfree(usb_get_serial_port_data(serial->port[j]));
  2589. usb_set_serial_port_data(serial->port[j],
  2590. NULL);
  2591. }
  2592. usb_set_serial_data(serial, NULL);
  2593. kfree(edge_serial);
  2594. return -ENOMEM;
  2595. }
  2596. memset(edge_port, 0, sizeof(struct edgeport_port));
  2597. spin_lock_init(&edge_port->ep_lock);
  2598. edge_port->port = serial->port[i];
  2599. usb_set_serial_port_data(serial->port[i], edge_port);
  2600. }
  2601. response = 0;
  2602. if (edge_serial->is_epic) {
  2603. /* EPIC thing, set up our interrupt polling now and our read
  2604. * urb, so that the device knows it really is connected. */
  2605. interrupt_in_found = bulk_in_found = bulk_out_found = false;
  2606. for (i = 0; i < serial->interface->altsetting[0]
  2607. .desc.bNumEndpoints; ++i) {
  2608. struct usb_endpoint_descriptor *endpoint;
  2609. int buffer_size;
  2610. endpoint = &serial->interface->altsetting[0].
  2611. endpoint[i].desc;
  2612. buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
  2613. if (!interrupt_in_found &&
  2614. (usb_endpoint_is_int_in(endpoint))) {
  2615. /* we found a interrupt in endpoint */
  2616. dbg("found interrupt in");
  2617. /* not set up yet, so do it now */
  2618. edge_serial->interrupt_read_urb =
  2619. usb_alloc_urb(0, GFP_KERNEL);
  2620. if (!edge_serial->interrupt_read_urb) {
  2621. err("out of memory");
  2622. return -ENOMEM;
  2623. }
  2624. edge_serial->interrupt_in_buffer =
  2625. kmalloc(buffer_size, GFP_KERNEL);
  2626. if (!edge_serial->interrupt_in_buffer) {
  2627. err("out of memory");
  2628. usb_free_urb(edge_serial->interrupt_read_urb);
  2629. return -ENOMEM;
  2630. }
  2631. edge_serial->interrupt_in_endpoint =
  2632. endpoint->bEndpointAddress;
  2633. /* set up our interrupt urb */
  2634. usb_fill_int_urb(
  2635. edge_serial->interrupt_read_urb,
  2636. dev,
  2637. usb_rcvintpipe(dev,
  2638. endpoint->bEndpointAddress),
  2639. edge_serial->interrupt_in_buffer,
  2640. buffer_size,
  2641. edge_interrupt_callback,
  2642. edge_serial,
  2643. endpoint->bInterval);
  2644. interrupt_in_found = true;
  2645. }
  2646. if (!bulk_in_found &&
  2647. (usb_endpoint_is_bulk_in(endpoint))) {
  2648. /* we found a bulk in endpoint */
  2649. dbg("found bulk in");
  2650. /* not set up yet, so do it now */
  2651. edge_serial->read_urb =
  2652. usb_alloc_urb(0, GFP_KERNEL);
  2653. if (!edge_serial->read_urb) {
  2654. err("out of memory");
  2655. return -ENOMEM;
  2656. }
  2657. edge_serial->bulk_in_buffer =
  2658. kmalloc(buffer_size, GFP_KERNEL);
  2659. if (!edge_serial->bulk_in_buffer) {
  2660. err("out of memory");
  2661. usb_free_urb(edge_serial->read_urb);
  2662. return -ENOMEM;
  2663. }
  2664. edge_serial->bulk_in_endpoint =
  2665. endpoint->bEndpointAddress;
  2666. /* set up our bulk in urb */
  2667. usb_fill_bulk_urb(edge_serial->read_urb, dev,
  2668. usb_rcvbulkpipe(dev,
  2669. endpoint->bEndpointAddress),
  2670. edge_serial->bulk_in_buffer,
  2671. le16_to_cpu(endpoint->wMaxPacketSize),
  2672. edge_bulk_in_callback,
  2673. edge_serial);
  2674. bulk_in_found = true;
  2675. }
  2676. if (!bulk_out_found &&
  2677. (usb_endpoint_is_bulk_out(endpoint))) {
  2678. /* we found a bulk out endpoint */
  2679. dbg("found bulk out");
  2680. edge_serial->bulk_out_endpoint =
  2681. endpoint->bEndpointAddress;
  2682. bulk_out_found = true;
  2683. }
  2684. }
  2685. if (!interrupt_in_found || !bulk_in_found || !bulk_out_found) {
  2686. err("Error - the proper endpoints were not found!");
  2687. return -ENODEV;
  2688. }
  2689. /* start interrupt read for this edgeport this interrupt will
  2690. * continue as long as the edgeport is connected */
  2691. response = usb_submit_urb(edge_serial->interrupt_read_urb,
  2692. GFP_KERNEL);
  2693. if (response)
  2694. err("%s - Error %d submitting control urb",
  2695. __func__, response);
  2696. }
  2697. return response;
  2698. }
  2699. /****************************************************************************
  2700. * edge_shutdown
  2701. * This function is called whenever the device is removed from the usb bus.
  2702. ****************************************************************************/
  2703. static void edge_shutdown(struct usb_serial *serial)
  2704. {
  2705. struct edgeport_serial *edge_serial = usb_get_serial_data(serial);
  2706. int i;
  2707. dbg("%s", __func__);
  2708. /* stop reads and writes on all ports */
  2709. for (i = 0; i < serial->num_ports; ++i) {
  2710. kfree(usb_get_serial_port_data(serial->port[i]));
  2711. usb_set_serial_port_data(serial->port[i], NULL);
  2712. }
  2713. /* free up our endpoint stuff */
  2714. if (edge_serial->is_epic) {
  2715. usb_kill_urb(edge_serial->interrupt_read_urb);
  2716. usb_free_urb(edge_serial->interrupt_read_urb);
  2717. kfree(edge_serial->interrupt_in_buffer);
  2718. usb_kill_urb(edge_serial->read_urb);
  2719. usb_free_urb(edge_serial->read_urb);
  2720. kfree(edge_serial->bulk_in_buffer);
  2721. }
  2722. kfree(edge_serial);
  2723. usb_set_serial_data(serial, NULL);
  2724. }
  2725. /****************************************************************************
  2726. * edgeport_init
  2727. * This is called by the module subsystem, or on startup to initialize us
  2728. ****************************************************************************/
  2729. static int __init edgeport_init(void)
  2730. {
  2731. int retval;
  2732. retval = usb_serial_register(&edgeport_2port_device);
  2733. if (retval)
  2734. goto failed_2port_device_register;
  2735. retval = usb_serial_register(&edgeport_4port_device);
  2736. if (retval)
  2737. goto failed_4port_device_register;
  2738. retval = usb_serial_register(&edgeport_8port_device);
  2739. if (retval)
  2740. goto failed_8port_device_register;
  2741. retval = usb_serial_register(&epic_device);
  2742. if (retval)
  2743. goto failed_epic_device_register;
  2744. retval = usb_register(&io_driver);
  2745. if (retval)
  2746. goto failed_usb_register;
  2747. atomic_set(&CmdUrbs, 0);
  2748. info(DRIVER_DESC " " DRIVER_VERSION);
  2749. return 0;
  2750. failed_usb_register:
  2751. usb_serial_deregister(&epic_device);
  2752. failed_epic_device_register:
  2753. usb_serial_deregister(&edgeport_8port_device);
  2754. failed_8port_device_register:
  2755. usb_serial_deregister(&edgeport_4port_device);
  2756. failed_4port_device_register:
  2757. usb_serial_deregister(&edgeport_2port_device);
  2758. failed_2port_device_register:
  2759. return retval;
  2760. }
  2761. /****************************************************************************
  2762. * edgeport_exit
  2763. * Called when the driver is about to be unloaded.
  2764. ****************************************************************************/
  2765. static void __exit edgeport_exit (void)
  2766. {
  2767. usb_deregister(&io_driver);
  2768. usb_serial_deregister(&edgeport_2port_device);
  2769. usb_serial_deregister(&edgeport_4port_device);
  2770. usb_serial_deregister(&edgeport_8port_device);
  2771. usb_serial_deregister(&epic_device);
  2772. }
  2773. module_init(edgeport_init);
  2774. module_exit(edgeport_exit);
  2775. /* Module information */
  2776. MODULE_AUTHOR(DRIVER_AUTHOR);
  2777. MODULE_DESCRIPTION(DRIVER_DESC);
  2778. MODULE_LICENSE("GPL");
  2779. MODULE_FIRMWARE("edgeport/boot.fw");
  2780. MODULE_FIRMWARE("edgeport/boot2.fw");
  2781. MODULE_FIRMWARE("edgeport/down.fw");
  2782. MODULE_FIRMWARE("edgeport/down2.fw");
  2783. module_param(debug, bool, S_IRUGO | S_IWUSR);
  2784. MODULE_PARM_DESC(debug, "Debug enabled or not");
  2785. module_param(low_latency, bool, S_IRUGO | S_IWUSR);
  2786. MODULE_PARM_DESC(low_latency, "Low latency enabled or not");