hso.c 83 KB

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