io_edgeport.c 92 KB

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