hso.c 83 KB

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