hso.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Jan Dumon <j.dumon@option.com>
  9. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  10. * <ajb@spheresystems.co.uk>
  11. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  12. * Copyright (C) 2008 Novell, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  26. * USA
  27. *
  28. *
  29. *****************************************************************************/
  30. /******************************************************************************
  31. *
  32. * Description of the device:
  33. *
  34. * Interface 0: Contains the IP network interface on the bulk end points.
  35. * The multiplexed serial ports are using the interrupt and
  36. * control endpoints.
  37. * Interrupt contains a bitmap telling which multiplexed
  38. * serialport needs servicing.
  39. *
  40. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  41. * port is opened, as this have a huge impact on the network port
  42. * throughput.
  43. *
  44. * Interface 2: Standard modem interface - circuit switched interface, this
  45. * can be used to make a standard ppp connection however it
  46. * should not be used in conjunction with the IP network interface
  47. * enabled for USB performance reasons i.e. if using this set
  48. * ideally disable_net=1.
  49. *
  50. *****************************************************************************/
  51. #include <linux/sched.h>
  52. #include <linux/slab.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/module.h>
  57. #include <linux/ethtool.h>
  58. #include <linux/usb.h>
  59. #include <linux/timer.h>
  60. #include <linux/tty.h>
  61. #include <linux/tty_driver.h>
  62. #include <linux/tty_flip.h>
  63. #include <linux/kmod.h>
  64. #include <linux/rfkill.h>
  65. #include <linux/ip.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/usb/cdc.h>
  68. #include <net/arp.h>
  69. #include <asm/byteorder.h>
  70. #include <linux/serial_core.h>
  71. #include <linux/serial.h>
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __packed;
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct work_struct reset_device;
  245. struct usb_device *usb;
  246. struct usb_interface *interface;
  247. struct device *dev;
  248. struct kref ref;
  249. struct mutex mutex;
  250. };
  251. /* Type of interface */
  252. #define HSO_INTF_MASK 0xFF00
  253. #define HSO_INTF_MUX 0x0100
  254. #define HSO_INTF_BULK 0x0200
  255. /* Type of port */
  256. #define HSO_PORT_MASK 0xFF
  257. #define HSO_PORT_NO_PORT 0x0
  258. #define HSO_PORT_CONTROL 0x1
  259. #define HSO_PORT_APP 0x2
  260. #define HSO_PORT_GPS 0x3
  261. #define HSO_PORT_PCSC 0x4
  262. #define HSO_PORT_APP2 0x5
  263. #define HSO_PORT_GPS_CONTROL 0x6
  264. #define HSO_PORT_MSD 0x7
  265. #define HSO_PORT_VOICE 0x8
  266. #define HSO_PORT_DIAG2 0x9
  267. #define HSO_PORT_DIAG 0x10
  268. #define HSO_PORT_MODEM 0x11
  269. #define HSO_PORT_NETWORK 0x12
  270. /* Additional device info */
  271. #define HSO_INFO_MASK 0xFF000000
  272. #define HSO_INFO_CRC_BUG 0x01000000
  273. /*****************************************************************************/
  274. /* Prototypes */
  275. /*****************************************************************************/
  276. /* Serial driver functions */
  277. static int hso_serial_tiocmset(struct tty_struct *tty,
  278. unsigned int set, unsigned int clear);
  279. static void ctrl_callback(struct urb *urb);
  280. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  281. static void hso_kick_transmit(struct hso_serial *serial);
  282. /* Helper functions */
  283. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  284. struct usb_device *usb, gfp_t gfp);
  285. static void handle_usb_error(int status, const char *function,
  286. struct hso_device *hso_dev);
  287. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  288. int type, int dir);
  289. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  290. static void hso_free_interface(struct usb_interface *intf);
  291. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  292. static int hso_stop_serial_device(struct hso_device *hso_dev);
  293. static int hso_start_net_device(struct hso_device *hso_dev);
  294. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  295. static int hso_stop_net_device(struct hso_device *hso_dev);
  296. static void hso_serial_ref_free(struct kref *ref);
  297. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  298. static int hso_mux_serial_read(struct hso_serial *serial);
  299. static void async_get_intf(struct work_struct *data);
  300. static void async_put_intf(struct work_struct *data);
  301. static int hso_put_activity(struct hso_device *hso_dev);
  302. static int hso_get_activity(struct hso_device *hso_dev);
  303. static void tiocmget_intr_callback(struct urb *urb);
  304. static void reset_device(struct work_struct *data);
  305. /*****************************************************************************/
  306. /* Helping functions */
  307. /*****************************************************************************/
  308. /* #define DEBUG */
  309. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  310. {
  311. return hso_dev->port_data.dev_net;
  312. }
  313. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  314. {
  315. return hso_dev->port_data.dev_serial;
  316. }
  317. /* Debugging functions */
  318. #ifdef DEBUG
  319. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  320. unsigned int len)
  321. {
  322. static char name[255];
  323. sprintf(name, "hso[%d:%s]", line_count, func_name);
  324. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  325. }
  326. #define DUMP(buf_, len_) \
  327. dbg_dump(__LINE__, __func__, (unsigned char *)buf_, len_)
  328. #define DUMP1(buf_, len_) \
  329. do { \
  330. if (0x01 & debug) \
  331. DUMP(buf_, len_); \
  332. } while (0)
  333. #else
  334. #define DUMP(buf_, len_)
  335. #define DUMP1(buf_, len_)
  336. #endif
  337. /* module parameters */
  338. static int debug;
  339. static int tty_major;
  340. static int disable_net;
  341. /* driver info */
  342. static const char driver_name[] = "hso";
  343. static const char tty_filename[] = "ttyHS";
  344. static const char *version = __FILE__ ": " MOD_AUTHOR;
  345. /* the usb driver itself (registered in hso_init) */
  346. static struct usb_driver hso_driver;
  347. /* serial structures */
  348. static struct tty_driver *tty_drv;
  349. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  350. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  351. static spinlock_t serial_table_lock;
  352. static const s32 default_port_spec[] = {
  353. HSO_INTF_MUX | HSO_PORT_NETWORK,
  354. HSO_INTF_BULK | HSO_PORT_DIAG,
  355. HSO_INTF_BULK | HSO_PORT_MODEM,
  356. 0
  357. };
  358. static const s32 icon321_port_spec[] = {
  359. HSO_INTF_MUX | HSO_PORT_NETWORK,
  360. HSO_INTF_BULK | HSO_PORT_DIAG2,
  361. HSO_INTF_BULK | HSO_PORT_MODEM,
  362. HSO_INTF_BULK | HSO_PORT_DIAG,
  363. 0
  364. };
  365. #define default_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)default_port_spec
  368. #define icon321_port_device(vendor, product) \
  369. USB_DEVICE(vendor, product), \
  370. .driver_info = (kernel_ulong_t)icon321_port_spec
  371. /* list of devices we support */
  372. static const struct usb_device_id hso_ids[] = {
  373. {default_port_device(0x0af0, 0x6711)},
  374. {default_port_device(0x0af0, 0x6731)},
  375. {default_port_device(0x0af0, 0x6751)},
  376. {default_port_device(0x0af0, 0x6771)},
  377. {default_port_device(0x0af0, 0x6791)},
  378. {default_port_device(0x0af0, 0x6811)},
  379. {default_port_device(0x0af0, 0x6911)},
  380. {default_port_device(0x0af0, 0x6951)},
  381. {default_port_device(0x0af0, 0x6971)},
  382. {default_port_device(0x0af0, 0x7011)},
  383. {default_port_device(0x0af0, 0x7031)},
  384. {default_port_device(0x0af0, 0x7051)},
  385. {default_port_device(0x0af0, 0x7071)},
  386. {default_port_device(0x0af0, 0x7111)},
  387. {default_port_device(0x0af0, 0x7211)},
  388. {default_port_device(0x0af0, 0x7251)},
  389. {default_port_device(0x0af0, 0x7271)},
  390. {default_port_device(0x0af0, 0x7311)},
  391. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  392. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  393. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  394. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  395. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  396. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  397. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  399. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  400. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  401. {USB_DEVICE(0x0af0, 0x7701)},
  402. {USB_DEVICE(0x0af0, 0x7706)},
  403. {USB_DEVICE(0x0af0, 0x7801)},
  404. {USB_DEVICE(0x0af0, 0x7901)},
  405. {USB_DEVICE(0x0af0, 0x7A01)},
  406. {USB_DEVICE(0x0af0, 0x7A05)},
  407. {USB_DEVICE(0x0af0, 0x8200)},
  408. {USB_DEVICE(0x0af0, 0x8201)},
  409. {USB_DEVICE(0x0af0, 0x8300)},
  410. {USB_DEVICE(0x0af0, 0x8302)},
  411. {USB_DEVICE(0x0af0, 0x8304)},
  412. {USB_DEVICE(0x0af0, 0x8400)},
  413. {USB_DEVICE(0x0af0, 0x8600)},
  414. {USB_DEVICE(0x0af0, 0x8800)},
  415. {USB_DEVICE(0x0af0, 0x8900)},
  416. {USB_DEVICE(0x0af0, 0x9000)},
  417. {USB_DEVICE(0x0af0, 0xd035)},
  418. {USB_DEVICE(0x0af0, 0xd055)},
  419. {USB_DEVICE(0x0af0, 0xd155)},
  420. {USB_DEVICE(0x0af0, 0xd255)},
  421. {USB_DEVICE(0x0af0, 0xd057)},
  422. {USB_DEVICE(0x0af0, 0xd157)},
  423. {USB_DEVICE(0x0af0, 0xd257)},
  424. {USB_DEVICE(0x0af0, 0xd357)},
  425. {USB_DEVICE(0x0af0, 0xd058)},
  426. {USB_DEVICE(0x0af0, 0xc100)},
  427. {}
  428. };
  429. MODULE_DEVICE_TABLE(usb, hso_ids);
  430. /* Sysfs attribute */
  431. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  432. struct device_attribute *attr,
  433. char *buf)
  434. {
  435. struct hso_device *hso_dev = dev_get_drvdata(dev);
  436. char *port_name;
  437. if (!hso_dev)
  438. return 0;
  439. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  440. case HSO_PORT_CONTROL:
  441. port_name = "Control";
  442. break;
  443. case HSO_PORT_APP:
  444. port_name = "Application";
  445. break;
  446. case HSO_PORT_APP2:
  447. port_name = "Application2";
  448. break;
  449. case HSO_PORT_GPS:
  450. port_name = "GPS";
  451. break;
  452. case HSO_PORT_GPS_CONTROL:
  453. port_name = "GPS Control";
  454. break;
  455. case HSO_PORT_PCSC:
  456. port_name = "PCSC";
  457. break;
  458. case HSO_PORT_DIAG:
  459. port_name = "Diagnostic";
  460. break;
  461. case HSO_PORT_DIAG2:
  462. port_name = "Diagnostic2";
  463. break;
  464. case HSO_PORT_MODEM:
  465. port_name = "Modem";
  466. break;
  467. case HSO_PORT_NETWORK:
  468. port_name = "Network";
  469. break;
  470. default:
  471. port_name = "Unknown";
  472. break;
  473. }
  474. return sprintf(buf, "%s\n", port_name);
  475. }
  476. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  477. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  478. {
  479. int idx;
  480. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  481. if (serial->rx_urb[idx] == urb)
  482. return idx;
  483. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  484. return -1;
  485. }
  486. /* converts mux value to a port spec value */
  487. static u32 hso_mux_to_port(int mux)
  488. {
  489. u32 result;
  490. switch (mux) {
  491. case 0x1:
  492. result = HSO_PORT_CONTROL;
  493. break;
  494. case 0x2:
  495. result = HSO_PORT_APP;
  496. break;
  497. case 0x4:
  498. result = HSO_PORT_PCSC;
  499. break;
  500. case 0x8:
  501. result = HSO_PORT_GPS;
  502. break;
  503. case 0x10:
  504. result = HSO_PORT_APP2;
  505. break;
  506. default:
  507. result = HSO_PORT_NO_PORT;
  508. }
  509. return result;
  510. }
  511. /* converts port spec value to a mux value */
  512. static u32 hso_port_to_mux(int port)
  513. {
  514. u32 result;
  515. switch (port & HSO_PORT_MASK) {
  516. case HSO_PORT_CONTROL:
  517. result = 0x0;
  518. break;
  519. case HSO_PORT_APP:
  520. result = 0x1;
  521. break;
  522. case HSO_PORT_PCSC:
  523. result = 0x2;
  524. break;
  525. case HSO_PORT_GPS:
  526. result = 0x3;
  527. break;
  528. case HSO_PORT_APP2:
  529. result = 0x4;
  530. break;
  531. default:
  532. result = 0x0;
  533. }
  534. return result;
  535. }
  536. static struct hso_serial *get_serial_by_shared_int_and_type(
  537. struct hso_shared_int *shared_int,
  538. int mux)
  539. {
  540. int i, port;
  541. port = hso_mux_to_port(mux);
  542. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  543. if (serial_table[i] &&
  544. (dev2ser(serial_table[i])->shared_int == shared_int) &&
  545. ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  546. return dev2ser(serial_table[i]);
  547. }
  548. }
  549. return NULL;
  550. }
  551. static struct hso_serial *get_serial_by_index(unsigned index)
  552. {
  553. struct hso_serial *serial = NULL;
  554. unsigned long flags;
  555. spin_lock_irqsave(&serial_table_lock, flags);
  556. if (serial_table[index])
  557. serial = dev2ser(serial_table[index]);
  558. spin_unlock_irqrestore(&serial_table_lock, flags);
  559. return serial;
  560. }
  561. static int get_free_serial_index(void)
  562. {
  563. int index;
  564. unsigned long flags;
  565. spin_lock_irqsave(&serial_table_lock, flags);
  566. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  567. if (serial_table[index] == NULL) {
  568. spin_unlock_irqrestore(&serial_table_lock, flags);
  569. return index;
  570. }
  571. }
  572. spin_unlock_irqrestore(&serial_table_lock, flags);
  573. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  574. return -1;
  575. }
  576. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  577. {
  578. unsigned long flags;
  579. spin_lock_irqsave(&serial_table_lock, flags);
  580. if (serial)
  581. serial_table[index] = serial->parent;
  582. else
  583. serial_table[index] = NULL;
  584. spin_unlock_irqrestore(&serial_table_lock, flags);
  585. }
  586. static void handle_usb_error(int status, const char *function,
  587. struct hso_device *hso_dev)
  588. {
  589. char *explanation;
  590. switch (status) {
  591. case -ENODEV:
  592. explanation = "no device";
  593. break;
  594. case -ENOENT:
  595. explanation = "endpoint not enabled";
  596. break;
  597. case -EPIPE:
  598. explanation = "endpoint stalled";
  599. break;
  600. case -ENOSPC:
  601. explanation = "not enough bandwidth";
  602. break;
  603. case -ESHUTDOWN:
  604. explanation = "device disabled";
  605. break;
  606. case -EHOSTUNREACH:
  607. explanation = "device suspended";
  608. break;
  609. case -EINVAL:
  610. case -EAGAIN:
  611. case -EFBIG:
  612. case -EMSGSIZE:
  613. explanation = "internal error";
  614. break;
  615. case -EILSEQ:
  616. case -EPROTO:
  617. case -ETIME:
  618. case -ETIMEDOUT:
  619. explanation = "protocol error";
  620. if (hso_dev)
  621. schedule_work(&hso_dev->reset_device);
  622. break;
  623. default:
  624. explanation = "unknown status";
  625. break;
  626. }
  627. /* log a meaningful explanation of an USB status */
  628. D1("%s: received USB status - %s (%d)", function, explanation, status);
  629. }
  630. /* Network interface functions */
  631. /* called when net interface is brought up by ifconfig */
  632. static int hso_net_open(struct net_device *net)
  633. {
  634. struct hso_net *odev = netdev_priv(net);
  635. unsigned long flags = 0;
  636. if (!odev) {
  637. dev_err(&net->dev, "No net device !\n");
  638. return -ENODEV;
  639. }
  640. odev->skb_tx_buf = NULL;
  641. /* setup environment */
  642. spin_lock_irqsave(&odev->net_lock, flags);
  643. odev->rx_parse_state = WAIT_IP;
  644. odev->rx_buf_size = 0;
  645. odev->rx_buf_missing = sizeof(struct iphdr);
  646. spin_unlock_irqrestore(&odev->net_lock, flags);
  647. /* We are up and running. */
  648. set_bit(HSO_NET_RUNNING, &odev->flags);
  649. hso_start_net_device(odev->parent);
  650. /* Tell the kernel we are ready to start receiving from it */
  651. netif_start_queue(net);
  652. return 0;
  653. }
  654. /* called when interface is brought down by ifconfig */
  655. static int hso_net_close(struct net_device *net)
  656. {
  657. struct hso_net *odev = netdev_priv(net);
  658. /* we don't need the queue anymore */
  659. netif_stop_queue(net);
  660. /* no longer running */
  661. clear_bit(HSO_NET_RUNNING, &odev->flags);
  662. hso_stop_net_device(odev->parent);
  663. /* done */
  664. return 0;
  665. }
  666. /* USB tells is xmit done, we should start the netqueue again */
  667. static void write_bulk_callback(struct urb *urb)
  668. {
  669. struct hso_net *odev = urb->context;
  670. int status = urb->status;
  671. /* Sanity check */
  672. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  673. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  674. return;
  675. }
  676. /* Do we still have a valid kernel network device? */
  677. if (!netif_device_present(odev->net)) {
  678. dev_err(&urb->dev->dev, "%s: net device not present\n",
  679. __func__);
  680. return;
  681. }
  682. /* log status, but don't act on it, we don't need to resubmit anything
  683. * anyhow */
  684. if (status)
  685. handle_usb_error(status, __func__, odev->parent);
  686. hso_put_activity(odev->parent);
  687. /* Tell the network interface we are ready for another frame */
  688. netif_wake_queue(odev->net);
  689. }
  690. /* called by kernel when we need to transmit a packet */
  691. static netdev_tx_t hso_net_start_xmit(struct sk_buff *skb,
  692. struct net_device *net)
  693. {
  694. struct hso_net *odev = netdev_priv(net);
  695. int result;
  696. /* Tell the kernel, "No more frames 'til we are done with this one." */
  697. netif_stop_queue(net);
  698. if (hso_get_activity(odev->parent) == -EAGAIN) {
  699. odev->skb_tx_buf = skb;
  700. return NETDEV_TX_OK;
  701. }
  702. /* log if asked */
  703. DUMP1(skb->data, skb->len);
  704. /* Copy it from kernel memory to OUR memory */
  705. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  706. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  707. /* Fill in the URB for shipping it out. */
  708. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  709. odev->parent->usb,
  710. usb_sndbulkpipe(odev->parent->usb,
  711. odev->out_endp->
  712. bEndpointAddress & 0x7F),
  713. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  714. odev);
  715. /* Deal with the Zero Length packet problem, I hope */
  716. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  717. /* Send the URB on its merry way. */
  718. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  719. if (result) {
  720. dev_warn(&odev->parent->interface->dev,
  721. "failed mux_bulk_tx_urb %d\n", result);
  722. net->stats.tx_errors++;
  723. netif_start_queue(net);
  724. } else {
  725. net->stats.tx_packets++;
  726. net->stats.tx_bytes += skb->len;
  727. }
  728. dev_kfree_skb(skb);
  729. /* we're done */
  730. return NETDEV_TX_OK;
  731. }
  732. static const struct ethtool_ops ops = {
  733. .get_link = ethtool_op_get_link
  734. };
  735. /* called when a packet did not ack after watchdogtimeout */
  736. static void hso_net_tx_timeout(struct net_device *net)
  737. {
  738. struct hso_net *odev = netdev_priv(net);
  739. if (!odev)
  740. return;
  741. /* Tell syslog we are hosed. */
  742. dev_warn(&net->dev, "Tx timed out.\n");
  743. /* Tear the waiting frame off the list */
  744. if (odev->mux_bulk_tx_urb &&
  745. (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  746. usb_unlink_urb(odev->mux_bulk_tx_urb);
  747. /* Update statistics */
  748. net->stats.tx_errors++;
  749. }
  750. /* make a real packet from the received USB buffer */
  751. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  752. unsigned int count, unsigned char is_eop)
  753. {
  754. unsigned short temp_bytes;
  755. unsigned short buffer_offset = 0;
  756. unsigned short frame_len;
  757. unsigned char *tmp_rx_buf;
  758. /* log if needed */
  759. D1("Rx %d bytes", count);
  760. DUMP(ip_pkt, min(128, (int)count));
  761. while (count) {
  762. switch (odev->rx_parse_state) {
  763. case WAIT_IP:
  764. /* waiting for IP header. */
  765. /* wanted bytes - size of ip header */
  766. temp_bytes =
  767. (count <
  768. odev->rx_buf_missing) ? count : odev->
  769. rx_buf_missing;
  770. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  771. odev->rx_buf_size, ip_pkt + buffer_offset,
  772. temp_bytes);
  773. odev->rx_buf_size += temp_bytes;
  774. buffer_offset += temp_bytes;
  775. odev->rx_buf_missing -= temp_bytes;
  776. count -= temp_bytes;
  777. if (!odev->rx_buf_missing) {
  778. /* header is complete allocate an sk_buffer and
  779. * continue to WAIT_DATA */
  780. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  781. if ((frame_len > DEFAULT_MRU) ||
  782. (frame_len < sizeof(struct iphdr))) {
  783. dev_err(&odev->net->dev,
  784. "Invalid frame (%d) length\n",
  785. frame_len);
  786. odev->rx_parse_state = WAIT_SYNC;
  787. continue;
  788. }
  789. /* Allocate an sk_buff */
  790. odev->skb_rx_buf = netdev_alloc_skb(odev->net,
  791. frame_len);
  792. if (!odev->skb_rx_buf) {
  793. /* We got no receive buffer. */
  794. D1("could not allocate memory");
  795. odev->rx_parse_state = WAIT_SYNC;
  796. return;
  797. }
  798. /* Copy what we got so far. make room for iphdr
  799. * after tail. */
  800. tmp_rx_buf =
  801. skb_put(odev->skb_rx_buf,
  802. sizeof(struct iphdr));
  803. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  804. sizeof(struct iphdr));
  805. /* ETH_HLEN */
  806. odev->rx_buf_size = sizeof(struct iphdr);
  807. /* Filip actually use .tot_len */
  808. odev->rx_buf_missing =
  809. frame_len - sizeof(struct iphdr);
  810. odev->rx_parse_state = WAIT_DATA;
  811. }
  812. break;
  813. case WAIT_DATA:
  814. temp_bytes = (count < odev->rx_buf_missing)
  815. ? count : odev->rx_buf_missing;
  816. /* Copy the rest of the bytes that are left in the
  817. * buffer into the waiting sk_buf. */
  818. /* Make room for temp_bytes after tail. */
  819. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  820. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  821. odev->rx_buf_missing -= temp_bytes;
  822. count -= temp_bytes;
  823. buffer_offset += temp_bytes;
  824. odev->rx_buf_size += temp_bytes;
  825. if (!odev->rx_buf_missing) {
  826. /* Packet is complete. Inject into stack. */
  827. /* We have IP packet here */
  828. odev->skb_rx_buf->protocol = cpu_to_be16(ETH_P_IP);
  829. skb_reset_mac_header(odev->skb_rx_buf);
  830. /* Ship it off to the kernel */
  831. netif_rx(odev->skb_rx_buf);
  832. /* No longer our buffer. */
  833. odev->skb_rx_buf = NULL;
  834. /* update out statistics */
  835. odev->net->stats.rx_packets++;
  836. odev->net->stats.rx_bytes += odev->rx_buf_size;
  837. odev->rx_buf_size = 0;
  838. odev->rx_buf_missing = sizeof(struct iphdr);
  839. odev->rx_parse_state = WAIT_IP;
  840. }
  841. break;
  842. case WAIT_SYNC:
  843. D1(" W_S");
  844. count = 0;
  845. break;
  846. default:
  847. D1(" ");
  848. count--;
  849. break;
  850. }
  851. }
  852. /* Recovery mechanism for WAIT_SYNC state. */
  853. if (is_eop) {
  854. if (odev->rx_parse_state == WAIT_SYNC) {
  855. odev->rx_parse_state = WAIT_IP;
  856. odev->rx_buf_size = 0;
  857. odev->rx_buf_missing = sizeof(struct iphdr);
  858. }
  859. }
  860. }
  861. static void fix_crc_bug(struct urb *urb, __le16 max_packet_size)
  862. {
  863. static const u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  864. u32 rest = urb->actual_length % le16_to_cpu(max_packet_size);
  865. if (((rest == 5) || (rest == 6)) &&
  866. !memcmp(((u8 *)urb->transfer_buffer) + urb->actual_length - 4,
  867. crc_check, 4)) {
  868. urb->actual_length -= 4;
  869. }
  870. }
  871. /* Moving data from usb to kernel (in interrupt state) */
  872. static void read_bulk_callback(struct urb *urb)
  873. {
  874. struct hso_net *odev = urb->context;
  875. struct net_device *net;
  876. int result;
  877. int status = urb->status;
  878. /* is al ok? (Filip: Who's Al ?) */
  879. if (status) {
  880. handle_usb_error(status, __func__, odev->parent);
  881. return;
  882. }
  883. /* Sanity check */
  884. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  885. D1("BULK IN callback but driver is not active!");
  886. return;
  887. }
  888. usb_mark_last_busy(urb->dev);
  889. net = odev->net;
  890. if (!netif_device_present(net)) {
  891. /* Somebody killed our network interface... */
  892. return;
  893. }
  894. if (odev->parent->port_spec & HSO_INFO_CRC_BUG)
  895. fix_crc_bug(urb, odev->in_endp->wMaxPacketSize);
  896. /* do we even have a packet? */
  897. if (urb->actual_length) {
  898. /* Handle the IP stream, add header and push it onto network
  899. * stack if the packet is complete. */
  900. spin_lock(&odev->net_lock);
  901. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  902. (urb->transfer_buffer_length >
  903. urb->actual_length) ? 1 : 0);
  904. spin_unlock(&odev->net_lock);
  905. }
  906. /* We are done with this URB, resubmit it. Prep the USB to wait for
  907. * another frame. Reuse same as received. */
  908. usb_fill_bulk_urb(urb,
  909. odev->parent->usb,
  910. usb_rcvbulkpipe(odev->parent->usb,
  911. odev->in_endp->
  912. bEndpointAddress & 0x7F),
  913. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  914. read_bulk_callback, odev);
  915. /* Give this to the USB subsystem so it can tell us when more data
  916. * arrives. */
  917. result = usb_submit_urb(urb, GFP_ATOMIC);
  918. if (result)
  919. dev_warn(&odev->parent->interface->dev,
  920. "%s failed submit mux_bulk_rx_urb %d\n", __func__,
  921. result);
  922. }
  923. /* Serial driver functions */
  924. static void hso_init_termios(struct ktermios *termios)
  925. {
  926. /*
  927. * The default requirements for this device are:
  928. */
  929. termios->c_iflag &=
  930. ~(IGNBRK /* disable ignore break */
  931. | BRKINT /* disable break causes interrupt */
  932. | PARMRK /* disable mark parity errors */
  933. | ISTRIP /* disable clear high bit of input characters */
  934. | INLCR /* disable translate NL to CR */
  935. | IGNCR /* disable ignore CR */
  936. | ICRNL /* disable translate CR to NL */
  937. | IXON); /* disable enable XON/XOFF flow control */
  938. /* disable postprocess output characters */
  939. termios->c_oflag &= ~OPOST;
  940. termios->c_lflag &=
  941. ~(ECHO /* disable echo input characters */
  942. | ECHONL /* disable echo new line */
  943. | ICANON /* disable erase, kill, werase, and rprnt
  944. special characters */
  945. | ISIG /* disable interrupt, quit, and suspend special
  946. characters */
  947. | IEXTEN); /* disable non-POSIX special characters */
  948. termios->c_cflag &=
  949. ~(CSIZE /* no size */
  950. | PARENB /* disable parity bit */
  951. | CBAUD /* clear current baud rate */
  952. | CBAUDEX); /* clear current buad rate */
  953. termios->c_cflag |= CS8; /* character size 8 bits */
  954. /* baud rate 115200 */
  955. tty_termios_encode_baud_rate(termios, 115200, 115200);
  956. }
  957. static void _hso_serial_set_termios(struct tty_struct *tty,
  958. struct ktermios *old)
  959. {
  960. struct hso_serial *serial = get_serial_by_tty(tty);
  961. struct ktermios *termios;
  962. if (!serial) {
  963. printk(KERN_ERR "%s: no tty structures", __func__);
  964. return;
  965. }
  966. D4("port %d", serial->minor);
  967. /*
  968. * Fix up unsupported bits
  969. */
  970. termios = tty->termios;
  971. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  972. termios->c_cflag &=
  973. ~(CSIZE /* no size */
  974. | PARENB /* disable parity bit */
  975. | CBAUD /* clear current baud rate */
  976. | CBAUDEX); /* clear current buad rate */
  977. termios->c_cflag |= CS8; /* character size 8 bits */
  978. /* baud rate 115200 */
  979. tty_encode_baud_rate(tty, 115200, 115200);
  980. }
  981. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  982. {
  983. int result;
  984. /* We are done with this URB, resubmit it. Prep the USB to wait for
  985. * another frame */
  986. usb_fill_bulk_urb(urb, serial->parent->usb,
  987. usb_rcvbulkpipe(serial->parent->usb,
  988. serial->in_endp->
  989. bEndpointAddress & 0x7F),
  990. urb->transfer_buffer, serial->rx_data_length,
  991. hso_std_serial_read_bulk_callback, serial);
  992. /* Give this to the USB subsystem so it can tell us when more data
  993. * arrives. */
  994. result = usb_submit_urb(urb, GFP_ATOMIC);
  995. if (result) {
  996. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  997. __func__, result);
  998. }
  999. }
  1000. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  1001. {
  1002. int count;
  1003. struct urb *curr_urb;
  1004. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  1005. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  1006. count = put_rxbuf_data(curr_urb, serial);
  1007. if (count == -1)
  1008. return;
  1009. if (count == 0) {
  1010. serial->curr_rx_urb_idx++;
  1011. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  1012. serial->curr_rx_urb_idx = 0;
  1013. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  1014. }
  1015. }
  1016. }
  1017. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1018. {
  1019. int count = 0;
  1020. struct urb *urb;
  1021. urb = serial->rx_urb[0];
  1022. if (serial->open_count > 0) {
  1023. count = put_rxbuf_data(urb, serial);
  1024. if (count == -1)
  1025. return;
  1026. }
  1027. /* Re issue a read as long as we receive data. */
  1028. if (count == 0 && ((urb->actual_length != 0) ||
  1029. (serial->rx_state == RX_PENDING))) {
  1030. serial->rx_state = RX_SENT;
  1031. hso_mux_serial_read(serial);
  1032. } else
  1033. serial->rx_state = RX_IDLE;
  1034. }
  1035. /* read callback for Diag and CS port */
  1036. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1037. {
  1038. struct hso_serial *serial = urb->context;
  1039. int status = urb->status;
  1040. /* sanity check */
  1041. if (!serial) {
  1042. D1("serial == NULL");
  1043. return;
  1044. } else if (status) {
  1045. handle_usb_error(status, __func__, serial->parent);
  1046. return;
  1047. }
  1048. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1049. D1("Actual length = %d\n", urb->actual_length);
  1050. DUMP1(urb->transfer_buffer, urb->actual_length);
  1051. /* Anyone listening? */
  1052. if (serial->open_count == 0)
  1053. return;
  1054. if (status == 0) {
  1055. if (serial->parent->port_spec & HSO_INFO_CRC_BUG)
  1056. fix_crc_bug(urb, serial->in_endp->wMaxPacketSize);
  1057. /* Valid data, handle RX data */
  1058. spin_lock(&serial->serial_lock);
  1059. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1060. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1061. spin_unlock(&serial->serial_lock);
  1062. } else if (status == -ENOENT || status == -ECONNRESET) {
  1063. /* Unlinked - check for throttled port. */
  1064. D2("Port %d, successfully unlinked urb", serial->minor);
  1065. spin_lock(&serial->serial_lock);
  1066. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1067. hso_resubmit_rx_bulk_urb(serial, urb);
  1068. spin_unlock(&serial->serial_lock);
  1069. } else {
  1070. D2("Port %d, status = %d for read urb", serial->minor, status);
  1071. return;
  1072. }
  1073. }
  1074. /*
  1075. * This needs to be a tasklet otherwise we will
  1076. * end up recursively calling this function.
  1077. */
  1078. static void hso_unthrottle_tasklet(struct hso_serial *serial)
  1079. {
  1080. unsigned long flags;
  1081. spin_lock_irqsave(&serial->serial_lock, flags);
  1082. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1083. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1084. else
  1085. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1086. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1087. }
  1088. static void hso_unthrottle(struct tty_struct *tty)
  1089. {
  1090. struct hso_serial *serial = get_serial_by_tty(tty);
  1091. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1092. }
  1093. static void hso_unthrottle_workfunc(struct work_struct *work)
  1094. {
  1095. struct hso_serial *serial =
  1096. container_of(work, struct hso_serial,
  1097. retry_unthrottle_workqueue);
  1098. hso_unthrottle_tasklet(serial);
  1099. }
  1100. /* open the requested serial port */
  1101. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1102. {
  1103. struct hso_serial *serial = get_serial_by_index(tty->index);
  1104. int result;
  1105. /* sanity check */
  1106. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1107. WARN_ON(1);
  1108. tty->driver_data = NULL;
  1109. D1("Failed to open port");
  1110. return -ENODEV;
  1111. }
  1112. mutex_lock(&serial->parent->mutex);
  1113. result = usb_autopm_get_interface(serial->parent->interface);
  1114. if (result < 0)
  1115. goto err_out;
  1116. D1("Opening %d", serial->minor);
  1117. kref_get(&serial->parent->ref);
  1118. /* setup */
  1119. spin_lock_irq(&serial->serial_lock);
  1120. tty->driver_data = serial;
  1121. tty_kref_put(serial->tty);
  1122. serial->tty = tty_kref_get(tty);
  1123. spin_unlock_irq(&serial->serial_lock);
  1124. /* check for port already opened, if not set the termios */
  1125. serial->open_count++;
  1126. if (serial->open_count == 1) {
  1127. serial->rx_state = RX_IDLE;
  1128. /* Force default termio settings */
  1129. _hso_serial_set_termios(tty, NULL);
  1130. tasklet_init(&serial->unthrottle_tasklet,
  1131. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1132. (unsigned long)serial);
  1133. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1134. hso_unthrottle_workfunc);
  1135. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1136. if (result) {
  1137. hso_stop_serial_device(serial->parent);
  1138. serial->open_count--;
  1139. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1140. }
  1141. } else {
  1142. D1("Port was already open");
  1143. }
  1144. usb_autopm_put_interface(serial->parent->interface);
  1145. /* done */
  1146. if (result)
  1147. hso_serial_tiocmset(tty, TIOCM_RTS | TIOCM_DTR, 0);
  1148. err_out:
  1149. mutex_unlock(&serial->parent->mutex);
  1150. return result;
  1151. }
  1152. /* close the requested serial port */
  1153. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1154. {
  1155. struct hso_serial *serial = tty->driver_data;
  1156. u8 usb_gone;
  1157. D1("Closing serial port");
  1158. /* Open failed, no close cleanup required */
  1159. if (serial == NULL)
  1160. return;
  1161. mutex_lock(&serial->parent->mutex);
  1162. usb_gone = serial->parent->usb_gone;
  1163. if (!usb_gone)
  1164. usb_autopm_get_interface(serial->parent->interface);
  1165. /* reset the rts and dtr */
  1166. /* do the actual close */
  1167. serial->open_count--;
  1168. if (serial->open_count <= 0) {
  1169. serial->open_count = 0;
  1170. spin_lock_irq(&serial->serial_lock);
  1171. if (serial->tty == tty) {
  1172. serial->tty->driver_data = NULL;
  1173. serial->tty = NULL;
  1174. tty_kref_put(tty);
  1175. }
  1176. spin_unlock_irq(&serial->serial_lock);
  1177. if (!usb_gone)
  1178. hso_stop_serial_device(serial->parent);
  1179. tasklet_kill(&serial->unthrottle_tasklet);
  1180. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1181. }
  1182. if (!usb_gone)
  1183. usb_autopm_put_interface(serial->parent->interface);
  1184. mutex_unlock(&serial->parent->mutex);
  1185. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1186. }
  1187. /* close the requested serial port */
  1188. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1189. int count)
  1190. {
  1191. struct hso_serial *serial = get_serial_by_tty(tty);
  1192. int space, tx_bytes;
  1193. unsigned long flags;
  1194. /* sanity check */
  1195. if (serial == NULL) {
  1196. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1197. return -ENODEV;
  1198. }
  1199. spin_lock_irqsave(&serial->serial_lock, flags);
  1200. space = serial->tx_data_length - serial->tx_buffer_count;
  1201. tx_bytes = (count < space) ? count : space;
  1202. if (!tx_bytes)
  1203. goto out;
  1204. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1205. serial->tx_buffer_count += tx_bytes;
  1206. out:
  1207. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1208. hso_kick_transmit(serial);
  1209. /* done */
  1210. return tx_bytes;
  1211. }
  1212. /* how much room is there for writing */
  1213. static int hso_serial_write_room(struct tty_struct *tty)
  1214. {
  1215. struct hso_serial *serial = get_serial_by_tty(tty);
  1216. int room;
  1217. unsigned long flags;
  1218. spin_lock_irqsave(&serial->serial_lock, flags);
  1219. room = serial->tx_data_length - serial->tx_buffer_count;
  1220. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1221. /* return free room */
  1222. return room;
  1223. }
  1224. /* setup the term */
  1225. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1226. {
  1227. struct hso_serial *serial = get_serial_by_tty(tty);
  1228. unsigned long flags;
  1229. if (old)
  1230. D5("Termios called with: cflags new[%d] - old[%d]",
  1231. tty->termios->c_cflag, old->c_cflag);
  1232. /* the actual setup */
  1233. spin_lock_irqsave(&serial->serial_lock, flags);
  1234. if (serial->open_count)
  1235. _hso_serial_set_termios(tty, old);
  1236. else
  1237. tty->termios = old;
  1238. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1239. /* done */
  1240. }
  1241. /* how many characters in the buffer */
  1242. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1243. {
  1244. struct hso_serial *serial = get_serial_by_tty(tty);
  1245. int chars;
  1246. unsigned long flags;
  1247. /* sanity check */
  1248. if (serial == NULL)
  1249. return 0;
  1250. spin_lock_irqsave(&serial->serial_lock, flags);
  1251. chars = serial->tx_buffer_count;
  1252. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1253. return chars;
  1254. }
  1255. static int tiocmget_submit_urb(struct hso_serial *serial,
  1256. struct hso_tiocmget *tiocmget,
  1257. struct usb_device *usb)
  1258. {
  1259. int result;
  1260. if (serial->parent->usb_gone)
  1261. return -ENODEV;
  1262. usb_fill_int_urb(tiocmget->urb, usb,
  1263. usb_rcvintpipe(usb,
  1264. tiocmget->endp->
  1265. bEndpointAddress & 0x7F),
  1266. &tiocmget->serial_state_notification,
  1267. sizeof(struct hso_serial_state_notification),
  1268. tiocmget_intr_callback, serial,
  1269. tiocmget->endp->bInterval);
  1270. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1271. if (result) {
  1272. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1273. result);
  1274. }
  1275. return result;
  1276. }
  1277. static void tiocmget_intr_callback(struct urb *urb)
  1278. {
  1279. struct hso_serial *serial = urb->context;
  1280. struct hso_tiocmget *tiocmget;
  1281. int status = urb->status;
  1282. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1283. struct uart_icount *icount;
  1284. struct hso_serial_state_notification *serial_state_notification;
  1285. struct usb_device *usb;
  1286. /* Sanity checks */
  1287. if (!serial)
  1288. return;
  1289. if (status) {
  1290. handle_usb_error(status, __func__, serial->parent);
  1291. return;
  1292. }
  1293. tiocmget = serial->tiocmget;
  1294. if (!tiocmget)
  1295. return;
  1296. usb = serial->parent->usb;
  1297. serial_state_notification = &tiocmget->serial_state_notification;
  1298. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1299. serial_state_notification->bNotification != B_NOTIFICATION ||
  1300. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1301. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1302. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1303. dev_warn(&usb->dev,
  1304. "hso received invalid serial state notification\n");
  1305. DUMP(serial_state_notification,
  1306. sizeof(struct hso_serial_state_notification));
  1307. } else {
  1308. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1309. UART_state_bitmap);
  1310. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1311. icount = &tiocmget->icount;
  1312. spin_lock(&serial->serial_lock);
  1313. if ((UART_state_bitmap & B_OVERRUN) !=
  1314. (prev_UART_state_bitmap & B_OVERRUN))
  1315. icount->parity++;
  1316. if ((UART_state_bitmap & B_PARITY) !=
  1317. (prev_UART_state_bitmap & B_PARITY))
  1318. icount->parity++;
  1319. if ((UART_state_bitmap & B_FRAMING) !=
  1320. (prev_UART_state_bitmap & B_FRAMING))
  1321. icount->frame++;
  1322. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1323. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1324. icount->rng++;
  1325. if ((UART_state_bitmap & B_BREAK) !=
  1326. (prev_UART_state_bitmap & B_BREAK))
  1327. icount->brk++;
  1328. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1329. (prev_UART_state_bitmap & B_TX_CARRIER))
  1330. icount->dsr++;
  1331. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1332. (prev_UART_state_bitmap & B_RX_CARRIER))
  1333. icount->dcd++;
  1334. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1335. spin_unlock(&serial->serial_lock);
  1336. tiocmget->intr_completed = 1;
  1337. wake_up_interruptible(&tiocmget->waitq);
  1338. }
  1339. memset(serial_state_notification, 0,
  1340. sizeof(struct hso_serial_state_notification));
  1341. tiocmget_submit_urb(serial,
  1342. tiocmget,
  1343. serial->parent->usb);
  1344. }
  1345. /*
  1346. * next few functions largely stolen from drivers/serial/serial_core.c
  1347. */
  1348. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1349. * - mask passed in arg for lines of interest
  1350. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1351. * Caller should use TIOCGICOUNT to see which one it was
  1352. */
  1353. static int
  1354. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1355. {
  1356. DECLARE_WAITQUEUE(wait, current);
  1357. struct uart_icount cprev, cnow;
  1358. struct hso_tiocmget *tiocmget;
  1359. int ret;
  1360. tiocmget = serial->tiocmget;
  1361. if (!tiocmget)
  1362. return -ENOENT;
  1363. /*
  1364. * note the counters on entry
  1365. */
  1366. spin_lock_irq(&serial->serial_lock);
  1367. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1368. spin_unlock_irq(&serial->serial_lock);
  1369. add_wait_queue(&tiocmget->waitq, &wait);
  1370. for (;;) {
  1371. spin_lock_irq(&serial->serial_lock);
  1372. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1373. spin_unlock_irq(&serial->serial_lock);
  1374. set_current_state(TASK_INTERRUPTIBLE);
  1375. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1376. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1377. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1378. ret = 0;
  1379. break;
  1380. }
  1381. schedule();
  1382. /* see if a signal did it */
  1383. if (signal_pending(current)) {
  1384. ret = -ERESTARTSYS;
  1385. break;
  1386. }
  1387. cprev = cnow;
  1388. }
  1389. current->state = TASK_RUNNING;
  1390. remove_wait_queue(&tiocmget->waitq, &wait);
  1391. return ret;
  1392. }
  1393. /*
  1394. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1395. * Return: write counters to the user passed counter struct
  1396. * NB: both 1->0 and 0->1 transitions are counted except for
  1397. * RI where only 0->1 is counted.
  1398. */
  1399. static int hso_get_count(struct tty_struct *tty,
  1400. struct serial_icounter_struct *icount)
  1401. {
  1402. struct uart_icount cnow;
  1403. struct hso_serial *serial = get_serial_by_tty(tty);
  1404. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1405. memset(&icount, 0, sizeof(struct serial_icounter_struct));
  1406. if (!tiocmget)
  1407. return -ENOENT;
  1408. spin_lock_irq(&serial->serial_lock);
  1409. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1410. spin_unlock_irq(&serial->serial_lock);
  1411. icount->cts = cnow.cts;
  1412. icount->dsr = cnow.dsr;
  1413. icount->rng = cnow.rng;
  1414. icount->dcd = cnow.dcd;
  1415. icount->rx = cnow.rx;
  1416. icount->tx = cnow.tx;
  1417. icount->frame = cnow.frame;
  1418. icount->overrun = cnow.overrun;
  1419. icount->parity = cnow.parity;
  1420. icount->brk = cnow.brk;
  1421. icount->buf_overrun = cnow.buf_overrun;
  1422. return 0;
  1423. }
  1424. static int hso_serial_tiocmget(struct tty_struct *tty)
  1425. {
  1426. int retval;
  1427. struct hso_serial *serial = get_serial_by_tty(tty);
  1428. struct hso_tiocmget *tiocmget;
  1429. u16 UART_state_bitmap;
  1430. /* sanity check */
  1431. if (!serial) {
  1432. D1("no tty structures");
  1433. return -EINVAL;
  1434. }
  1435. spin_lock_irq(&serial->serial_lock);
  1436. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1437. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1438. tiocmget = serial->tiocmget;
  1439. if (tiocmget) {
  1440. UART_state_bitmap = le16_to_cpu(
  1441. tiocmget->prev_UART_state_bitmap);
  1442. if (UART_state_bitmap & B_RING_SIGNAL)
  1443. retval |= TIOCM_RNG;
  1444. if (UART_state_bitmap & B_RX_CARRIER)
  1445. retval |= TIOCM_CD;
  1446. if (UART_state_bitmap & B_TX_CARRIER)
  1447. retval |= TIOCM_DSR;
  1448. }
  1449. spin_unlock_irq(&serial->serial_lock);
  1450. return retval;
  1451. }
  1452. static int hso_serial_tiocmset(struct tty_struct *tty,
  1453. unsigned int set, unsigned int clear)
  1454. {
  1455. int val = 0;
  1456. unsigned long flags;
  1457. int if_num;
  1458. struct hso_serial *serial = get_serial_by_tty(tty);
  1459. /* sanity check */
  1460. if (!serial) {
  1461. D1("no tty structures");
  1462. return -EINVAL;
  1463. }
  1464. if ((serial->parent->port_spec & HSO_PORT_MASK) != HSO_PORT_MODEM)
  1465. return -EINVAL;
  1466. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1467. spin_lock_irqsave(&serial->serial_lock, flags);
  1468. if (set & TIOCM_RTS)
  1469. serial->rts_state = 1;
  1470. if (set & TIOCM_DTR)
  1471. serial->dtr_state = 1;
  1472. if (clear & TIOCM_RTS)
  1473. serial->rts_state = 0;
  1474. if (clear & TIOCM_DTR)
  1475. serial->dtr_state = 0;
  1476. if (serial->dtr_state)
  1477. val |= 0x01;
  1478. if (serial->rts_state)
  1479. val |= 0x02;
  1480. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1481. return usb_control_msg(serial->parent->usb,
  1482. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1483. 0x21, val, if_num, NULL, 0,
  1484. USB_CTRL_SET_TIMEOUT);
  1485. }
  1486. static int hso_serial_ioctl(struct tty_struct *tty,
  1487. unsigned int cmd, unsigned long arg)
  1488. {
  1489. struct hso_serial *serial = get_serial_by_tty(tty);
  1490. int ret = 0;
  1491. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1492. if (!serial)
  1493. return -ENODEV;
  1494. switch (cmd) {
  1495. case TIOCMIWAIT:
  1496. ret = hso_wait_modem_status(serial, arg);
  1497. break;
  1498. default:
  1499. ret = -ENOIOCTLCMD;
  1500. break;
  1501. }
  1502. return ret;
  1503. }
  1504. /* starts a transmit */
  1505. static void hso_kick_transmit(struct hso_serial *serial)
  1506. {
  1507. u8 *temp;
  1508. unsigned long flags;
  1509. int res;
  1510. spin_lock_irqsave(&serial->serial_lock, flags);
  1511. if (!serial->tx_buffer_count)
  1512. goto out;
  1513. if (serial->tx_urb_used)
  1514. goto out;
  1515. /* Wakeup USB interface if necessary */
  1516. if (hso_get_activity(serial->parent) == -EAGAIN)
  1517. goto out;
  1518. /* Switch pointers around to avoid memcpy */
  1519. temp = serial->tx_buffer;
  1520. serial->tx_buffer = serial->tx_data;
  1521. serial->tx_data = temp;
  1522. serial->tx_data_count = serial->tx_buffer_count;
  1523. serial->tx_buffer_count = 0;
  1524. /* If temp is set, it means we switched buffers */
  1525. if (temp && serial->write_data) {
  1526. res = serial->write_data(serial);
  1527. if (res >= 0)
  1528. serial->tx_urb_used = 1;
  1529. }
  1530. out:
  1531. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1532. }
  1533. /* make a request (for reading and writing data to muxed serial port) */
  1534. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1535. struct urb *ctrl_urb,
  1536. struct usb_ctrlrequest *ctrl_req,
  1537. u8 *ctrl_urb_data, u32 size)
  1538. {
  1539. int result;
  1540. int pipe;
  1541. /* Sanity check */
  1542. if (!serial || !ctrl_urb || !ctrl_req) {
  1543. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1544. return -EINVAL;
  1545. }
  1546. /* initialize */
  1547. ctrl_req->wValue = 0;
  1548. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1549. ctrl_req->wLength = cpu_to_le16(size);
  1550. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1551. /* Reading command */
  1552. ctrl_req->bRequestType = USB_DIR_IN |
  1553. USB_TYPE_OPTION_VENDOR |
  1554. USB_RECIP_INTERFACE;
  1555. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1556. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1557. } else {
  1558. /* Writing command */
  1559. ctrl_req->bRequestType = USB_DIR_OUT |
  1560. USB_TYPE_OPTION_VENDOR |
  1561. USB_RECIP_INTERFACE;
  1562. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1563. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1564. }
  1565. /* syslog */
  1566. D2("%s command (%02x) len: %d, port: %d",
  1567. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1568. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1569. /* Load ctrl urb */
  1570. ctrl_urb->transfer_flags = 0;
  1571. usb_fill_control_urb(ctrl_urb,
  1572. serial->parent->usb,
  1573. pipe,
  1574. (u8 *) ctrl_req,
  1575. ctrl_urb_data, size, ctrl_callback, serial);
  1576. /* Send it on merry way */
  1577. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1578. if (result) {
  1579. dev_err(&ctrl_urb->dev->dev,
  1580. "%s failed submit ctrl_urb %d type %d\n", __func__,
  1581. result, type);
  1582. return result;
  1583. }
  1584. /* done */
  1585. return size;
  1586. }
  1587. /* called by intr_callback when read occurs */
  1588. static int hso_mux_serial_read(struct hso_serial *serial)
  1589. {
  1590. if (!serial)
  1591. return -EINVAL;
  1592. /* clean data */
  1593. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1594. /* make the request */
  1595. if (serial->num_rx_urbs != 1) {
  1596. dev_err(&serial->parent->interface->dev,
  1597. "ERROR: mux'd reads with multiple buffers "
  1598. "not possible\n");
  1599. return 0;
  1600. }
  1601. return mux_device_request(serial,
  1602. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1603. serial->parent->port_spec & HSO_PORT_MASK,
  1604. serial->rx_urb[0],
  1605. &serial->ctrl_req_rx,
  1606. serial->rx_data[0], serial->rx_data_length);
  1607. }
  1608. /* used for muxed serial port callback (muxed serial read) */
  1609. static void intr_callback(struct urb *urb)
  1610. {
  1611. struct hso_shared_int *shared_int = urb->context;
  1612. struct hso_serial *serial;
  1613. unsigned char *port_req;
  1614. int status = urb->status;
  1615. int i;
  1616. usb_mark_last_busy(urb->dev);
  1617. /* sanity check */
  1618. if (!shared_int)
  1619. return;
  1620. /* status check */
  1621. if (status) {
  1622. handle_usb_error(status, __func__, NULL);
  1623. return;
  1624. }
  1625. D4("\n--- Got intr callback 0x%02X ---", status);
  1626. /* what request? */
  1627. port_req = urb->transfer_buffer;
  1628. D4(" port_req = 0x%.2X\n", *port_req);
  1629. /* loop over all muxed ports to find the one sending this */
  1630. for (i = 0; i < 8; i++) {
  1631. /* max 8 channels on MUX */
  1632. if (*port_req & (1 << i)) {
  1633. serial = get_serial_by_shared_int_and_type(shared_int,
  1634. (1 << i));
  1635. if (serial != NULL) {
  1636. D1("Pending read interrupt on port %d\n", i);
  1637. spin_lock(&serial->serial_lock);
  1638. if (serial->rx_state == RX_IDLE &&
  1639. serial->open_count > 0) {
  1640. /* Setup and send a ctrl req read on
  1641. * port i */
  1642. if (!serial->rx_urb_filled[0]) {
  1643. serial->rx_state = RX_SENT;
  1644. hso_mux_serial_read(serial);
  1645. } else
  1646. serial->rx_state = RX_PENDING;
  1647. } else {
  1648. D1("Already a read pending on "
  1649. "port %d or port not open\n", i);
  1650. }
  1651. spin_unlock(&serial->serial_lock);
  1652. }
  1653. }
  1654. }
  1655. /* Resubmit interrupt urb */
  1656. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1657. }
  1658. /* called for writing to muxed serial port */
  1659. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1660. {
  1661. if (NULL == serial)
  1662. return -EINVAL;
  1663. return mux_device_request(serial,
  1664. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1665. serial->parent->port_spec & HSO_PORT_MASK,
  1666. serial->tx_urb,
  1667. &serial->ctrl_req_tx,
  1668. serial->tx_data, serial->tx_data_count);
  1669. }
  1670. /* write callback for Diag and CS port */
  1671. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1672. {
  1673. struct hso_serial *serial = urb->context;
  1674. int status = urb->status;
  1675. struct tty_struct *tty;
  1676. /* sanity check */
  1677. if (!serial) {
  1678. D1("serial == NULL");
  1679. return;
  1680. }
  1681. spin_lock(&serial->serial_lock);
  1682. serial->tx_urb_used = 0;
  1683. tty = tty_kref_get(serial->tty);
  1684. spin_unlock(&serial->serial_lock);
  1685. if (status) {
  1686. handle_usb_error(status, __func__, serial->parent);
  1687. tty_kref_put(tty);
  1688. return;
  1689. }
  1690. hso_put_activity(serial->parent);
  1691. if (tty) {
  1692. tty_wakeup(tty);
  1693. tty_kref_put(tty);
  1694. }
  1695. hso_kick_transmit(serial);
  1696. D1(" ");
  1697. }
  1698. /* called for writing diag or CS serial port */
  1699. static int hso_std_serial_write_data(struct hso_serial *serial)
  1700. {
  1701. int count = serial->tx_data_count;
  1702. int result;
  1703. usb_fill_bulk_urb(serial->tx_urb,
  1704. serial->parent->usb,
  1705. usb_sndbulkpipe(serial->parent->usb,
  1706. serial->out_endp->
  1707. bEndpointAddress & 0x7F),
  1708. serial->tx_data, serial->tx_data_count,
  1709. hso_std_serial_write_bulk_callback, serial);
  1710. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1711. if (result) {
  1712. dev_warn(&serial->parent->usb->dev,
  1713. "Failed to submit urb - res %d\n", result);
  1714. return result;
  1715. }
  1716. return count;
  1717. }
  1718. /* callback after read or write on muxed serial port */
  1719. static void ctrl_callback(struct urb *urb)
  1720. {
  1721. struct hso_serial *serial = urb->context;
  1722. struct usb_ctrlrequest *req;
  1723. int status = urb->status;
  1724. struct tty_struct *tty;
  1725. /* sanity check */
  1726. if (!serial)
  1727. return;
  1728. spin_lock(&serial->serial_lock);
  1729. serial->tx_urb_used = 0;
  1730. tty = tty_kref_get(serial->tty);
  1731. spin_unlock(&serial->serial_lock);
  1732. if (status) {
  1733. handle_usb_error(status, __func__, serial->parent);
  1734. tty_kref_put(tty);
  1735. return;
  1736. }
  1737. /* what request? */
  1738. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1739. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1740. D4("Actual length of urb = %d\n", urb->actual_length);
  1741. DUMP1(urb->transfer_buffer, urb->actual_length);
  1742. if (req->bRequestType ==
  1743. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1744. /* response to a read command */
  1745. serial->rx_urb_filled[0] = 1;
  1746. spin_lock(&serial->serial_lock);
  1747. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1748. spin_unlock(&serial->serial_lock);
  1749. } else {
  1750. hso_put_activity(serial->parent);
  1751. if (tty)
  1752. tty_wakeup(tty);
  1753. /* response to a write command */
  1754. hso_kick_transmit(serial);
  1755. }
  1756. tty_kref_put(tty);
  1757. }
  1758. /* handle RX data for serial port */
  1759. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1760. {
  1761. struct tty_struct *tty;
  1762. int write_length_remaining = 0;
  1763. int curr_write_len;
  1764. /* Sanity check */
  1765. if (urb == NULL || serial == NULL) {
  1766. D1("serial = NULL");
  1767. return -2;
  1768. }
  1769. /* All callers to put_rxbuf_data hold serial_lock */
  1770. tty = tty_kref_get(serial->tty);
  1771. /* Push data to tty */
  1772. if (tty) {
  1773. write_length_remaining = urb->actual_length -
  1774. serial->curr_rx_urb_offset;
  1775. D1("data to push to tty");
  1776. while (write_length_remaining) {
  1777. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1778. tty_kref_put(tty);
  1779. return -1;
  1780. }
  1781. curr_write_len = tty_insert_flip_string
  1782. (tty, urb->transfer_buffer +
  1783. serial->curr_rx_urb_offset,
  1784. write_length_remaining);
  1785. serial->curr_rx_urb_offset += curr_write_len;
  1786. write_length_remaining -= curr_write_len;
  1787. tty_flip_buffer_push(tty);
  1788. }
  1789. }
  1790. if (write_length_remaining == 0) {
  1791. serial->curr_rx_urb_offset = 0;
  1792. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1793. }
  1794. tty_kref_put(tty);
  1795. return write_length_remaining;
  1796. }
  1797. /* Base driver functions */
  1798. static void hso_log_port(struct hso_device *hso_dev)
  1799. {
  1800. char *port_type;
  1801. char port_dev[20];
  1802. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1803. case HSO_PORT_CONTROL:
  1804. port_type = "Control";
  1805. break;
  1806. case HSO_PORT_APP:
  1807. port_type = "Application";
  1808. break;
  1809. case HSO_PORT_GPS:
  1810. port_type = "GPS";
  1811. break;
  1812. case HSO_PORT_GPS_CONTROL:
  1813. port_type = "GPS control";
  1814. break;
  1815. case HSO_PORT_APP2:
  1816. port_type = "Application2";
  1817. break;
  1818. case HSO_PORT_PCSC:
  1819. port_type = "PCSC";
  1820. break;
  1821. case HSO_PORT_DIAG:
  1822. port_type = "Diagnostic";
  1823. break;
  1824. case HSO_PORT_DIAG2:
  1825. port_type = "Diagnostic2";
  1826. break;
  1827. case HSO_PORT_MODEM:
  1828. port_type = "Modem";
  1829. break;
  1830. case HSO_PORT_NETWORK:
  1831. port_type = "Network";
  1832. break;
  1833. default:
  1834. port_type = "Unknown";
  1835. break;
  1836. }
  1837. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1838. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1839. } else
  1840. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1841. dev2ser(hso_dev)->minor);
  1842. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1843. port_type, port_dev);
  1844. }
  1845. static int hso_start_net_device(struct hso_device *hso_dev)
  1846. {
  1847. int i, result = 0;
  1848. struct hso_net *hso_net = dev2net(hso_dev);
  1849. if (!hso_net)
  1850. return -ENODEV;
  1851. /* send URBs for all read buffers */
  1852. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1853. /* Prep a receive URB */
  1854. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1855. hso_dev->usb,
  1856. usb_rcvbulkpipe(hso_dev->usb,
  1857. hso_net->in_endp->
  1858. bEndpointAddress & 0x7F),
  1859. hso_net->mux_bulk_rx_buf_pool[i],
  1860. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1861. hso_net);
  1862. /* Put it out there so the device can send us stuff */
  1863. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1864. GFP_NOIO);
  1865. if (result)
  1866. dev_warn(&hso_dev->usb->dev,
  1867. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1868. i, result);
  1869. }
  1870. return result;
  1871. }
  1872. static int hso_stop_net_device(struct hso_device *hso_dev)
  1873. {
  1874. int i;
  1875. struct hso_net *hso_net = dev2net(hso_dev);
  1876. if (!hso_net)
  1877. return -ENODEV;
  1878. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1879. if (hso_net->mux_bulk_rx_urb_pool[i])
  1880. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1881. }
  1882. if (hso_net->mux_bulk_tx_urb)
  1883. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1884. return 0;
  1885. }
  1886. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1887. {
  1888. int i, result = 0;
  1889. struct hso_serial *serial = dev2ser(hso_dev);
  1890. if (!serial)
  1891. return -ENODEV;
  1892. /* If it is not the MUX port fill in and submit a bulk urb (already
  1893. * allocated in hso_serial_start) */
  1894. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1895. for (i = 0; i < serial->num_rx_urbs; i++) {
  1896. usb_fill_bulk_urb(serial->rx_urb[i],
  1897. serial->parent->usb,
  1898. usb_rcvbulkpipe(serial->parent->usb,
  1899. serial->in_endp->
  1900. bEndpointAddress &
  1901. 0x7F),
  1902. serial->rx_data[i],
  1903. serial->rx_data_length,
  1904. hso_std_serial_read_bulk_callback,
  1905. serial);
  1906. result = usb_submit_urb(serial->rx_urb[i], flags);
  1907. if (result) {
  1908. dev_warn(&serial->parent->usb->dev,
  1909. "Failed to submit urb - res %d\n",
  1910. result);
  1911. break;
  1912. }
  1913. }
  1914. } else {
  1915. mutex_lock(&serial->shared_int->shared_int_lock);
  1916. if (!serial->shared_int->use_count) {
  1917. result =
  1918. hso_mux_submit_intr_urb(serial->shared_int,
  1919. hso_dev->usb, flags);
  1920. }
  1921. serial->shared_int->use_count++;
  1922. mutex_unlock(&serial->shared_int->shared_int_lock);
  1923. }
  1924. if (serial->tiocmget)
  1925. tiocmget_submit_urb(serial,
  1926. serial->tiocmget,
  1927. serial->parent->usb);
  1928. return result;
  1929. }
  1930. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1931. {
  1932. int i;
  1933. struct hso_serial *serial = dev2ser(hso_dev);
  1934. struct hso_tiocmget *tiocmget;
  1935. if (!serial)
  1936. return -ENODEV;
  1937. for (i = 0; i < serial->num_rx_urbs; i++) {
  1938. if (serial->rx_urb[i]) {
  1939. usb_kill_urb(serial->rx_urb[i]);
  1940. serial->rx_urb_filled[i] = 0;
  1941. }
  1942. }
  1943. serial->curr_rx_urb_idx = 0;
  1944. serial->curr_rx_urb_offset = 0;
  1945. if (serial->tx_urb)
  1946. usb_kill_urb(serial->tx_urb);
  1947. if (serial->shared_int) {
  1948. mutex_lock(&serial->shared_int->shared_int_lock);
  1949. if (serial->shared_int->use_count &&
  1950. (--serial->shared_int->use_count == 0)) {
  1951. struct urb *urb;
  1952. urb = serial->shared_int->shared_intr_urb;
  1953. if (urb)
  1954. usb_kill_urb(urb);
  1955. }
  1956. mutex_unlock(&serial->shared_int->shared_int_lock);
  1957. }
  1958. tiocmget = serial->tiocmget;
  1959. if (tiocmget) {
  1960. wake_up_interruptible(&tiocmget->waitq);
  1961. usb_kill_urb(tiocmget->urb);
  1962. }
  1963. return 0;
  1964. }
  1965. static void hso_serial_common_free(struct hso_serial *serial)
  1966. {
  1967. int i;
  1968. if (serial->parent->dev)
  1969. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1970. tty_unregister_device(tty_drv, serial->minor);
  1971. for (i = 0; i < serial->num_rx_urbs; i++) {
  1972. /* unlink and free RX URB */
  1973. usb_free_urb(serial->rx_urb[i]);
  1974. /* free the RX buffer */
  1975. kfree(serial->rx_data[i]);
  1976. }
  1977. /* unlink and free TX URB */
  1978. usb_free_urb(serial->tx_urb);
  1979. kfree(serial->tx_data);
  1980. }
  1981. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1982. int rx_size, int tx_size)
  1983. {
  1984. struct device *dev;
  1985. int minor;
  1986. int i;
  1987. minor = get_free_serial_index();
  1988. if (minor < 0)
  1989. goto exit;
  1990. /* register our minor number */
  1991. serial->parent->dev = tty_register_device(tty_drv, minor,
  1992. &serial->parent->interface->dev);
  1993. dev = serial->parent->dev;
  1994. dev_set_drvdata(dev, serial->parent);
  1995. i = device_create_file(dev, &dev_attr_hsotype);
  1996. /* fill in specific data for later use */
  1997. serial->minor = minor;
  1998. serial->magic = HSO_SERIAL_MAGIC;
  1999. spin_lock_init(&serial->serial_lock);
  2000. serial->num_rx_urbs = num_urbs;
  2001. /* RX, allocate urb and initialize */
  2002. /* prepare our RX buffer */
  2003. serial->rx_data_length = rx_size;
  2004. for (i = 0; i < serial->num_rx_urbs; i++) {
  2005. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2006. if (!serial->rx_urb[i]) {
  2007. dev_err(dev, "Could not allocate urb?\n");
  2008. goto exit;
  2009. }
  2010. serial->rx_urb[i]->transfer_buffer = NULL;
  2011. serial->rx_urb[i]->transfer_buffer_length = 0;
  2012. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2013. GFP_KERNEL);
  2014. if (!serial->rx_data[i]) {
  2015. dev_err(dev, "%s - Out of memory\n", __func__);
  2016. goto exit;
  2017. }
  2018. }
  2019. /* TX, allocate urb and initialize */
  2020. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2021. if (!serial->tx_urb) {
  2022. dev_err(dev, "Could not allocate urb?\n");
  2023. goto exit;
  2024. }
  2025. serial->tx_urb->transfer_buffer = NULL;
  2026. serial->tx_urb->transfer_buffer_length = 0;
  2027. /* prepare our TX buffer */
  2028. serial->tx_data_count = 0;
  2029. serial->tx_buffer_count = 0;
  2030. serial->tx_data_length = tx_size;
  2031. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2032. if (!serial->tx_data) {
  2033. dev_err(dev, "%s - Out of memory\n", __func__);
  2034. goto exit;
  2035. }
  2036. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2037. if (!serial->tx_buffer) {
  2038. dev_err(dev, "%s - Out of memory\n", __func__);
  2039. goto exit;
  2040. }
  2041. return 0;
  2042. exit:
  2043. hso_serial_common_free(serial);
  2044. return -1;
  2045. }
  2046. /* Creates a general hso device */
  2047. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2048. int port_spec)
  2049. {
  2050. struct hso_device *hso_dev;
  2051. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2052. if (!hso_dev)
  2053. return NULL;
  2054. hso_dev->port_spec = port_spec;
  2055. hso_dev->usb = interface_to_usbdev(intf);
  2056. hso_dev->interface = intf;
  2057. kref_init(&hso_dev->ref);
  2058. mutex_init(&hso_dev->mutex);
  2059. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2060. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2061. INIT_WORK(&hso_dev->reset_device, reset_device);
  2062. return hso_dev;
  2063. }
  2064. /* Removes a network device in the network device table */
  2065. static int remove_net_device(struct hso_device *hso_dev)
  2066. {
  2067. int i;
  2068. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2069. if (network_table[i] == hso_dev) {
  2070. network_table[i] = NULL;
  2071. break;
  2072. }
  2073. }
  2074. if (i == HSO_MAX_NET_DEVICES)
  2075. return -1;
  2076. return 0;
  2077. }
  2078. /* Frees our network device */
  2079. static void hso_free_net_device(struct hso_device *hso_dev)
  2080. {
  2081. int i;
  2082. struct hso_net *hso_net = dev2net(hso_dev);
  2083. if (!hso_net)
  2084. return;
  2085. remove_net_device(hso_net->parent);
  2086. if (hso_net->net)
  2087. unregister_netdev(hso_net->net);
  2088. /* start freeing */
  2089. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2090. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2091. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2092. hso_net->mux_bulk_rx_buf_pool[i] = NULL;
  2093. }
  2094. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2095. kfree(hso_net->mux_bulk_tx_buf);
  2096. hso_net->mux_bulk_tx_buf = NULL;
  2097. if (hso_net->net)
  2098. free_netdev(hso_net->net);
  2099. kfree(hso_dev);
  2100. }
  2101. static const struct net_device_ops hso_netdev_ops = {
  2102. .ndo_open = hso_net_open,
  2103. .ndo_stop = hso_net_close,
  2104. .ndo_start_xmit = hso_net_start_xmit,
  2105. .ndo_tx_timeout = hso_net_tx_timeout,
  2106. };
  2107. /* initialize the network interface */
  2108. static void hso_net_init(struct net_device *net)
  2109. {
  2110. struct hso_net *hso_net = netdev_priv(net);
  2111. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2112. /* fill in the other fields */
  2113. net->netdev_ops = &hso_netdev_ops;
  2114. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2115. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2116. net->type = ARPHRD_NONE;
  2117. net->mtu = DEFAULT_MTU - 14;
  2118. net->tx_queue_len = 10;
  2119. SET_ETHTOOL_OPS(net, &ops);
  2120. /* and initialize the semaphore */
  2121. spin_lock_init(&hso_net->net_lock);
  2122. }
  2123. /* Adds a network device in the network device table */
  2124. static int add_net_device(struct hso_device *hso_dev)
  2125. {
  2126. int i;
  2127. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2128. if (network_table[i] == NULL) {
  2129. network_table[i] = hso_dev;
  2130. break;
  2131. }
  2132. }
  2133. if (i == HSO_MAX_NET_DEVICES)
  2134. return -1;
  2135. return 0;
  2136. }
  2137. static int hso_rfkill_set_block(void *data, bool blocked)
  2138. {
  2139. struct hso_device *hso_dev = data;
  2140. int enabled = !blocked;
  2141. int rv;
  2142. mutex_lock(&hso_dev->mutex);
  2143. if (hso_dev->usb_gone)
  2144. rv = 0;
  2145. else
  2146. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2147. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2148. USB_CTRL_SET_TIMEOUT);
  2149. mutex_unlock(&hso_dev->mutex);
  2150. return rv;
  2151. }
  2152. static const struct rfkill_ops hso_rfkill_ops = {
  2153. .set_block = hso_rfkill_set_block,
  2154. };
  2155. /* Creates and sets up everything for rfkill */
  2156. static void hso_create_rfkill(struct hso_device *hso_dev,
  2157. struct usb_interface *interface)
  2158. {
  2159. struct hso_net *hso_net = dev2net(hso_dev);
  2160. struct device *dev = &hso_net->net->dev;
  2161. char *rfkn;
  2162. rfkn = kzalloc(20, GFP_KERNEL);
  2163. if (!rfkn)
  2164. dev_err(dev, "%s - Out of memory\n", __func__);
  2165. snprintf(rfkn, 20, "hso-%d",
  2166. interface->altsetting->desc.bInterfaceNumber);
  2167. hso_net->rfkill = rfkill_alloc(rfkn,
  2168. &interface_to_usbdev(interface)->dev,
  2169. RFKILL_TYPE_WWAN,
  2170. &hso_rfkill_ops, hso_dev);
  2171. if (!hso_net->rfkill) {
  2172. dev_err(dev, "%s - Out of memory\n", __func__);
  2173. kfree(rfkn);
  2174. return;
  2175. }
  2176. if (rfkill_register(hso_net->rfkill) < 0) {
  2177. rfkill_destroy(hso_net->rfkill);
  2178. kfree(rfkn);
  2179. hso_net->rfkill = NULL;
  2180. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2181. return;
  2182. }
  2183. }
  2184. static struct device_type hso_type = {
  2185. .name = "wwan",
  2186. };
  2187. /* Creates our network device */
  2188. static struct hso_device *hso_create_net_device(struct usb_interface *interface,
  2189. int port_spec)
  2190. {
  2191. int result, i;
  2192. struct net_device *net;
  2193. struct hso_net *hso_net;
  2194. struct hso_device *hso_dev;
  2195. hso_dev = hso_create_device(interface, port_spec);
  2196. if (!hso_dev)
  2197. return NULL;
  2198. /* allocate our network device, then we can put in our private data */
  2199. /* call hso_net_init to do the basic initialization */
  2200. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2201. if (!net) {
  2202. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2203. goto exit;
  2204. }
  2205. hso_net = netdev_priv(net);
  2206. hso_dev->port_data.dev_net = hso_net;
  2207. hso_net->net = net;
  2208. hso_net->parent = hso_dev;
  2209. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2210. USB_DIR_IN);
  2211. if (!hso_net->in_endp) {
  2212. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2213. goto exit;
  2214. }
  2215. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2216. USB_DIR_OUT);
  2217. if (!hso_net->out_endp) {
  2218. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2219. goto exit;
  2220. }
  2221. SET_NETDEV_DEV(net, &interface->dev);
  2222. SET_NETDEV_DEVTYPE(net, &hso_type);
  2223. /* registering our net device */
  2224. result = register_netdev(net);
  2225. if (result) {
  2226. dev_err(&interface->dev, "Failed to register device\n");
  2227. goto exit;
  2228. }
  2229. /* start allocating */
  2230. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2231. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2232. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2233. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2234. goto exit;
  2235. }
  2236. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2237. GFP_KERNEL);
  2238. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2239. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2240. goto exit;
  2241. }
  2242. }
  2243. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2244. if (!hso_net->mux_bulk_tx_urb) {
  2245. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2246. goto exit;
  2247. }
  2248. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2249. if (!hso_net->mux_bulk_tx_buf) {
  2250. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2251. goto exit;
  2252. }
  2253. add_net_device(hso_dev);
  2254. hso_log_port(hso_dev);
  2255. hso_create_rfkill(hso_dev, interface);
  2256. return hso_dev;
  2257. exit:
  2258. hso_free_net_device(hso_dev);
  2259. return NULL;
  2260. }
  2261. static void hso_free_tiomget(struct hso_serial *serial)
  2262. {
  2263. struct hso_tiocmget *tiocmget;
  2264. if (!serial)
  2265. return;
  2266. tiocmget = serial->tiocmget;
  2267. if (tiocmget) {
  2268. usb_free_urb(tiocmget->urb);
  2269. tiocmget->urb = NULL;
  2270. serial->tiocmget = NULL;
  2271. kfree(tiocmget);
  2272. }
  2273. }
  2274. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2275. static void hso_free_serial_device(struct hso_device *hso_dev)
  2276. {
  2277. struct hso_serial *serial = dev2ser(hso_dev);
  2278. if (!serial)
  2279. return;
  2280. set_serial_by_index(serial->minor, NULL);
  2281. hso_serial_common_free(serial);
  2282. if (serial->shared_int) {
  2283. mutex_lock(&serial->shared_int->shared_int_lock);
  2284. if (--serial->shared_int->ref_count == 0)
  2285. hso_free_shared_int(serial->shared_int);
  2286. else
  2287. mutex_unlock(&serial->shared_int->shared_int_lock);
  2288. }
  2289. hso_free_tiomget(serial);
  2290. kfree(serial);
  2291. kfree(hso_dev);
  2292. }
  2293. /* Creates a bulk AT channel */
  2294. static struct hso_device *hso_create_bulk_serial_device(
  2295. struct usb_interface *interface, int port)
  2296. {
  2297. struct hso_device *hso_dev;
  2298. struct hso_serial *serial;
  2299. int num_urbs;
  2300. struct hso_tiocmget *tiocmget;
  2301. hso_dev = hso_create_device(interface, port);
  2302. if (!hso_dev)
  2303. return NULL;
  2304. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2305. if (!serial)
  2306. goto exit;
  2307. serial->parent = hso_dev;
  2308. hso_dev->port_data.dev_serial = serial;
  2309. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2310. num_urbs = 2;
  2311. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2312. GFP_KERNEL);
  2313. /* it isn't going to break our heart if serial->tiocmget
  2314. * allocation fails don't bother checking this.
  2315. */
  2316. if (serial->tiocmget) {
  2317. tiocmget = serial->tiocmget;
  2318. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2319. if (tiocmget->urb) {
  2320. mutex_init(&tiocmget->mutex);
  2321. init_waitqueue_head(&tiocmget->waitq);
  2322. tiocmget->endp = hso_get_ep(
  2323. interface,
  2324. USB_ENDPOINT_XFER_INT,
  2325. USB_DIR_IN);
  2326. } else
  2327. hso_free_tiomget(serial);
  2328. }
  2329. }
  2330. else
  2331. num_urbs = 1;
  2332. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2333. BULK_URB_TX_SIZE))
  2334. goto exit;
  2335. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2336. USB_DIR_IN);
  2337. if (!serial->in_endp) {
  2338. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2339. goto exit2;
  2340. }
  2341. if (!
  2342. (serial->out_endp =
  2343. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2344. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2345. goto exit2;
  2346. }
  2347. serial->write_data = hso_std_serial_write_data;
  2348. /* and record this serial */
  2349. set_serial_by_index(serial->minor, serial);
  2350. /* setup the proc dirs and files if needed */
  2351. hso_log_port(hso_dev);
  2352. /* done, return it */
  2353. return hso_dev;
  2354. exit2:
  2355. hso_serial_common_free(serial);
  2356. exit:
  2357. hso_free_tiomget(serial);
  2358. kfree(serial);
  2359. kfree(hso_dev);
  2360. return NULL;
  2361. }
  2362. /* Creates a multiplexed AT channel */
  2363. static
  2364. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2365. int port,
  2366. struct hso_shared_int *mux)
  2367. {
  2368. struct hso_device *hso_dev;
  2369. struct hso_serial *serial;
  2370. int port_spec;
  2371. port_spec = HSO_INTF_MUX;
  2372. port_spec &= ~HSO_PORT_MASK;
  2373. port_spec |= hso_mux_to_port(port);
  2374. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2375. return NULL;
  2376. hso_dev = hso_create_device(interface, port_spec);
  2377. if (!hso_dev)
  2378. return NULL;
  2379. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2380. if (!serial)
  2381. goto exit;
  2382. hso_dev->port_data.dev_serial = serial;
  2383. serial->parent = hso_dev;
  2384. if (hso_serial_common_create
  2385. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2386. goto exit;
  2387. serial->tx_data_length--;
  2388. serial->write_data = hso_mux_serial_write_data;
  2389. serial->shared_int = mux;
  2390. mutex_lock(&serial->shared_int->shared_int_lock);
  2391. serial->shared_int->ref_count++;
  2392. mutex_unlock(&serial->shared_int->shared_int_lock);
  2393. /* and record this serial */
  2394. set_serial_by_index(serial->minor, serial);
  2395. /* setup the proc dirs and files if needed */
  2396. hso_log_port(hso_dev);
  2397. /* done, return it */
  2398. return hso_dev;
  2399. exit:
  2400. if (serial) {
  2401. tty_unregister_device(tty_drv, serial->minor);
  2402. kfree(serial);
  2403. }
  2404. if (hso_dev)
  2405. kfree(hso_dev);
  2406. return NULL;
  2407. }
  2408. static void hso_free_shared_int(struct hso_shared_int *mux)
  2409. {
  2410. usb_free_urb(mux->shared_intr_urb);
  2411. kfree(mux->shared_intr_buf);
  2412. mutex_unlock(&mux->shared_int_lock);
  2413. kfree(mux);
  2414. }
  2415. static
  2416. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2417. {
  2418. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2419. if (!mux)
  2420. return NULL;
  2421. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2422. USB_DIR_IN);
  2423. if (!mux->intr_endp) {
  2424. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2425. goto exit;
  2426. }
  2427. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2428. if (!mux->shared_intr_urb) {
  2429. dev_err(&interface->dev, "Could not allocate intr urb?\n");
  2430. goto exit;
  2431. }
  2432. mux->shared_intr_buf =
  2433. kzalloc(le16_to_cpu(mux->intr_endp->wMaxPacketSize),
  2434. GFP_KERNEL);
  2435. if (!mux->shared_intr_buf) {
  2436. dev_err(&interface->dev, "Could not allocate intr buf?\n");
  2437. goto exit;
  2438. }
  2439. mutex_init(&mux->shared_int_lock);
  2440. return mux;
  2441. exit:
  2442. kfree(mux->shared_intr_buf);
  2443. usb_free_urb(mux->shared_intr_urb);
  2444. kfree(mux);
  2445. return NULL;
  2446. }
  2447. /* Gets the port spec for a certain interface */
  2448. static int hso_get_config_data(struct usb_interface *interface)
  2449. {
  2450. struct usb_device *usbdev = interface_to_usbdev(interface);
  2451. u8 config_data[17];
  2452. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2453. s32 result;
  2454. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2455. 0x86, 0xC0, 0, 0, config_data, 17,
  2456. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2457. return -EIO;
  2458. }
  2459. switch (config_data[if_num]) {
  2460. case 0x0:
  2461. result = 0;
  2462. break;
  2463. case 0x1:
  2464. result = HSO_PORT_DIAG;
  2465. break;
  2466. case 0x2:
  2467. result = HSO_PORT_GPS;
  2468. break;
  2469. case 0x3:
  2470. result = HSO_PORT_GPS_CONTROL;
  2471. break;
  2472. case 0x4:
  2473. result = HSO_PORT_APP;
  2474. break;
  2475. case 0x5:
  2476. result = HSO_PORT_APP2;
  2477. break;
  2478. case 0x6:
  2479. result = HSO_PORT_CONTROL;
  2480. break;
  2481. case 0x7:
  2482. result = HSO_PORT_NETWORK;
  2483. break;
  2484. case 0x8:
  2485. result = HSO_PORT_MODEM;
  2486. break;
  2487. case 0x9:
  2488. result = HSO_PORT_MSD;
  2489. break;
  2490. case 0xa:
  2491. result = HSO_PORT_PCSC;
  2492. break;
  2493. case 0xb:
  2494. result = HSO_PORT_VOICE;
  2495. break;
  2496. default:
  2497. result = 0;
  2498. }
  2499. if (result)
  2500. result |= HSO_INTF_BULK;
  2501. if (config_data[16] & 0x1)
  2502. result |= HSO_INFO_CRC_BUG;
  2503. return result;
  2504. }
  2505. /* called once for each interface upon device insertion */
  2506. static int hso_probe(struct usb_interface *interface,
  2507. const struct usb_device_id *id)
  2508. {
  2509. int mux, i, if_num, port_spec;
  2510. unsigned char port_mask;
  2511. struct hso_device *hso_dev = NULL;
  2512. struct hso_shared_int *shared_int;
  2513. struct hso_device *tmp_dev = NULL;
  2514. if_num = interface->altsetting->desc.bInterfaceNumber;
  2515. /* Get the interface/port specification from either driver_info or from
  2516. * the device itself */
  2517. if (id->driver_info)
  2518. port_spec = ((u32 *)(id->driver_info))[if_num];
  2519. else
  2520. port_spec = hso_get_config_data(interface);
  2521. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2522. dev_err(&interface->dev, "Not our interface\n");
  2523. return -ENODEV;
  2524. }
  2525. /* Check if we need to switch to alt interfaces prior to port
  2526. * configuration */
  2527. if (interface->num_altsetting > 1)
  2528. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2529. interface->needs_remote_wakeup = 1;
  2530. /* Allocate new hso device(s) */
  2531. switch (port_spec & HSO_INTF_MASK) {
  2532. case HSO_INTF_MUX:
  2533. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2534. /* Create the network device */
  2535. if (!disable_net) {
  2536. hso_dev = hso_create_net_device(interface,
  2537. port_spec);
  2538. if (!hso_dev)
  2539. goto exit;
  2540. tmp_dev = hso_dev;
  2541. }
  2542. }
  2543. if (hso_get_mux_ports(interface, &port_mask))
  2544. /* TODO: de-allocate everything */
  2545. goto exit;
  2546. shared_int = hso_create_shared_int(interface);
  2547. if (!shared_int)
  2548. goto exit;
  2549. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2550. if (port_mask & i) {
  2551. hso_dev = hso_create_mux_serial_device(
  2552. interface, i, shared_int);
  2553. if (!hso_dev)
  2554. goto exit;
  2555. }
  2556. }
  2557. if (tmp_dev)
  2558. hso_dev = tmp_dev;
  2559. break;
  2560. case HSO_INTF_BULK:
  2561. /* It's a regular bulk interface */
  2562. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2563. if (!disable_net)
  2564. hso_dev =
  2565. hso_create_net_device(interface, port_spec);
  2566. } else {
  2567. hso_dev =
  2568. hso_create_bulk_serial_device(interface, port_spec);
  2569. }
  2570. if (!hso_dev)
  2571. goto exit;
  2572. break;
  2573. default:
  2574. goto exit;
  2575. }
  2576. /* save our data pointer in this device */
  2577. usb_set_intfdata(interface, hso_dev);
  2578. /* done */
  2579. return 0;
  2580. exit:
  2581. hso_free_interface(interface);
  2582. return -ENODEV;
  2583. }
  2584. /* device removed, cleaning up */
  2585. static void hso_disconnect(struct usb_interface *interface)
  2586. {
  2587. hso_free_interface(interface);
  2588. /* remove reference of our private data */
  2589. usb_set_intfdata(interface, NULL);
  2590. }
  2591. static void async_get_intf(struct work_struct *data)
  2592. {
  2593. struct hso_device *hso_dev =
  2594. container_of(data, struct hso_device, async_get_intf);
  2595. usb_autopm_get_interface(hso_dev->interface);
  2596. }
  2597. static void async_put_intf(struct work_struct *data)
  2598. {
  2599. struct hso_device *hso_dev =
  2600. container_of(data, struct hso_device, async_put_intf);
  2601. usb_autopm_put_interface(hso_dev->interface);
  2602. }
  2603. static int hso_get_activity(struct hso_device *hso_dev)
  2604. {
  2605. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2606. if (!hso_dev->is_active) {
  2607. hso_dev->is_active = 1;
  2608. schedule_work(&hso_dev->async_get_intf);
  2609. }
  2610. }
  2611. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2612. return -EAGAIN;
  2613. usb_mark_last_busy(hso_dev->usb);
  2614. return 0;
  2615. }
  2616. static int hso_put_activity(struct hso_device *hso_dev)
  2617. {
  2618. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2619. if (hso_dev->is_active) {
  2620. hso_dev->is_active = 0;
  2621. schedule_work(&hso_dev->async_put_intf);
  2622. return -EAGAIN;
  2623. }
  2624. }
  2625. hso_dev->is_active = 0;
  2626. return 0;
  2627. }
  2628. /* called by kernel when we need to suspend device */
  2629. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2630. {
  2631. int i, result;
  2632. /* Stop all serial ports */
  2633. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2634. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2635. result = hso_stop_serial_device(serial_table[i]);
  2636. if (result)
  2637. goto out;
  2638. }
  2639. }
  2640. /* Stop all network ports */
  2641. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2642. if (network_table[i] &&
  2643. (network_table[i]->interface == iface)) {
  2644. result = hso_stop_net_device(network_table[i]);
  2645. if (result)
  2646. goto out;
  2647. }
  2648. }
  2649. out:
  2650. return 0;
  2651. }
  2652. /* called by kernel when we need to resume device */
  2653. static int hso_resume(struct usb_interface *iface)
  2654. {
  2655. int i, result = 0;
  2656. struct hso_net *hso_net;
  2657. /* Start all serial ports */
  2658. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2659. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2660. if (dev2ser(serial_table[i])->open_count) {
  2661. result =
  2662. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2663. hso_kick_transmit(dev2ser(serial_table[i]));
  2664. if (result)
  2665. goto out;
  2666. }
  2667. }
  2668. }
  2669. /* Start all network ports */
  2670. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2671. if (network_table[i] &&
  2672. (network_table[i]->interface == iface)) {
  2673. hso_net = dev2net(network_table[i]);
  2674. if (hso_net->flags & IFF_UP) {
  2675. /* First transmit any lingering data,
  2676. then restart the device. */
  2677. if (hso_net->skb_tx_buf) {
  2678. dev_dbg(&iface->dev,
  2679. "Transmitting"
  2680. " lingering data\n");
  2681. hso_net_start_xmit(hso_net->skb_tx_buf,
  2682. hso_net->net);
  2683. hso_net->skb_tx_buf = NULL;
  2684. }
  2685. result = hso_start_net_device(network_table[i]);
  2686. if (result)
  2687. goto out;
  2688. }
  2689. }
  2690. }
  2691. out:
  2692. return result;
  2693. }
  2694. static void reset_device(struct work_struct *data)
  2695. {
  2696. struct hso_device *hso_dev =
  2697. container_of(data, struct hso_device, reset_device);
  2698. struct usb_device *usb = hso_dev->usb;
  2699. int result;
  2700. if (hso_dev->usb_gone) {
  2701. D1("No reset during disconnect\n");
  2702. } else {
  2703. result = usb_lock_device_for_reset(usb, hso_dev->interface);
  2704. if (result < 0)
  2705. D1("unable to lock device for reset: %d\n", result);
  2706. else {
  2707. usb_reset_device(usb);
  2708. usb_unlock_device(usb);
  2709. }
  2710. }
  2711. }
  2712. static void hso_serial_ref_free(struct kref *ref)
  2713. {
  2714. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2715. hso_free_serial_device(hso_dev);
  2716. }
  2717. static void hso_free_interface(struct usb_interface *interface)
  2718. {
  2719. struct hso_serial *hso_dev;
  2720. struct tty_struct *tty;
  2721. int i;
  2722. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2723. if (serial_table[i] &&
  2724. (serial_table[i]->interface == interface)) {
  2725. hso_dev = dev2ser(serial_table[i]);
  2726. spin_lock_irq(&hso_dev->serial_lock);
  2727. tty = tty_kref_get(hso_dev->tty);
  2728. spin_unlock_irq(&hso_dev->serial_lock);
  2729. if (tty)
  2730. tty_hangup(tty);
  2731. mutex_lock(&hso_dev->parent->mutex);
  2732. tty_kref_put(tty);
  2733. hso_dev->parent->usb_gone = 1;
  2734. mutex_unlock(&hso_dev->parent->mutex);
  2735. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2736. }
  2737. }
  2738. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2739. if (network_table[i] &&
  2740. (network_table[i]->interface == interface)) {
  2741. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2742. /* hso_stop_net_device doesn't stop the net queue since
  2743. * traffic needs to start it again when suspended */
  2744. netif_stop_queue(dev2net(network_table[i])->net);
  2745. hso_stop_net_device(network_table[i]);
  2746. cancel_work_sync(&network_table[i]->async_put_intf);
  2747. cancel_work_sync(&network_table[i]->async_get_intf);
  2748. if (rfk) {
  2749. rfkill_unregister(rfk);
  2750. rfkill_destroy(rfk);
  2751. }
  2752. hso_free_net_device(network_table[i]);
  2753. }
  2754. }
  2755. }
  2756. /* Helper functions */
  2757. /* Get the endpoint ! */
  2758. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2759. int type, int dir)
  2760. {
  2761. int i;
  2762. struct usb_host_interface *iface = intf->cur_altsetting;
  2763. struct usb_endpoint_descriptor *endp;
  2764. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2765. endp = &iface->endpoint[i].desc;
  2766. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2767. (usb_endpoint_type(endp) == type))
  2768. return endp;
  2769. }
  2770. return NULL;
  2771. }
  2772. /* Get the byte that describes which ports are enabled */
  2773. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2774. {
  2775. int i;
  2776. struct usb_host_interface *iface = intf->cur_altsetting;
  2777. if (iface->extralen == 3) {
  2778. *ports = iface->extra[2];
  2779. return 0;
  2780. }
  2781. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2782. if (iface->endpoint[i].extralen == 3) {
  2783. *ports = iface->endpoint[i].extra[2];
  2784. return 0;
  2785. }
  2786. }
  2787. return -1;
  2788. }
  2789. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2790. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2791. struct usb_device *usb, gfp_t gfp)
  2792. {
  2793. int result;
  2794. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2795. usb_rcvintpipe(usb,
  2796. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2797. shared_int->shared_intr_buf,
  2798. 1,
  2799. intr_callback, shared_int,
  2800. shared_int->intr_endp->bInterval);
  2801. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2802. if (result)
  2803. dev_warn(&usb->dev, "%s failed mux_intr_urb %d\n", __func__,
  2804. result);
  2805. return result;
  2806. }
  2807. /* operations setup of the serial interface */
  2808. static const struct tty_operations hso_serial_ops = {
  2809. .open = hso_serial_open,
  2810. .close = hso_serial_close,
  2811. .write = hso_serial_write,
  2812. .write_room = hso_serial_write_room,
  2813. .ioctl = hso_serial_ioctl,
  2814. .set_termios = hso_serial_set_termios,
  2815. .chars_in_buffer = hso_serial_chars_in_buffer,
  2816. .tiocmget = hso_serial_tiocmget,
  2817. .tiocmset = hso_serial_tiocmset,
  2818. .get_icount = hso_get_count,
  2819. .unthrottle = hso_unthrottle
  2820. };
  2821. static struct usb_driver hso_driver = {
  2822. .name = driver_name,
  2823. .probe = hso_probe,
  2824. .disconnect = hso_disconnect,
  2825. .id_table = hso_ids,
  2826. .suspend = hso_suspend,
  2827. .resume = hso_resume,
  2828. .reset_resume = hso_resume,
  2829. .supports_autosuspend = 1,
  2830. };
  2831. static int __init hso_init(void)
  2832. {
  2833. int i;
  2834. int result;
  2835. /* put it in the log */
  2836. printk(KERN_INFO "hso: %s\n", version);
  2837. /* Initialise the serial table semaphore and table */
  2838. spin_lock_init(&serial_table_lock);
  2839. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2840. serial_table[i] = NULL;
  2841. /* allocate our driver using the proper amount of supported minors */
  2842. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2843. if (!tty_drv)
  2844. return -ENOMEM;
  2845. /* fill in all needed values */
  2846. tty_drv->magic = TTY_DRIVER_MAGIC;
  2847. tty_drv->owner = THIS_MODULE;
  2848. tty_drv->driver_name = driver_name;
  2849. tty_drv->name = tty_filename;
  2850. /* if major number is provided as parameter, use that one */
  2851. if (tty_major)
  2852. tty_drv->major = tty_major;
  2853. tty_drv->minor_start = 0;
  2854. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2855. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2856. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2857. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2858. tty_drv->init_termios = tty_std_termios;
  2859. hso_init_termios(&tty_drv->init_termios);
  2860. tty_set_operations(tty_drv, &hso_serial_ops);
  2861. /* register the tty driver */
  2862. result = tty_register_driver(tty_drv);
  2863. if (result) {
  2864. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2865. __func__, result);
  2866. return result;
  2867. }
  2868. /* register this module as an usb driver */
  2869. result = usb_register(&hso_driver);
  2870. if (result) {
  2871. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2872. result);
  2873. /* cleanup serial interface */
  2874. tty_unregister_driver(tty_drv);
  2875. return result;
  2876. }
  2877. /* done */
  2878. return 0;
  2879. }
  2880. static void __exit hso_exit(void)
  2881. {
  2882. printk(KERN_INFO "hso: unloaded\n");
  2883. tty_unregister_driver(tty_drv);
  2884. /* deregister the usb driver */
  2885. usb_deregister(&hso_driver);
  2886. }
  2887. /* Module definitions */
  2888. module_init(hso_init);
  2889. module_exit(hso_exit);
  2890. MODULE_AUTHOR(MOD_AUTHOR);
  2891. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2892. MODULE_LICENSE(MOD_LICENSE);
  2893. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2894. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2895. module_param(debug, int, S_IRUGO | S_IWUSR);
  2896. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2897. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2898. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2899. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2900. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2901. module_param(disable_net, int, S_IRUGO | S_IWUSR);