hso.c 83 KB

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